From a26c817cc6f03d750f5b10568630920a405a47c2 Mon Sep 17 00:00:00 2001 From: dam Date: Wed, 10 Aug 2022 15:55:51 +0000 Subject: Prototype of export_database and replace_char functions. --- task_tracker.c | 70 +++++++++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 64 insertions(+), 6 deletions(-) diff --git a/task_tracker.c b/task_tracker.c index cb41edc..7de66be 100644 --- a/task_tracker.c +++ b/task_tracker.c @@ -1,4 +1,6 @@ -// Compile with: gcc task_tracker.c -Wall -O2 -m64 -lncurses -o task_tracker.x64 +// Compilation command: +// - release: gcc task_tracker.c -Wall -O2 -m64 -lncurses -o task_tracker.x64 +// - debug : gcc task_tracker.c -Wall -g3 -m64 -lncurses -o task_tracker.x64 #include @@ -12,7 +14,7 @@ #define STR(X) STR_(X) // Force argument expansions before converting to string. #define MAX_TASK_NAME 127 -#define INITIAL_DAY_OF_WEEK 1 // (0-6, Sunday = 0) +#define FIRST_DAY_OF_WEEK 1 // (0-6, Sunday = 0) #define LOG_FILE_NAME "log.txt" #define DB_FILE_NAME "database.bin" @@ -26,6 +28,9 @@ struct task_event { struct task_info { uint32_t hash; +// uint32_t time[sizeof(DAYS_OF_WEEK)]; +// #define DAYS_ON_WEEK sizeof(DAYS_OF_WEEK) +// uint32_t time[DAYS_ON_WEEK]; uint32_t time[7]; char name[MAX_TASK_NAME+1]; // Allow space for null termination char. }; @@ -34,7 +39,8 @@ struct task_info { const char* DB_FILE_SIGNATURE = "TTBF:1.0"; const uint8_t FILE_SIGNATURE_SIZE = sizeof(DB_FILE_SIGNATURE); const size_t TASK_INFO_SIZE = sizeof(struct task_info); - +const char* DAYS_OF_WEEK[] = { "sun", "mon", "tue", "wed", "thu", "fri", "sat" }; +const uint8_t DAYS_ON_WEEK = sizeof(DAYS_OF_WEEK)/sizeof(char*); struct task_info* tasks = NULL; uint32_t tasks_count = 0; @@ -46,6 +52,15 @@ uint32_t tasks_names_size = 0; uint32_t tasks_names_capacity = 0; +char* replace_char(char* string, char find, char replace){ + char *idx = string; + while((idx = strchr(idx, find)) != NULL) { + *idx = replace; + *idx++; + } + return string; +} + void print_task(const struct task_info* task) { printf("name: '%s'\n", task->name); printf("hash: '%" PRIu32 "'\n", task->hash); @@ -106,6 +121,41 @@ uint32_t load_database(struct task_info** database) { return number_of_entries; } +void export_database(const struct task_info* database, uint32_t number_of_entries) { + + #define DB_CSV_FILE_NAME "database.csv" + + // TODO Add offset for FIRST_DAY_OF_WEEK + + FILE* file = fopen("./" DB_CSV_FILE_NAME, "w"); + + fprintf(file, "%s,%s,%s,%s,%s,%s,%s,%s\n", + "task", + DAYS_OF_WEEK[(0+FIRST_DAY_OF_WEEK)%DAYS_ON_WEEK], + DAYS_OF_WEEK[(1+FIRST_DAY_OF_WEEK)%DAYS_ON_WEEK], + DAYS_OF_WEEK[(2+FIRST_DAY_OF_WEEK)%DAYS_ON_WEEK], + DAYS_OF_WEEK[(3+FIRST_DAY_OF_WEEK)%DAYS_ON_WEEK], + DAYS_OF_WEEK[(4+FIRST_DAY_OF_WEEK)%DAYS_ON_WEEK], + DAYS_OF_WEEK[(5+FIRST_DAY_OF_WEEK)%DAYS_ON_WEEK], + DAYS_OF_WEEK[(6+FIRST_DAY_OF_WEEK)%DAYS_ON_WEEK] + ); + + for (uint32_t idx = 0; idx < number_of_entries; idx++) { + const struct task_info* task = &database[idx]; + fprintf(file, "%s,%" PRIu32 ",%" PRIu32 ",%" PRIu32 ",%" PRIu32 ",%" PRIu32 ",%" PRIu32 ",%" PRIu32 "\n", + task->name, + task->time[0], + task->time[1], + task->time[2], + task->time[3], + task->time[4], + task->time[5], + task->time[6] + ); + } + fclose(file); +} + uint32_t CRC32(const uint8_t data[], size_t data_length) { static const uint32_t crc32_table[] = { @@ -207,14 +257,15 @@ void add_task_event(char* name, uint64_t start, uint64_t end) { void prototype() { + /////////////////////////////////////////////////////////////////////////// + // Get current day of the week. time_t now_ut = time(NULL); uint8_t week_day = localtime(&now_ut)->tm_wday; - printf("=== Bazinga : %d\n", week_day); - + printf("# current day of the week: %d\n", week_day); /////////////////////////////////////////////////////////////////////////// - // Test moving database data from memory to file and back to memory. + // Store database from memory to binary file. struct task_info task; memset(&task, '0', TASK_INFO_SIZE); @@ -231,13 +282,20 @@ void prototype() { store_database(&task, 1); + /////////////////////////////////////////////////////////////////////////// + // Store database from memory to binary file. struct task_info* task_dest = NULL; uint32_t number_of_entries = load_database(&task_dest); print_task(task_dest); fprintf(stderr, ">>>>> %" PRIu32 "\n", number_of_entries); free(task_dest); + + /////////////////////////////////////////////////////////////////////////// + // Export database to CSV file. + export_database(&task, 1); + return; -- cgit v1.2.3