This repository has been archived by the owner on Sep 19, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
269 lines (221 loc) · 4.69 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
/*
*
* main.c
*
* Baltasar Dinis 89416
* IAED project
*
* main file
*
* defines the interaction for the program
*
* dependencies:
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <ctype.h>
#include "task.h"
#include "p_task.h"
#include "input.h"
#include "planner.h"
#include "cmd.h"
/*
* number of possible command
* also the number of implemented functionallities,
* represented by their wrapper functions
*/
#define N_CMDS 8
/*-------------------------------*/
/* prototypes */
/*-------------------------------*/
void add(planner *project);
void duration(planner *project);
void depend(planner *project);
void remove_task(planner *project);
void path(planner *project);
void first(planner *project);
void second(planner *project);
void exit_proj(planner *project);
/*-------------------------------*/
/*-------------------------------*/
/*-------------------------------*/
int main(int argc, char *argv[])
{
cmd command;
planner *project;
/* function array, with the implemented functionalities */
void (*func[])(planner *) =
{&add, &duration, &depend, &remove_task, &path, &first, &second, &exit_proj};
project = planner_();
do {
if (end_of_file())
return 1;
command = get_cmd();
if (valid_command(command)) {
func[command](project);
}
else {
printf("illegal arguments\n");
}
/* move stream to next line */
flush_line();
} while(command != EXIT);
return 0;
}
/*
* function: add
*
* adds a task to the project
* project: ptr to planner
*
* parses the rest of the cmd string to call the add_task function
* correct input:
* add <id> <description> <dur> <ids>
*/
void add(planner *project)
{
size_t n_ids;
unsigned long id, dur, *ids;
char *description;
if (!get_ulong(&id)) {
printf("illegal arguments\n");
return;
}
if (!get_quoted_str(&description)) {
printf("illegal arguments\n");
return;
}
if (!get_ulong(&dur)) {
printf("illegal arguments\n");
free(description);
return;
}
if (!get_ulong_list(&ids, &n_ids)) {
printf("illegal arguments\n");
free(description);
return;
}
add_task(project, id, description, dur, ids, n_ids);
free(description);
if (ids != NULL)
free(ids);
}
/*
* function: duration
*
* lists the tasks with a given (or longer) duration
* project: ptr to planner
*
* parses the rest of the cmd string to call the list_task_duration function
*/
void duration(planner *project)
{
unsigned long dur = 0;
if (end_of_line()) {
print_with_duration(project, 0);
}
else if (!get_ulong(&dur) || !end_of_line()) {
printf("illegal arguments\n");
return;
}
else {
print_with_duration(project, dur);
}
}
/*
* function: depend
*
* lists the dependencies of a task
* project: ptr to planner
*
* parses the rest of the cmd string to call the list_dependencies function
*/
void depend(planner *project)
{
unsigned long id = 0;
if (!get_ulong(&id) || !end_of_line()) {
printf("illegal arguments\n");
return;
}
print_dependencies(project, id);
}
/*
* function: remove_task
*
* removes a task from the project
* project: ptr to planner
*
* parses the rest of the cmd string to call the remove_task_id function
*/
void remove_task(planner *project)
{
unsigned long id;
if (!get_ulong(&id) || !end_of_line()) {
printf("illegal arguments\n");
return;
}
remove_task_id(project, id);
}
/*
* function: path
*
* lists the critical path of a project
* project: ptr to planner
*
* parses the rest of the cmd string to call the proj_path function
*/
void path(planner *project)
{
if (!end_of_line()) {
printf("illegal arguments\n");
}
print_critical_path(project);
}
/*
* function: first
*
* prints the largest task_id of the project
* project: ptr to planner
*
* parses the rest of the cmd string to call the print_largest_id function
*/
void first(planner *project)
{
if (!end_of_line()) {
printf("illegal arguments\n");
}
print_largest_id(project);
}
/*
* function: second
*
* prints the id of the task with more dependencies
* project: ptr to planner
*
* parses the rest of the cmd string to call the print_task_with_more_dependencies
* dependencies
*/
void second(planner *project)
{
if (!end_of_line()) {
printf("illegal arguments\n");
}
print_task_with_more_dependencies(project);
}
/*
* function: exit_proj
*
* packs everything up to quit the program
* project: ptr to planner
*
* parses the rest of the cmd string to call the free_project function
*/
void exit_proj(planner *project)
{
if (!end_of_line()) {
printf("illegal arguments\n");
}
free_planner(project);
}