X-Git-Url: https://git.danieliu.xyz/?p=taskasaur.git;a=blobdiff_plain;f=parser.c;h=9300bca91cb8ce81d4af3034625e549606022444;hp=a0053314ad60f5ae166131a7092e85aaaadd1e97;hb=HEAD;hpb=fad59c5fb6ea2c0f71928bf1479aaf6678615814 diff --git a/parser.c b/parser.c index a005331..9300bca 100644 --- a/parser.c +++ b/parser.c @@ -43,6 +43,38 @@ const MD_PARSER parser = { &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 @@ -52,7 +84,7 @@ read_file(char* file_name, long* size) file = fopen(file_name, "r"); if (file == NULL) { - printf("Something went wrong opening file\n"); + perror("read_file > fopen"); return NULL; } @@ -78,36 +110,6 @@ read_file(char* file_name, long* size) return output; } -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; -} - void enter_todolist(State* state, char* list_name) { @@ -135,6 +137,7 @@ exit_todolist(State* state) sb->todolist_list[sb->todolist_count-1] = state->cur_todolist; state->cur_todolist = NULL; + #undef sb } void @@ -144,8 +147,8 @@ enter_todoitem(State* state, char* item_name) new_todoitem = malloc(sizeof(TodoItem)); new_todoitem->item_name = item_name; - new_todoitem->description = NULL; - new_todoitem->due = NULL; + new_todoitem->description = strdup(""); + new_todoitem->due = strdup(""); new_todoitem->subtask_list = malloc(0); new_todoitem->subtask_count = 0; @@ -158,20 +161,15 @@ exit_todoitem(State* state) { #define st state->cur_todolist - TodoList* todolist; - TodoItem** item_list; - if (state->cur_todoitem == NULL) return; /* append current item to todo list */ - todolist = state->cur_todolist; - item_list = todolist->item_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 @@ -183,7 +181,7 @@ set_description(State* state, char* description) void set_due(State* state, char* due) { - + state->cur_todoitem->due = due; } void @@ -283,7 +281,7 @@ leave_span(MD_SPANTYPE type, void* detail, void* userdata) switch (type) { case MD_SPAN_STRONG: - printf("date, %s\n", state->last_block_text); + set_due(state, state->last_block_text); break; } return 0; @@ -319,3 +317,118 @@ 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; +}