-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcommandmode.cpp
207 lines (189 loc) · 4.32 KB
/
commandmode.cpp
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
/************************************************************************
** RollNo:2018201033 Name : Darshan Kansagara **
************************************************************************/
//**********************************************************************
// Header file Included
//**********************************************************************
#include "myheader.h"
vector<string> tokens;
//**********************************************************************
// It handles absolute and relative paths in cmd mode
//**********************************************************************
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;
}
//**********************************************************************
// It splits input string into tokens separeted by space
//**********************************************************************
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 abspath=pathProcessing(sub);
tokens.push_back(abspath);
}
else{
tokens.push_back(sub);
if(tokens[0]!="create_file" && tokens[0]!="create_dir" && tokens[0]!="search")
{
flag=1;
}
}
i++;
}
}
//**********************************************************************
// After each command runs, it clear command line area
//**********************************************************************
void clearCommand()
{
int lastLine = rowsize + 1;
printf("%c[%d;%dH",27,lastLine,1);
printf("%c[2K", 27);
cout<<":";
}
//**********************************************************************
// Method that starts the CommandMode
//**********************************************************************
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 gpath = gotoPath(tokens);
char *path = new char[gpath.length() + 1];
strcpy(path, gpath.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;
}