-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfcr.cpp
More file actions
111 lines (100 loc) · 3.52 KB
/
fcr.cpp
File metadata and controls
111 lines (100 loc) · 3.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include <filesystem>
using namespace std;
namespace fs = std::filesystem;
bool fileExists(const string&);
string info = "FCR File creator\n===============\n\nFile creator is a software that creates files in command promt.\nfcr <filename> <option>\nOptions:\n\n-n\t\t\t Opens file with notepad\n-s\t\t\t Starts file with default application\n-w\t\t\t Allows to write file in console\n--version\t\t Shows version";
string version = "1.0.0";
int main(int argc, char* argv[]) {
// Loop to convert argv to string vector for easier handling
vector<string> args;
string c_arg;
args.resize(argc);
for(int i=0; i<argc; i++) {
c_arg = argv[i];
args.at(i) = c_arg;
}
// Different methods of interaction
if(argc == 1) { /* Info statement is printed if no parameter is specified. */
cout << info << endl;
return 0;
}
else if(argc == 2) { /* Two parameter cases */
if(args[1] == "--version") { /* Version is printed */
cout << "FCR version " << version << endl;
}
else { /* File is created without any actions */
ofstream file{args[1]};
}
}
else if(argc > 2) { /* File creation with parameters */
if(args[2] == "-n"){ /* -n (Opened with notepad) */
if(fileExists(args[1])) { /* If file already exists */
string com = "notepad " + args[1];
system(com.c_str());
} else {
ofstream file{args[1]};
file.close();
string com = "notepad " + args[1];
system(com.c_str());
}
}
else if(args[2] == "-s") { /* -s (Opened with standard app) */
if(fileExists(args[1])) { /* If file already exists */
string com = "start " + args[1];
system(com.c_str());
} else {
ofstream file{args[1]};
file.close();
string com = "start " + args[1];
system(com.c_str());
}
}
else if(args[2] == "-w") { /* -w (Writen in console) */
cout << "========== " << args[1] << " ==========" << endl;
if(fileExists(args[1])) { /* If file already exists */
ifstream file{args[1]};
char content;
if(file.is_open()) {
while(file) {
content = file.get();
cout << content;
}
file.close();
}
}
bool loop = true;
string input = "";
ofstream file(args[1], ios::app);
do { /* Loops through until '/end' is entered */
cin >> input;
if(input == "/end") {
loop = false;
} else{
file << input << endl;
}
} while(loop == true);
file.close();
}
}
return 0;
}
bool fileExists(const string& arg) { /* Manager if file exists */
fs::path file{arg};
if(fs::exists(file)) {
string answer;
cout << arg << " already exists. Type 'e' to edit or 'o' to overwrite: ";
cin >> answer;
cout << endl;
if(answer == "e") {
return true;
} else {
return false;
}
} else {
return false;
}
}