Repository navigation
Expand file tree
/
Copy pathtasks.c
More file actions
375 lines (332 loc) · 12.3 KB
/
Copy pathtasks.c
File metadata and controls
375 lines (332 loc) · 12.3 KB
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "tasks.h"
typedef struct Task{
char *name;
int complete;
int priority;
int task_id;
struct Task *next;
//Date
int day, month, year;
} Task;
int addTask(TaskManager *manager, char *task_name, int id, int priority, int day, int month, int year, int isComplete){
Task *newTask = malloc(sizeof(Task));
if(newTask == NULL || task_name == NULL){
printf("Add Task Failure.\n");
return 0;
}
newTask->name = strdup(task_name);
newTask->complete = isComplete;
newTask->priority = priority;
if(id <= 0){
newTask->task_id = manager->next_id;
manager->next_id++;
manager->size++;
}
else{
newTask->task_id = id;
}
newTask->day = day;
newTask->month = month;
newTask->year = year;
newTask->next = newTask ;
if (manager->tail == NULL){
manager->tail = newTask;
return 1;
}
// Circular tail pointer makes insertion O(1) complexity.
newTask->next = manager->tail->next;
manager->tail->next = newTask; //updates the tail pointer to point to the newest node (task id start - 1 2 3 4 - task id tail)
manager->tail = newTask; //tail should be the last node in the list
return 1;
}
int deleteTask(TaskManager *manager, int del_task_id){
if (manager->tail == NULL){
printf("There are no list items to delete.\n");
return 0;
}
Task *temp = manager->tail->next;
Task *prev = manager->tail;
do{
if(temp->task_id == del_task_id){
prev->next = temp->next;
if(temp == manager->tail && temp->next == temp){ // Need extra case for when the tail deletion is the last node in the list
free(temp->name);
free(temp);
manager->tail = NULL; // Sets the tail ptr to null
return 1;
}
if (temp == manager->tail) {
manager->tail = prev;
}
free(temp->name); //needed because we allocated name earlier with strdup()
free(temp);
manager->size--;
return 1;
}
prev = prev->next;
temp = temp->next;
}while(temp != manager->tail->next);
return 0;
}
int completeTask(TaskManager *manager, int complete_task_id){
if(manager->tail == NULL){
printf("There are no list items to complete.\n");
return 0;
}
Task *temp = manager->tail->next;
do{
if(temp->task_id == complete_task_id){
temp->complete = !temp->complete;
return 1;
}
temp = temp->next;
}while(temp != manager->tail->next);
return 0;
}
int printList(TaskManager *manager, int sort){
if(manager->tail == NULL){
printf("To-Do List is EMPTY!\nGO FIND SOMETHING TO DO!!\n");
return 0;
}
Task *temp = manager->tail->next;
if(sort == 1){ //PRIORITY PRINT
printf("\n== HIGH PRIORITY TASKS ==");
printf("\n------------------------------");
do{
if(temp->priority == HIGH){ //PRINTING THE HIGH PRIO TASKS
printf("\n[%d] %s [%s]", temp->task_id, temp->name, (temp->complete ? "DONE" : " "));
printf("\nDue: %d/%d/%d", temp->day, temp->month, temp->year);
}
temp = temp->next;
} while(temp != manager->tail->next);
printf("\n\n== MEDIUM PRIORITY TASKS ==");
printf("\n------------------------------");
do{
if(temp->priority == MEDIUM){ //PRINTING THE MED PRIO TASKS
printf("\n[%d] %s [%s]", temp->task_id, temp->name, (temp->complete ? "DONE" : " "));
printf("\nDue: %d/%d/%d", temp->day, temp->month, temp->year);
}
temp = temp->next;
} while(temp != manager->tail->next);
printf("\n\n== LOW PRIORITY TASKS ==");
printf("\n------------------------------");
do{
if(temp->priority == LOW){ //PRINTING THE LOW PRIO TASKS
printf("\n[%d] %s [%s]", temp->task_id, temp->name, (temp->complete ? "DONE" : " "));
printf("\nDue: %d/%d/%d", temp->day, temp->month, temp->year);
}
temp = temp->next;
} while(temp != manager->tail->next);
}
else if(sort == 2){ //COMPLETION PRINT
printf("\n=== COMPLETED TASKS ===");
printf("\n------------------------------");
do{
if(temp->complete){
printf("\n[%d] %s", temp->task_id, temp->name);
printf("\nDue: %d/%d/%d", temp->day, temp->month, temp->year);
}
temp = temp->next;
} while(temp != manager->tail->next);
printf("\n\n=== INCOMPLETE TASKS ===");
printf("\n------------------------------");
do{
if(!temp->complete){
printf("\n[%d] %s", temp->task_id, temp->name);
printf("\nDue: %d/%d/%d", temp->day, temp->month, temp->year);
}
temp = temp->next;
} while(temp != manager->tail->next);
}
return 1;
}
void freeList(TaskManager *manager){
if(manager->tail == NULL){
printf("List already Empty Gang...\n");
}
else{
Task *cur;
Task *prev = manager->tail->next;
Task *head = manager->tail->next; //holds the head ptr so we make sure this address is cleared.
do{
cur = prev->next;
free(prev->name);
free(prev);
prev = cur;
} while(cur != head);
manager->tail = NULL;
manager->size = 0;
manager->next_id = 1;
}
}
void editTask(TaskManager *manager, int task_id){
if(manager->tail == NULL){
printf("List is empty.\n");
}
else{
Task *temp = manager->tail->next;
int found = 0;
do{
if(temp->task_id == task_id){
found = !found;
break;
}
temp = temp->next;
} while(temp != manager->tail->next);
if(!found){
printf("Task mentioned was not found. Sorry!");
}
else{
while(found){
int choice;
printf("What would you like to change about %s?\n", temp->name);
printf("1. Change task Name.\n");
printf("2. Update task Priority.\n");
printf("3. Update task Completion.\n");
printf("4. Update task Due Date\n");
printf("5. Exit Task Editing\n");
printf("Your Choice: ");
char buff1[10];
fgets(buff1, sizeof(buff1), stdin);
choice = atoi(buff1);
switch(choice){
case 1: //CHANGE TASK NAME
printf("What would you like the new task name to be: ");
char name[100];
fgets(name, sizeof(name), stdin);
name[strcspn(name, "\n")] = '\0';
free(temp->name);
temp->name = strdup(name);
printf("Edit Successful!");
break;
case 2://CHANGE TASK PRIORITY
printf("What should the new Priority be? (1 = LOW, 2 = MEDIUM, 3 = HIGH): ");
char buff2[10];
fgets(buff2, sizeof(buff2), stdin);
int priority = atoi(buff2);
if(priority != LOW && priority != MEDIUM && priority != HIGH){
printf("Wrong priority, try again.");
break;
}
temp->priority = priority;
printf("Priority updated!");
break;
case 3: //TOGGLE TASK COMPLETION
completeTask(manager, temp->task_id);
printf("Toggled Task Completion!");
break;
case 4:
printf("Date: ");
char buff3[10];
fgets(buff3, sizeof(buff3), stdin);
int day = atoi(buff3);
printf("Month: ");
char buff4[10];
fgets(buff4, sizeof(buff4), stdin);
int month = atoi(buff4);
printf("Year: ");
char buff5[10];
fgets(buff5, sizeof(buff5), stdin);
int year = atoi(buff5);
temp->day = day, temp->month = month, temp->year = year;
printf("The new task date is %d/%d/%d.", temp->day, temp->month, temp->year);
break;
case 5: //EXIT TASK EDITING
printf("Exitting task editing...");
found = !found;
break;
default:
printf("Wrong choice, try again.");
break;
}
printf("\n");
}
}
}
}
void searchTaskByName(TaskManager *manager, char* task_name){
if (manager->tail == NULL){
printf("\nList is still empty G.");
return;
}
Task *temp = manager->tail->next;
do{
if(strcmp(task_name, temp->name) == 0){
printf("\nFound it!");
printf("\n[%d] %s [%s]", temp->task_id, temp->name, (temp->complete ? "DONE" : " "));
printf("\nDue: %d/%d/%d", temp->day, temp->month, temp->year);
return;
}
temp = temp->next;
} while(temp != manager->tail->next);
printf("\nCouldn't find the task you inputted. Try inputting the exact name structure.");
}
void searchTaskById(TaskManager *manager, int task_id){
if (manager->tail == NULL){
printf("\nList is still empty G.");
return;
}
Task *temp = manager->tail->next;
do{
if(task_id == temp->task_id){
printf("\nFound it!");
printf("\n[%d] %s [%s]", temp->task_id, temp->name, (temp->complete ? "DONE" : " "));
printf("\nDue: %d/%d/%d", temp->day, temp->month, temp->year);
return;
}
temp = temp->next;
} while(temp != manager->tail->next);
printf("\nCouldn't find the task you inputted. Try again.");
}
void saveTasks(TaskManager *manager){
if(manager->tail == NULL){
printf("No files saved to tasklist.txt.\n");
return;
}
FILE *fptr;
fptr = fopen("tasklist.txt", "w");
if(fptr == NULL){
printf("Unable to open tasklist.txt\n");
return;
}
Task *temp = manager->tail->next;
// Stores in form: size|next_id
fprintf(fptr, "%d|%d", manager->size, manager->next_id);
do{
//Stores in form of task_id|task_name|task_priority|task_completion|due_day/due_month/due_year
fprintf(fptr, "\n%d|%s|%d|%d|%d/%d/%d", temp->task_id, temp->name, temp->priority, temp->complete,
temp->day, temp->month, temp->year);
temp = temp->next;
} while(temp != manager->tail->next);
printf("Saved tasks to tasklist.txt!\n");
fclose(fptr);
}
void loadTasks(TaskManager *manager){
FILE *fptr;
fptr = fopen("tasklist.txt", "r");
if(fptr == NULL){
printf("Couldn't read from the tasklist.txt");
return;
}
char line[150];
if(fgets(line, sizeof(line), fptr)){
int size, next_id;
if(sscanf(line, "%d|%d", &size, &next_id)){
manager->size = size;
manager->next_id = next_id;
}else { printf("Bad file format. Couldn't load tasks from tasklist.txt.");
return;}
char name[100];
int id, priority, isComplete, day, month, year;
while(fgets(line, sizeof(line), fptr)){
if(sscanf(line, "%d|%[^|]|%d|%d|%d/%d/%d", &id, name, &priority, &isComplete, &day, &month, &year)){
addTask(manager, name, id, priority, day, month, year, isComplete);
}else{ return; }
}
printf("Tasks Loaded from tasklist.txt!\n");
}
fclose(fptr);
}