-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcommandmode.cpp
More file actions
201 lines (193 loc) · 5.14 KB
/
commandmode.cpp
File metadata and controls
201 lines (193 loc) · 5.14 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
#include "myheader.h"
vector<string> tokens;
/**
* @brief Handles absolute and relative paths in cmd mode
* @param str The input path
* @return The absolute path
*/
string pathProcessing(string str)
{
char firstChar = str[0];
string absolutePath = "";
string basePath = string(root);
if (firstChar == '/')
{
absolutePath = basePath + str;
}
else if (firstChar == '~')
{
absolutePath = basePath + str.substr(1, str.length());
}
else if (firstChar == '.')
{
absolutePath = string(curPath) + str.substr(1, str.length());
}
else
{
absolutePath = string(curPath) + "/" + str;
}
return absolutePath;
}
/**
* @brief Splits the input string into tokens separated by space and stores them in the 'tokens' vector
* @param str The input string
*/
void inputProcessing(string str)
{
unsigned int i = 0;
tokens.clear();
int flag = 0;
while (i < str.length())
{
string sub = "";
while (str[i] != ' ' && i < str.length())
{
if (str[i] == '\\')
{
sub += str[i + 1];
i = i + 2;
}
else
{
sub += str[i];
i++;
}
}
if (flag == 1)
{
string absolutePath = pathProcessing(sub);
tokens.push_back(absolutePath);
}
else
{
tokens.push_back(sub);
if (tokens[0] != "create_file" && tokens[0] != "create_dir" && tokens[0] != "search")
{
flag = 1;
}
}
i++;
}
}
/**
* @brief Clears the command line area after each command runs
*/
void clearCommand()
{
int lastLine = rowSize + 1;
printf("%c[%d;%dH", 27, lastLine, 1);
printf("%c[2K", 27);
cout << ":";
}
/**
* @brief Starts the command mode, takes input from the user, and processes it
* @return 0 if the command mode exited normally, otherwise a non-zero value
*/
int startCommandMode()
{
char ch;
do
{
string input;
while (((ch = getchar()) != 10) && ch != 27)
{
if (ch == 127)
{
clearCommand();
if (input.length() <= 1)
{
input = "";
}
else
{
input = input.substr(0, input.length() - 1);
}
cout << input;
}
else
{
input = input + ch;
cout << ch;
}
}
inputProcessing(input);
if (ch == 10)
{
string command = tokens[0];
if (command == "copy")
{
// cout<<"Copy command : "<<endl;
copyCommand(tokens);
clearCommand();
}
else if (command == "move")
{
// cout<<"Move command : "<<endl;
moveCommand(tokens);
clearCommand();
}
else if (command == "rename")
{
// cout<<"rename command : "<<endl;
renameFiles(tokens);
clearCommand();
}
else if (command == "create_file")
{
// cout<<"create_file command : "<<endl;
createNewFiles(tokens);
clearCommand();
}
else if (command == "create_dir")
{
// cout<<"create_dir command : "<<endl;
makeDirectories(tokens);
clearCommand();
}
else if (command == "delete_file")
{
// cout<<"delete_file command : "<<endl;
removeFiles(tokens);
clearCommand();
}
else if (command == "delete_dir")
{
// cout<<"delete_dir command : "<<endl;
removeDirectories(tokens);
clearCommand();
}
else if (command == "goto")
{
// cout<<"goto command : "<<endl;
string gotoPath = gotoPath(tokens);
char *path = new char[gotoPath.length() + 1];
strcpy(path, gotoPath.c_str());
back_stack.push(string(curPath));
clearStack(forw_stack);
curPath = path;
// cout<<"\ngoto path : "<<path<<endl;
return 1;
}
else if (command == "search")
{
// cout<<"search command : "<<endl;
int status = searchCommand(tokens);
if (status != 0)
return 2;
clearCommand();
}
else if (command == "snapshot")
{
// cout<<"snapshot command : "<<endl;
takeSnapshot(tokens);
clearCommand();
}
else
{
showError("Invalid Command");
clearCommand();
}
}
} while (ch != 27);
return 0;
}