-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_fix.c
417 lines (366 loc) · 10.6 KB
/
main_fix.c
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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
/*
* File: main_fix.c
* Author: Tiago Antunes
* Description: A program that simulates a file system in C.
*/
#include "proj2.h"
/* List of available commands and descriptions. */
const char *LIST_CMD[N_CMD] =
{HELP, QUIT, SET, PRINT, FIND, LIST, SEARCH, DELETE};
const char *LIST_CMD_DESC[N_CMD] =
{HELP_DESC, QUIT_DESC, SET_DESC, PRINT_DESC, FIND_DESC, LIST_DESC, SEARCH_DESC, DELETE_DESC};
const char *slash = "/";
/* Reads commands from stdin and executes the corresponding functions. */
int main() {
char cmd[MAXCHARCMD];
Item root = startFS();
while(scanf("%s", cmd) && !eq(cmd, QUIT)) {
if (eq(cmd, HELP))
getHelp();
else if (eq(cmd, SET))
setValue(root);
else if (eq(cmd, PRINT))
printPathValue(root->subdiretories);
else if (eq(cmd, FIND))
findPathValue(root);
else if (eq(cmd, LIST))
listSubPath(root);
else if (eq(cmd, SEARCH))
searchPath(root);
else if (eq(cmd, DELETE))
deletePath(root);
}
freeDir(root);
return 0;
}
/* Prints all available commands. */
void getHelp() {
int i;
for (i = 0; i < N_CMD; i++) {
printf("%s: %s\n", LIST_CMD[i], LIST_CMD_DESC[i]);
}
}
/* Concatenates the string src to the string dest */
char *strConcat(char *dest, char *src) {
int len1 = strlen(src);
int len2 = strlen(dest);
dest = (char *) realloc(dest, sizeof(char) * (len1+len2+1));
strcat(dest, src);
return dest;
}
/* Divides path into the names between "/" and returns that list. */
char **cutPath(char **dirs, char path[], int *i) {
int j=0, n=5;
char *token;
dirs = (char**) malloc(n*sizeof(char*));
token = strtok(path, slash);
while (token != NULL) {
char *dir;
dir = stringdup(token);
if(j > n-1) {
n = 2*n;
dirs = (char**) realloc(dirs, n*sizeof(char *));
}
*(dirs+j) = dir;
j++;
token = strtok(NULL, slash);
}
free(token);
*i = j-1;
return dirs;
}
/* Adds or modifies a value in the path given by the user. */
void setValue(diretory root) {
int i = 0;
char buffer[MAXCHAR];
char path[MAXCHAR];
char buf[MAXCHAR] = {0};
char *value = NULL;
char **dirs = NULL;
char *name = (char *) malloc(sizeof(char));
name = strcpy(name, ROOT);
/* Get input */
fgets(buffer,MAXCHAR,stdin);
sscanf(buffer, "%s", path);
sscanf(buffer, "%*s%[^\n]", buf);
value = shortValue(buf);
/* Separate paths */
dirs = cutPath(dirs, path, &i);
/* Create of modifie the path */
setValueAux(root, dirs, name, value, 0, i);
free(value);
while (i >= 0) {
free(*(dirs+i));
i--;
}
free(dirs);
}
/* Searches for the directory with name "name"
in the subdiretories of root and returns it. */
Item findOrCreateDir(Item root, Item parent, char *name) {
root = STsearch(root->tree, name);
if (root == NULL) {
char *new_name = stringdup(name);
Item new_dir = newDir(new_name, NULL, parent);
insertEndDirSub(parent, new_dir);
STinsert(parent->tree, new_dir);
root = new_dir;
}
return root;
}
/* Finds the position and creates or modifies the directory in the given path. */
void setValueAux(Item root, char **dirs, char *name, char *value, int level, int depth) {
name = strConcat(name, *(dirs+level));
root = findOrCreateDir(root, root, name);
if (level == depth) {
free(value(root));
value(root) = stringdup(value);
return;
}
else {
name = strConcat(name, ROOT);
setValueAux(root, dirs, name, value, level+1, depth);
}
}
/* Print all paths and associated values. */
void printPathValue(Item root) {
/* While still possible go in depth. */
while (root != NULL && root->subdiretories != NULL) {
if (value(root) != NULL)
printf("%s %s\n", name(root), value(root));
printPathValue(root->subdiretories);
break;
}
/* Go through all components of the path. */
while (root != NULL) {
if (root->subdiretories == NULL) {
if (value(root) != NULL)
printf("%s %s\n", name(root), value(root));
}
printPathValue(root->next);
break;
}
}
/* Prints the value stored in the path given by the user. */
void findPathValue(Item root) {
int i=0;
char buffer[MAXCHAR];
char path[MAXCHAR];
char **dirs = NULL;
char *name = (char *) malloc(sizeof(char));
name = strcpy(name, ROOT);
/* Get input */
fgets(buffer,MAXCHAR,stdin);
sscanf(buffer, "%s", path);
/* Separate paths */
dirs = cutPath(dirs, path, &i);
/* Find the path and print the value. */
findPathValueAux(root, dirs, name, 0, i);
while (i >= 0) {
free(*(dirs+i));
i--;
}
free(dirs);
}
/* Search for the path given and print the value associated. */
void findPathValueAux(Item root, char **dirs, char *name, int level, int depth) {
name = strConcat(name, *(dirs+level));
root = STsearch(root->tree, name);
if (root == NULL) {
printf("not found\n");
return;
}
if (level == depth) {
if (value(root) == NULL) {
printf("no data\n");
}
else {
printf("%s\n", value(root));
}
return;
}
else {
name = strConcat(name, ROOT);
findPathValueAux(root, dirs, name, level+1, depth);
}
}
/* List all imediate components of a path in alphabetical order. */
void listSubPath(Item root) {
int i=0;
char buffer[MAXCHAR];
char path[MAXCHAR];
char **dirs = NULL;
char *name = (char *) malloc(sizeof(char));
name = strcpy(name, ROOT);
/* Get input */
fgets(buffer,MAXCHAR,stdin);
if (sscanf(buffer, "%s", path) == -1) {
STsort(*(root->tree), visit);
return;
}
/* Separate paths */
dirs = cutPath(dirs, path, &i);
/* Search and print the components */
listSubPathAux(root, dirs, name, 0, i);
while (i >= 0) {
free(*(dirs+i));
i--;
}
free(dirs);
}
/* Search for the given path and print all the imediate
components in alphabetical order */
void listSubPathAux(diretory root, char **dirs, char *name, int level, int depth) {
name = strConcat(name, *(dirs+level));
root = STsearch(root->tree, name);
if (root == NULL) {
printf("not found\n");
return;
}
if (level == depth) {
STsort(*(root->tree), visit);
return;
}
else {
name = strConcat(name, ROOT);
listSubPathAux(root, dirs, name, level+1, depth);
}
}
/* Searches for the path that has the value given by the user. */
void searchPath(diretory root) {
char buffer[MAXCHAR] = {0};
char *value;
/* Get input */
fgets(buffer, MAXCHAR, stdin);
value = shortValue(buffer);
/* Search path. */
if (!searchPathAux(root->subdiretories, value)) {
printf("not found\n");
}
free(value);
}
/* Search for the first diretory with the given value. */
int searchPathAux(diretory root, char *value) {
if (root != NULL && value(root) != NULL && eq(value(root), value)) {
printf("%s\n", name(root));
return 1;
}
else {
int in = 0; /* States if the value was found or not. */
/* Search in depth. */
while (root != NULL && root->subdiretories != NULL) {
in = searchPathAux(root->subdiretories, value);
break;
}
/* Search on same level. */
while (root != NULL && in != 1) {
in = searchPathAux(root->next, value);
break;
}
return in;
}
}
/* Deletes a path and all its subpaths. */
void deletePath(diretory root) {
int i=0;
char buffer[MAXCHAR];
char path[MAXCHAR];
char **dirs = NULL;
char *name = (char *) malloc(sizeof(char));
name = strcpy(name, ROOT);
/* Get inputs */
fgets(buffer,MAXCHAR,stdin);
if (sscanf(buffer, "%s", path) == -1) {
STfree(root->tree);
root->subdiretories = NULL;
root->last_subdiretories = NULL;
return;
}
dirs = cutPath(dirs, path, &i);
/* Delete the path. */
deletePathAux(root, dirs, name, 0, i);
while (i >= 0) {
free(*(dirs+i));
i--;
}
free(dirs);
}
/* Search and delete the given path and all its subpaths. */
void deletePathAux(diretory root, char **dirs, char *name, int level, int depth) {
diretory target = (diretory) malloc(sizeof(struct node));
name = strConcat(name, *(dirs+level));
target = STsearch(root->tree, name);
if (target == NULL) {
printf("not found\n");
return;
}
else if (level == depth) {
if (target->previous == NULL) {
target->upper->subdiretories = target->next;
if (target->next != NULL) {
target->next->previous = NULL;
}
}
if (target->next == NULL) {
target->upper->last_subdiretories = target->previous;
if (target->previous != NULL) {
target->previous->next = NULL;
}
}
if (target->previous != NULL && target->next != NULL) {
target->previous->next = target->next;
target->next->previous = target->previous;
}
if (target->upper != NULL) {
STdelete(target->upper->tree, target->name);
}
return;
}
else {
name = strConcat(name, ROOT);
deletePathAux(target, dirs, name, level+1, depth);
}
}
/* Builds a string with no spaces on start and end. */
char *shortValue(char buf[]) {
int len = 0;
int new_len;
int min, max;
char *value = NULL;
len = strlen(buf);
for(min = 0; isspace_char(buf[min]); min++) {}
for(max = len-1; isspace_char(buf[max]); max--) {}
if (min <= max) {
new_len = max - min + 2;
value = (char*) malloc(new_len * sizeof(char));
copystr(buf, min, max, value);
}
return value;
}
/* Copies the contents of buf between
min and max to a new string */
void copystr(char buf[], int min, int max, char *value) {
int i = 0;
while(min <= max) {
*(value + i) = buf[min];
min++;
i++;
}
*(value+i) = '\0';
}
/* Checks if character c is a space character */
int isspace_char(char c) {
return c == ' ' || c == '\t' || c == '\0' || c == '\n' || c == '\r';
}
char *stringdup(char *orig) {
char *cpy;
if (orig == NULL) {
cpy = NULL;
}
else {
int len = strlen(orig);
cpy = (char*) malloc(len+1 * sizeof(char));
strcpy(cpy, orig);
}
return cpy;
}