-
Notifications
You must be signed in to change notification settings - Fork 0
/
main-try1.c
181 lines (150 loc) · 4.39 KB
/
main-try1.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
#include <stdio.h>
#include <stdlib.h>
#include <getopt.h>
#include <string.h>
#include <ctype.h>
#include <time.h>
#include <pthread.h>
#include "fs/operations.h"
#define MAX_COMMANDS 150000
#define MAX_INPUT_SIZE 100
int numberThreads = 0;
char inputCommands[MAX_COMMANDS][MAX_INPUT_SIZE];
int numberCommands = 0;
int headQueue = 0;
int insertCommand(char* data) {
if(numberCommands != MAX_COMMANDS) {
strcpy(inputCommands[numberCommands++], data);
return 1;
}
return 0;
}
char* removeCommand() {
if(numberCommands > 0){
numberCommands--;
return inputCommands[headQueue++];
}
return NULL;
}
void errorParse(){
fprintf(stderr, "Error: command invalid\n");
exit(EXIT_FAILURE);
}
void processInput(FILE *input){
int i = 0;
char line[MAX_INPUT_SIZE];
/* break loop with ^Z or ^D */
while (fgets(line, sizeof(line)/sizeof(char), input)) {
char token, type;
char name[MAX_INPUT_SIZE];
int numTokens = sscanf(line, "%c %s %c", &token, name, &type);
/* perform minimal validation */
if (numTokens < 1) {
continue;
}
switch (token) {
case 'c':
if(numTokens != 3)
errorParse();
if(insertCommand(line))
break;
return;
case 'l':
if(numTokens != 2)
errorParse();
if(insertCommand(line))
break;
return;
case 'd':
if(numTokens != 2)
errorParse();
if(insertCommand(line))
break;
return;
case '#':
break;
default: { /* error */
errorParse();
}
}
i++;
}
}
void applyCommands(){
while (numberCommands > 0){
const char* command = removeCommand();
if (command == NULL){
continue;
}
char token, type;
char name[MAX_INPUT_SIZE];
int numTokens = sscanf(command, "%c %s %c", &token, name, &type);
if (numTokens < 2) {
fprintf(stderr, "Error: invalid command in Queue\n");
exit(EXIT_FAILURE);
}
int searchResult;
switch (token) {
case 'c':
switch (type) {
case 'f':
printf("Create file: %s\n", name);
create(name, T_FILE);
break;
case 'd':
printf("Create directory: %s\n", name);
create(name, T_DIRECTORY);
break;
default:
fprintf(stderr, "Error: invalid node type\n");
exit(EXIT_FAILURE);
}
break;
case 'l':
searchResult = lookup(name);
if (searchResult >= 0)
printf("Search: %s found\n", name);
else
printf("Search: %s not found\n", name);
break;
case 'd':
printf("Delete: %s\n", name);
delete(name);
break;
default: { /* error */
fprintf(stderr, "Error: command to apply\n");
exit(EXIT_FAILURE);
}
}
}
}
int main(int argc, char* argv[]) {
/* verify if number of arguments is right */
/* !! mudar p 5 dps de implementar o ultimo parametro !! */
if (argc != 4) {
printf("Number of arguments invalid.\n");
exit(EXIT_FAILURE);
}
/* init filesystem */
init_fs();
/* opening the input and output files */
FILE *input, *output;
input = fopen(argv[1],"r");
if (input == NULL) {
perror(argv[1]);
}
output = fopen(argv[2],"w");
clock_t begin = clock();
/* process input and print tree */
processInput(input);
applyCommands();
print_tecnicofs_tree(output);
/* release allocated memory */
destroy_fs();
/* closing the files */
fclose(input);
fclose(output);
clock_t end = clock();
printf("TecnicoFS completed in %f seconds\n", (double) (end-begin)/CLOCKS_PER_SEC);
exit(EXIT_SUCCESS);
}