-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgoal_list.c
315 lines (279 loc) · 10.1 KB
/
goal_list.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
//contains the main program for the checklist
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "goal_list.h"
#include "date_and_time.h"
#include "dynamic_arrays.h"
#define MAX_INPUT 1000
#define ARRAY_SIZE 2
int main()
{
//print_current_goals();
char input[MAX_INPUT];
char ** argv;
int argc = 0;
goal * current_goals;
goal * completed_goals;
int current_goals_size;
int completed_goals_size;
int current_goals_max_size;
int completed_goals_max_size;
FILE * read_file;
FILE * write_file;
if ((read_file = fopen("my_goals.goal", "r")) != NULL)
{
read_goal_array(¤t_goals, read_file, ¤t_goals_size, ¤t_goals_max_size);
read_goal_array(&completed_goals, read_file, &completed_goals_size, &completed_goals_max_size);
fclose(read_file);
} else
{
current_goals_size = 0;
completed_goals_size = 0;
current_goals_max_size = ARRAY_SIZE;
completed_goals_max_size = ARRAY_SIZE;
current_goals = new_array(current_goals_max_size);
completed_goals = new_array(completed_goals_max_size);
}
do
{
fgets(input, MAX_INPUT, stdin);
argv = parse_args(input, &argc);
if (argc > 0)
{
char * command = argv[0];
char * target_str = NULL;
if ((strcmp(command, "add")) == 0 || (strcmp(command, "a") == 0))
{
if (argc <= 1)
{
printf("Please specify a target array in your second argument. Type c/complete for completed goals, or g/goals for current goals.\n");
} else {
switch (get_target_array(argv[1]))
{
case 0:
add_goal(argv, argc, ¤t_goals, ¤t_goals_size, ¤t_goals_max_size);
break;
case 1:
add_goal(argv, argc, &completed_goals, &completed_goals_size, &completed_goals_max_size);
complete_goal(completed_goals, completed_goals_size - 1);
break;
default:
printf("Invalid target array. Type c/complete for completed goals, or g/goals for current goals.\n");
break;
}
}
} else if (strcmp(command, "wday") == 0)
{
int wday = get_wday(atoi(argv[1]), atoi(argv[2]), atoi(argv[3]));
printf("weekday = %d\n", wday);
} else if (strcmp(command, "show") == 0 || strcmp(argv[0], "s") == 0)
{if (argc <= 1)
{
printf("Please specify a target array in your second argument. Type c/complete for completed goals, or g/goals for current goals.\n");
} else {
switch (get_target_array(argv[1]))
{
case 0:
printf("Current goals:\n");
print_goal_array(current_goals, current_goals_size);
break;
case 1:
printf("Completed goals:\n");
print_goal_array(completed_goals, completed_goals_size);
break;
default:
printf("Invalid target array. Type c/complete for completed goals, or g/goals for current goals.\n");
break;
}
}
} else if (strcmp(command, "delete") == 0 || strcmp(command, "d") == 0)
{
{if (argc < 2)
{
printf("Please specify a target array in your second argument. Type c/complete for completed goals, or g/goals for current goals.\n");
} else if (argc < 3){
printf("Invalid input. Syntax for delete command should read \"delete <target_array> <index>\n");
} else {
int index = atoi(argv[2]);
switch (get_target_array(argv[1]))
{
case 0:
if (index < 0 || index >= current_goals_size)
{
printf("Error: %d is not a valid index in the current goals array.\n", index);
} else {
printf("Goal [%d] - \"%s\" deleted from current goals array\n", index, current_goals[index].name);
remove_element(current_goals, ¤t_goals_size, index);
}
break;
case 1:
if (index < 0 || index >= completed_goals_size)
{
printf("Error: %d is not a valid index in the completed goals array.\n", index);
} else {
printf("Goal [%d] - \"%s\" deleted from completed goals array\n", index, completed_goals[index].name);
remove_element(completed_goals, &completed_goals_size, index);
}
break;
default:
printf("Invalid target array. Type c/complete for completed goals, or g/goals for current goals.\n");
break;
}
}
}
} else if (strcmp(command, "complete") == 0 || strcmp(command, "c") == 0)
{
if (argc < 2)
{
printf("Invalid input. Syntax for complete command should read \"complete <index>\"\n");
} else
{
int index = atoi(argv[1]);
if (index < 0 || index >= current_goals_size)
{
printf("Error: %d is not a valid index in the current goals array.\n", index);
} else {
complete_goal(current_goals, index);
goal removed = current_goals[index];
remove_element(current_goals, ¤t_goals_size, index);
add_element(&completed_goals, &completed_goals_max_size, &completed_goals_size, removed);
}
}
} else if (strcmp(command, "renew") == 0 || strcmp(command, "r") == 0){
if (argc < 2)
{
printf("Invalid input. Syntax for renew command should read \"renew <index>\"\n");
} else {
int index = atoi(argv[1]);
if (index < 0 || index >= completed_goals_size)
{
printf("Erorr: %d is not a valid index in the completed goals array.\n", index);
} else {
renew_goal(completed_goals, index);
goal removed = completed_goals[index];
remove_element(completed_goals, &completed_goals_size, index);
add_element(¤t_goals, ¤t_goals_max_size, ¤t_goals_size, removed);
}
}
} else if (strcmp(command, "change_target_date") == 0 || strcmp(command, "ctd") == 0)
{
if (argc < 4)
{
printf("Invalid input. Syntax for change_target_date command should read \"change_target_date <target_array> <index> <date>\"\n");
} else
{
int index = atoi(argv[2]);
switch (get_target_array(argv[1]))
{
case 0:
if (index < 0 || index >= current_goals_size)
{
printf("Error: %d is not a valid index in the current goals array.\n", index);
} else
{
if (argc < 5)
{
set_date_target(current_goals, index, argv[3]);
} else
{
set_date_target_w_time(current_goals, index, argv[3], argv[4]);
}
}
break;
case 1:
if (index < 0 || index >= completed_goals_size)
{
printf("Error: %d is not a valid index in the completed goals array.\n", index);
} else
{
if (argc < 5)
{
set_date_target(completed_goals, index, argv[3]);
} else
{
set_date_target_w_time(completed_goals, index, argv[3], argv[4]);
}
}
break;
default:
printf("Invalid target array. Type c/complete for completed goals, or g/goals for current goals.\n");
break;
}
}
} else if (strcmp(command, "change_name") == 0 || strcmp(command, "cn") == 0)
{
if (argc < 4)
{
printf("Invalid input. Syntax for change_name command should read \"change_name <target_array> <index> <new_name>\"\n");
} else {
int index = atoi(argv[2]);
switch (get_target_array(argv[1]))
{
case 0:
if (index < 0 || index >= current_goals_size)
{
printf("Error: %d is not a valid index in the current goals array.\n", index);
} else
{
set_name(current_goals, index, argv[3]);
}
break;
case 1:
if (index < 0 || index >= completed_goals_size)
{
printf("Error: %d is not a valid index in the completed goals array.\n", index);
} else
{
set_name(completed_goals, index, argv[3]);
}
break;
default:
printf("Invalid target array. Type c/complete for completed goals, or g/goals for current goals.\n");
break;
}
}
} else if (strcmp(command, "save") == 0 | strcmp(command, "sv") == 0)
{
if ((write_file = fopen("my_goals.goal", "w")) == NULL)
{
printf("Error opening my_goals.goal for writing");
return -1;
} else
{
write_goal_array(current_goals, write_file, current_goals_size);
write_goal_array(completed_goals, write_file, completed_goals_size);
fclose(write_file);
}
}
else if (strcmp(command, "exit") == 0 || strcmp(command, "quit") == 0)
{
break;
} else {
printf("Invalid command. type help for a list of commands.\n");
}
}
free_parsed_args(argv);
} while (1);
//write to file
if ((write_file = fopen("my_goals.goal", "w")) == NULL)
{
printf("Error opening my_goals.goal for writing");
return -1;
}
write_goal_array(current_goals, write_file, current_goals_size);
write_goal_array(completed_goals, write_file, completed_goals_size);
fclose(write_file);
//free all used memory
for (int i = 0; i < current_goals_size; i++)
{
free_goal_name(current_goals[i]);
}
for (int i = 0; i < completed_goals_size; i++)
{
free_goal_name(completed_goals[i]);
}
free(current_goals);
free(completed_goals);
}