X-Git-Url: https://git.danieliu.xyz/?a=blobdiff_plain;f=parser.c;h=b7520852d4c1728b64f0cc8d9bbe5d9197f44469;hb=bcb6f1950bfd2867df61d5c93aa73b73e093edbc;hp=e0564c15137d0ed89ba07553acaef19ce23e77e1;hpb=8fb19bb7969617821e9c2c1c64d113ff2c26abaf;p=taskasaur.git diff --git a/parser.c b/parser.c index e0564c1..b752085 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 @@ -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 @@ -333,3 +317,97 @@ 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); + + // write the other fields later!!! + + } + + } + + 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 */ + + free(cur_todoitem); + } + + free(cur_todolist); + } + + /* free(board->board_name); */ + free(board); + + return 0; +}