aboutsummaryrefslogtreecommitdiff
path: root/main.c
diff options
context:
space:
mode:
authordam <dam@gudinoff>2022-08-24 00:35:06 +0000
committerdam <dam@gudinoff>2022-08-24 00:35:06 +0000
commitceec90a42dc3b538b68191d041e1112bc523adb0 (patch)
tree1ac866b81418c3345a2325c6981c76e939d36c2b /main.c
parent338d3e054b0305817e84ac61b8db6fd1d9dee09b (diff)
downloadtask-time-tracker-ceec90a42dc3b538b68191d041e1112bc523adb0.tar.zst
task-time-tracker-ceec90a42dc3b538b68191d041e1112bc523adb0.zip
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.
Diffstat (limited to 'main.c')
-rw-r--r--main.c77
1 files changed, 44 insertions, 33 deletions
diff --git a/main.c b/main.c
index 531212f..c551194 100644
--- a/main.c
+++ b/main.c
@@ -6,6 +6,7 @@
#include <assert.h>
#include <errno.h>
#include <inttypes.h>
+#include <limits.h>
#include <ncurses.h>
#include <stdbool.h>
#include <stdlib.h>
@@ -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)