-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfileManagement.cpp
318 lines (254 loc) · 9.74 KB
/
fileManagement.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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
#include "fileManagement.h"
#include "fakeDos.h"
#include <unistd.h>
#include <io.h>
#include <map>
#include <vector>
#include <fstream>
// Change a virtual route (e.g: root:\users\xxx) to a real route
string route_formating(string fakeDosFolderPath, string str){
if (str == "root") return fakeDosFolderPath;
return fakeDosFolderPath + "\\" + str.substr(5);
}
bool find(vector<string> &fileNames, string name){
for (int i = 0; i < int(fileNames.size()); i++){
if (fileNames[i] == name) return true;
}
return false;
}
vector<string> getFiles(string current_route){
vector<string> files;
_finddata_t file;
long lf;
if ( (lf = _findfirst(current_route.c_str(), &file))== -1){
cout << current_route<<"not found."<<endl;
} else{
while(_findnext(lf, &file)==0){
if (strcmp(file.name, ".") == 0
|| strcmp(file.name, "..") == 0) continue;
files.push_back(file.name);
}
}
_findclose(lf);
return files;
}
void printNames(string true_current_route, string fake_current_route){
cout << endl<<"Files and dirs under '"<<fake_current_route<<"': "<<endl;
true_current_route = true_current_route + "\\*";
vector<string> files = getFiles(true_current_route);
if (files.size()==0){
cout << "(Empty)"<<endl;
}else{
for (int i = 0; i< int(files.size());i++){
cout << files[i]<<endl;
}
}
cout << endl;
}
void show_content(string fakeDosFolderPath, string current_user, map<string, string> &user_route){
//system()
string true_current_route;
string fake_current_route = user_route[current_user];
true_current_route = route_formating(fakeDosFolderPath,fake_current_route);
//system(("dir "+ current_route).c_str());
printNames(true_current_route, fake_current_route);
}
void make_dir(string name, string fakeDosFolderPath, string ¤t_user,map<string, string> &user_route){
string current_route;
current_route = route_formating(fakeDosFolderPath, user_route[current_user]);
string command1 = "cd "+ current_route;
string command2 = "mkdir "+name;
system((command1 + "&&" + command2).c_str());
}
bool isTypeSpecified(string name){
if (name.find('.') == string::npos) return false;
return true;
}
// name 包含名称及文件类型;e.g: apple.app
void make_file(string name,string fakeDosFolderPath, string ¤t_user,
map<string, string> &user_route){
if (!isTypeSpecified(name)){
cout << "Error: File type not specified. (e.g: .txt, .app, ...)"<<endl;
return;
}
string current_route;
current_route = route_formating(fakeDosFolderPath,user_route[current_user]);
string command1 = "cd "+ current_route;
string command2 = "cd .> "+name;
system((command1 + " && " + command2).c_str());
cout << "Successfully create file "<< name
<< " under "<<user_route[current_user]<<"."<<endl<<endl;
}
string upper_route(string ¤t_route){
int len = current_route.length();
for (int i = len-1; i>=0 ; i--){
char ch = current_route[i];
if (ch == '\\'){
string sub_route = current_route.substr(0,i);
if (sub_route != "root:") return sub_route;
else return "root";
}
}
return "root";
}
// 开头为 "root"
bool isPath(string &str){
if (str.find("root") == 0) return true;
return false;
}
void change_path(string name, string fakeDosFolderPath,
string current_user, map<string, string> &user_route){
string target_path;
if (name == "u"){
user_route[current_user] = upper_route(user_route[current_user]);
} else if (name == "rt"){
user_route[current_user] = "root";
} else {
if (isPath(name)){ //若输入为虚拟路径
target_path = route_formating(fakeDosFolderPath, name);
if (_access(target_path.c_str(),0)== -1){
cout << "Error: Directory doesn't exist."<<endl;
return;
} else {
user_route[current_user] = name;
}
} else if (isTypeSpecified(name)){ // If input is a file, e.g: xxx.txt
cout << "Error: Input is not a directory."<<endl;
return;
} else { // 若输入为名称:判断当前目录下是否存在
string current_route = route_formating(fakeDosFolderPath, user_route[current_user]);
vector<string> fileNames = getFiles(current_route + "\\*");
if (find(fileNames,name)){
user_route[current_user] = user_route[current_user] + "\\"+name;
} else {
cout << "Error: Directory doesn't exist."<<endl;
return;
}
}
}
write_users();
}
// 添加错误input handling
void del_dir(string name,string fakeDosFolderPath,string current_user, map<string, string> &user_route){
string current_route;
current_route = route_formating(fakeDosFolderPath, user_route[current_user]);
string command = "rd /s /q "+ current_route + "\\" + name;
system(command.c_str());
}
void del_file(string name, string fakeDosFolderPath,string current_user, map<string, string> &user_route){
string current_route;
if (!isTypeSpecified(name)){
cout << "Error: File type not specified. (e.g: .txt, .app...)"<<endl;
return;
}
current_route = route_formating(fakeDosFolderPath, user_route[current_user]);
string command = "del "+ current_route + "\\" + name;
system(command.c_str());
}
// Copy "this" to "target"
void copy(string this_name, string target_name, string fakeDosFolderPath,
string current_user, map<string, string> &user_route){
string current_route, target_path;
current_route = route_formating(fakeDosFolderPath, user_route[current_user]);
vector<string> fileNames = getFiles(current_route + "\\*");
if (!find(fileNames,this_name)){
cout << "Error: File/directory not found." << endl;
return;
}else{
if (isPath(target_name)){
target_path = route_formating(fakeDosFolderPath, target_name);
} else {
target_path = user_route[current_user] + "\\" + target_name;
target_path = route_formating(fakeDosFolderPath,target_path);
}
if (_access(target_path.c_str(),0)== -1){
cout << "Error: Directory doesn't exist."<<endl;
return;
} else {
if (isTypeSpecified(this_name)){
string command1 = "cd "+ current_route;
string command2 = "xcopy "+ this_name + " "+ target_name;
system((command1 + " && "+ command2).c_str());
} else {//若拷贝文件夹:先切换到目标路径,创建同名文件夹,再执行拷贝
string command1 = "cd "+ target_path;
string command2 = "md "+ this_name;
string command3 = "cd "+ current_route;
string command4 = "xcopy "+ this_name + " "+ target_path + "\\" + this_name +" /s /e";
system( (command1 + "&&"
+ command2 + "&&"
+ command3 + "&&"
+ command4).c_str());
}
}
}
}
void move(string this_name, string target_name, string fakeDosFolderPath,
string current_user, map<string, string> &user_route){
// Debug
// cout << "Enter move()"<<endl;
string current_route, target_path;
current_route = route_formating(fakeDosFolderPath, user_route[current_user]);
vector<string> fileNames = getFiles(current_route + "\\*");
if (!find(fileNames,this_name)){
cout << "Error: File/directory not found." << endl;
return;
} else {
if (!isPath(target_name)) target_name = user_route[current_user] + "\\" + target_name;
target_path = route_formating(fakeDosFolderPath,target_name);
if (_access(target_path.c_str(),0)== -1){
cout << "Error: Directory doesn't exist."<<endl;
return;
} else {
string command1 = "cd "+ current_route;
string command2 = "move "+ this_name + " "+ target_path;
system((command1 + " && "+ command2).c_str());
}
}
}
void read(string name, string fakeDosFolderPath,
string current_user, map<string, string> &user_route){
ifstream infile;
ofstream outfile;
string current_route;
current_route = route_formating(fakeDosFolderPath, user_route[current_user]);
current_route = current_route + "\\"+ name +".txt";
infile.open(current_route.c_str());
if (infile.fail()){
cout << "Error: file doesn't exist."<<endl;
infile.close();
return;
}else{
cout << "Read text file '"<<name<<"': "<<endl;
while(true){
string line;
getline(infile, line);
if(infile.fail()) break;
cout << line << endl;
}
cout <<endl;
}
}
void write(string name, string fakeDosFolderPath,
string current_user, map<string, string> &user_route){
ifstream infile;
ofstream outfile;
string current_route;
current_route = route_formating(fakeDosFolderPath, user_route[current_user]);
current_route = current_route + "\\"+ name +".txt";
infile.open(current_route.c_str());
if (infile.fail()){
cout << "Error: file doesn't exist."<<endl;
infile.close();
return;
}else{
infile.close();
cout << "Write in text file '"<< name << "' (enter 'eee' to terminate): "<<endl;
outfile.open(current_route.c_str(), ios::app);
while(true){
string line;
getline(cin, line);
if (line == "eee") break;
outfile << line<<endl;
}
}
}