-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
257 lines (200 loc) · 5.8 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
#include "diskSim.h"
#include <iostream>
using namespace std;
int parseInput (char *input, char *command, char *sargs, int *index, int *count, char *writechar)
{
if (input == NULL || command == NULL || sargs == NULL) {
DEBUG("HUH?");
return -1;
}
input[strlen(input) - 1] = '\0';
if (strchr(input, '\r') != NULL)
input[strlen(input) - 1] = '\0';
if (input[0] == '\0') {
DEBUG("No Command entered" << endl);
return -1;
}
const char s[2] = " ";
char *token;
token = strtok(input, s);
strcpy(command, token);
if (strcmp(token, "exit") == 0)
goto nextEmpty;
if (strcmp(token, "dump") == 0)
goto nextEmpty;
if (strcmp(token, "cr") == 0)
goto word2;
if (strcmp(token, "op") == 0)
goto word2;
if (strcmp(token, "cl") == 0)
goto word2;
if (strcmp(token, "rd") == 0)
goto word2;
if (strcmp(token, "wr") == 0)
goto word2;
if (strcmp(token, "sk") == 0)
goto word2;
if (strcmp(token, "de") == 0)
goto word2;
if (strcmp(token, "dr") == 0)
goto nextEmpty;
if (strcmp(token, "sv") == 0)
goto word2;
if (strcmp(token, "in") == 0)
goto word2;
cout << "error ";
DEBUG(token << "not a valid command");
return -1;
word2:
token = strtok(NULL, s);
if (token == NULL) {
cout << "error";
DEBUG("Not enough arguments for " << command);
return -1;
}
if (strcmp(command, "cr") == 0) {
strcpy(sargs, token);
goto nextEmpty;
}
if (strcmp(command, "de") == 0) {
strcpy(sargs, token);
goto nextEmpty;
}
if (strcmp(command, "op") == 0) {
strcpy(sargs, token);
goto nextEmpty;
}
if (strcmp(command, "sv") == 0) {
strcpy(sargs, token);
goto nextEmpty;
}
if (strcmp(command, "in") == 0) {
strcpy(sargs, token);
goto nextEmpty;
}
if (strcmp(command, "rd") == 0) {
*index = atoi(token);
if((*index < 4) && (*index > 0))
goto word3;
cout << "error ";
DEBUG("Invalid FD: read\n");
return -1;
}
if (strcmp(command, "wr") == 0) {
*index = atoi(token);
if((*index < 4) && (*index > 0))
goto word3;
cout << "error ";
DEBUG("Invalid FD: write\n");
return -1;
}
if (strcmp(command, "sk") == 0) {
*index = atoi(token);
if((*index < 4) && (*index > 0))
goto word3;
cout << "error ";
DEBUG("Invalid FD: seek\n");
return -1;
}
if (strcmp(command, "cl") == 0) {
*index = atoi(token);
if((*index < 4) && (*index > 0))
goto nextEmpty;
cout << "error ";
DEBUG("Invalid FD: close\n");
return -1;
}
word3:
token = strtok(NULL, s);
if (token == NULL) {
cout << "error";
DEBUG("Not enough arguments for " << command);
return -1;
}
if(strcmp(command, "rd") == 0) {
*count = atoi(token);
goto nextEmpty;
}
if(strcmp(command, "sk") == 0) {
*count = atoi(token);
goto nextEmpty;
}
if(strcmp(command, "wr") == 0) {
*writechar = token[0];
goto word4;
}
word4:
token = strtok(NULL, s);
if (token == NULL) {
cout << "error";
DEBUG("Not enough arguments for " << command);
return -1;
}
if(strcmp(command, "wr") == 0) {
*count = atoi(token);
goto nextEmpty;
}
nextEmpty:
token = strtok(NULL, s);
if (token == NULL) return 0;
cout << "error ";
DEBUG(command << "too many arguments" << endl);
return -1;
}
int main()
{
FileSystemSim simulation;
char input[100];
char command[30];
int index;
int count;
char sargs[30];
char writechar;
DEBUG("Begin Simulation:" << endl);
while (1)
{
/* Reset command data */
memset((void*) input, '\0', 100);
memset((void*) command, '\0', 30);
memset((void*) sargs, '\0', 30);
index = 0;
count = 0;
writechar = 0;
/* Get line and parse */
if (fgets(input, 100, stdin) == NULL) break;
if (parseInput(input, command, sargs, &index, &count, &writechar)) {
DEBUG("\n");
continue;
}
DEBUG("Command: " << command << endl);
DEBUG("Sargs: " << sargs << endl);
DEBUG("Index: " << index << endl);
DEBUG("Count: " << count << endl);
DEBUG("WriteChar: " << writechar << endl);
if (strcmp(command, "exit") == 0)
break;
if (strcmp(command, "dump") == 0)
simulation.dump();
if (strcmp(command, "op") == 0)
simulation.open(sargs);
if (strcmp(command, "cr") == 0)
simulation.createFile(sargs);
if (strcmp(command, "de") == 0)
simulation.del(sargs);
if ((strcmp(command, "cl") == 0) && (index != 0))
simulation.close(index);
if (strcmp(command, "rd") == 0)
simulation.read(index, count);
if (strcmp(command, "wr") == 0)
simulation.write(index, writechar, count);
if (strcmp(command, "sk") == 0)
simulation.seek(index, count);
if (strcmp(command, "dr") == 0)
simulation.printFiles();
if (strcmp(command, "sv") == 0)
simulation.save(sargs);
if (strcmp(command, "in") == 0)
simulation.load(sargs);
}
return 0;
}