-
Notifications
You must be signed in to change notification settings - Fork 1
/
task.c
198 lines (166 loc) · 4.81 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
#define _GNU_SOURCE // For asprintf()
#include <math.h> //for parsedate(), task_dump()
#include <regex.h> //gnu's regex.h
#include <stdbool.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h> //for parsedate()
#include <regex.h> //gnu's regex.h
#include "dbg.h"
//#include "str.h" //from libstr library
#include "bstrlib.h"
#include "date.h"
#include "task.h"
// Make a new task.
Task* task_new(){
Task* t = (Task*)malloc(sizeof(Task));
task_clear(t);
return t;
}
// Free a task.
void task_free(Task* task){
bcstrfree(task->description);
free(task);
}
// Set the task's string to the string given.
void task_set_string(Task* t, const char* string){
t->description = strdup(string);
}
// Appends the string to the task.
int task_append(Task* t, const char* string){
// If it's a null, return true, there's an error.
if (t == NULL) return 1;
if (!t->description) t->description = strdup(string);
else asprintf(&(t->description), "%s%s", t->description, string);
// No error, so return false.
return 0;
}
// Get the priority of this task.
char get_priority(Task* task){
char ch = task->priority;
return ch;
}
// Set the completion status of this task.
void task_set_complete(Task* task, bool status){
task->complete = status;
}
void task_set_lineno(Task* task, int lineno){ task->linenumber = lineno; }
void task_show(Task* t){
char* dumped = task_dump(t);
printf("%d: %s\n", t->linenumber, dumped);
}
/** Dumps out the current task's data in Todo.txt format.*/
char* task_dump(Task* t){
// Sanity checks.
if (t == NULL) return NULL;
if (t->description == NULL) return NULL;
if (strcmp(t->description, "") == 0) return NULL;
char* returnString;
int err = asprintf(&returnString, "%s", t->description);
check(err != 0, "Asprintf cannot load description of task");
// Build the completion part
if (t->complete){
// TODO: Add the completion date.
//char* date = get_date();
check(asprintf(&returnString, "x %s", returnString) != -1, "Asprintf died.");
}
// return the string.
return returnString;
error:
free(returnString);
return NULL;
}
//converts from form "YYYY-MM-DD" to DDMMYYYY as an int
date* parsedate(char* expr){
int year = 0;
int month = 0;
int day = 0;
sscanf(expr, "%d-%d-%d", &year, &month, &day);
date* d = date_new(year, month, day);
return d;
}
static date *extract_date(bstring taskstr){
bstring datestr = bmidstr(taskstr, 0, 11);
return parsedate(bstr2cstr(datestr, ' '));
}
static char extract_priority(bstring taskstr){
bstring prio = bmidstr(taskstr, 0, 4);
if (prio->data[0] == '(' &&
prio->data[2] == ')' &&
prio->data[3] == ' ' &&
isupper(prio->data[1])){
return prio->data[1];
} else {
return '\0';
}
bdestroy(prio);
}
static bool extract_complete(bstring taskstr){
if (taskstr->data[0] == 'x')
return true;
else return false;
}
static bstring remove_part(bool b, bstring s, size_t len){
bstring ret;
if (b){
ret = bmidstr(s, len, s->slen);
} else ret = s;
return ret;
}
//Parse a string into a Task.
int task_parse(Task* task, char* str){
//sanity checks
check(task, "Task was not given.");
check(str, "String is null.");
bstring s = bfromcstr(str);
task->complete = extract_complete(s);
s = remove_part(task->complete, s, 2);
if (task->complete && isdigit(s->data[0])){
task->date_completed = extract_date(s);
s = remove_part(true, s, 11);
}
if (isdigit(s->data[0])){
task->date_started = extract_date(s);
s = remove_part(true, s, 11);
}
task->priority = extract_priority(s);
bool x = (task->priority != '\0');
s = remove_part(x, s, 4);
task->description = bstr2cstr(s, ' ');
bdestroy(s);
return 0;
error:
return 1;
}
// Returns true if the search finds something; false otherwise.
bool task_has_keyword(Task* t, const char* keyword){
if (strstr(t->description, keyword) == NULL){
return false;
} else {
return true;
}
}
int compare_completion(Task* a, Task* b){
if (a->complete && !b->complete) return 1;
if (!a->complete && b->complete) return -1;
return 0;
}
int task_default_compare(void* a, void* b){
Task* ta = a;
Task* tb = b;
// Move all the complete tasks to the bottom.
int complete = compare_completion(a,b);
if (complete != 0) return complete;
return strcmp(ta->description, tb->description);
}
void task_clear(Task* t){
t->linenumber = 0;
// Set the description to a newly allocated space in memory with nothing in it.
t->description = NULL;
t->priority = ' ';
t->complete = false;
t->date_started = NULL;
t->date_completed = NULL;
}