-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhandleInput.cpp
More file actions
191 lines (185 loc) · 5.77 KB
/
handleInput.cpp
File metadata and controls
191 lines (185 loc) · 5.77 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
#include "filesystem.h"
int handleInput(FileSystem &disk) {
/*
objective: to create a terminal like CLI to handle disk operations
input: reference to disk
return:
0: continue
1: exit
*/
std::string command;
std::cout << KGRN << "(myFileSystem)> " << KRST;
getline(std::cin, command);
if (command.empty()) return 1;
// split command into function and argument part
std::vector<std::string> tokens;
tokens = split(command, ' ');
std::string fun = tokens[0];
std::string arg = "";
if (tokens.size() > 1) {
arg = tokens[1];
}
std::string arg2 = "";
if (tokens.size() > 2) {
arg2 = tokens[2];
}
enum Choice {
HELP, INFO, TREE, READ, CHECK, WRITE, FORMAT, EXIT, NEW_FILE, CAT, DEL, UNDEL, MKDIR, CD, PWD, LS, END
};
std::string CHOICES[] = {
"help", "info", "tree", "read", "check", "write", "format", "exit", "new", "cat", "del", "undel", "mkdir", "chdir", "pwd", "list", ""
};
std::string choices[] = {
"h", "info", "tree", "r", "c", "w", "f", "q", "n", "cat", "del", "undel", "mkdir", "cd", "pwd", "ls", ""
};
Choice selected = END;
for (int i = 0; i < END; ++i) {
if (CHOICES[i] == fun || choices[i] == fun) {
selected = static_cast<Choice>(i);
break;
}
}
int sector;
// handle command
switch (selected) {
case READ: {
if (arg.empty()) {
std::cout << "usage: read SECTOR" << std::endl;
return 0;
}
// convert string to int
std::istringstream(arg) >> sector;
char readBuffer[kSectorSize_g];
disk.readSector(sector, readBuffer);
for (int i = 0; i < kSectorSize_g; ++i) {
std::cout << readBuffer[i];
}
std::cout << std::endl;
break;
}
case WRITE: {
if (arg.empty()) {
std::cout << "usage: write SECTOR" << std::endl;
return 0;
}
std::istringstream(arg) >> sector;
if (disk.getStatus(sector) == FileSystem::BUSY) {
std::cout << "Overwrite sector data (y/n)? _\b";
char t;
std::cin >> t;
if (t == 'y' || t == 'Y') {
char buf[kSectorSize_g];
createBuffer('.', buf);
disk.writeSector(sector, buf);
} else {
std::cin.ignore(32767, '\n');
return 0;
}
} else {
char buf[kSectorSize_g];
createBuffer('.', buf);
disk.writeSector(sector, buf);
disk.updateStatus(sector, 1);
}
break;
}
case CHECK: {
if (arg.empty()) {
std::cout << "usage: check SECTOR" << std::endl;
return 0;
}
std::istringstream(arg) >> sector;
int status = disk.getStatus(sector);
std::cout << "sector #" << sector << " is ";
if (status < disk.END && status > -1) {
std::cout << disk.statusMsg[status] << std::endl;
} else {
std::cout << "pointing to sector #" << status << std::endl;
}
break;
}
case FORMAT:
disk.format();
break;
case HELP: {
std::ifstream helpFile;
helpFile.open("help.txt", std::ios::in | std::ios::ate);
int helpFileSize = helpFile.tellg();
char helpMsg[helpFileSize];
helpFile.seekg(0);
helpFile.read(helpMsg, helpFileSize);
helpFile.close();
std::cout << helpMsg << std::endl;
break;
}
case INFO:
disk.info();
break;
case TREE:
disk.tree();
break;
case NEW_FILE: {
if (arg.empty()) {
std::cout << "usage: new FILE_NAME [SRC_FILE]" << std::endl;
return 0;
}
disk.createFile(arg.c_str(), arg2.c_str());
break;
}
case CAT: {
if (arg.empty()) {
std::cout << "usage: cat FILE_NAME" << std::endl;
return 0;
}
disk.readFile(arg.c_str());
break;
}
case DEL: {
if (arg.empty()) {
std::cout << "usage: del FILE_NAME" << std::endl;
return 0;
}
disk.deleteFile(arg.c_str());
break;
}
case UNDEL: {
if (arg.empty()) {
std::cout << "usage: undel FILE_NAME" << std::endl;
return 0;
}
disk.undeleteFile(arg.c_str());
break;
}
case MKDIR: {
if (arg.empty()) {
std::cout << "usage: mkdir DIR_NAME" << std::endl;
return 0;
}
disk.createDir(arg.c_str());
break;
}
case CD: {
if (arg.empty()) {
std::cout << "usage: cd DIR_NAME" << std::endl;
return 0;
}
std::vector<std::string> dir = split(arg, '/');
for (int i = 0; i < dir.size(); ++i) {
disk.changeDir(dir[i].c_str());
}
break;
}
case PWD:
disk.printWorkingDir();
break;
case LS:
disk.listDirectoryContents();
break;
case EXIT:
return 1;
default:
std::cout << "(myFileSystem): command not found: " << fun << std::endl;
return 0;
}
return 0;
}