aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--task_tracker.c173
1 files changed, 125 insertions, 48 deletions
diff --git a/task_tracker.c b/task_tracker.c
index 7de66be..5472f12 100644
--- a/task_tracker.c
+++ b/task_tracker.c
@@ -3,6 +3,8 @@
// - debug : gcc task_tracker.c -Wall -g3 -m64 -lncurses -o task_tracker.x64
+#include <assert.h>
+#include <errno.h>
#include <inttypes.h>
#include <ncurses.h>
#include <stdbool.h>
@@ -18,6 +20,7 @@
#define LOG_FILE_NAME "log.txt"
#define DB_FILE_NAME "database.bin"
+#define DB_CSV_PATH_NAME "./database.csv"
struct task_event {
@@ -26,23 +29,23 @@ struct task_event {
time_t end;
};
-struct task_info {
+typedef struct {
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.
-};
+} task_t;
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 size_t TASK_INFO_SIZE = sizeof(task_t);
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;
+task_t* tasks = NULL;
uint32_t tasks_count = 0;
uint32_t tasks_capacity = 0;
@@ -61,7 +64,7 @@ char* replace_char(char* string, char find, char replace){
return string;
}
-void print_task(const struct task_info* task) {
+void print_task(const task_t* task) {
printf("name: '%s'\n", task->name);
printf("hash: '%" PRIu32 "'\n", task->hash);
printf("t[0]: '%" PRIu32 "'\n", task->time[0]);
@@ -73,13 +76,19 @@ void print_task(const struct task_info* task) {
printf("t[6]: '%" PRIu32 "'\n", task->time[6]);
}
-void store_database(const struct task_info* database, uint32_t number_of_entries) {
+void store_database(const task_t* database, uint32_t number_of_entries) {
+ // TODO Maybe move this to global level?
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 size_t TASK_INFO_SIZE = sizeof(task_t);
+ // Open file.
FILE* file = fopen("./" DB_FILE_NAME, "w");
+ if (file == NULL) {
+ fprintf(stderr, "Failed to open database file: %s.\n", strerror(errno)); // TODO Fix message.
+ return;
+ }
fwrite(DB_FILE_SIGNATURE, sizeof(char), FILE_SIGNATURE_SIZE, file);
fwrite(&number_of_entries, sizeof(number_of_entries), 1, file);
@@ -88,60 +97,64 @@ void store_database(const struct task_info* database, uint32_t number_of_entries
fclose(file);
}
-uint32_t load_database(struct task_info** database) {
+uint32_t load_database(task_t** database) {
+ // TODO Maybe move this to global level?
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 size_t TASK_INFO_SIZE = sizeof(task_t);
+ // Open file.
FILE* file = fopen("./" DB_FILE_NAME, "r");
+ if (file == NULL) {
+ fprintf(stderr, "Failed to open database file: %s.\n", strerror(errno)); // TODO Fix message.
+ return 0;
+ }
+ // Validate file signature.
char file_signature[FILE_SIGNATURE_SIZE];
fread(&file_signature, sizeof(char), FILE_SIGNATURE_SIZE, file);
if (strncmp(file_signature, DB_FILE_SIGNATURE, FILE_SIGNATURE_SIZE) != 0) {
- printf("Invalid file signature.\n");
+ fprintf(stderr, "Invalid file signature.\n");
return 0;
}
- fprintf(stderr, "-4\n");
+ // Read number of entries.
uint32_t number_of_entries;
fread(&number_of_entries, sizeof(number_of_entries), 1, file);
- fprintf(stderr, "number of entries: %" PRIu32 "\n", number_of_entries);
- fprintf(stderr, "pointer_value: %p : *%p\n", database, *database);
+ // Read database entries.
*database = calloc(number_of_entries, TASK_INFO_SIZE);
- fprintf(stderr, "pointer_value: %p : *%p\n", database, *database);
-
fread(*database, TASK_INFO_SIZE, number_of_entries, file);
- fclose(file);
+ // Make sure we are reading all the file.
+ assert(fgetc(file) == EOF);
- print_task(*database);
+ fclose(file);
return number_of_entries;
}
-void export_database(const struct task_info* database, uint32_t number_of_entries) {
+void export_database(const task_t* database, uint32_t number_of_entries, const char* path_name) {
- #define DB_CSV_FILE_NAME "database.csv"
+ assert(database != NULL);
+ assert(path_name != NULL);
- // TODO Add offset for FIRST_DAY_OF_WEEK
+ FILE* file = fopen(path_name, "w");
- 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]
+ DAYS_OF_WEEK[0],
+ DAYS_OF_WEEK[1],
+ DAYS_OF_WEEK[2],
+ DAYS_OF_WEEK[3],
+ DAYS_OF_WEEK[4],
+ DAYS_OF_WEEK[5],
+ DAYS_OF_WEEK[6]
);
for (uint32_t idx = 0; idx < number_of_entries; idx++) {
- const struct task_info* task = &database[idx];
+ const task_t* task = &database[idx];
fprintf(file, "%s,%" PRIu32 ",%" PRIu32 ",%" PRIu32 ",%" PRIu32 ",%" PRIu32 ",%" PRIu32 ",%" PRIu32 "\n",
task->name,
task->time[0],
@@ -153,7 +166,57 @@ void export_database(const struct task_info* database, uint32_t number_of_entrie
task->time[6]
);
}
+
+ fclose(file);
+}
+
+uint32_t import_database(task_t** database, const char* path_name) {
+
+ assert(database != NULL);
+ assert(path_name != NULL);
+
+ FILE* file = fopen(path_name, "r");
+ if (file == NULL) {
+ fprintf(stderr, "Failed to open database file: %s.\n", strerror(errno)); // TODO Fix message.
+ return 0;
+ }
+
+ uint32_t number_of_entries = 0;
+ task_t task;
+ int error;
+
+ fscanf(file, "%*[^\n]\n");
+ do {
+ // TODO prepare memory for database
+
+ error = fscanf(file,
+ "%" STR(MAX_TASK_NAME) "[^,],%" SCNu32 ",%" SCNu32 ",%" SCNu32 ",%" SCNu32 ",%" SCNu32 ",%" SCNu32 ",%" SCNu32 "\n",
+ task.name,
+ &task.time[0],
+ &task.time[1],
+ &task.time[2],
+ &task.time[3],
+ &task.time[4],
+ &task.time[5],
+ &task.time[6]
+ );
+
+ // TODO At this point we are reading one more... that's why we are reduccing the number_of_entries after the while.
+ task.hash = hash_string(task.name);
+ print_task(&task);
+ number_of_entries++;
+
+ } while(error > 0);
+ assert(number_of_entries == 3);
+
+// if (error != EOF) {
+// fprintf(stderr, "Failed to parse database file: %s.\n", strerror(errno));
+// }
+
+
fclose(file);
+
+ return number_of_entries;
}
uint32_t CRC32(const uint8_t data[], size_t data_length) {
@@ -238,12 +301,12 @@ void add_task(char* name) {
if (tasks_count >= tasks_capacity)
{
tasks_capacity = tasks_capacity == 0 ? 16 : tasks_capacity << 1;
- tasks = realloc(tasks, sizeof(struct task_info) * tasks_capacity);
+ tasks = realloc(tasks, sizeof(task_t) * tasks_capacity);
}
// Add new task entry.
- struct task_info* new_task = &tasks[tasks_count];
- memset(new_task, '0', sizeof(struct task_info));
+ task_t* new_task = &tasks[tasks_count];
+ memset(new_task, '0', sizeof(task_t));
strncpy(new_task->name, name, MAX_TASK_NAME);
new_task->name[MAX_TASK_NAME] = '\0';
printf(">>> %s\n", new_task->name); // TODO debug
@@ -259,15 +322,17 @@ void prototype() {
///////////////////////////////////////////////////////////////////////////
// Get current day of the week.
+ fprintf(stderr, "# current day of the week --------------------- |\n");
time_t now_ut = time(NULL);
uint8_t week_day = localtime(&now_ut)->tm_wday;
- printf("# current day of the week: %d\n", week_day);
+ fprintf(stderr, "> %d\n", week_day);
///////////////////////////////////////////////////////////////////////////
// Store database from memory to binary file.
-
- struct task_info task;
+ /*
+ fprintf(stderr, "# store database ------------------------------ |\n");
+ task_t task;
memset(&task, '0', TASK_INFO_SIZE);
strncpy(task.name, "TASK-00 : Sample task name.", MAX_TASK_NAME+1);
task.time[0] = 0;
@@ -280,22 +345,34 @@ void prototype() {
task.hash = hash_string(task.name);
print_task(&task);
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);
+ // Load database from binary file to memory.
+ fprintf(stderr, "# load database ------------------------------- |\n");
+ task_t* task_load = NULL;
+ uint32_t number_of_entries_load = load_database(&task_load);
- print_task(task_dest);
- fprintf(stderr, ">>>>> %" PRIu32 "\n", number_of_entries);
- free(task_dest);
+ fprintf(stderr, "> loaded %" PRIu32 " entries.\n", number_of_entries_load);
+ print_task(task_load);
+ free(task_load);
///////////////////////////////////////////////////////////////////////////
// Export database to CSV file.
+ /*
+ fprintf(stderr, "# export to CSV ------------------------------- |\n");
export_database(&task, 1);
+ export_database(NULL, 1, "");
+ */
+
+ fprintf(stderr, "# import from CSV ----------------------------- |\n");
+ task_t* task_import = NULL;
+ uint32_t number_of_entries_import = import_database(&task_import, DB_CSV_PATH_NAME);
+ fprintf(stderr, "> imported %" PRIu32 " entries.\n", number_of_entries_import);
+ print_task(task_import);
+ free(task_import);
return;
@@ -345,7 +422,7 @@ void prototype() {
const char* format = "%" STR(MAX_TASK_NAME) "[^,]" SPACER ",%" SCNu32 ",%" SCNu32 ",%" SCNu32 ",%" SCNu32 ",%" SCNu32 ",%" SCNu32 ",%" SCNu32;
-// struct task_info task_A;
+// task_t task_A;
// sscanf(task_info_A, format,
// task_A.name,
// &task_A.time[0],
@@ -359,7 +436,7 @@ void prototype() {
const char* format_X = "%" STR(MAX_TASK_NAME) "[^,]";
- struct task_info task_A;
+ task_t task_A;
sscanf(task_info_A, format_X,
task_A.name
); // TODO WIP
@@ -376,7 +453,7 @@ void prototype() {
&task_A.time[6]
); // TODO WIP
-// struct task_info task_B;
+// task_t task_B;
// sscanf(task_info_B,format,
// task_B.name,
// &task_B.time[0],
@@ -388,7 +465,7 @@ void prototype() {
// &task_B.time[6]
// ); // TODO WIP
- struct task_info task_B;
+ task_t task_B;
sscanf(task_info_B,format_X,
task_B.name
); // TODO WIP
@@ -475,7 +552,7 @@ void prototype() {
void draw_table() {
- struct task_info asd;
+ task_t asd;
}
void draw_header() {