aboutsummaryrefslogtreecommitdiff
path: root/main.c
diff options
context:
space:
mode:
Diffstat (limited to 'main.c')
-rw-r--r--main.c130
1 files changed, 63 insertions, 67 deletions
diff --git a/main.c b/main.c
index 5445157..a8ecef3 100644
--- a/main.c
+++ b/main.c
@@ -35,16 +35,16 @@ typedef struct /*__attribute__((__packed__))*/ {
uint64_t modified_on;
size_t count;
size_t capacity;
- task_t* tasks;
+ task_t *tasks;
ptrdiff_t active_task;
ptrdiff_t selected_task; // TODO Maybe use this instead of indexes?
} database_t;
-const char* DB_FILE_SIGN = "TTT:B:01";
+const char *DB_FILE_SIGN = "TTT:B:01";
const size_t SIZEOF_DB_FILE_SIGN = sizeof(DB_FILE_SIGN);
const size_t SIZEOF_TASK_T = sizeof(task_t);
const size_t SIZEOF_DATABASE_T = sizeof(database_t);
-const char* DAYS_OF_WEEK[] = { "sun", "mon", "tue", "wed", "thu", "fri", "sat" };
+const char *DAYS_OF_WEEK[] = { "sun", "mon", "tue", "wed", "thu", "fri", "sat" };
const uint8_t DAYS_ON_WEEK = sizeof(DAYS_OF_WEEK)/sizeof(char*);
database_t database;
@@ -54,7 +54,7 @@ database_t archive; // TODO To be implemented in the future.
// The string should have capacity for 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(char* string, size_t length) {
+size_t truncate_string_utf8(char *string, size_t length) {
// Check for special cases where no truncation is required.
if (length == 0 || string[length-1] == '\0') {
@@ -72,8 +72,8 @@ size_t truncate_string_utf8(char* string, size_t length) {
// Uses strchr to replace all instances of find by replace.
// Returns string.
-char* replace_char(char* string, char find, char replace) {
- char* idx = string;
+char *replace_char(char *string, char find, char replace) {
+ char *idx = string;
while((idx = strchr(idx, find)) != NULL) {
*idx = replace;
idx++;
@@ -81,7 +81,7 @@ char* replace_char(char* string, char find, char replace) {
return string;
}
-void print_task(const task_t* task) {
+void print_task(const task_t *task) {
printf("name: '%s'\n", task->name);
printf("t[0]: '%" PRIu32 "'\n", task->time[0]);
printf("t[1]: '%" PRIu32 "'\n", task->time[1]);
@@ -98,7 +98,7 @@ void print_task(const task_t* task) {
// Creates new task returned in the pointer. If necessary, expands database capacity.
// Returns success.
-bool create_task(database_t* db, task_t** task) {
+bool create_task(database_t *db, task_t **task) {
assert(db != NULL);
if (db->count == DB_MAX_CAP) {
@@ -113,7 +113,7 @@ bool create_task(database_t* db, task_t** task) {
current_capacity >= DB_MAX_CAP >> 2 ? DB_MAX_CAP :
current_capacity << 1;
- task_t* new_tasks = realloc(db->tasks, new_capacity * SIZEOF_TASK_T);
+ task_t *new_tasks = realloc(db->tasks, new_capacity * SIZEOF_TASK_T);
if (new_tasks == NULL) {
fprintf(stderr, "Failed to expand database.\n");
return false;
@@ -137,7 +137,7 @@ bool create_task(database_t* db, task_t** task) {
return true;
}
-bool delete_task(database_t* db, task_t* task) {
+bool delete_task(database_t *db, task_t *task) {
assert(db != NULL);
assert(task != NULL);
assert(task >= db->tasks && task < &db->tasks[db->count]);
@@ -164,7 +164,7 @@ bool delete_task(database_t* db, task_t* task) {
size_t current_capacity = db->capacity;
if (db->count <= (current_capacity >> 2)) {
size_t new_capacity = current_capacity >> 1;
- task_t* new_tasks = realloc(db->tasks, new_capacity * SIZEOF_TASK_T);
+ task_t *new_tasks = realloc(db->tasks, new_capacity * SIZEOF_TASK_T);
if (new_tasks == NULL) {
fprintf(stderr, "Failed to shrink database.\n");
return false;
@@ -176,20 +176,20 @@ bool delete_task(database_t* db, task_t* task) {
return true;
}
-void clear_database(database_t* db) {
+void clear_database(database_t *db) {
free(db->tasks);
memset(db, 0, SIZEOF_TASK_T);
}
// Stores data from database into binary file.
// Returns success.
-bool store_database(const database_t* db, const char* path_name) {
+bool store_database(const database_t *db, const char *path_name) {
assert(db != NULL);
assert(path_name != NULL);
// Open file.
- FILE* file = fopen(path_name, "w");
+ FILE *file = fopen(path_name, "w");
if (file == NULL) {
fprintf(stderr, "Failed to open file '%s' while storing database: %s.\n", path_name, strerror(errno));
return false;
@@ -205,13 +205,13 @@ bool store_database(const database_t* db, const char* path_name) {
// Loads data from binary file into database.
// Returns success.
-bool load_database(database_t* db, const char* path_name) {
+bool load_database(database_t *db, const char *path_name) {
assert(db != NULL);
assert(path_name != NULL);
// Open file.
- FILE* file = fopen(path_name, "r");
+ FILE *file = fopen(path_name, "r");
if (file == NULL) {
fprintf(stderr, "Failed to open file '%s' while loading database: %s.\n", path_name, strerror(errno));
return false;
@@ -243,12 +243,12 @@ bool load_database(database_t* db, const char* path_name) {
// Exports data into CSV file.
// Returns success.
-bool export_to_csv(const database_t* db, const char* path_name) {
+bool export_to_csv(const database_t *db, const char *path_name) {
assert(db != NULL);
assert(path_name != NULL);
- FILE* file = fopen(path_name, "w");
+ FILE *file = fopen(path_name, "w");
if (file == NULL) {
fprintf(stderr, "Failed to open file '%s' while exporting to CSV: %s.\n", path_name, strerror(errno));
return false;
@@ -266,7 +266,7 @@ bool export_to_csv(const database_t* db, const char* path_name) {
);
for (uint32_t idx = 0; idx < db->count; idx++) {
- const task_t* task = &db->tasks[idx];
+ const task_t *task = &db->tasks[idx];
fprintf(file, "%s,%" PRIu32 ",%" PRIu32 ",%" PRIu32 ",%" PRIu32 ",%" PRIu32 ",%" PRIu32 ",%" PRIu32 "\n",
task->name,
task->time[0],
@@ -285,22 +285,22 @@ bool export_to_csv(const database_t* db, const char* path_name) {
// Imports CSV file into database.
// Returns success.
-bool import_from_csv(database_t* db, const char* path_name) {
+bool import_from_csv(database_t *db, const char *path_name) {
assert(db != NULL);
assert(path_name != NULL);
- FILE* file = fopen(path_name, "r");
+ FILE *file = fopen(path_name, "r");
if (file == NULL) {
fprintf(stderr, "Failed to open file '%s' while importing from CSV: %s.\n", path_name, strerror(errno));
return false;
}
uint32_t number_of_entries = 0;
- task_t* task;
+ task_t *task;
char *line_buffer = NULL;
size_t line_buffer_size = 0;
ssize_t read_characters = 0;
- char* name_delimiter;
+ char *name_delimiter;
// Skip header line.
fscanf(file, "%*[^\n]\n");
@@ -371,7 +371,7 @@ enum TEST {
void prototype(int level) {
- const char* done = "# -- done -- -- -- /\n";
+ const char *done = "# -- done -- -- -- /\n";
///////////////////////////////////////////////////////////////////////////
// Get current UTC time.
@@ -471,7 +471,7 @@ void prototype(int level) {
// Check size of structs
if (level & T_SOS) {
size_t size;
- char* name;
+ char *name;
fprintf(stderr, "# check structs sizes ------------------------- \\\n");
name = "database_t";
@@ -487,7 +487,7 @@ void prototype(int level) {
}
-char* line_buffer;
+char *line_buffer;
int size_x, size_y, pos_x, pos_y;
uint8_t selected_layout = 0;
@@ -496,14 +496,14 @@ uint8_t selected_layout = 0;
typedef struct {
int table_size;
int table_headers_size;
- char* table_headers[TABLE_HEADERS_SIZE];
+ char *table_headers[TABLE_HEADERS_SIZE];
} layout_t;
-layout_t* layouts = NULL;
+layout_t *layouts = NULL;
void initialize_layouts() {
- layout_t* layout = NULL;
+ layout_t *layout = NULL;
layouts = calloc(2, sizeof(layout_t));
@@ -558,9 +558,9 @@ void free_memory() {
void draw_header() {
- layout_t* layout = &layouts[selected_layout];
+ layout_t *layout = &layouts[selected_layout];
int table_size = layout->table_size;
- char** table_headers = layout->table_headers;
+ char **table_headers = layout->table_headers;
mvaddch(0, 0, ACS_ULCORNER);
for (int idx = 1; idx < size_x-1; idx++) {
@@ -589,13 +589,13 @@ void draw_header() {
}
void draw_table() {
- task_t* task;
- task_t* active_task;
- task_t* selected_task;
+ task_t *task;
+ task_t *active_task;
+ task_t *selected_task;
- layout_t* layout = &layouts[selected_layout];
+ 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 Maybe move this to side function?
start_color();
@@ -733,9 +733,9 @@ void draw_table() {
void draw_footer() {
const char *app_name_version = "TTT v1";
- int row, col; /* to store the number of rows and the number of colums of the screen */
- initscr(); /* start the curses mode */
- getmaxyx(stdscr,row,col); /* get the number of rows and columns */
+ int row, col; // To store the number of rows and the number of colums of the screen.
+ initscr(); // Start the curses mode.
+ getmaxyx(stdscr,row,col); // Get the number of rows and columns.
mvaddch(row-1, 0, ACS_LLCORNER);
addch(' ');
@@ -749,9 +749,9 @@ void draw_footer() {
// TODO This code is now repeated accross the header, table and footer. Clean this up.
- layout_t* layout = &layouts[selected_layout];
+ layout_t *layout = &layouts[selected_layout];
int table_size = layout->table_size;
-// char** table_headers = layout->table_headers;
+// char **table_headers = layout->table_headers;
int start_columns = size_x - 1 - 8*table_size - 2;
int columns[] = {
@@ -783,7 +783,7 @@ int main(int argc, char *argv[]) {
// TODO Parse commands using: https://stackoverflow.com/questions/9642732/parsing-command-line-arguments-in-c
if (argc > 1) {
- char* action;
+ char *action;
bool do_action = false;
for (int idx = 1; idx < argc; idx++) {
@@ -876,14 +876,14 @@ int main(int argc, char *argv[]) {
ch = KEY_RESIZE;
do {
- task_t* active_task = database.tasks + database.active_task;
- task_t* selected_task = database.tasks + database.selected_task;
+ task_t *active_task = database.tasks + database.active_task;
+ task_t *selected_task = database.tasks + database.selected_task;
switch(ch)
{
case KEY_F(1):
{
- task_t* new_task;
+ task_t *new_task;
create_task(&database, &new_task);
int row = database.count;
mvaddch(row, 0, ACS_DIAMOND);
@@ -906,7 +906,7 @@ int main(int argc, char *argv[]) {
strcpy(new_task->name, "-- new task --");
}
new_task->name[MAX_TASK_NAME-1] = '\0';
- char* name = new_task->name;
+ char *name = new_task->name;
truncate_string_utf8(name, MAX_TASK_NAME-1);
curs_set(0);
break;
@@ -941,7 +941,7 @@ int main(int argc, char *argv[]) {
case ' ':
if (true) {
- task_t* next_task = selected_task;
+ task_t *next_task = selected_task;
if (active_task > 0) {
// TODO Add remaining time to task.
database.active_task = -1;
@@ -993,7 +993,7 @@ int main(int argc, char *argv[]) {
draw_footer();
}
else {
- const char* INVALID_WINDOW_MESSAGE = "Please expand window.";
+ const char *INVALID_WINDOW_MESSAGE = "Please expand window.";
mvaddstr(size_y / 2, (size_x - strlen(INVALID_WINDOW_MESSAGE)) / 2, INVALID_WINDOW_MESSAGE);
}
@@ -1014,31 +1014,27 @@ WINDOW *create_newwin(int height, int width, int starty, int startx) {
WINDOW *local_win;
local_win = newwin(height, width, starty, startx);
- box(local_win, 0 , 0); /* 0, 0 gives default characters
- * for the vertical and horizontal
- * lines */
- wrefresh(local_win); /* Show that box */
+ box(local_win, 0 , 0); // 0, 0 gives default characters for the vertical and horizontal lines.
+ wrefresh(local_win); // Show that box.
return local_win;
}
void destroy_win(WINDOW *local_win) {
- /* box(local_win, ' ', ' '); : This won't produce the desired
- * result of erasing the window. It will leave it's four corners
- * and so an ugly remnant of window.
- */
+ // box(local_win, ' ', ' '); : This won't produce the desired
+ // result of erasing the window. It will leave it's four corners
+ // and so an ugly remnant of window.
wborder(local_win, ' ', ' ', ' ',' ',' ',' ',' ',' ');
- /* The parameters taken are
- * 1. win: the window on which to operate
- * 2. ls: character to be used for the left side of the window
- * 3. rs: character to be used for the right side of the window
- * 4. ts: character to be used for the top side of the window
- * 5. bs: character to be used for the bottom side of the window
- * 6. tl: character to be used for the top left corner of the window
- * 7. tr: character to be used for the top right corner of the window
- * 8. bl: character to be used for the bottom left corner of the window
- * 9. br: character to be used for the bottom right corner of the window
- */
+ // The parameters taken are
+ // 1. win: the window on which to operate
+ // 2. ls: character to be used for the left side of the window
+ // 3. rs: character to be used for the right side of the window
+ // 4. ts: character to be used for the top side of the window
+ // 5. bs: character to be used for the bottom side of the window
+ // 6. tl: character to be used for the top left corner of the window
+ // 7. tr: character to be used for the top right corner of the window
+ // 8. bl: character to be used for the bottom left corner of the window
+ // 9. br: character to be used for the bottom right corner of the window
wrefresh(local_win);
delwin(local_win);
}