X-Git-Url: https://git.danieliu.xyz/?p=taskasaur.git;a=blobdiff_plain;f=parser.c;h=9300bca91cb8ce81d4af3034625e549606022444;hp=e0564c15137d0ed89ba07553acaef19ce23e77e1;hb=HEAD;hpb=8fb19bb7969617821e9c2c1c64d113ff2c26abaf diff --git a/parser.c b/parser.c index e0564c1..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,37 +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 = malloc(sizeof(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) { @@ -125,27 +126,18 @@ enter_todolist(State* state, char* list_name) void exit_todolist(State* state) { - Board* board; - TodoList** todolist_list; + #define sb state->board - if (state->cur_todolist == NULL) { - return; - } + if (state->cur_todolist == NULL) return; exit_todoitem(state); - /* append new todolist to board */ - board = state->board; - todolist_list = board->todolist_list; - - board->todolist_count += 1; - todolist_list = realloc(todolist_list, board->todolist_count*sizeof(TodoList*)); - todolist_list[board->todolist_count-1] = state->cur_todolist; + 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; - /* save */ - board->todolist_list = todolist_list; - + #undef sb } void @@ -155,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; @@ -167,25 +159,17 @@ enter_todoitem(State* state, char* item_name) void exit_todoitem(State* state) { - TodoList* todolist; - TodoItem** item_list; + #define st state->cur_todolist - if (state->cur_todoitem == NULL) { - return; - } + if (state->cur_todoitem == NULL) return; /* append current item to todo list */ - todolist = state->cur_todolist; - item_list = todolist->item_list; - - todolist->item_count += 1; - item_list = realloc(item_list, todolist->item_count*sizeof(TodoItem*)); - item_list[todolist->item_count-1] = state->cur_todoitem; + 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; - /* save */ - todolist->item_list = item_list; - + #undef st } void @@ -197,7 +181,7 @@ set_description(State* state, char* description) void set_due(State* state, char* due) { - + state->cur_todoitem->due = due; } void @@ -297,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; @@ -333,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; +}