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 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) (limited to 'main.c') 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(); -- cgit v1.2.3