This repository has been archived by the owner on Jan 25, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtask.c
446 lines (352 loc) · 12.1 KB
/
task.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
#include "task.h"
// ---------------------------------------------------------------------------
// Task struct basic functions
/**
* Compute task's end time by adding duration to start time.
* @param task the task in question.
* @return task's end time.
*/
time_t get_end_time(const Task *task) {
return task->t_time + task->t_duration_in_mins*SECS_PER_MIN;
}
/**
* Task struct basic data entry.
* @param task where entered data reside.
*/
void input_task(Task *task) {
int temp;
printf("Name: "); gets(task->t_name);
printf("Importance (0-255): "); scanf("%d", &temp);
task->t_importance_rtn = (uint8_t)temp;
printf("Repeated: "); scanf("%d", &temp);
task->t_repeat_cnt = (uint16_t)temp;
printf("Time (secs from 1/1/1900): "); scanf("%d", &task->t_time);
printf("Duration (minutes): "); scanf("%d", &temp);
task->t_duration_in_mins = (uint16_t)temp;
printf("Flags: "); scanf("%d", &task->flags);
}
/**
* Print task's fields onto the screen.
* @param task task to print.
*/
void print_task(const Task *task) {
time_t t_end = get_end_time(task);
printf("\nTask: %s\n", task->t_name);
printf("Importance: %d\n", task->t_importance_rtn);
printf("Time: from %s ", time2str(&task->t_time));
printf("to %s\n", time2str(&t_end));
printf("Repeated %d times\n", task->t_repeat_cnt);
printf("Active: %s\n", (task->flags & FLAG_ACTIVE)?"Yes":"No");
printf("Daily: %s\n", (task->flags & FLAG_DAILY)?"Yes":"No");
printf("Weekly: %s\n", (task->flags & FLAG_WEEKLY)?"Yes":"No");
printf("Collision warning: %s\n",
(task->flags & FLAG_COLLISION_WARNING)?"Yes":"No");
}
/**
* Update task based on current time and it's flags.
* Check if task's time has passed,
* update it based on whether it's daily, weekly or one-time.
* @param task the task being checked.
* @param now time of type time_t used as reference.
*/
void update_task(Task *task) {
time_t now;
time(&now); // get current time
if(now<get_end_time(task)) // compare with target date
return;
// Task has passed, check if it is recurrent (daily or weekly):
if(task->flags & FLAG_DAILY)
while(now>=get_end_time(task)) { // make next time arrangement
task->t_time += SECS_PER_DAY;
task->t_repeat_cnt++; // add 1 to repeated times
}
else if(task->flags & FLAG_WEEKLY)
while(now>=get_end_time(task)) {
task->t_time += SECS_PER_WEEK;
task->t_repeat_cnt++;
}
else // a one-time job, deactivate it
task->flags &= ~FLAG_ACTIVE; // deactivate task
}
// ---------------------------------------------------------------------------
// File manipulation functions
/**
* Open tasks file, return an integer.
* @param file_name name of the file containing data of tasks.
* @return number of tasks if successful, else -1.
*/
long int get_task_cnt(const char *file_name) {
FILE *fp;
long int file_size;
fp = fopen(file_name, "rb");
if(fp == NULL) return UNSUCCESSFUL;
fseek(fp, 0, SEEK_END);
file_size = ftell(fp);
fclose(fp);
if(file_size%sizeof(Task)) {
printf("Error: Invalid file structure...\n");
return UNSUCCESSFUL;
}
return file_size/sizeof(Task);
}
/**
* Save task onto hard disk, return an integer.
* @param task task to save.
* @param file_name name of file to open.
* @return 0 is successful, else -1.
*/
int save_task(Task *task, const char *file_name) {
FILE *fp = fopen(file_name, "ab");
if(fp == NULL) {
fp = fopen(file_name, "wb");
if(fp == NULL) {
printf("Error: Unable to open file...\n");
return UNSUCCESSFUL;
}
}
fwrite(task, sizeof(Task), 1, fp);
fclose(fp);
return SUCCESSFUL;
}
/**
* Read a task from file, return an integer.
* @param task place-holder for the task read from file.
* @param index number of tasks from the beginning of the file to the task
* in question.
* @param file_name name of the file containing data of tasks.
* @return 0 if successful, else -1.
*/
int read_task(Task *task, long int index, const char *file_name) {
FILE *fp;
fp = fopen(file_name, "rb");
if(fp == NULL) return UNSUCCESSFUL;
fseek(fp, index*sizeof(Task), SEEK_SET);
if(fread(task, sizeof(Task), 1, fp) == NULL) {
printf("Error: Specified index exceeds file size...\n");
return UNSUCCESSFUL;
}
fclose(fp);
return SUCCESSFUL;
}
/**
* Read a number of consecutive tasks from file, return an integer.
* @param task place-holder for the task read from file.
* @param index number of tasks from the beginning of the file to the first
task read.
* @param file_name name of the file containing data of tasks.
* @return number of task read if successful, else -1.
*/
int read_tasks(Task **tasks,
long int index,
int num_to_read,
const char *file_name) {
FILE *fp;
int task_cnt;
fp = fopen(file_name, "rb");
if(fp == NULL) return UNSUCCESSFUL;
*tasks = realloc(*tasks, num_to_read*sizeof(Task));
fseek(fp, index*sizeof(Task), SEEK_SET);
for(task_cnt = 0;
fread(*(tasks+task_cnt), sizeof(Task), 1, fp) != NULL
&& task_cnt < num_to_read;
task_cnt++);
fclose(fp);
*tasks = realloc(*tasks, task_cnt*sizeof(Task));
return task_cnt;
}
/**
* Get on going tasks from file, return an integer.
* @param tasks place-holder for tasks read from file.
* @param file_name name of the file containing data of tasks.
* @return number of on going task if successful, else -1.
*/
int get_current_tasks(Task **tasks, const char *file_name) {
FILE *fp;
time_t now;
long int task_cnt;
long int task_cnt_max;
task_cnt_max = get_task_cnt(file_name);
if(task_cnt_max<1) return UNSUCCESSFUL;
fp = fopen(file_name, "rb");
if(fp == NULL) return UNSUCCESSFUL;
*tasks = realloc(*tasks, task_cnt_max*sizeof(Task));
time(&now); // get current time
task_cnt = 0;
while(fread((*tasks+task_cnt), sizeof(Task), 1, fp) != NULL) {
if((*tasks+task_cnt)->flags & FLAG_ACTIVE
&& (*tasks+task_cnt)->t_time < now
&& get_end_time((*tasks+task_cnt)) > now) {
task_cnt++;
}
}
fclose(fp);
*tasks = realloc(*tasks, task_cnt*sizeof(Task));
return task_cnt;
}
/**
* Get next task from file, return an integer.
* @param task place-holder for the task read from file.
* @param file_name name of the file containing data of tasks.
* @return number of minutes until read task start if successful, else -1.
*/
int get_next_task(Task *task,
uint8_t importance_threshold,
const char *file_name) {
FILE *fp;
Task *buffer;
time_t now;
time_t min_time = TIME_T_MAX;
fp = fopen(file_name, "rb");
if(fp == NULL) return UNSUCCESSFUL;
buffer = (Task *)malloc(sizeof(Task));
time(&now); // get current time
while(fread(buffer, sizeof(Task), 1, fp) != NULL) {
if(buffer->flags & FLAG_ACTIVE
&& buffer->t_importance_rtn > importance_threshold
&& buffer->t_time > now
&& buffer->t_time - now < min_time) {
*task = *buffer;
min_time = task->t_time - now;
}
}
fclose(fp);
free(buffer);
if(buffer == NULL) return UNSUCCESSFUL;
if(min_time == TIME_T_MAX) return UNSUCCESSFUL;
return min_time/SECS_PER_MIN;
}
/**
* Read tasks to be completed today from file, save to another file.
* @param dest_file_name name of the file to save to.
* @param file_name name of the file containing data of tasks.
* @return number of tasks read if successful, else -1.
*/
int get_day_tasks(const char *dest_file_name, const char *file_name) {
FILE *fp;
FILE *fp_out;
Task *task;
time_t now;
time_t midnight;
int task_cnt;
fp = fopen(file_name, "rb");
if(fp == NULL) return UNSUCCESSFUL;
fp_out = fopen(dest_file_name, "wb");
if(fp == NULL) {
fclose(fp);
return UNSUCCESSFUL;
}
task = (Task *)malloc(sizeof(Task));
time(&now); // get current time
midnight = get_midnight(now); // get midnight
task_cnt = 0;
while(fread(task, sizeof(Task), 1, fp) != NULL) {
if(task->flags & FLAG_ACTIVE
&& task->t_time > now
&& task->t_time < midnight) {
task_cnt++;
fwrite(task, sizeof(Task), 1, fp_out);
}
}
fclose(fp);
fclose(fp_out);
free(task);
return task_cnt;
}
/**
* Read important tasks 'til next sunday from file.
* @param dest_file_name name of the file to save to.
* @param file_name name of the file containing data of tasks.
* @return number of tasks read if successful, else -1.
*/
int get_week_tasks(const char *dest_file_name, const char *file_name) {
FILE *fp;
FILE *fp_out;
Task *task;
time_t now;
time_t weekend;
int task_cnt;
fp = fopen(file_name, "rb");
if(fp == NULL) return UNSUCCESSFUL;
fp_out = fopen(dest_file_name, "wb");
if(fp == NULL) {
fclose(fp);
return UNSUCCESSFUL;
}
task = (Task *)malloc(sizeof(Task));
time(&now); // get current time
weekend = get_weekend_midnight(now); // get weekend midnight
task_cnt = 0;
while(fread(task, sizeof(Task), 1, fp) != NULL) {
if(task->flags & FLAG_ACTIVE
&& task->t_time > now
&& task->t_time < weekend
&& task->t_importance_rtn >= IMPORTANCE_THRESHOLD) {
task_cnt++;
fwrite(task, sizeof(Task), 1, fp_out);
}
}
fclose(fp);
fclose(fp_out);
free(task);
return task_cnt;
}
/**
* Read and update all tasks from file.
* @param file_name name of file to open.
* @return 0 is successful, else -1.
*/
int update_all_tasks(const char *file_name) {
FILE *fp;
FILE *fp_tmp;
Task *task;
fp = fopen(file_name, "rb");
if(fp == NULL) return UNSUCCESSFUL;
fp_tmp = fopen(TMP_FILE_NAME, "wb");
if(fp_tmp == NULL) {
fclose(fp); // close fp as it has been opened
printf("Error: Unable to open file...\n");
return UNSUCCESSFUL;
}
task = (Task *)malloc(sizeof(Task));
while(fread(task, sizeof(Task), 1, fp) != NULL) {
if(task->flags & FLAG_ACTIVE) update_task(task);
fwrite(task, sizeof(Task), 1, fp_tmp);
}
fclose(fp);
fclose(fp_tmp);
free(task);
remove(file_name);
rename(TMP_FILE_NAME, file_name);
return SUCCESSFUL;
}
/**
* Remove a task on file, return an integer.
* @param index number of tasks from the beginning of the file to the task
* in question.
* @param file_name name of the file containing data of tasks.
* @return 0 if successful, else -1.
*/
int delete_task(long int index, const char *file_name) {
FILE *fp;
FILE *fp_tmp;
Task *task;
fp = fopen(file_name, "rb");
if(fp == NULL) return UNSUCCESSFUL;
fp_tmp = fopen(TMP_FILE_NAME, "wb");
if(fp_tmp == NULL) {
fclose(fp); // close fp as it has been opened
printf("Error: Unable to open file...\n");
return UNSUCCESSFUL;
}
task = (Task *)malloc(sizeof(Task));
for(long int i = 0;
fread(task, sizeof(Task), 1, fp) != NULL;
i++)
if(i != index) fwrite(task, sizeof(Task), 1, fp_tmp);
fclose(fp);
fclose(fp_tmp);
free(task);
remove(file_name);
rename(TMP_FILE_NAME, file_name);
return SUCCESSFUL;
}