-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
324 lines (287 loc) · 7.68 KB
/
main.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
319
320
321
322
323
324
#include <iostream>
#include <bitset>
#include "user.h"
#include "myfile.h"
#include "mynode.h"
#include "userdir.h"
#include "rootdir.h"
using namespace std;
void testnode(){
Mynode node=Mynode("node",2);
cout<<node.getNodename()<<":"<<node.getBlockNo()<<endl;
}
void testuser(){
User user=User("zhang","123");
//user.login("zhang","123");
cout<<user.getUserName()<<endl;
user.logout("zhang");
}
void testfile(){
Myfile file=Myfile("txt","d:/",bitset<4>(string("1111")),"hello world!","david",9);
cout<<file.getContent()<<endl;
cout<<file.getFileAddress()<<endl;
cout<<file.getFileAuthor()<<endl;
cout<<file.getFileLength()<<endl;
cout<<file.getFileName()<<endl;
cout<<file.getProtectedBit()<<endl;
cout<<file.getBlockNo()<<endl;
cout<<file.readFile()<<endl;
file.print();
file.writeFile("ok!");
cout<<file.readFile()<<endl;
}
void testsub(){
Userdir sub=Userdir("zhang","123",1);
cout<<sub.getDirAddr()<<endl;
cout<<sub.getDirName()<<endl;
cout<<sub.getDirBlockNo()<<endl;
sub.createFile("hello",bitset<4>(string("1100")));
sub.dirLogin("zhang","123");
sub.createFile("hello",bitset<4>(string("1100")));
sub.createFile("hello1",bitset<4>(string("1100"))); //test for uniqueness
sub.readfile(0);
sub.readfile(1);
sub.listFile();
int block=sub.getFileBolckNo("hello");
cout<<sub.readfile(block)<<endl;
sub.writefile(block,"haha");
cout<<sub.readfile(block)<<endl;
sub.deleteFile("hello");
cout<<sub.readfile(block)<<endl;
}
void testroot(){
Rootdir root=Rootdir();
cout<<root.getRootAddr()<<endl;
root.createDir("c");
root.rootLogin("/","123");
root.createDir("c");
root.createDir("c");
root.dirLogin("c");
root.createFile("c","aaa");
root.createFile("c","aaa");
int file=root.openFile("c","aaa");
cout<<root.readFile("c",file)<<endl;
root.writeFile("c",file,"jjj");
root.listDirFile("c");
root.deleteFile("c","aaa");
root.listDirFile("c");
root.createDir("d"); //test for uniqueness
root.listDir();
cout<<root.getDirBlock("c")<<endl;
root.deleteDir("c");
cout<<root.getDirBlock("c")<<endl;
}
Rootdir root=Rootdir();
string currentFileName="";
int currentFileBlock=-1;
string currentDirName="";
int currentDirBlock=-1;
void login(string password){
if(currentDirName==""){
root.rootLogin("/",password);
return;
} else {
root.dirLogin(currentDirName,password);
return;
}
}
void logout(){
if(currentDirName==""){
root.rootLogout();
return;
} else {
root.dirLogout(currentDirName);
return;
}
}
void showList(){
if(currentDirName==""){
root.listDir();
return;
} else {
root.listDirFile(currentDirName);
return;
}
}
void createDir(string dirname){
if(currentDirName!=""){
cout<<"您现在在子文件夹,请先返回主文件夹!"<<endl;
return;
}
root.createDir(dirname);
cout<<"成功创建文件夹:"<<dirname<<endl;
}
void deleteDir(string dirname){
if(currentDirName!=""){
cout<<"您现在在子文件夹,请先返回主文件夹!"<<endl;
return;
}
root.deleteDir(dirname);
}
void enterDir(string dirname){
// in sub directory
if(currentDirName!=""){
if(dirname==".."){
currentDirName="";
currentDirBlock=-1;
return;
}else{
cout<<"您现在在子文件夹,请先返回主文件夹!"<<endl;
return;
}
}
// in root directory
int block=root.getDirBlock(dirname);
if(block==-1){
return;
}
currentDirName=dirname;
currentDirBlock=block;
}
void createFile(string filename){
if(currentDirName==""){
cout<<"请先进入一个子文件夹!"<<endl;
return;
}
root.createFile(currentDirName,filename);
cout<<"成功创建文件:"<<filename<<endl;
}
void delFile(string filename){
if(currentDirName==""){
cout<<"请先进入一个子文件夹!"<<endl;
return;
}
root.deleteFile(currentDirName,filename);
}
void openFile(string dirname,string filename){
int file=root.openFile(dirname,filename);
if(file==-1){
cout<<"打开文件失败!"<<endl;
return;
}
currentFileBlock=file;
currentFileName=filename;
cout<<"打开文件"<<filename<<"成功"<<endl;
}
void closeFile(){
currentFileBlock=-1;
currentFileName="";
}
void readFile(string filename){
if(currentDirName==""){
cout<<"请先进入一个子文件夹!"<<endl;
return;
}
if(filename!=currentFileName){
cout<<"请先打开文件!"<<endl;
}
cout<<"文件内容:"<<root.readFile(currentDirName,currentFileBlock)<<endl;
}
void writeFile(string filename,string content){
if(currentDirName==""){
cout<<"请先进入一个子文件夹!"<<endl;
return;
}
if(filename!=currentFileName){
cout<<"请先打开文件!"<<endl;
}
root.writeFile(currentDirName,currentFileBlock,content);
}
void mainloop(){
string command;
string para1;
string para2;
cout<<"欢迎来到myinux文件系统!以下为指令格式:"<<endl
<<"login password"<<"登录当前文件夹的账户"<<endl
<<"logout"<<"退出当前文件夹的账户"<<endl
<<"dir"<<"列当前文件夹的目录"<<endl
<<"createDir directoryName"<<"在主目录下创建一个新的用户文件夹(需要登录)"<<endl
<<"deleteDir directoryName"<<"在主目录下删除一个用户文件夹(需要登录)"<<endl
<<"cd directoryName"<<"进入某个子文件夹"<<endl
<<"create filename"<<"在当前用户文件夹内创建文件(需要登录)"<<endl
<<"delete filename"<<"在当前用户文件夹内删除文件(需要登录)"<<endl
<<"open dirname filename"<<"打开指定文件夹内指定文件"<<endl
<<"close"<<"关闭已打开的文件"<<endl
<<"read filename"<<"读取已经打开的文件的内容(根据文件保护码可能需要登录)"<<endl
<<"write filename content"<<"向已经打开的文件内写内容(覆盖型,根据文件保护码可能需要登录)"<<endl
<<"exit"<<"退出本系统"<<endl;
while(true){
cout<<"/"<<currentDirName<<"/:";
cin>>command;
if (command=="login"){
cin>>para1;
login(para1);
continue;
}
if (command=="logout"){
logout();
continue;
}
if (command=="dir"){
showList();
continue;
}
if (command=="createDir"){
cin>>para1;
createDir(para1);
continue;
}
if (command=="deleteDir"){
cin>>para1;
deleteDir(para1);
continue;
}
if (command=="cd"){
cin>>para1;
enterDir(para1);
continue;
}
if (command=="create"){
cin>>para1;
createFile(para1);
continue;
}
if (command=="delete"){
cin>>para1;
delFile(para1);
continue;
}
if (command=="open"){
cin>>para1;
cin>>para2;
openFile(para1,para2);
continue;
}
if (command=="close"){
closeFile();
continue;
}
if (command=="read"){
cin>>para1;
readFile(para1);
continue;
}
if (command=="write"){
cin>>para1;
cin>>para2;
writeFile(para1,para2);
continue;
}
if (command=="exit"){
cout<<"退出系统!"<<endl;
break;
}
cout<<"未定义指令,请重新输入!"<<endl;
}
}
int main()
{
/*root缺少read file,write file方法*/
//testuser();
//testfile();
//testnode();
//testsub();
//testroot();
mainloop();
return 0;
}