-
Notifications
You must be signed in to change notification settings - Fork 0
/
taskmanager.c
152 lines (126 loc) · 4.06 KB
/
taskmanager.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
#include "taskmanager.h"
#define CMP_IF_ZERO(A,B) A? A:B
struct tm __get_localetime(){
/*
Return: the current time
*/
time_t t = time(NULL);
struct tm time = *localtime(&t);
return time;
}
char *get_formated_timestamp(struct tm *timestamp){
/*
format tm to formated string that can be used with sqlite timestamp
@timestamp: the desired time to format
*/
if (!timestamp || timestamp->tm_year == 0) // default year means the time stamp isn't intialized
return NULL;
char * buffer = (char *) malloc(sizeof(char)*30);
strftime(buffer, 30, "\"%Y-%m-%d %H:%M\"",timestamp);
return buffer;
}
task * create_task(const char *todo,size_t n,struct tm reminder, enum RemindFreq remind_freq, uint8_t set_reminder){
/*
Return: pointer to the task created
*/
task * p = (task*) malloc(sizeof(task));
p->todo= (char*) malloc(sizeof(char)*n);
strcpy(p->todo,todo);
p->creation_time = __get_localetime();
p->archive = NOT_ARCHIVE;
if(set_reminder)
p->reminder_time = reminder;
p->has_reminder = set_reminder;
p->remind_freq = remind_freq;
return p;
}
int get_reminder_format(char *ireminder,struct tm *reminder_time){
/*
*Takes the user input for reminder, and generate timestamp upon.
*it searches for two formats:
*--'+NF' -- where N= duration, F= Unit (Month,Day,etc..)
*-- 'YYYY MM DD HH mm' the exact time, what is not found in the timestamp is added as now
*@ireminder : input string for reminder format
*@reminder_time : processed and returned timestamp for reminder
*@Returns: 0 for success and -1 if the format is wrong
*/
*reminder_time = __get_localetime();
if(ireminder[0] == '+') //+NF
{
char plus,param;
int duration;
sscanf(ireminder,"%c%d%c",&plus,&duration,¶m);
if(!duration)
return -1;
if(param =='\0')
return -1;
switch(param){
case 'm':
reminder_time->tm_min += duration;
break;
case 'H':
reminder_time->tm_hour += duration;
break;
case 'D':
reminder_time->tm_mday += duration;
break;
case 'W':
reminder_time->tm_mday += duration * 7;
break;
case 'M':
reminder_time->tm_mon += duration;
break;
case 'Y':
reminder_time->tm_year += duration;
break;
default:
return -1;
}
}
else{
int year,month,day,hour,min;
sscanf(ireminder,"%d %d %d %d %d",&year,&month,&day,&hour,&min);
if (!year && !month && !day && !hour && !min)
return -1;
reminder_time->tm_year = year ? year-1900:reminder_time->tm_year;
reminder_time->tm_mon = month ? month-1 :reminder_time->tm_mon;
reminder_time->tm_mday = CMP_IF_ZERO(day,reminder_time->tm_mday);
reminder_time->tm_hour = CMP_IF_ZERO(hour,reminder_time->tm_hour);
}
mktime(reminder_time);
return 0;
}
int get_frequent_reminder(char * iremind,enum RemindFreq *remind_freq){
/*
Sets the enum according the input of the user
@iremind: input remind format to be processed
@remind_freq: returned remind_freq
@Return: 0 for success , -1 for invalid format
*/
int i;
iremind [strlen(iremind) - 1] = 0; // removes /n character
for(i=0; iremind[i] != '\0';i++)
iremind[i] = tolower(iremind[i]); //Lowering all characters to make it general
if(!strcmp(iremind,"once")){
*remind_freq = ONCE;
return 0;
}
else if(!strcmp(iremind,"daily")){
*remind_freq = DAILY;
return 0;
}
else if(!strcmp(iremind,"weekly"))
{
*remind_freq = WEEKLY;
return 0;
}
else if(!strcmp(iremind,"monthly")){
*remind_freq = MONTHLY;
return 0;
}
else if(!strcmp(iremind,"yearly")){
*remind_freq = YEARLY;
return 0;
}
return -1;
}