From 383f9ab9b5772e67d17961c1bf3bd1c4aafa70fb Mon Sep 17 00:00:00 2001 From: Daniel Liu Date: Tue, 29 Dec 2020 16:27:49 -0500 Subject: [PATCH] working on parser --- headers/parser.h | 34 +++++++ makefile | 12 +-- old.c | 227 ++++++++++++++++++++++++++++++++++++++++++++++ parser.c | 112 ++++++++++++++++------- taskasaur.c | 228 ++--------------------------------------------- 5 files changed, 354 insertions(+), 259 deletions(-) create mode 100644 headers/parser.h create mode 100644 old.c diff --git a/headers/parser.h b/headers/parser.h new file mode 100644 index 0000000..4081537 --- /dev/null +++ b/headers/parser.h @@ -0,0 +1,34 @@ + +#include +#include +#include +#include + +typedef struct SubTask { + char* subtask_name; + int done; +} SubTask; + +typedef struct TodoItem { + char* item_name; + char* description; + char* due; + SubTask* subtask_list; + int subtask_count; +} TodoItem; + +typedef struct TodoList { + char* list_name; + TodoItem* item_list; + int item_count; +} TodoList; + +typedef struct Board { + char* board_name; + TodoList* todolist_list; + int todolist_count; +} Board; + + +extern Board* begin_parse(char* board_path); + diff --git a/makefile b/makefile index 12b8d45..6717864 100644 --- a/makefile +++ b/makefile @@ -1,12 +1,12 @@ CC=gcc -make: taskasaur test +make: taskasaur -taskasaur: taskasur.c - $(CC) -lncurses -lmenu taskasaur.c -o taskasaur +parser.o: parser.c headers/parser.h + $(CC) -c parser.c -test: parser.c - $(CC) -lncurses -lmenu -lmd4c parser.c -o parser +taskasaur: taskasaur.c parser.o + $(CC) -o $@ $^ -lncurses -lmenu -lmd4c clean: - rm taskasaur parser + rm taskasaur *.o diff --git a/old.c b/old.c new file mode 100644 index 0000000..28c25a8 --- /dev/null +++ b/old.c @@ -0,0 +1,227 @@ +#include +#include +#include +#include +#include +#include +#include + +#define SELECTED_COLOR 1 +#define NON_SELECTED_COLOR 2 + +struct todo_item { + char* name; + char* description; + char** items; +}; + +void winch_handler(int sig); + +char** read_todo(FILE* file, int* length); + +WINDOW* create_win(int height, int width, int y, int x); +MENU* create_todo_menu(WINDOW* win, char** todo_list, int todo_length); + +void on_select(char *item); + +void free_todo(char** todo_list, int todo_length); + +#include "config.h" + +int +main(int argc, char** argv) +{ + int flag; + FILE* board_file; + char** todos; + int todo_length; + + int height, width; + int ch; + + WINDOW* todo_win; + MENU* todo_menu; + + signal(SIGWINCH, winch_handler); + + // read command line args + flag = getopt(argc, argv, "o:n:"); + switch (flag) { + case 'o': + + // read from task file (might need to check for read and write permissions) + board_file = fopen(optarg, "r"); + if (!board_file) { + printf("%s does not exist\n", optarg); + return 1; + } + + todos = read_todo(board_file, &todo_length); + fclose(board_file); + + break; + + case 'n': + + // make sure file does not exist + // however, it maybe be possible that an different error has occured (besides the file not existing) + if (access(optarg, F_OK) == 0) { + printf("%s already exists\n", optarg); + return 1; + } + // create a file here + board_file = fopen(optarg, "w"); + // write init stuff here + fclose(board_file); + printf("Successfully created %s\n", optarg); + + todos = malloc(0); + todo_length = 0; + + break; + + case -1: + case '?': + printf("Help string\n"); + return 2; + } + + // start ncurses + initscr(); + cbreak(); + noecho(); + curs_set(0); + keypad(stdscr, TRUE); + start_color(); + + /* colors */ + init_pair(SELECTED_COLOR, selected_color, COLOR_BLACK); + init_pair(NON_SELECTED_COLOR, non_selected_color, COLOR_BLACK); + + getmaxyx(stdscr, height, width); + + todo_win = create_win(20, 40, 5, 5); + todo_menu = create_todo_menu(todo_win, todos, todo_length); + post_menu(todo_menu); + refresh(); + wrefresh(todo_win); + + while ((ch = getch()) != BINDING_QUIT) { + + switch (ch) { + case BINDING_SCROLL_UP: + menu_driver(todo_menu, REQ_UP_ITEM); + break; + case BINDING_SCROLL_DOWN: + menu_driver(todo_menu, REQ_DOWN_ITEM); + break; + case BINDING_JUMP_TOP: + menu_driver(todo_menu, REQ_FIRST_ITEM); + break; + case BINDING_JUMP_BOTTOM: + menu_driver(todo_menu, REQ_LAST_ITEM); + break; + case BINDING_SELECT: + break; + } + wrefresh(todo_win); + + } + + endwin(); + + /* Free mem */ + unpost_menu(todo_menu); + free_todo(todos, todo_length); + + return 0; +} + +void +winch_handler(int sig) +{ + endwin(); + refresh(); +} + +char** +read_todo(FILE* file, int* length) +{ // apparently getline isn't rly that portable, so consider other options + char** out_arr; + int out_len; + char* lineptr; + size_t len; + ssize_t nread; + + out_arr = NULL; + out_len = 0; + lineptr = NULL; + len = 0; + + while ((nread = getline(&lineptr, &len, file)) != -1) { + out_len++; + out_arr = realloc(out_arr, (sizeof(char*))*out_len); // bad to keep resizing? + + lineptr[strcspn(lineptr, "\n")] = 0; // remove newline + out_arr[out_len-1] = lineptr; + + lineptr = NULL; + } + + *length = out_len; + return out_arr; +} + +WINDOW* +create_win(int height, int width, int y, int x) +{ + WINDOW* new_win = newwin(height, width, y, x); + wrefresh(new_win); + return new_win; +} + +MENU* +create_todo_menu(WINDOW* win, char** todo_list, int todo_length) +{ + MENU* todo_menu; + ITEM** item_list; + ITEM* cur_item; + int wheight, wwidth; + + item_list = malloc((todo_length+1)*sizeof(ITEM*)); + for (int i = 0; i < todo_length; i++) { + item_list[i] = new_item(todo_list[i], ""); + set_item_userptr(item_list[i], on_select); + } + item_list[todo_length] = NULL; // last item needs to be a null pointer for some reason? + + todo_menu = new_menu(item_list); + + getmaxyx(win, wheight, wwidth); + set_menu_win(todo_menu, win); + set_menu_sub(todo_menu, derwin(win, wheight-2, wwidth-2, 1, 2)); + set_menu_mark(todo_menu, ""); + set_menu_spacing(todo_menu, 1, 2, 1); + set_menu_fore(todo_menu, COLOR_PAIR(SELECTED_COLOR)); + set_menu_back(todo_menu, COLOR_PAIR(NON_SELECTED_COLOR)); + + box(win, 0, 0); //temp + + return todo_menu; +} + +void +on_select(char *item) +{ + printf("lol"); +} + +void +free_todo(char** todo_list, int todo_length) +{ + // probably check if list is too short or too long + for (int i = 0; i < todo_length; i++) { + free(todo_list[i]); + } + free(todo_list); +} diff --git a/parser.c b/parser.c index 1c3cb13..6f597f2 100644 --- a/parser.c +++ b/parser.c @@ -1,17 +1,23 @@ -#include -#include -#include -#include - -typedef struct TodoItem { - char* name; - char* description; - char* due; - char** items; -} TodoItem; + +#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(); +void exit_todolist(); + +void enter_todoitem(); +void exit_todoitem(); + /* callbacks to parser */ int enter_block(MD_BLOCKTYPE type, void* detail, void* userdata); int leave_block(MD_BLOCKTYPE type, void* detail, void* userdata); @@ -33,19 +39,6 @@ const MD_PARSER parser = { &syntax }; -int -main(int argc, char** argv) -{ - const char* input_buffer; - long input_size; - - input_buffer = read_file("test_board.md", &input_size); - - int out = md_parse(input_buffer, input_size, &parser, NULL); - - return 0; -} - char* read_file(char* file_name, long* size) { // apparently using seek isnt the greatest, may change to chunk reading later @@ -81,22 +74,66 @@ read_file(char* file_name, long* size) return output; } +Board* +begin_parse(char* board_path) +{ + const char* input_buffer; + long input_size; + State* state; + + /* 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; + + state->board = malloc(sizeof(Board)); + state->board->todolist_list = malloc(0); + state->board->todolist_count = 0; + + md_parse(input_buffer, input_size, &parser, state); + + free((char*)input_buffer); + + return state->board; +} + int enter_block(MD_BLOCKTYPE type, void* detail, void* userdata) { - switch (type) { - case MD_BLOCK_H: - printf("Found h%d block\n", ((MD_BLOCK_H_DETAIL*)detail)->level); - break; - - // no need for default case for now :> - } return 0; } int leave_block(MD_BLOCKTYPE type, void* detail, void* userdata) { + State* state; + state = (State*)userdata; + + switch (type) { + + case MD_BLOCK_H: + + switch(((MD_BLOCK_H_DETAIL*)detail)->level) { + + case 1: + printf("leave h1, %s\n", state->last_block_text); + + case 2: + printf("leave h2, %s\n", state->last_block_text); + break; + case 3: + printf("leave h3, %s\n", state->last_block_text); + break; + + } + + break; + + // no need for default case for now :> + } return 0; } @@ -114,6 +151,19 @@ leave_span(MD_SPANTYPE type, void* detail, void* userdata) 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; } diff --git a/taskasaur.c b/taskasaur.c index 28c25a8..8b6c182 100644 --- a/taskasaur.c +++ b/taskasaur.c @@ -1,227 +1,11 @@ -#include -#include -#include -#include -#include -#include -#include +#include "headers/parser.h" -#define SELECTED_COLOR 1 -#define NON_SELECTED_COLOR 2 - -struct todo_item { - char* name; - char* description; - char** items; -}; - -void winch_handler(int sig); - -char** read_todo(FILE* file, int* length); - -WINDOW* create_win(int height, int width, int y, int x); -MENU* create_todo_menu(WINDOW* win, char** todo_list, int todo_length); - -void on_select(char *item); - -void free_todo(char** todo_list, int todo_length); - -#include "config.h" - -int -main(int argc, char** argv) +int +main(int argc, char** argv) { - int flag; - FILE* board_file; - char** todos; - int todo_length; - - int height, width; - int ch; - - WINDOW* todo_win; - MENU* todo_menu; - - signal(SIGWINCH, winch_handler); - - // read command line args - flag = getopt(argc, argv, "o:n:"); - switch (flag) { - case 'o': - - // read from task file (might need to check for read and write permissions) - board_file = fopen(optarg, "r"); - if (!board_file) { - printf("%s does not exist\n", optarg); - return 1; - } - - todos = read_todo(board_file, &todo_length); - fclose(board_file); + Board* board; - break; + board = begin_parse("test_board.md"); - case 'n': - - // make sure file does not exist - // however, it maybe be possible that an different error has occured (besides the file not existing) - if (access(optarg, F_OK) == 0) { - printf("%s already exists\n", optarg); - return 1; - } - // create a file here - board_file = fopen(optarg, "w"); - // write init stuff here - fclose(board_file); - printf("Successfully created %s\n", optarg); - - todos = malloc(0); - todo_length = 0; - - break; - - case -1: - case '?': - printf("Help string\n"); - return 2; - } - - // start ncurses - initscr(); - cbreak(); - noecho(); - curs_set(0); - keypad(stdscr, TRUE); - start_color(); - - /* colors */ - init_pair(SELECTED_COLOR, selected_color, COLOR_BLACK); - init_pair(NON_SELECTED_COLOR, non_selected_color, COLOR_BLACK); - - getmaxyx(stdscr, height, width); - - todo_win = create_win(20, 40, 5, 5); - todo_menu = create_todo_menu(todo_win, todos, todo_length); - post_menu(todo_menu); - refresh(); - wrefresh(todo_win); - - while ((ch = getch()) != BINDING_QUIT) { - - switch (ch) { - case BINDING_SCROLL_UP: - menu_driver(todo_menu, REQ_UP_ITEM); - break; - case BINDING_SCROLL_DOWN: - menu_driver(todo_menu, REQ_DOWN_ITEM); - break; - case BINDING_JUMP_TOP: - menu_driver(todo_menu, REQ_FIRST_ITEM); - break; - case BINDING_JUMP_BOTTOM: - menu_driver(todo_menu, REQ_LAST_ITEM); - break; - case BINDING_SELECT: - break; - } - wrefresh(todo_win); - - } - - endwin(); - - /* Free mem */ - unpost_menu(todo_menu); - free_todo(todos, todo_length); - - return 0; -} - -void -winch_handler(int sig) -{ - endwin(); - refresh(); -} - -char** -read_todo(FILE* file, int* length) -{ // apparently getline isn't rly that portable, so consider other options - char** out_arr; - int out_len; - char* lineptr; - size_t len; - ssize_t nread; - - out_arr = NULL; - out_len = 0; - lineptr = NULL; - len = 0; - - while ((nread = getline(&lineptr, &len, file)) != -1) { - out_len++; - out_arr = realloc(out_arr, (sizeof(char*))*out_len); // bad to keep resizing? - - lineptr[strcspn(lineptr, "\n")] = 0; // remove newline - out_arr[out_len-1] = lineptr; - - lineptr = NULL; - } - - *length = out_len; - return out_arr; -} - -WINDOW* -create_win(int height, int width, int y, int x) -{ - WINDOW* new_win = newwin(height, width, y, x); - wrefresh(new_win); - return new_win; -} - -MENU* -create_todo_menu(WINDOW* win, char** todo_list, int todo_length) -{ - MENU* todo_menu; - ITEM** item_list; - ITEM* cur_item; - int wheight, wwidth; - - item_list = malloc((todo_length+1)*sizeof(ITEM*)); - for (int i = 0; i < todo_length; i++) { - item_list[i] = new_item(todo_list[i], ""); - set_item_userptr(item_list[i], on_select); - } - item_list[todo_length] = NULL; // last item needs to be a null pointer for some reason? - - todo_menu = new_menu(item_list); - - getmaxyx(win, wheight, wwidth); - set_menu_win(todo_menu, win); - set_menu_sub(todo_menu, derwin(win, wheight-2, wwidth-2, 1, 2)); - set_menu_mark(todo_menu, ""); - set_menu_spacing(todo_menu, 1, 2, 1); - set_menu_fore(todo_menu, COLOR_PAIR(SELECTED_COLOR)); - set_menu_back(todo_menu, COLOR_PAIR(NON_SELECTED_COLOR)); - - box(win, 0, 0); //temp - - return todo_menu; -} - -void -on_select(char *item) -{ - printf("lol"); -} - -void -free_todo(char** todo_list, int todo_length) -{ - // probably check if list is too short or too long - for (int i = 0; i < todo_length; i++) { - free(todo_list[i]); - } - free(todo_list); + return 0; } -- 2.20.1