-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
491 lines (449 loc) · 11.4 KB
/
main.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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
#include "task.h"
#include <stdio.h>
#include <limits.h>
#define HASH 1023
#define MAXSTRING 8001
char *getInput();
void clearbuffer();
int addTask(char *buffer, List *ordered, Hash *table);
int duration(int validPath, List *ordered, char last);
int depend(Hash *hashtable, char last);
int removeTask(Hash *hashtable, List *ordered, int *path, char last);
int path(Hash *table, List *ordered, char last);
char getCommand(char *command);
int main()
{
List *ordered = newList();
Hash *hashtable = createTable(HASH, sizeof(List));
int inputFlag = 0, validPath = 0;
char command[12], last;
do
{
last = getCommand(command);
inputFlag = 1;
if (!strcmp(command, "add"))
{
char *buffer;
if (last != ' ')
{
inputFlag = 1;
if (last != '\n')
clearbuffer();
}
else
{
buffer = getInput();
inputFlag = addTask(buffer, ordered, hashtable);
free(buffer);
}
validPath = 0;
}
else if (!strcmp(command, "duration"))
{
inputFlag = duration(validPath, ordered, last);
}
else if (!strcmp(command, "depend"))
{
inputFlag = depend(hashtable, last);
}
else if (!strcmp(command, "remove"))
{
inputFlag = removeTask(hashtable, ordered, &validPath, last);
}
else if (!strcmp(command, "path"))
{
inputFlag = path(hashtable, ordered, last);
if (!inputFlag)
validPath = 1;
}
else if (!strcmp(command, "exit"))
{
if (last != '\n')
{
clearbuffer();
inputFlag = 1;
}
else
break;
}
else
{
inputFlag = 1;
if (last != '\n')
clearbuffer();
}
if (inputFlag)
{
printf("illegal arguments\n");
inputFlag = 0;
}
} while (strcmp(command, "exit"));
deleteListOfTasks(ordered);
listFree(ordered);
deleteTable(hashtable);
return 0;
}
/**
* Receives a string pointer
* Returns the last read character
* Fills the array with given input
*/
char getCommand(char *command)
{
char c;
int i;
command[0] = '\0';
if ((c = getchar()) == ' ' || c == '\n')
return c;
command[0] = c;
for (i = 1; i < 11 && (c = getchar()) != ' ' && c != '\n'; i++)
command[i] = c;
command[i] = '\0';
return c;
}
/**
* Returns a pointer to a user input line
*/
char *getInput()
{
unsigned int i = 0, limit = 1000;
char *inputLine = malloc(sizeof(char) * limit);
char c;
while ((c = getchar()) != '\n')
{
if (i >= limit)
{
limit += 1000;
/* Resizes according to input size in blocks of 1000 */
inputLine = (char *)realloc(inputLine, sizeof(char) * limit);
}
inputLine[i++] = c;
}
if (i >= limit)
inputLine = (char *)realloc(inputLine, sizeof(char) * (limit + 1));
inputLine[i] = '\0';
return inputLine;
}
/**
* Clears the buffer input until next \n
*/
void clearbuffer()
{
char c;
while ((c = getchar()) != '\n')
;
}
/**
* Receives a char pointer, a List pointer and a Hash pointer
* Adds a task, given its info in the buffer, to the ordered list and to the hashtable
* Returns true if input was incorrect
*/
int addTask(char *buffer, List *ordered, Hash *table)
{
int i = 0, it = 0, counter, helpCount;
unsigned long id, duration, helper;
char *description, *c, ch;
List *precedents, *idFind;
Task *task, *idFindTask;
/* Gets ID */
c = malloc(sizeof(char) * 12);
while ((ch = buffer[it]) != ' ' && ch != '-' && it < 11)
c[it++] = ch;
c[it] = '\0';
if (ch != ' ' || (strlen(c) == 10 && strcmp(c, "4294967295") > 0))
return 1;
i += sscanf(c, "%lu", &id);
if (i != 1 || id <= 0)
{
free(c);
return 1;
}
/* Checks if ID is already used */
if ((idFind = findInTable(table, id)) && (idFindTask = findById(idFind, id)))
{
printf("id already exists\n");
free(c);
return 0;
}
/* Gets description */
it++;
description = malloc(sizeof(char) * MAXSTRING);
helpCount = 0;
counter = 0;
while (counter != 2 && helpCount < MAXSTRING - 1)
{
ch = buffer[it++];
if (ch == '\"')
counter++;
description[helpCount++] = ch;
}
description[helpCount] = '\0';
description = realloc(description, sizeof(char) * (strlen(description) + 1));
if (ch != '\"')
{
free(c);
free(description);
return 1;
}
it++;
helpCount = it;
while ((ch = buffer[it]) != ' ' && ch != '\n' && ch != '-' && ch != '\0' && it - helpCount < 11)
{
c[it - helpCount] = ch;
it++;
}
c[it - helpCount] = '\0';
if ((ch != ' ' && ch != '\n' && ch != '\0') || (strlen(c) == 10 && strcmp(c, "4294967295") > 0))
{
free(c);
free(description);
return 1;
}
/* Gets duration*/
i += sscanf(c, "%lu", &duration);
if (i != 2 || duration <= 0)
{
free(description);
free(c);
return 1;
}
/* Gets task precedents and adds to list */
precedents = newList();
free(c);
c = strtok(buffer + it, " ");
if (c != NULL)
{
do
{
i = sscanf(c, "%lu", &helper);
if (((idFind = findInTable(table, helper)) && (idFindTask = findById(idFind, helper))) && i == 1)
addEl(precedents, idFindTask);
else if (i == 1)
{
printf("no such task\n");
free(description);
listFree(precedents);
return 0;
}
else
{
free(description);
listFree(precedents);
return 1;
}
} while ((c = strtok(NULL, " \n")));
}
/* Creates task and frees memory */
task = createTask(id, duration, description, precedents);
free(description);
listFree(precedents);
addEl(ordered, task);
insertInTable(table, task, id);
return 0;
}
/**
* Receives a int, a List pointer and a char
* Prints all the elements with equal or bigger duration than the one given in input
* Returns true if input was incorrect
*/
int duration(int validPath, List *ordered, char last)
{
char c[12], ch;
int i = 0, limit = 100;
unsigned long duration;
void (*printFn)();
List ** vec;
Iterator * it;
if (last == '\n')
duration = 0;
else
{
/* Gets duration value */
while ((ch = getchar()) != '\n' && i < 11 && ch >= '0' && ch <= '9' && ch != '-')
c[i++] = ch;
c[i] = '\0';
if (ch != '\n')
{ /* Verifies if it was read a non negative number*/
clearbuffer();
return 1;
}
if (i == 0)
duration = 0;
else
{
sscanf(c, "%lu", &duration);
if (duration == 0) /* Wrong input */
return 1;
}
}
if (validPath)
printFn = printInfoTaskWithTimes;
else
printFn = printInfoTaskNoTimes;
/* Create array of all tasks */
vec = malloc(sizeof(Task*) * limit);
it = createIterator(ordered);
for (i = 0; hasNext(it); i++) {
if (i >= limit)
vec = realloc(vec, sizeof(Task*) * (i+100));
vec[i] = next(it);
}
killIterator(it);
/* Print elements */
i--;
for (; i >= 0; i--) {
if (((Task *)current(vec[i]))->duration >= duration)
print(vec[i], printFn);
}
free(vec);
return 0;
}
/**
* Receives a Hash pointer and a char
* Prints all the dependencies of task given by input
* Returns true if input was incorrect
*/
int depend(Hash *hashtable, char last)
{
int i = 0;
unsigned long id;
Task *task;
Iterator *it;
char c[12], ch;
/* Verify and get input */
if (last == '\n')
return 1;
while ((ch = getchar()) != '\n' && i < 11 && ch >= '0' && ch <= '9' && ch != '-')
c[i++] = ch;
c[i] = '\0';
if (ch != '\n')
{
clearbuffer();
return 1;
}
sscanf(c, "%lu", &id);
if (id == 0)
return 1;
/* Find task */
task = findById(findInTable(hashtable, id), id);
if (!task)
{
printf("no such task\n");
return 0;
}
/* Print precedents */
it = iteratePrecedents(task);
printf("%lu:", id);
if (hasNext(it))
printRecursive(next(it), printId);
else
printf(" no dependencies");
printf("\n");
killIterator(it);
return 0;
}
/**
* Receives a Hash pointer, a List pointer, a int pointer and a char
* Removes the task given specified via input from the memory (including from hash and list)
* Returns true if input was incorrect
*/
int removeTask(Hash *hashtable, List *ordered, int *path, char last)
{
int i = 0;
unsigned long id;
Task *task;
Iterator *it;
List *tmp, *helper;
char ch, c[12];
if (last == '\n')
return 1;
while ((ch = getchar()) != '\n' && i < 11 && ch >= '0' && ch <= '9' && ch != '-')
c[i++] = ch;
c[i] = '\0';
if (ch != '\n')
{
clearbuffer();
return 1;
}
sscanf(c, "%lu", &id);
if (id == 0)
return 1;
task = findById(findInTable(hashtable, id), id);
if (!task)
{
printf("no such task\n");
return 0;
}
else if (hasDependencies(task))
{
printf("task with dependencies\n");
return 0;
}
/* Remove from ordered linked list */
helper = ordered;
it = createIterator(ordered);
while (hasNext(it))
{
tmp = helper;
helper = next(it);
if (current(helper) == task)
{
removeEl(tmp, helper);
break;
}
}
killIterator(it);
/* Remove from hashtable */
tmp = findInTable(hashtable, id);
it = createIterator(tmp);
while (hasNext(it))
{
helper = next(it);
if (current(helper) == task)
{
removeEl(tmp, helper);
break;
}
}
killIterator(it);
deleteTask(task);
*path = 0;
return 0;
}
/**
* Receives a hash pointer, a list pointer and a char
* Calculates and prints the critical path of the program
* Returns true if input was incorrect
*/
int path(Hash *table, List *ordered, char last)
{
unsigned long int maxDuration = 0, tmp;
Iterator *it = createIterator(ordered);
Task *helper;
List *endingTasks;
if (last != '\n')
{
clearbuffer();
return 1;
}
endingTasks = newList();
while (hasNext(it))
{
helper = (Task *)current(next(it));
initializeTask(helper, ULONG_MAX);
if (!hasDependencies(helper))
addEl(endingTasks, helper);
tmp = helper->earlyStart + helper->duration;
if (maxDuration < tmp)
maxDuration = tmp;
}
killIterator(it);
it = createIterator(endingTasks);
while (hasNext(it))
updateLateStart((Task *)current(next(it)), maxDuration);
killIterator(it);
printCriticalPath(ordered);
printf("project duration = %lu\n", maxDuration);
listFree(endingTasks);
return 0;
}