diff options
| author | dam <dam@gudinoff> | 2022-08-31 17:13:09 +0000 |
|---|---|---|
| committer | dam <dam@gudinoff> | 2022-08-31 17:13:09 +0000 |
| commit | da47dcec5c4f73c5ebdc542ee209b3ee289b947f (patch) | |
| tree | 705fc1b7547e984357b3852a132e45558712e0d0 /main.c | |
| parent | f1f5a61dd8db0349a0f9e611eef3c348a96f23d6 (diff) | |
| download | task-time-tracker-da47dcec5c4f73c5ebdc542ee209b3ee289b947f.tar.zst task-time-tracker-da47dcec5c4f73c5ebdc542ee209b3ee289b947f.zip | |
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.
Diffstat (limited to 'main.c')
| -rw-r--r-- | main.c | 19 |
1 files changed, 13 insertions, 6 deletions
@@ -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(); |
