-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathparsing.cpp
More file actions
226 lines (203 loc) · 5.55 KB
/
Copy pathparsing.cpp
File metadata and controls
226 lines (203 loc) · 5.55 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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
#include <iostream>
#include <vector>
#include <string>
#include <sstream>
#ifdef _WIN32
#include <direct.h> // windows specific
#include "windows/system commands/WindowsSystem.h" //windows specific
#include "windows/network commands/networkCommandsWindows.cpp" // windows specific
#include "windows/directory commands/DirectoryCommandsWindows.cpp"
#define OS 0
#endif // _WIN32
#ifdef __linux__
#include <unistd.h>
#include "linux/system commands/LinuxSystem.cpp"
#include "linux/network commands/networkCommandsLinux.cpp"
#include "linux/directory commands/DirectoryCommandsLinux.cpp"
#define OS 1
#endif // __linux__
#include "fileCommands.cpp"
#include "echo.cpp"
using namespace std;
enum Command {
MKDIR,
CAT,
RM,
CP,
MV,
GREP,
TOUCH,
RMDIR,
LS,
CD,
PWD,
NEOFETCH,
WHOAMI,
IFCONFIG,
ECHO,
UNKNOWN
//place commands here
};
Command hashit(string const& inString) {
if (inString == "mkdir") return MKDIR;
if (inString == "cat") return CAT;
if (inString == "rm") return RM;
if (inString == "cp") return CP;
if (inString == "mv") return MV;
if (inString == "grep") return GREP;
if (inString == "touch") return TOUCH;
if (inString == "rmdir") return RMDIR;
if (inString == "ls") return LS;
if (inString == "cd") return CD;
if (inString == "pwd") return PWD;
if (inString == "neofetch") return NEOFETCH;
if (inString == "whoami") return WHOAMI;
if (inString == "ifconfig") return IFCONFIG;
if (inString == "echo") return ECHO;
return UNKNOWN;
}
vector<string> parseInput(const string& input) {
vector<string> tokens;
istringstream stream(input);
string token;
while (stream >> token) {
tokens.push_back(token);
}
return tokens;
}
int main() {
System* pSystemObject = nullptr;
Network* pNetworkObject = nullptr;
Directories* pDirectoriesObject = nullptr;
#ifdef _WIN32
pSystemObject = new WindowsSystem();
pNetworkObject = new NetworkCommands();
pDirectoriesObject = new DirectoryCommands();
#endif
#ifdef __linux__
pSystemObject = new LinuxSystem();
pNetworkObject = new NetworkCommands();
pDirectoriesObject = new DirectoryCommands();
#endif
FileManager manager = FileManager();
EchoCommand echohandler = EchoCommand();
string path = pDirectoriesObject->getPath();
int status = 0;
while (true) {
cout << path << ">> ";
string input;
getline(cin, input);
vector<string> tokens = parseInput(input);
switch (hashit(tokens[0])) {
case MKDIR:
pDirectoriesObject->mkdir(tokens[1]);
break;
case CAT:
if (tokens.size() == 2)
{
manager.catFile(tokens[1]);
}
else
{
cerr << "Incorrect number of arguments for cat. Expected 1." << endl;
}
// cat
break;
case RM:
if (tokens.size() == 2)
{
manager.removeFile(tokens[1]);
}
else
{
cerr << "Incorrect number of arguments for rm. Expected 1." << endl;
}
// rm
break;
case CP:
if (tokens.size() == 3)
{
manager.moveOrCopyFile(tokens[1], tokens[2], false);
}
else
{
cerr << "Incorrect number of arguments for cp. Expected 2." << endl;
}
// cp
break;
case MV:
if (tokens.size() == 3)
{
manager.moveOrCopyFile(tokens[1], tokens[2], true);
}
else
{
cerr << "Incorrect number of arguments for mv. Expected 2." << endl;
}
// mv
break;
case GREP:
if (tokens.size() == 3)
{
manager.grepFile(tokens[1], tokens[2]);
}
else
{
cerr << "Incorrect number of arguments for grep. Expected 2." << endl;
}
// grep
break;
case TOUCH:
if (tokens.size() == 2)
{
manager.touchFile(tokens[1]);
}
else
{
cerr << "Incorrect number of arguments for grep. Expected 1." << endl;
}
// touch
break;
case RMDIR:
pDirectoriesObject->rmdir(tokens[1]);
path = pDirectoriesObject->getPath();
break;
case LS:
if (tokens.size() == 1) {
pDirectoriesObject->ls();
}
else {
pDirectoriesObject->ls(tokens[1]);
}
break;
case CD:
pDirectoriesObject->cd(tokens[1]);
path = pDirectoriesObject->getPath();
break;
case PWD:
pDirectoriesObject->pwd();
break;
case NEOFETCH:
pSystemObject->NeoFetch();
// neofetch
break;
case WHOAMI:
pSystemObject->WhoAmI();
// whoami
break;
case IFCONFIG:
pNetworkObject->ifconfigCommand();
// ifconfig
break;
case ECHO:
echohandler.execute(input);
break;
default:
status = system(input.c_str());
if (status != 0)
cout << "Unknown command" << endl;
break;
}
}
return 0;
}