From da47dcec5c4f73c5ebdc542ee209b3ee289b947f Mon Sep 17 00:00:00 2001 From: dam Date: Wed, 31 Aug 2022 17:13:09 +0000 Subject: Reduced DB_MAX_CAP for nearest power-of-two. Fixed export_to_csv to clean commas and use size_t and ptrdiff_t instead of uin32_t indexes. Protected GUI against database reaching its maximum capacity. --- main.c | 19 +++++++++++++------ readme.md | 2 +- 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/main.c b/main.c index a8ecef3..87718ed 100644 --- a/main.c +++ b/main.c @@ -24,7 +24,7 @@ #define DB_BIN_PATH_NAME "./database.bin" #define DB_CSV_PATH_NAME "./database.csv" -#define DB_MAX_CAP PTRDIFF_MAX +#define DB_MAX_CAP ((PTRDIFF_MAX >> 1) + 1) typedef struct /*__attribute__((__packed__))*/ { uint32_t time[7]; @@ -110,7 +110,7 @@ bool create_task(database_t *db, task_t **task) { size_t current_capacity = db->capacity; if((db->count + 1) > current_capacity) { size_t new_capacity = current_capacity == 0 ? 2 : - current_capacity >= DB_MAX_CAP >> 2 ? DB_MAX_CAP : + current_capacity > DB_MAX_CAP >> 1 ? DB_MAX_CAP : // Protect against DB_MAX_CAP != power-of-two. current_capacity << 1; task_t *new_tasks = realloc(db->tasks, new_capacity * SIZEOF_TASK_T); @@ -265,10 +265,13 @@ bool export_to_csv(const database_t *db, const char *path_name) { DAYS_OF_WEEK[6] ); - for (uint32_t idx = 0; idx < db->count; idx++) { - const task_t *task = &db->tasks[idx]; + char name[MAX_TASK_NAME]; + task_t *limit = db->tasks + db->count; + for (task_t *task = db->tasks; task < limit; task++) { + memcpy(name, task->name, MAX_TASK_NAME); + replace_char(name, ',', ' '); fprintf(file, "%s,%" PRIu32 ",%" PRIu32 ",%" PRIu32 ",%" PRIu32 ",%" PRIu32 ",%" PRIu32 ",%" PRIu32 "\n", - task->name, + name, task->time[0], task->time[1], task->time[2], @@ -820,6 +823,7 @@ int main(int argc, char *argv[]) { action = "--t_ecsv"; do_action = strncmp(argv[idx], action, strlen(action)) == 0; if (do_action) { + load_database(&database, DB_BIN_PATH_NAME); prototype(T_ECSV); return EXIT_SUCCESS; } @@ -884,7 +888,10 @@ int main(int argc, char *argv[]) { case KEY_F(1): { task_t *new_task; - create_task(&database, &new_task); + if (create_task(&database, &new_task) == false) { + // ERROR + break; + } int row = database.count; mvaddch(row, 0, ACS_DIAMOND); clrtoeol(); diff --git a/readme.md b/readme.md index bff27b5..08972d9 100644 --- a/readme.md +++ b/readme.md @@ -15,11 +15,11 @@ Task Time Tracker - [x] Change capacity to size_t. - [x] Change active_task to active_task_ptrdiff. - [x] use selected_task_ptrdiff? +- [x] Make sure task names don't include commas ','; - [ ] On compact layout, start task names right after the vertical line, otherwise, add a space (as is now). - [ ] Allow two view modes: one to iew non-archived tasks; another to view archived tasks. - [ ] To add a new task, create it and enter the task-name-edit mode. To edit, use the ACS_CKBOARD to show empty spaces and ACS_LARROW and ACS_RARROW to show continuation on horizontal direction. - [ ] Status of task will allow to keep counting time even when the process gets terminated forcefully; -- [ ] Make sure task names don't include commas ','; - [ ] Review code: char !uint8_t; - [ ] Change task order (use task_t tmp_task + memcpy); - [ ] Create task using keys: `c` and `C`; -- cgit v1.2.3