diff options
| -rw-r--r-- | main.c | 73 | ||||
| -rw-r--r-- | readme.md | 10 |
2 files changed, 33 insertions, 50 deletions
@@ -41,7 +41,7 @@ #define FIRST_DAY_OF_WEEK 1 // (0-6, Sunday = 0). #define NUM_WEEK_DAYS 7 // Just to avoid magic numbers. -#define LOG_FILE_NAME "log.txt" +#define HOME_PATH_ENV "HOME" // #if defined(_WIN64) #define HOME_PATH_ENV "USERPROFILE" #endif #define APP_FOLDER_NAME ".task_time_tracker" #define DB_FILE_NAME "database.bin" #define AR_FILE_NAME "archive.csv" @@ -220,7 +220,6 @@ char *sprint_time5_utf8(char *string, intmax_t time, int space) { } int64_t add_int64(int64_t x, int64_t y) { - if (y > 0 && x > INT64_MAX - y) return INT64_MAX; @@ -231,7 +230,6 @@ int64_t add_int64(int64_t x, int64_t y) { } int64_t sub_int64(int64_t x, int64_t y) { - if (y < 0 && x > INT64_MAX + y) return INT64_MAX; @@ -1021,6 +1019,15 @@ void draw_tui(database_st *db, layout_st *layout) { mvaddstr(y, x, string_buffer); } +static inline void *mem_alloc(size_t mem_size, const char *error_tag) { + void *mem_pointer = malloc(mem_size); + if(mem_pointer == NULL && mem_size > 0) { + fprintf(stderr, "Failed to allocate memory (%s): %s.\n", (error_tag == NULL ? "undefined" : error_tag), strerror(errno)); + exit(EXIT_FAILURE); + } + return mem_pointer; +} + void free_memory() { reset_database(&database); reset_database(&archive); @@ -1033,55 +1040,30 @@ void free_memory() { bool initialize_app_folder() { size_t temp_size; - char* home_path = getenv("HOME"); - -#if defined(_WIN64) - home_path = getenv("USERPROFILE"); -#endif - - if (home_path != NULL) - { - temp_size = strlen(home_path) + 1 + strlen(APP_FOLDER_NAME) + 1; // Add space for folder separator and '\0'. - app_folder = malloc(temp_size); - if (app_folder == NULL && temp_size > 0) { - fprintf(stderr, "Failed to allocate memory for app folder: %s.\n", strerror(errno)); - return false; - } - snprintf(app_folder, temp_size, "%s/%s", home_path, APP_FOLDER_NAME); - - // Create app folder. - mkdir(app_folder, 0740); - if (errno != 0 && errno != EEXIST) { - fprintf(stderr, "Failed to create app folder '%s': %s.\n", app_folder, strerror(errno)); - return false; - } + + char *home_path = getenv(HOME_PATH_ENV); + if (home_path == NULL) { + home_path = "."; } - else { - temp_size = 3; - app_folder = malloc(temp_size); - if (app_folder == NULL && temp_size > 0) { - fprintf(stderr, "Failed to allocate memory for app folder: %s.\n", strerror(errno)); - return false; - } - snprintf(app_folder, temp_size, "./"); + temp_size = strlen(home_path) + 1 + strlen(APP_FOLDER_NAME) + 1; // Add space for folder separator and '\0'. + app_folder = mem_alloc(temp_size, "app folder"); + snprintf(app_folder, temp_size, "%s/%s", home_path, APP_FOLDER_NAME); + + // Create app folder. + mkdir(app_folder, 0740); + if (errno != 0 && errno != EEXIST) { + fprintf(stderr, "Failed to create app folder '%s': %s.\n", app_folder, strerror(errno)); + return false; } // Set database file path. temp_size = strlen(app_folder) + 1 + strlen(DB_FILE_NAME) + 1; // Add space for folder separator and '\0'. - db_file_path = malloc(temp_size); - if (db_file_path == NULL && temp_size > 0) { - fprintf(stderr, "Failed to allocate memory for database file path: %s.\n", strerror(errno)); - return false; - } + db_file_path = mem_alloc(temp_size, "database file path"); snprintf(db_file_path, temp_size, "%s/%s", app_folder, DB_FILE_NAME); // Set archive file path. temp_size = strlen(app_folder) + 1 + strlen(AR_FILE_NAME) + 1; // Add space for folder separator and '\0'. - ar_file_path = malloc(temp_size); - if (ar_file_path == NULL && temp_size > 0) { - fprintf(stderr, "Failed to allocate memory for archive file path: %s.\n", strerror(errno)); - return false; - } + ar_file_path = mem_alloc(temp_size, "archive file path"); snprintf(ar_file_path, temp_size, "%s/%s", app_folder, AR_FILE_NAME); return true; @@ -1092,7 +1074,10 @@ void exit_gracefully(int signal) { ungetch('q'); } -void static read_input_to_string_buffer_with_space(int row, int column, int length, int space) { +void read_input_to_string_buffer_with_space(int row, int column, int length, int space) { + assert(length < string_buffer_size); + assert(space < string_buffer_size); + snprintf(string_buffer, string_buffer_size, "%*s", space, ""); attron(A_UNDERLINE); mvaddstr(row, column, string_buffer); @@ -59,20 +59,18 @@ Task Time Tracker - [x] Check if string_buffer needs to be cleared. We may be leaking info on the string_buffer. - [x] Replaced `sprintf` by `snprintf`; - [x] Make sure that string_buffer bounds are respected; +- [x] Rename `MAX_TASK_NAME` to something more informative; - [ ] Compress code: - [x] Re-do sprint_time5_utf8: -12 delta LOC; - [x] Re-do truncate_string_utf8: 0 delta LOC; - [x] Implement `read_input_to_string_buffer`: -24 delta LOC; - - [ ] wrap malloc (and maybe others) in a function with error checking - ```c - static inline void *MallocOrDie(size_t MemSize) { void *AllocMem = malloc(MemSize); /* Some implementations return null on a 0 length alloc, * we may as well allow this as it increases compatibility * with very few side effects */ if(!AllocMem && MemSize) { printf("Could not allocate memory!"); exit(-1); } return AllocMem; } - ``` + - [x] Wrap malloc (and maybe others) in a function with error checking; - [ ] Move database actions into functions: - `select_task_by_delta(database_st *db, ptrdiff???)` - `select_task_by_index(database_st *db, size_t???)` - `sect_active(database_st *db, task_st *task)` - [ ] Check if draw_tui may be simplified by drawing entire lines of tasks at once and draw columns separators after; -- [ ] Rename `MAX_TASK_NAME` to `TASK_NAME_BUFFER_SIZE`; -- [ ] Try to fix flickering of ncurses; +- [ ] Fix bug: archiving/unarchiving task introduces " ," at end of name and increases the number of spaces before comma; - [ ] Review all code for bugs related to auto-cast on ptrdiff_t/size_t (signed/unsigned); +- [ ] Try to fix flickering of ncurses; - [ ] Go over all `TODO` items; |
