+/* processing */
+void enter_todolist(State* state, char* list_name);
+void exit_todolist(State* state);
+
+void enter_todoitem(State* state, char* item_name);
+void exit_todoitem(State* state);
+
+void set_description(State* state, char* description);
+void set_due(State* state, char* due); // make an acc date struct later
+void add_subtask(State* state, char* subtask_name, SubTaskState subtask_state);
+
+/* callbacks to parser */
+int enter_block(MD_BLOCKTYPE type, void* detail, void* userdata);
+int leave_block(MD_BLOCKTYPE type, void* detail, void* userdata);
+int enter_span(MD_SPANTYPE type, void* detail, void* userdata);
+int leave_span(MD_SPANTYPE type, void* detail, void* userdata);
+int text(MD_TEXTTYPE type, const MD_CHAR* text, MD_SIZE size, void* userdata);
+void debug_log(const char* msg, void* userdata);
+void syntax(void);
+
+const MD_PARSER parser = {
+ 0,
+ MD_FLAG_TASKLISTS,
+ &enter_block,
+ &leave_block,
+ &enter_span,
+ &leave_span,
+ &text,
+ &debug_log,
+ &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
+ FILE* file;
+ char* output;
+ long fsize;
+
+ file = fopen(file_name, "r");
+ if (file == NULL) {
+ perror("read_file > fopen");
+ return NULL;
+ }
+
+ fseek(file, 0, SEEK_END);
+ fsize = ftell(file);
+ fseek(file, 0, SEEK_SET);
+
+ output = malloc(fsize+1); // maybe error check malloc
+ fread(output, 1, fsize, file);
+ if (ferror(file)) {
+ printf("Something went wrong reading file\n");
+ fclose(file);
+ free(output);
+ return NULL;
+ }
+ output[fsize] = 0; // add null terminator
+
+ /* clean up */
+ fclose(file);
+
+ /* return */
+ *size = fsize;
+ return output;
+}
+
+void
+enter_todolist(State* state, char* list_name)
+{
+ TodoList* new_todolist;
+
+ new_todolist = malloc(sizeof(TodoList));
+ new_todolist->list_name = list_name;
+ new_todolist->item_list = malloc(0);
+ new_todolist->item_count = 0;
+
+ state->cur_todolist = new_todolist;
+}
+
+void
+exit_todolist(State* state)
+{
+ #define sb state->board
+
+ if (state->cur_todolist == NULL) return;
+
+ exit_todoitem(state);
+
+ 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;
+
+ #undef sb
+}
+
+void
+enter_todoitem(State* state, char* item_name)
+{
+ TodoItem* new_todoitem;
+
+ new_todoitem = malloc(sizeof(TodoItem));
+ new_todoitem->item_name = item_name;
+ new_todoitem->description = strdup("");
+ new_todoitem->due = strdup("");
+ new_todoitem->subtask_list = malloc(0);
+ new_todoitem->subtask_count = 0;
+
+ state->cur_todoitem = new_todoitem;
+
+}
+
+void
+exit_todoitem(State* state)
+{
+ #define st state->cur_todolist
+
+ if (state->cur_todoitem == NULL) return;
+
+ /* append current item to todo 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
+set_description(State* state, char* description)
+{
+ state->cur_todoitem->description = description;
+}
+
+void
+set_due(State* state, char* due)
+{
+ state->cur_todoitem->due = due;
+}
+
+void
+add_subtask(State* state, char* subtask_name, SubTaskState subtask_state)
+{
+ SubTask* new_subtask;
+ SubTask** subtask_list;
+
+ /* create new subtask */
+ new_subtask = malloc(sizeof(SubTask));
+ new_subtask->subtask_name = subtask_name;
+ new_subtask->done = subtask_state;
+
+ /* add it */
+ subtask_list = state->cur_todoitem->subtask_list;
+
+ state->cur_todoitem->subtask_count += 1;
+ subtask_list = realloc(subtask_list, state->cur_todoitem->subtask_count*sizeof(SubTask*));
+ subtask_list[state->cur_todoitem->subtask_count-1] = new_subtask;
+
+ state->cur_todoitem->subtask_list = subtask_list;
+
+}
+
+int
+enter_block(MD_BLOCKTYPE type, void* detail, void* userdata)
+{
+ return 0;
+}