-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtodo.c
394 lines (310 loc) · 9.92 KB
/
todo.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <ctype.h>
#include <unistd.h>
#include "todo.h"
// readHeader reads header into the head and sets cursor of the file
// to the end of header
int readHeader(header *head, FILE *fd) {
if (head == NULL) {
return 0; // valid error
}
// rewind to the beginning of the file
// NOTE: fseek calls fflush if needed
if (fseek(fd, 0L, SEEK_SET) != 0) {
return 0;
}
memset(head, 0, sizeof(header));
if (fread(head, sizeof(header), 1, fd) != 1) {
return 0;
}
/* long int cursor = ftell(fd); */
/* printf("cusor at %ld, size is: %d", cursor, sizeof(header)); */
return 1;
}
int writeHeader(header *head, FILE *fd) {
// TODO: should i zero header space in the file?
// get current position of cursor
long int cursor = ftell(fd);
if (cursor == -1) {
return 0;
}
// rewind to the beginning of the file
// NOTE: fseek calls fflush if needed
if (fseek(fd, 0L, SEEK_SET) != 0) {
return 0;
}
// wirte header to the file
if (fwrite(head, sizeof(header), 1, fd) != 1) {
return 0;
}
// return to the previous position
return fseek(fd, cursor, SEEK_SET) == 0;
}
int isNumber(char *str) {
int len = strlen(str);
for (int i = 0; i != len; i++) {
if (!isdigit(str[i])) return 0;
}
return 1;
}
int lookupTask(FILE *stream, int id, task *t) {
if (t == NULL) {
// TODO: create temporaray task
}
// TODO: should i use temp task?
header head;
if (!readHeader(&head, stream)) {
fprintf(stderr, "could not read file header: %s", strerror(errno));
return 0;
}
int low = 0;
int high = head.count - 1;
int mid;
int offset;
while (low <= high) {
mid = ((unsigned int)low + (unsigned int)high) >> 1;
offset = sizeof(header)+(sizeof(task)*mid); // TODO: bounds check
if(fseek(stream, offset, SEEK_SET) == -1) {
fprintf(stderr, "could not seek file offset %d: %s", offset, strerror(errno));
return 0;
}
if (fread(t, sizeof(task), 1, stream) != 1) {
fprintf(stderr, "could not read task: %s", strerror(errno));
return 0;
}
if (t->id < id) {
low = mid + 1;
} else if (t->id > id) {
high = mid - 1;
} else {
if(fseek(stream, offset, SEEK_SET) == -1) {
fprintf(stderr, "could not seek file offset %d: %s", offset, strerror(errno));
return 0;
}
return 1;
}
}
return 0;
}
int doneCommand(int argc, char **argv, char *file_path) {
if (argc < 1) {
fprintf(stderr, "done: missing task id argument\n");
return 0;
}
int target_id = atoi(argv[0]);
if(access(file_path, F_OK) != 0) {
return 1;
}
FILE *stream;
if (!(stream = fopen(file_path, "r+"))) {
fprintf(stderr, "done: could not open todo file %s: %s\n",
file_path, strerror(errno));
return 0;
}
task t;
memset(&t, 0, sizeof(task));
if (!lookupTask(stream, target_id, &t)) {
fprintf(stderr, "done: could not find task %d\n", target_id);
fclose(stream);
return 0;
}
t.done_at = time(NULL);
if (fwrite(&t, sizeof(task), 1, stream) != 1) {
fprintf(stderr, "done: could not write task to the file: %s\n", strerror(errno));
fclose(stream);
return 0;
}
fclose(stream);
return 1;
}
static int cmptasks(const void *p1, const void *p2) {
const task *t1 = (task *)p1;
const task *t2 = (task *)p2;
const int t1_done = (t1->done_at > 0);
const int t2_done = (t2->done_at > 0);
if (t1_done != t2_done)
return t1_done > t2_done ? 1 : -1;
if (t1->priority == t2->priority) {
if (t1->id == t1->id) return 0;
return t1->id < t2->id ? -1 : 1;
}
return t1->priority > t2->priority ? -1 : 1;
}
// list of known priority tags in order highest priority
char *colors[] = {"\033[32m","\033[34m","\033[33m","\033[31m"};
int listCommand(int argc, char **argv, char *file_path) {
int narg = 0;
int show_done = 0;
char project[MAX_PROJECT];
memset(&project, 0, sizeof(project));
for (; narg != argc; narg += 2) {
if ((narg+1) > argc || argv[narg][0] != '-') {
break;
}
if (strcmp("-p", argv[narg]) == 0 ||
strcmp("--project", argv[narg]) == 0) {
if (strlen(argv[narg + 1]) + 1 > MAX_PROJECT) {
fprintf(stderr, "list: project name \"%s\" is to long\n", argv[narg + 1]);
continue;
}
strcpy(project, argv[narg + 1]);
continue;
}
if (strcmp("-d", argv[narg]) == 0 ||
strcmp("--done", argv[narg]) == 0) {
show_done = 1;
continue;
}
}
if(access(file_path, F_OK) != 0) {
return 1;
}
FILE *stream;
if (!(stream = fopen(file_path, "r"))) {
fprintf(stderr, "list: could not open todo file %s: %s\n",
file_path, strerror(errno));
return 0;
}
header head;
if (!readHeader(&head, stream)) {
fprintf(stderr, "list: could not read file %s header: %s",
file_path, strerror(errno));
return 0;
}
task *tasks = malloc(sizeof(task) * head.count);
task *t = tasks;
while (fread(t, sizeof(task), 1, stream) == 1) t++;
qsort(tasks, head.count, sizeof(task), cmptasks);
for (int i = 0; i < head.count; i++) {
if (strlen(project) > 0 && strcasecmp(project, tasks[i].project) != 0) {
continue;
}
int is_done = (tasks[i].done_at > 0);
if (!show_done && is_done) continue;
int color = 0;
for (int j = 63; j <= 257; j+= 64) {
if (tasks[i].priority < j) {
break;
}
color++;
}
if (is_done) printf("\033[9m");
printf("%3d [%s]\t%s%s\033[0m",
tasks[i].id, tasks[i].project, colors[color], tasks[i].description);
if (is_done) printf("\033[0m");
printf("\n");
}
if (errno != 0) {
fprintf(stderr, "list: could not read task from file: %s\n", strerror(errno));
fclose(stream);
return 0;
}
fclose(stream);
return 1;
}
int addCommand(int argc, char **argv, char *file_path) {
task t;
header h;
memset(&h, 0, sizeof(header));
memset(&t, 0, sizeof(task));
// TODO(nk2ge5k): open file and read header
// if file does not eixsts than initialize empty header
int narg = 0;
for (; narg != argc; narg += 2) {
if ((narg+1) > argc || argv[narg][0] != '-') {
break;
}
if (strcmp("-p", argv[narg]) == 0 ||
strcmp("--project", argv[narg]) == 0) {
if (strlen(argv[narg + 1]) + 1 > MAX_PROJECT) {
fprintf(stderr, "add: project name \"%s\" is to long\n", argv[narg + 1]);
return 0;
}
strcpy(t.project, argv[narg + 1]);
continue;
}
if (strcmp("-P", argv[narg]) == 0 ||
strcmp("--priority", argv[narg]) == 0) {
if (!isNumber(argv[narg+1])) {
fprintf(stderr, "add: invalid value for \"%s\" option, expected number\n",
argv[narg]);
return 0;
}
int p = atoi(argv[narg+1]);
if (p < 0 || 255 <= p) {
fprintf(stderr, "add: invalid value of \"%s\" option, "
"expected number between 0 and 255\n", argv[narg]);
return 0;
}
t.priority = (priority)p;
continue;
}
fprintf(stderr, "add: unknown option \"%s\"\n", argv[narg]);
return 0;
}
if (narg == argc) {
fprintf(stderr, "add: task description is required\n");
return 0;
}
for (; narg != argc; narg++) {
if (strlen(t.description) + strlen(argv[narg]) + 2 > MAX_DESCRIPTION) {
fprintf(stderr, "add: message too long\n");
return 0;
}
strcat(t.description, argv[narg]);
strcat(t.description, " ");
}
FILE *stream;
if (access(file_path, R_OK | W_OK) == 0) {
if (!(stream = fopen(file_path, "r+"))) {
fprintf(stderr, "add: could not open todo file %s: %s\n",
file_path, strerror(errno));
fclose(stream);
return 0;
}
// TODO: read header
if (!readHeader(&h, stream)) {
fprintf(stderr, "add: could not read file header: %s\n",
strerror(errno));
fclose(stream);
return 0;
}
} else {
// TODO: initFile(FILE *fd)
if (!(stream = fopen(file_path, "w+"))) {
fprintf(stderr, "add: could not open todo file %s: %s\n",
file_path, strerror(errno));
fclose(stream);
return 0;
}
if (!writeHeader(&h, stream)) {
fprintf(stderr, "add: could not write header to the file: %s\n",
strerror(errno));
fclose(stream);
return 0;
}
}
if (fseek(stream, 0, SEEK_END) != 0) {
fprintf(stderr, "add: failed to seek file offset: %s", strerror(errno));
fclose(stream);
return 0;
}
t.created_at = time(NULL);
t.id = ++h._id_seq;
h.count++;
size_t written = fwrite(&t, sizeof(task), 1, stream);
if (written != 1) {
fprintf(stderr, "add: could not write task to the file: %s\n", strerror(errno));
fclose(stream);
return 0;
}
if (!writeHeader(&h, stream)){
fprintf(stderr, "add: could not write header to the file: %s\n", strerror(errno));
fclose(stream);
return 0;
}
return 1;
}