2 #include "headers/parser.h"
6 TodoList* cur_todolist;
7 TodoItem* cur_todoitem;
8 /* for use during processing */
12 char* read_file(char* file_name, long* size);
15 void enter_todolist(State* state, char* list_name);
16 void exit_todolist(State* state);
18 void enter_todoitem(State* state, char* item_name);
19 void exit_todoitem(State* state);
21 void set_description(State* state, char* description);
22 void set_due(State* state, char* due); // make an acc date struct later
23 void add_subtask(State* state, char* subtask_name, SubTaskState subtask_state);
25 /* callbacks to parser */
26 int enter_block(MD_BLOCKTYPE type, void* detail, void* userdata);
27 int leave_block(MD_BLOCKTYPE type, void* detail, void* userdata);
28 int enter_span(MD_SPANTYPE type, void* detail, void* userdata);
29 int leave_span(MD_SPANTYPE type, void* detail, void* userdata);
30 int text(MD_TEXTTYPE type, const MD_CHAR* text, MD_SIZE size, void* userdata);
31 void debug_log(const char* msg, void* userdata);
34 const MD_PARSER parser = {
48 log_todo(Board* board)
50 for (int i = 0; i < board->todolist_count; i++) {
52 printf("List =-=-=-=-=-==-=-=-=-=-=-\n");
53 todolist = board->todolist_list[i];
54 printf("List name: %s\n", todolist->list_name);
55 printf("Num of items: %d\n", todolist->item_count);
57 for (int j = 0; j < todolist->item_count; j++) {
59 printf("Item =-=-=-=-=-\n");
60 todoitem = todolist->item_list[j];
61 printf("Item name: %s\n", todoitem->item_name);
62 printf("Description: %s\n", todoitem->description);
63 printf("Num of subtasks: %d\n", todoitem->subtask_count);
65 for (int k = 0; k < todoitem->subtask_count; k++) {
69 subtask = todoitem->subtask_list[k];
70 printf("Subtask: %s, %d\n", subtask->subtask_name, subtask->done);
77 begin_parse(char* board_path)
79 const char* input_buffer;
84 /* read entire file */
85 input_buffer = read_file(board_path, &input_size);
88 state.cur_todolist = NULL;
89 state.cur_todoitem = NULL;
91 new_board = malloc(sizeof(Board));
92 new_board->todolist_list = malloc(0);
93 new_board->todolist_count = 0;
94 state.board = new_board;
96 md_parse(input_buffer, input_size, &parser, &state);
99 exit_todolist(&state);
101 free((char*)input_buffer);
107 read_file(char* file_name, long* size)
108 { // apparently using seek isnt the greatest, may change to chunk reading later
113 file = fopen(file_name, "r");
115 printf("Something went wrong opening file\n");
119 fseek(file, 0, SEEK_END);
121 fseek(file, 0, SEEK_SET);
123 output = malloc(fsize+1); // maybe error check malloc
124 fread(output, 1, fsize, file);
126 printf("Something went wrong reading file\n");
131 output[fsize] = 0; // add null terminator
142 enter_todolist(State* state, char* list_name)
144 TodoList* new_todolist;
146 new_todolist = malloc(sizeof(TodoList));
147 new_todolist->list_name = list_name;
148 new_todolist->item_list = malloc(0);
149 new_todolist->item_count = 0;
151 state->cur_todolist = new_todolist;
155 exit_todolist(State* state)
157 #define sb state->board
159 if (state->cur_todolist == NULL) return;
161 exit_todoitem(state);
163 sb->todolist_count += 1;
164 sb->todolist_list = realloc(sb->todolist_list, sb->todolist_count*sizeof(TodoList*));
165 sb->todolist_list[sb->todolist_count-1] = state->cur_todolist;
166 state->cur_todolist = NULL;
172 enter_todoitem(State* state, char* item_name)
174 TodoItem* new_todoitem;
176 new_todoitem = malloc(sizeof(TodoItem));
177 new_todoitem->item_name = item_name;
178 new_todoitem->description = NULL;
179 new_todoitem->due = NULL;
180 new_todoitem->subtask_list = malloc(0);
181 new_todoitem->subtask_count = 0;
183 state->cur_todoitem = new_todoitem;
188 exit_todoitem(State* state)
190 #define st state->cur_todolist
193 TodoItem** item_list;
195 if (state->cur_todoitem == NULL) return;
197 /* append current item to todo list */
198 todolist = state->cur_todolist;
199 item_list = todolist->item_list;
202 st->item_list = realloc(st->item_list, st->item_count*sizeof(TodoItem*));
203 st->item_list[st->item_count-1] = state->cur_todoitem;
204 state->cur_todoitem = NULL;
209 set_description(State* state, char* description)
211 state->cur_todoitem->description = description;
215 set_due(State* state, char* due)
221 add_subtask(State* state, char* subtask_name, SubTaskState subtask_state)
223 SubTask* new_subtask;
224 SubTask** subtask_list;
226 /* create new subtask */
227 new_subtask = malloc(sizeof(SubTask));
228 new_subtask->subtask_name = subtask_name;
229 new_subtask->done = subtask_state;
232 subtask_list = state->cur_todoitem->subtask_list;
234 state->cur_todoitem->subtask_count += 1;
235 subtask_list = realloc(subtask_list, state->cur_todoitem->subtask_count*sizeof(SubTask*));
236 subtask_list[state->cur_todoitem->subtask_count-1] = new_subtask;
238 state->cur_todoitem->subtask_list = subtask_list;
243 enter_block(MD_BLOCKTYPE type, void* detail, void* userdata)
249 leave_block(MD_BLOCKTYPE type, void* detail, void* userdata)
252 state = (State*)userdata;
258 MD_BLOCK_H_DETAIL* h_detail;
259 h_detail = ((MD_BLOCK_H_DETAIL*)detail);
261 switch(h_detail->level) {
267 exit_todolist(state);
268 enter_todolist(state, state->last_block_text);
272 exit_todoitem(state);
273 enter_todoitem(state, state->last_block_text);
280 set_description(state, state->last_block_text);
284 MD_BLOCK_LI_DETAIL* li_detail;
287 li_detail = ((MD_BLOCK_LI_DETAIL*)detail);
289 assert(li_detail->is_task != 0); // all lists should be task lists
291 done = li_detail->task_mark == ' ' ? SubTaskState_todo : SubTaskState_done;
293 add_subtask(state, state->last_block_text, done);
296 // no need for default case for now :>
302 enter_span(MD_SPANTYPE type, void* detail, void* userdata)
310 leave_span(MD_SPANTYPE type, void* detail, void* userdata)
313 state = (State*)userdata;
317 printf("date, %s\n", state->last_block_text);
324 text(MD_TEXTTYPE type, const MD_CHAR* text, MD_SIZE size, void* userdata){
329 state = (State*)userdata;
331 content = malloc(size*sizeof(char)+1);
332 memcpy(content, text, size);
335 /* printf("%s\n", content); */
336 state->last_block_text = content;
342 debug_log(const char* msg, void* userdata)