Added task_list_t

This commit is contained in:
augustin64 2023-07-13 11:55:50 +02:00
parent 965473c87b
commit 488499050b
2 changed files with 19 additions and 18 deletions

View File

@ -213,7 +213,7 @@ void get_task(int id, task_t* t) {
// t->id will be (-1) if not found // 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* db = get_db();
sqlite3_stmt *stmt; sqlite3_stmt *stmt;
@ -221,38 +221,33 @@ int list_tasks(bool show_completed) {
if (ret != SQLITE_OK) { if (ret != SQLITE_OK) {
printf("(get_task) failure fetching data\n"); 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) { while (sqlite3_step(stmt) == SQLITE_ROW) {
task_t t; task_t t;
t.id = strtol((char*)sqlite3_column_text(stmt, 0), NULL, 10); 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.done = strtol((char*)sqlite3_column_text(stmt, 2), NULL, 10);
t.due_to = strtol((char*)sqlite3_column_text(stmt, 3), 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) { task_list_t* cur = malloc(sizeof(task_list_t)); // Add the parsed task to the beginning of the list
if (show_completed) { cur->task = t;
printf(BOLD GREEN); // Task completed cur->next = list;
} list = cur;
} 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);
} }
} }
sqlite3_finalize(stmt); sqlite3_finalize(stmt);
sqlite3_close(db); sqlite3_close(db);
return 0; return list;
} }
int get_new_task_id() { int get_new_task_id() {

View File

@ -12,4 +12,10 @@ struct task {
}; };
typedef struct task task_t; typedef struct task task_t;
struct task_list {
task_t task;
struct task_list* next;
};
typedef struct task_list task_list_t;
#endif #endif