From ceec90a42dc3b538b68191d041e1112bc523adb0 Mon Sep 17 00:00:00 2001 From: dam Date: Wed, 24 Aug 2022 00:35:06 +0000 Subject: Checks if CHAR_BIT is 8. Using char as unit of string. Completed truncate_string_utf8 function. Fixed all warnings. Fix bug on replace_char function. --- main.c | 77 ++++++++++++++++++++++++++++++++++++--------------------------- readme.md | 9 ++++---- 2 files changed, 48 insertions(+), 38 deletions(-) diff --git a/main.c b/main.c index 531212f..c551194 100644 --- a/main.c +++ b/main.c @@ -6,6 +6,7 @@ #include #include #include +#include #include #include #include @@ -48,28 +49,27 @@ uint32_t selected_task = -1; // The string should have, at least length number of items. // The terminating null byte ('\0') is included in length. // The function returns the amount of items that got discarded counting from length. -size_t truncate_string_utf8(uint8_t* string, size_t length) { +size_t truncate_string_utf8(char* string, size_t length) { - size_t idx = length - 1; - - // Check for special case where no truncation is required. - if (string[idx] == '\0') { + // Check for special cases where no truncation is required. + if (length == 0 || string[length] == '\0') { + string[length] = '\0'; return 0; } // Search for a non-UTF8-sequence-item so we can truncate the string. + size_t idx = length - 1; while(idx > 0 && ((string[idx] & 0xC0) == 0x80)) { idx--; } - string[idx] = '\0'; #if DEBUG // Check if there is a null byte before the place where we terminated the string. - size_t idx_dbd = idx - 1; - while(idx_dbd >= 0) { - assert(string[idx_dbd] != '\0' && "Found unexpected null byte. The user is at fault."); - idx_dbd--; + size_t idx_dbg = idx; + while (idx_dbg > 0) { + assert(string[idx_dbg-1] != '\0'); + idx_dbg--; } #endif @@ -77,10 +77,10 @@ size_t truncate_string_utf8(uint8_t* string, size_t length) { } char* replace_char(char* string, char find, char replace){ - char *idx = string; + char* idx = string; while((idx = strchr(idx, find)) != NULL) { *idx = replace; - *idx++; + idx++; } return string; } @@ -96,7 +96,12 @@ void print_task(const task_t* task) { printf("t[6]: '%" PRIu32 "'\n", task->time[6]); } -void store_database(const task_t* database, uint32_t number_of_entries, const uint8_t* path_name) { +void initialization() { + // Make sure architecture uses 8bits per char. + assert(CHAR_BIT == 8); +} + +void store_database(const task_t* database, uint32_t number_of_entries, const char* path_name) { assert(database != NULL); assert(path_name != NULL); @@ -253,9 +258,8 @@ uint32_t import_database(task_t** database, const char* path_name) { } -void prt(uint8_t* str, uint8_t size) { // TODO Debug function... to be removed. +void prt(char* str, uint8_t size) { // TODO Debug function... to be removed. fprintf(stderr, ">"); - int count = 0; for (uint8_t idx = 0; idx < size; idx++) { if (idx % 2 == 0) { fprintf(stderr, " "); @@ -267,20 +271,30 @@ void prt(uint8_t* str, uint8_t size) { // TODO Debug function... to be removed. void prototype() { - int count; uint8_t size = 20; - uint8_t* test_string; - test_string = calloc(size, sizeof(uint8_t)); - fprintf(stderr, "%s", "çéº𒐫\n"); // C3 A7 - C3 A9 - C2 BA - F0 92 90 AB - sprintf(test_string, "%s", "çéº𒐫\nxpto "); + char* test_string; + // C3 A7 - C3 A9 - C2 BA - 2C - F0 92 90 AB + const char* DUMMY = "çéº,,,𒐫"; + test_string = calloc(size, sizeof(char)); + sprintf(test_string, "%s", DUMMY); + + fprintf(stderr, "%s\n", test_string); + prt(test_string, size); + // C3 A7 - C3 A9 - C2 BA - 2E - F0 92 90 AB + replace_char(test_string, ',', ','); + fprintf(stderr, "%s\n", test_string); prt(test_string, size); + // test_string[10] = '\0'; // prt(test_string, size); - uint8_t trunc = truncate_string_utf8(test_string, 3); - prt(test_string, size); - fprintf(stderr, ">>> %d : '%s'\n", trunc, test_string); +// test_string[1] = '\0'; +// uint8_t trunc = truncate_string_utf8(test_string, 3); +// fprintf(stderr, "%d:%s\n", trunc, test_string); +// prt(test_string, size); + + return; FILE* file = fopen("./test.txt", "w"); @@ -395,7 +409,7 @@ void prototype() { char* line_buffer; -int size_x, size_y; +int size_x, size_y, pos_x, pos_y; uint8_t selected_layout = 0; #define TABLE_HEADERS_SIZE 9 @@ -487,11 +501,10 @@ void draw_header() { void draw_table() { task_t* task; - int x, y; layout_t* layout = &layouts[selected_layout]; int table_size = layout->table_size; - char** table_headers = layout->table_headers; +// char** table_headers = layout->table_headers; // TODO Colors start_color(); @@ -539,14 +552,14 @@ void draw_table() { } // Print columns separators. - getyx(stdscr,y,x); + pos_y = getcury(stdscr); for (int c_idx = 0; c_idx < sizeof(columns)/sizeof(int); c_idx++) { - mvaddch(y, columns[c_idx], ACS_VLINE); + mvaddch(pos_y, columns[c_idx], ACS_VLINE); } // Go to next line. - y++; - move(y, 0); + pos_y++; + move(pos_y, 0); } } @@ -575,6 +588,7 @@ WINDOW *create_newwin(int height, int width, int starty, int startx); void destroy_win(WINDOW *local_win); int main(int argc, char *argv[]) { + initialization(); // TODO Parse commands using: https://stackoverflow.com/questions/9642732/parsing-command-line-arguments-in-c if (argc > 1) { @@ -616,9 +630,6 @@ int main(int argc, char *argv[]) { refresh(); my_win = create_newwin(height, width, starty, startx); - int rows; - int colums; - ch = KEY_RESIZE; do { switch(ch) diff --git a/readme.md b/readme.md index 210b5e7..8489b9f 100644 --- a/readme.md +++ b/readme.md @@ -4,14 +4,13 @@ Task Time Tracker # notes - Only one task may be active; - The log will be a circular array. During app startup the array will be loaded from a csv file. The log array should have a fixed length. Each string in the array should also have fixed length. Loading and storing it to a file will be implemented using readline and writeline operations. In order for the log file to be CSV compatible, we must always use the same format when writing to the log array. Best approach is to use a function that enfores the CSV format. The log should be written to a file every 5 minutes (not set in stone), if required; such may be done using a `log_is_dirty` flag. A possible structure for the log entries is: {uint64_t timestamp; uint8_t action[16]; uint8_t task_name[MAX_TASK_NAME+1]; uint8_t notes[16]; }. -- Truncate UTF8 string: - - If null-termination is found at or before the max_size; + # to-do list -- [ ] Include check on number of char bits; -- [ ] Decide once for all if I'll be using uint8_t or char for strings. +- [x] Include check on number of char bits; +- [x] Decide once for all if I'll be using uint8_t or char for strings: use char. - [x] maybe rename to task-time-tracker? -- [ ] Remove hash stuff; +- [x] Remove hash stuff; - [ ] Tasks should have a `modified_on` timestamp field; - [ ] Status of task will allow to keep counting time even when the process gets terminated forcefully; - [ ] Make sure task names don't include commas ','; -- cgit v1.2.3