From 488499050b0f46c2e0954d16ffb8fe41e2ebd9f9 Mon Sep 17 00:00:00 2001 From: augustin64 Date: Thu, 13 Jul 2023 11:55:50 +0200 Subject: [PATCH] Added task_list_t --- src/db.c | 31 +++++++++++++------------------ src/include/struct.h | 6 ++++++ 2 files changed, 19 insertions(+), 18 deletions(-) diff --git a/src/db.c b/src/db.c index 0bcbcc5..42dd54d 100644 --- a/src/db.c +++ b/src/db.c @@ -213,7 +213,7 @@ void get_task(int id, task_t* t) { // t->id will be (-1) if not found } -int list_tasks(bool show_completed) { +task_list_t* get_task_list(bool include_completed) { sqlite3* db = get_db(); sqlite3_stmt *stmt; @@ -221,38 +221,33 @@ int list_tasks(bool show_completed) { if (ret != SQLITE_OK) { printf("(get_task) failure fetching data\n"); - return 1; + return NULL; } - time_t now = time(0); + task_list_t* list = NULL; while (sqlite3_step(stmt) == SQLITE_ROW) { task_t t; t.id = strtol((char*)sqlite3_column_text(stmt, 0), NULL, 10); - t.text = (char*)sqlite3_column_text(stmt, 1); t.done = strtol((char*)sqlite3_column_text(stmt, 2), NULL, 10); t.due_to = strtol((char*)sqlite3_column_text(stmt, 3), NULL, 10); - //* Warning: t just lives here (particularly t.text) + + if (!t.done || include_completed) { // We don't want to allocate memory for things that will not be used + char* text = (char*)sqlite3_column_text(stmt, 1); + t.text = malloc(sizeof(char)*(strlen(text)+1)); + strcpy(t.text, text); - if (t.done) { - if (show_completed) { - printf(BOLD GREEN); // Task completed - } - } else if (difftime(now, t.due_to) <= 0 || t.due_to == 0) { - printf(BOLD YELLOW); // Task not completed but that is fine - } else { - printf(BOLD RED); // Task not completed but should be ! - } - - if (!t.done || show_completed) { - printf("[%d]" RESET " %s\n", t.id, t.text); + task_list_t* cur = malloc(sizeof(task_list_t)); // Add the parsed task to the beginning of the list + cur->task = t; + cur->next = list; + list = cur; } } sqlite3_finalize(stmt); sqlite3_close(db); - return 0; + return list; } int get_new_task_id() { diff --git a/src/include/struct.h b/src/include/struct.h index f607d59..1ba1eb0 100644 --- a/src/include/struct.h +++ b/src/include/struct.h @@ -12,4 +12,10 @@ struct task { }; typedef struct task task_t; +struct task_list { + task_t task; + struct task_list* next; +}; +typedef struct task_list task_list_t; + #endif \ No newline at end of file