-
Notifications
You must be signed in to change notification settings - Fork 0
/
Command.cpp
108 lines (94 loc) · 2.27 KB
/
Command.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
#include "Global.h"
#include "Command.h"
#include <iostream>
#include <sstream>
#include <fstream>
using namespace std;
vector<string> Command::getCommand(const string& str) {
/* 初始化cmdList */
vector<string> cmdList;
cmdList.clear();
stringstream cmd(str); // 把str作为流输入
string cmdTemp;
while (cmd >> cmdTemp) {
cmdList.push_back(cmdTemp);
}
return cmdList;
}
void Command::autotest() {
string filename = "./TestFiles/cmd.txt";
ifstream infile("./TestFiles/cmd.txt", ios::in | ios::binary);
if (!infile.is_open()) {
cerr << "无法打开文件" << filename << endl;
exit(ERROR_OPEN_FILE);
}
string buffer;
while (getline(infile, buffer)) {
executeCommand(getCommand(buffer));
}
}
void Command::executeCommand(const vector<string>& cmdList) {
/* 判断cmd */
if (cmdList.size() == 0) {
return;
}
string cmd = cmdList[0]; // 取首项作为指令
if (cmd == "format") {
cout << "格式化整个磁盘空间,请重启程序" << endl;
globalOpenFileTable.Format();
globalINodeTable.Format();
globalBufferManager.Bformat();
globalFileSystem.Format();
exit(0);
}
else if (cmd == "autotest") {
autotest();
}
else if (cmd == "creat") {
// globalUser.Mkdir(cmdList[1]);
// cout << cmdList[1] << " " << cmdList[2] << endl;
globalUser.Create(cmdList[1], cmdList[2]);
}
else if (cmd == "open") {
globalUser.Open(cmdList[1], cmdList[2]);
f_handler[cmdList[1]] = globalUser.u_ar0[User::EAX];
}
else if (cmd == "read") {
if (cmdList[2] == "-o") {
globalUser.Read(to_string(f_handler[cmdList[1]]), cmdList[3], cmdList[4]);
}
else {
globalUser.Read(to_string(f_handler[cmdList[1]]), "", cmdList[2]);
}
}
else if (cmd == "write") {
globalUser.Write(to_string(f_handler[cmdList[1]]), cmdList[2], cmdList[3]);
}
else if (cmd == "seek") {
globalUser.Seek(to_string(f_handler[cmdList[1]]), cmdList[2], cmdList[3]);
}
else if (cmd == "close") {
globalUser.Close(to_string(f_handler[cmdList[1]]));
}
else if (cmd == "mkdir") {
globalUser.Mkdir(cmdList[1]);
}
else if (cmd == "rm") {
globalUser.Delete(cmdList[1]);
}
else if (cmd == "ls") {
globalUser.Ls();
}
else if (cmd == "cd") {
globalUser.Cd(cmdList[1]);
}
else if (cmd == "exit") {
exit(0);
}
else if (cmd == "") {
return;
}
else {
cout << "未识别到的符号: " << cmd << endl;
}
}