From 0bf5e72a2ee87af0e7ac3e2b769359084829a20f Mon Sep 17 00:00:00 2001 From: Louie S Date: Tue, 4 Jul 2023 17:59:48 -0700 Subject: Fix date on entries --- src/db/sqlite3/db.c | 17 ++++++++++++----- src/entry.c | 16 ++++++++++++---- src/entry.h | 5 +++-- src/main.c | 26 +++++++++++++++++--------- 4 files changed, 44 insertions(+), 20 deletions(-) diff --git a/src/db/sqlite3/db.c b/src/db/sqlite3/db.c index 4f3b136..09a0bf4 100644 --- a/src/db/sqlite3/db.c +++ b/src/db/sqlite3/db.c @@ -1,6 +1,7 @@ #include #include #include +#include #include #include #include @@ -171,14 +172,14 @@ int create_tables(const char *path) { // callback functions int group_count_callback(void *first_arg, int argc, char **argv, char **azColName) { Group **groups = (Group **) first_arg; - *groups = malloc(sizeof(Group) * atoi(argv[0])); + *groups = calloc(atoi(argv[0]), sizeof(Group)); return 0; } int entry_count_callback(void *first_arg, int argc, char **argv, char **azColName) { Entry **entries = (Entry **) first_arg; - *entries = malloc(sizeof(Entry) * atoi(argv[0])); + *entries = calloc(atoi(argv[0]), sizeof(Entry)); return 0; } @@ -203,6 +204,7 @@ int load_groups_callback(void *first_arg, int argc, char **argv, char **azColNam int load_entries_callback(void *first_arg, int argc, char **argv, char **azColName) { Entry **entries = (Entry **) first_arg; + struct tm due_date = {0}; // check that enough arguments were passed if(argc < 8) { @@ -211,9 +213,14 @@ int load_entries_callback(void *first_arg, int argc, char **argv, char **azColNa } entry_set_id(&((*entries)[callback_index]), (argv[0] ? atoi(argv[0]) : 0)); - entry_set_id(&((*entries)[callback_index]), (argv[1] ? atoi(argv[1]) : 0)); - fprintf(stderr, "time format: %s\n", (argv[2] ? argv[2] : "")); - //entry_set_due_date(&((*entries)[callback_index]), (argv[1] ? argv[1] : "")); // TODO + entry_set_group_id(&((*entries)[callback_index]), (argv[1] ? atoi(argv[1]) : 0)); + if(argv[2] && strlen(argv[2])) { + strptime(argv[2], "%F", &due_date); + printf("DEBUG: %s\n", argv[2]); + printf("DEBUG: %d\n", due_date.tm_year); + entry_set_due_date(&((*entries)[callback_index]), &due_date); + } + else entry_set_due_date(&((*entries)[callback_index]), NULL); entry_set_alt_due_date(&((*entries)[callback_index]), (argv[3] ? argv[3] : "")); entry_set_title(&((*entries)[callback_index]), (argv[4] ? argv[4] : "")); entry_set_color(&((*entries)[callback_index]), (argv[5] ? argv[5] : "")); diff --git a/src/entry.c b/src/entry.c index c2958ed..253ef5e 100644 --- a/src/entry.c +++ b/src/entry.c @@ -11,8 +11,9 @@ int entry_get_group_id(Entry *e) { return e->group_id; } -struct tm entry_get_due_date(Entry *e) { - return e->due_date; +struct tm *entry_get_due_date(Entry *e) { + if(e->due_date_set) return &e->due_date; + return NULL; } char *entry_get_alt_due_date(Entry *e) { @@ -49,8 +50,15 @@ void entry_set_group_id(Entry *e, int id) { e->group_id = id; } -void entry_set_due_date(Entry *e, struct tm due_date) { - e->due_date = due_date; +void entry_set_due_date(Entry *e, struct tm *due_date) { + if(due_date != NULL) { + e->due_date = *due_date; + e->due_date_set = true; + } + else { + e->due_date = (struct tm){0}; + e->due_date_set = false; + } } void entry_set_alt_due_date(Entry *e, char *alt_due_date) { diff --git a/src/entry.h b/src/entry.h index 819c1cc..ad8f4e0 100644 --- a/src/entry.h +++ b/src/entry.h @@ -11,6 +11,7 @@ typedef struct { int id; // id from database int group_id; // id of corresponding group struct tm due_date; + bool due_date_set; char alt_due_date[BUF_LEN]; char title[BUF_LEN]; char color[BUF_LEN]; // TODO consider making an enum @@ -22,7 +23,7 @@ typedef struct { // getters int entry_get_id(Entry *e); int entry_get_group_id(Entry *e); -struct tm entry_get_due_date(Entry *e); +struct tm *entry_get_due_date(Entry *e); char *entry_get_alt_due_date(Entry *e); char *entry_get_title(Entry *e); char *entry_get_color(Entry *e); @@ -33,7 +34,7 @@ char *entry_get_url(Entry *e); // setters void entry_set_id(Entry *e, int id); void entry_set_group_id(Entry *e, int id); -void entry_set_due_date(Entry *e, struct tm due_date); +void entry_set_due_date(Entry *e, struct tm *due_date); void entry_set_alt_due_date(Entry *e, char *alt_due_date); void entry_set_title(Entry *e, char *title); void entry_set_color(Entry *e, char *color); diff --git a/src/main.c b/src/main.c index c447047..70e4516 100644 --- a/src/main.c +++ b/src/main.c @@ -1,4 +1,5 @@ #include +#include #include "db.h" #include "draw.h" @@ -13,6 +14,7 @@ int main() { db_init(); groups = db_load_groups(&group_count); + entries = db_load_entries(&entry_count); for(int i = 0; i < group_count; ++i) { fprintf(stderr, "group %d:\n", i+1); @@ -22,22 +24,28 @@ int main() { fprintf(stderr, "\t Url: %s\n", group_get_url(&(groups[i]))); } - /* + // DEBUG + char due_date[BUF_LEN]; for(int i = 0; i < entry_count; ++i) { fprintf(stderr, "entry %d:\n", i+1); - fprintf(stderr, "\t ID: %d\n", group_get_id(&(groups[i]))); - fprintf(stderr, "\tName: %s\n", group_get_name(&(groups[i]))); - fprintf(stderr, "\tDesc: %s\n", group_get_desc(&(groups[i]))); - fprintf(stderr, "\t Url: %s\n", group_get_url(&(groups[i]))); + fprintf(stderr, "\t ID: %d\n", entry_get_id(&(entries[i]))); + fprintf(stderr, "\t Group ID: %d\n", entry_get_group_id(&(entries[i]))); + memset(due_date, 0, BUF_LEN); + if(entry_get_due_date(&(entries[i])) != NULL) + strftime(due_date, BUF_LEN, "%F", entry_get_due_date(&(entries[i]))); + fprintf(stderr, "\t Due date: %s\n", due_date); + fprintf(stderr, "\tAlt due date: %s\n", entry_get_alt_due_date(&(entries[i]))); + fprintf(stderr, "\t Title: %s\n", entry_get_title(&(entries[i]))); + fprintf(stderr, "\t Color: %s\n", entry_get_color(&(entries[i]))); + fprintf(stderr, "\t Highlight: %s\n", entry_get_highlight(&(entries[i]))); + fprintf(stderr, "\t Done: %d\n", entry_get_done(&(entries[i]))); + fprintf(stderr, "\t Url: %s\n", entry_get_url(&(entries[i]))); } - */ - - entries = db_load_entries(&entry_count); if(groups == NULL) perror("could not get groups"); if(entries == NULL) perror("could not get entries"); - tui_init(&groups, group_count, &entries, entry_count); + //tui_init(&groups, group_count, &entries, entry_count); return 0; } -- cgit