From 9a5fbc66dd640ccb7b4e1c98437b6c9d76db9e4b Mon Sep 17 00:00:00 2001 From: augustin64 Date: Wed, 20 Sep 2023 16:55:30 +0200 Subject: [PATCH] Add 'category' to db schema --- src/main.c | 22 +++++++++++++++++++--- src/main/db.c | 23 ++++++----------------- src/main/include/struct.h | 1 + src/main/tasks.c | 30 +++++++++++++++++++++++++++++- 4 files changed, 55 insertions(+), 21 deletions(-) diff --git a/src/main.c b/src/main.c index 1c9ca9f..f7a5de6 100644 --- a/src/main.c +++ b/src/main.c @@ -11,8 +11,8 @@ void help(char* name) { printf(BLUE "\t-- TaKl " VERSION " --\n" RESET); printf("Utilisation: %s ( list | add | reschedule | info | rm | done )\n", name); - printf("\tlist [-a:voir les tâches complétées]\n"); - printf("\tadd [date]\n"); + printf("\tlist [category] [-a:voir les tâches complétées]\n"); + printf("\tadd [category:] [date]\n"); printf("\treschedule [date]\n"); printf("\tinfo ...\n"); printf("\tdone ...\n"); @@ -35,7 +35,7 @@ time_t parseDateTime(const char* input) { date.tm_year = current_time->tm_year; date.tm_mon = current_time->tm_mon; date.tm_mday = current_time->tm_mday; - date.tm_hour = current_time->tm_hour; + date.tm_hour = current_time->tm_hour-1; //TODO: buggy in non-UTC date.tm_min = current_time->tm_min; date.tm_sec = current_time->tm_sec; @@ -88,14 +88,27 @@ int add(int argc, char* argv[]) { task_t t = create_task(argv[2], due_to); if (t.id == -1) { + if (t.category) { + free(t.category); + free(t.text); + } return 1; } if (add_task(t) != 0) { printf("Erreur lors de l'ajout à la base de données\n"); + if (t.category) { + free(t.category); + free(t.text); + } return 1; } + + if (t.category) { + free(t.category); + free(t.text); + } printf(BOLD YELLOW "[%d]" RESET " ajoutée\n", t.id); return 0; } @@ -165,6 +178,9 @@ int info(int argc, char* argv[]) { print_task(t); free(t.text); + if (t.category) { + free(t.category); + } } return 0; diff --git a/src/main/db.c b/src/main/db.c index bbe8490..06cb89b 100644 --- a/src/main/db.c +++ b/src/main/db.c @@ -50,6 +50,7 @@ sqlite3* get_db() { "CREATE TABLE tasks ( \ id INTEGER PRIMARY KEY,\ text TEXT NOT NULL,\ + category TEXT DEFAULT '', \ done INTEGER, \ due_to INTEGER \ );", @@ -96,42 +97,30 @@ int add_task(task_t t) { sqlite3* db = get_db(); sqlite3_stmt* stmt; - int ret = sqlite3_prepare_v2(db, "INSERT INTO tasks (id, text, done, due_to) VALUES (?1, ?2, ?3, ?4);", -1, &stmt, 0); + sqlite3_prepare_v2(db, "INSERT INTO tasks (id, text, category, done, due_to) VALUES (?1, ?2, ?3, ?4, ?5);", -1, &stmt, 0); - if (ret != SQLITE_OK) { - printf("(get_task) failure fetching data\n"); - return 1; - } char str_id[10]; sprintf(str_id, "%d", (int)t.id); sqlite3_bind_text(stmt, 1, str_id, -1, SQLITE_STATIC); sqlite3_bind_text(stmt, 2, t.text, -1, SQLITE_STATIC); + sqlite3_bind_text(stmt, 3, t.category, -1, SQLITE_STATIC); char str_done[2]; // Just a boolean sprintf(str_done, "%d", t.done); - sqlite3_bind_text(stmt, 3, str_done, -1, SQLITE_STATIC); + sqlite3_bind_text(stmt, 4, str_done, -1, SQLITE_STATIC); char str_due_to[15]; sprintf(str_due_to, "%ld", t.due_to); - sqlite3_bind_text(stmt, 4, str_due_to, -1, SQLITE_STATIC); + sqlite3_bind_text(stmt, 5, str_due_to, -1, SQLITE_STATIC); - ret = sqlite3_step(stmt); + sqlite3_step(stmt); sqlite3_finalize(stmt); sqlite3_close(db); notify_change(); - if (ret != SQLITE_DONE) { - if (ret == 19) { - printf("Identifiant déjà utilisé\n"); - } else { - printf("Return value: %d\n", ret); - } - - return 1; - } return 0; } diff --git a/src/main/include/struct.h b/src/main/include/struct.h index 1ba1eb0..365ad25 100644 --- a/src/main/include/struct.h +++ b/src/main/include/struct.h @@ -7,6 +7,7 @@ struct task { int id; char* text; + char* category; bool done; time_t due_to; }; diff --git a/src/main/tasks.c b/src/main/tasks.c index ffcc193..3d89c54 100644 --- a/src/main/tasks.c +++ b/src/main/tasks.c @@ -4,20 +4,48 @@ Fonctions utilitaires concernant les tâches #include #include #include +#include #include #include "include/db.h" #include "include/struct.h" #include "include/colors.h" +void parse_input(char* in_text, char** out_text, char** out_category) { + int n = strlen(in_text); + + int i=0; + for (;i < n; i++) { + if (in_text[i] == ':') { + break; + } + } + + if (i == n) { + *out_text = in_text; + *out_category = NULL; + } else { + *out_text = malloc(sizeof(char)*(n-i)); + *out_category = malloc(sizeof(char)*i); + + memcpy(*out_text, (void*)in_text+((i+1)*sizeof(char)), (size_t)((n-i)*sizeof(char))); + memcpy(*out_category, in_text, (size_t)(i*sizeof(char))); + (*out_category)[i] = '\0'; + } +} + task_t create_task(char* text, time_t due_to) { task_t task; task.id = get_new_task_id(); - task.text = text; task.done = false; task.due_to = due_to; + task.text = NULL; + task.category = NULL; + + parse_input(text, &(task.text), &(task.category)); + printf("task_id: %d\n", task.id); return task; }