-
Notifications
You must be signed in to change notification settings - Fork 1
/
parser.c
434 lines (326 loc) · 10.2 KB
/
parser.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
#include "headers/parser.h"
typedef struct State {
Board* board;
TodoList* cur_todolist;
TodoItem* cur_todoitem;
/* for use during processing */
char* last_block_text;
} State;
char* read_file(char* file_name, long* size);
/* processing */
void enter_todolist(State* state, char* list_name);
void exit_todolist(State* state);
void enter_todoitem(State* state, char* item_name);
void exit_todoitem(State* state);
void set_description(State* state, char* description);
void set_due(State* state, char* due); // make an acc date struct later
void add_subtask(State* state, char* subtask_name, SubTaskState subtask_state);
/* callbacks to parser */
int enter_block(MD_BLOCKTYPE type, void* detail, void* userdata);
int leave_block(MD_BLOCKTYPE type, void* detail, void* userdata);
int enter_span(MD_SPANTYPE type, void* detail, void* userdata);
int leave_span(MD_SPANTYPE type, void* detail, void* userdata);
int text(MD_TEXTTYPE type, const MD_CHAR* text, MD_SIZE size, void* userdata);
void debug_log(const char* msg, void* userdata);
void syntax(void);
const MD_PARSER parser = {
0,
MD_FLAG_TASKLISTS,
&enter_block,
&leave_block,
&enter_span,
&leave_span,
&text,
&debug_log,
&syntax
};
/* reading */
Board*
begin_parse(char* board_path)
{
const char* input_buffer;
long input_size;
State state;
Board* new_board;
/* read entire file */
input_buffer = read_file(board_path, &input_size);
/* setup state */
state.cur_todolist = NULL;
state.cur_todoitem = NULL;
new_board = malloc(sizeof(Board));
new_board->todolist_list = malloc(0);
new_board->todolist_count = 0;
state.board = new_board;
md_parse(input_buffer, input_size, &parser, &state);
/* finish calls */
exit_todolist(&state);
free((char*)input_buffer);
return state.board;
}
char*
read_file(char* file_name, long* size)
{ // apparently using seek isnt the greatest, may change to chunk reading later
FILE* file;
char* output;
long fsize;
file = fopen(file_name, "r");
if (file == NULL) {
perror("read_file > fopen");
return NULL;
}
fseek(file, 0, SEEK_END);
fsize = ftell(file);
fseek(file, 0, SEEK_SET);
output = malloc(fsize+1); // maybe error check malloc
fread(output, 1, fsize, file);
if (ferror(file)) {
printf("Something went wrong reading file\n");
fclose(file);
free(output);
return NULL;
}
output[fsize] = 0; // add null terminator
/* clean up */
fclose(file);
/* return */
*size = fsize;
return output;
}
void
enter_todolist(State* state, char* list_name)
{
TodoList* new_todolist;
new_todolist = malloc(sizeof(TodoList));
new_todolist->list_name = list_name;
new_todolist->item_list = malloc(0);
new_todolist->item_count = 0;
state->cur_todolist = new_todolist;
}
void
exit_todolist(State* state)
{
#define sb state->board
if (state->cur_todolist == NULL) return;
exit_todoitem(state);
sb->todolist_count += 1;
sb->todolist_list = realloc(sb->todolist_list, sb->todolist_count*sizeof(TodoList*));
sb->todolist_list[sb->todolist_count-1] = state->cur_todolist;
state->cur_todolist = NULL;
#undef sb
}
void
enter_todoitem(State* state, char* item_name)
{
TodoItem* new_todoitem;
new_todoitem = malloc(sizeof(TodoItem));
new_todoitem->item_name = item_name;
new_todoitem->description = strdup("");
new_todoitem->due = strdup("");
new_todoitem->subtask_list = malloc(0);
new_todoitem->subtask_count = 0;
state->cur_todoitem = new_todoitem;
}
void
exit_todoitem(State* state)
{
#define st state->cur_todolist
if (state->cur_todoitem == NULL) return;
/* append current item to todo list */
st->item_count += 1;
st->item_list = realloc(st->item_list, st->item_count*sizeof(TodoItem*));
st->item_list[st->item_count-1] = state->cur_todoitem;
state->cur_todoitem = NULL;
#undef st
}
void
set_description(State* state, char* description)
{
state->cur_todoitem->description = description;
}
void
set_due(State* state, char* due)
{
state->cur_todoitem->due = due;
}
void
add_subtask(State* state, char* subtask_name, SubTaskState subtask_state)
{
SubTask* new_subtask;
SubTask** subtask_list;
/* create new subtask */
new_subtask = malloc(sizeof(SubTask));
new_subtask->subtask_name = subtask_name;
new_subtask->done = subtask_state;
/* add it */
subtask_list = state->cur_todoitem->subtask_list;
state->cur_todoitem->subtask_count += 1;
subtask_list = realloc(subtask_list, state->cur_todoitem->subtask_count*sizeof(SubTask*));
subtask_list[state->cur_todoitem->subtask_count-1] = new_subtask;
state->cur_todoitem->subtask_list = subtask_list;
}
int
enter_block(MD_BLOCKTYPE type, void* detail, void* userdata)
{
return 0;
}
int
leave_block(MD_BLOCKTYPE type, void* detail, void* userdata)
{
State* state;
state = (State*)userdata;
switch (type) {
case MD_BLOCK_H:;
MD_BLOCK_H_DETAIL* h_detail;
h_detail = ((MD_BLOCK_H_DETAIL*)detail);
switch(h_detail->level) {
case 1:
break;
case 2:
exit_todolist(state);
enter_todolist(state, state->last_block_text);
break;
case 3:
exit_todoitem(state);
enter_todoitem(state, state->last_block_text);
break;
}
break;
case MD_BLOCK_QUOTE:
set_description(state, state->last_block_text);
break;
case MD_BLOCK_LI:;
MD_BLOCK_LI_DETAIL* li_detail;
SubTaskState done;
li_detail = ((MD_BLOCK_LI_DETAIL*)detail);
assert(li_detail->is_task != 0); // all lists should be task lists
done = li_detail->task_mark == ' ' ? SubTaskState_todo : SubTaskState_done;
add_subtask(state, state->last_block_text, done);
break;
// no need for default case for now :>
}
return 0;
}
int
enter_span(MD_SPANTYPE type, void* detail, void* userdata)
{
return 0;
}
int
leave_span(MD_SPANTYPE type, void* detail, void* userdata)
{
State* state;
state = (State*)userdata;
switch (type) {
case MD_SPAN_STRONG:
set_due(state, state->last_block_text);
break;
}
return 0;
}
int
text(MD_TEXTTYPE type, const MD_CHAR* text, MD_SIZE size, void* userdata){
State* state;
char* content;
state = (State*)userdata;
content = malloc(size*sizeof(char)+1);
memcpy(content, text, size);
content[size] = 0;
/* printf("%s\n", content); */
state->last_block_text = content;
return 0;
}
void
debug_log(const char* msg, void* userdata)
{
printf(msg);
return;
}
void
syntax(void)
{
return;
}
/* writing */
int
begin_write(char* board_path, Board* board)
{ // TODO, make a backup file of board before write in case it crashes
FILE* file;
file = fopen(board_path, "w+");
if (file == NULL) {
perror("begin_write > fopen");
return -1;
}
for (int i = 0; i < board->todolist_count; i++) {
TodoList* cur_todolist = board->todolist_list[i];
fprintf(file, "## %s\n", cur_todolist->list_name);
for (int j = 0; j < cur_todolist->item_count; j++) {
TodoItem* cur_todoitem = cur_todolist->item_list[j];
fprintf(file, "### %s\n", cur_todoitem->item_name);
/* bug rn, for some reason date is being written as description too */
if (strlen(cur_todoitem->description) > 0) {
fprintf(file, "> %s\n", cur_todoitem->description);
}
if (strlen(cur_todoitem->due) > 0) {
fprintf(file, "**%s**\n", cur_todoitem->due);
}
for (int k = 0; k < cur_todoitem->subtask_count; k++) {
SubTask* cursubtask = cur_todoitem->subtask_list[k];
char done_char = (cursubtask->done == SubTaskState_done) ? 'X' : ' ';
fprintf(file, "- [%c] %s\n", done_char, cursubtask->subtask_name);
}
}
}
fclose(file);
return 0;
}
/* others */
void
log_todo(Board* board)
{
for (int i = 0; i < board->todolist_count; i++) {
TodoList* todolist;
printf("List =-=-=-=-=-==-=-=-=-=-=-\n");
todolist = board->todolist_list[i];
printf("List name: %s\n", todolist->list_name);
printf("Num of items: %d\n", todolist->item_count);
for (int j = 0; j < todolist->item_count; j++) {
TodoItem* todoitem;
printf("Item =-=-=-=-=-\n");
todoitem = todolist->item_list[j];
printf("Item name: %s\n", todoitem->item_name);
printf("Description: %s\n", todoitem->description);
printf("Num of subtasks: %d\n", todoitem->subtask_count);
for (int k = 0; k < todoitem->subtask_count; k++) {
SubTask* subtask;
int done;
subtask = todoitem->subtask_list[k];
printf("Subtask: %s, %d\n", subtask->subtask_name, subtask->done);
}
}
}
}
int
free_board(Board* board)
{ // all the fields are being leaked rn
for (int i = 0; i < board->todolist_count; i++) {
TodoList* cur_todolist = board->todolist_list[i];
/* free(cur_todolist->list_name); */
for (int j = 0; j < cur_todolist->item_count; j++) {
TodoItem* cur_todoitem = cur_todolist->item_list[j];
/* free(cur_todoitem->item_name); */
/* free(cur_todoitem->description); */
/* free(cur_todoitem->due); */
/* free subtask later too */
for (int k = 0; k < cur_todoitem->subtask_count; k++) {
SubTask* cur_subtask = cur_todoitem->subtask_list[k];
/* free(cur_subtask->subtask_name); */
free(cur_subtask);
}
free(cur_todoitem);
}
free(cur_todolist);
}
/* free(board->board_name); */
free(board);
return 0;
}