diff --git a/src/dodo.c b/src/dodo.c index cc1e2e7..40eee5a 100644 --- a/src/dodo.c +++ b/src/dodo.c @@ -182,9 +182,7 @@ void display_column_headings_for_all_task_lists(){ // Print Heading void display_heading(){ - char* colnames = "active_id,title,due_date"; char * str; - int width = 0; printf("\n"); str = "Today"; @@ -201,13 +199,13 @@ void display_heading(){ // pass in the args and return the title and due date // due date passed as NULL if for delete // TODO input validation for strings implement in strings! -int parse_args(int argc, char** argv, char** title, char** due_date){ +int parse_args(int argc, char** argv, filtered_tasks* task){ if ( argc > 2 ){ - *title = argv[2]; + x_strcpy(task->title, argv[2], ARG_MAX); } if ( argc > 3 ){ - *due_date = argv[3]; + x_strcpy(task->due_date, argv[3], ARG_MAX); } } @@ -339,6 +337,7 @@ int view_tasks(sqlite3 *db){ // Set the table and cols to be printed filtered_tasks* tasks = malloc(sizeof(filtered_tasks)); + // TODO check that malloc is ok x_strcpy(tasks->table, "tasks", ARG_MAX); x_strcpy(tasks->selected_columns, "active_id, title, due_date", ARG_MAX); @@ -349,47 +348,54 @@ int view_tasks(sqlite3 *db){ // Print "today" tasks x_strcpy(tasks->status, "today", ARG_MAX); if ( display_task_list(TODAY_COL_START, db, tasks) ){ + free(tasks); return -1; } // Print "backlog" tasks x_strcpy(tasks->status, "backlog", ARG_MAX); if ( display_task_list(BACKLOG_COL_START, db, tasks) ){ + free(tasks); return -1; } // Print "blocked" tasks x_strcpy(tasks->status, "blocked", ARG_MAX); if ( display_task_list(BLOCKED_COL_START, db, tasks) ){ + free(tasks); return -1; } free(tasks); - return 0; } // TODO: the way this ensures that we are only passing in // valid inputs is stupid and ugly FIX int add_new_task(sqlite3 *db, int argc, char** argv){ + int rc = 0; - char* table = "tasks"; - char* title = NULL; - char* due_date = NULL; - char* colnames = "(title, due_date)"; char values[100]; sqlite3_stmt* out_stmt; - parse_args(argc, argv, &title, &due_date); + filtered_tasks* task = malloc(sizeof(filtered_tasks)); + x_strcpy(task->table, "tasks", ARG_MAX); + task->title[0] = '\0'; + task->due_date[0] = '\0'; - if ( due_date != NULL ){ - snprintf(values, 100, "('%s', '%s')", title, due_date); - }else{ - colnames = "(title)"; - snprintf(values, 100, "('%s')", title); + parse_args(argc, argv, task); + + if ( task->title[0] != '\0' ){ + if ( task->due_date[0] != '\0' ){ + x_strcpy(task->selected_columns, "(title, due_date)", ARG_MAX); + snprintf(values, 100, "('%s', '%s')", task->title, task->due_date); + }else{ + x_strcpy(task->selected_columns, "(title)", ARG_MAX); + snprintf(values, 100, "('%s')", task->title); + } } - if ( gen_sql_insert_stmt(db, &out_stmt, table, colnames, values) ){ + if ( gen_sql_insert_stmt(db, &out_stmt, task->table, task->selected_columns, values) ){ return -1; } if ( ( rc = sqlite3_step(out_stmt) ) == SQLITE_DONE){ @@ -403,20 +409,27 @@ int add_new_task(sqlite3 *db, int argc, char** argv){ int update_task_status(sqlite3 *db, int argc, char** argv){ int rc = 0; - char* table = "tasks"; - char* title_or_active_id; sqlite3_stmt* out_stmt; - parse_args(argc, argv, &title_or_active_id, NULL); + filtered_tasks* task = malloc(sizeof(filtered_tasks)); - if ( gen_sql_update_stmt(db, &out_stmt, table, argv[1], title_or_active_id) ){ + x_strcpy(task->table, "tasks", ARG_MAX); + + // TODO: this is not just title but also active ID so fix this + parse_args(argc, argv, task); + + if ( gen_sql_update_stmt(db, &out_stmt, task->table, argv[1], task->title) ){ + free(task); return -1; } if ( ( rc = sqlite3_step(out_stmt) ) == SQLITE_DONE){ + free(task); return 0; } checksqlerr(rc, "broken in update_task_status"); + + free(task); return -1; } @@ -424,20 +437,26 @@ int update_task_status(sqlite3 *db, int argc, char** argv){ int complete_task(sqlite3 *db, int argc, char** argv){ int rc = 0; - char* table = "tasks"; - char* title; sqlite3_stmt* out_stmt; + filtered_tasks* task = malloc(sizeof(filtered_tasks)); - parse_args(argc, argv, &title, NULL); - if ( gen_sql_update_stmt(db, &out_stmt, table, NULL, title) ){ + x_strcpy(task->table, "tasks", ARG_MAX); + + parse_args(argc, argv, task); + if ( gen_sql_update_stmt(db, &out_stmt, task->table, NULL, task->title) ){ + free(task); return -1; } + if ( ( rc = sqlite3_step(out_stmt) ) == SQLITE_DONE){ + free(task); return 0; } checksqlerr(rc, "broken in complete_task"); + + free(task); return -1; } @@ -467,19 +486,24 @@ int update_task(sqlite3 *db, int argc, char** argv){ int del_task(sqlite3 *db, int argc, char** argv){ int rc = 0; - char* table = "tasks"; - char* title; sqlite3_stmt* out_stmt; + filtered_tasks* task = malloc(sizeof(filtered_tasks)); - parse_args(argc, argv, &title, NULL); - if ( gen_sql_delete_stmt(db, &out_stmt, table, title) ){ + x_strcpy(task->table, "tasks", ARG_MAX); + + parse_args(argc, argv, task); + if ( gen_sql_delete_stmt(db, &out_stmt, task->table, task->title) ){ + free(task); return -1; } if ( ( rc = sqlite3_step(out_stmt) ) == SQLITE_DONE){ + free(task); return 0; } checksqlerr(rc, "broken in del_task"); + + free(task); return -1; } diff --git a/src/dodo.h b/src/dodo.h index 2392936..132dab9 100644 --- a/src/dodo.h +++ b/src/dodo.h @@ -29,8 +29,8 @@ typedef struct { int active_id; char status[ARG_MAX]; char project_tag[ARG_MAX]; - date creation_date; - date due_date; + char creation_date[ARG_MAX]; + char due_date[ARG_MAX]; }filtered_tasks; @@ -48,7 +48,7 @@ int gen_sql_select_stmt(sqlite3 *db, sqlite3_stmt** out_stmt, filtered_tasks* ta int checksqlerr(int rc, char *errmsg); int opendb(sqlite3 **db, char* filename); void display_heading(); -int parse_args(int argc, char** argv, char** title, char** due_date); +int parse_args(int argc, char** argv, filtered_tasks* task); int get_num_rows(sqlite3 *db, char* table, char* status); int print_fixed_width(const unsigned char* str, int width); int display_task_list(int start_col, sqlite3 *db, filtered_tasks*);