aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authordam <dam@gudinoff>2022-12-16 00:10:29 +0000
committerdam <dam@gudinoff>2022-12-16 00:10:29 +0000
commited4c07fec790af982dbbfc40695da38da438bfb8 (patch)
tree221940e63077add3c1177be3c55ccdca1ea5f14b
parenta38e679a2d2f3debe47fc74b6c511e9b6806f7cd (diff)
downloadtask-time-tracker-ed4c07fec790af982dbbfc40695da38da438bfb8.tar.zst
task-time-tracker-ed4c07fec790af982dbbfc40695da38da438bfb8.zip
Fixed logic to edit task time.
-rw-r--r--main.c84
-rw-r--r--readme.md2
2 files changed, 43 insertions, 43 deletions
diff --git a/main.c b/main.c
index 0623ef6..6ef30f6 100644
--- a/main.c
+++ b/main.c
@@ -152,7 +152,7 @@ size_t truncate_string_utf8(char *string, size_t length) {
}
// Returns true when the string is empty or consists of white space characters.
-bool is_empty_string(char *string) {
+bool is_empty_string(const char *string) {
for (int idx = 0; string[idx] != '\0'; idx++) {
switch(string[idx]) {
case ' ':
@@ -532,6 +532,19 @@ void set_task_time(database_st *db, task_st *task, int day, int64_t time) {
*total = add_int64(*total, *timer);
}
+// Adds the time on the day and task provided (and adjusts database totals).
+void add_task_time(database_st *db, task_st *task, int day, int64_t time) {
+ assert(db != NULL);
+ assert(task != NULL);
+ assert(task >= db->tasks && task - db->tasks < db->count);
+
+ // Make sure we sync before applying the changes.
+ update_times(db);
+
+ task->times[day] = add_int64(task->times[day], time);
+ db->total_times[day] = add_int64(db->total_times[day], time);
+}
+
// Resets database to the initial state and deallocates all memory taken by tasks.
void reset_database(database_st *db) {
assert(db != NULL);
@@ -1244,6 +1257,7 @@ int main(int argc, char *argv[]) {
" # adds # seconds;\n"
" #m specifies # as minutes;\n"
" #h specifies # as hours;\n"
+ " #d specifies # as days;\n"
" #y specifies # as years.\n"
" UP Select task above.\n"
" DOWN Select task below.\n"
@@ -1474,7 +1488,7 @@ int main(int argc, char *argv[]) {
break;
}
- // Prepare row to input new task name.
+ // Prepare position to input time operation.
int selected_day = key - '1';
int input_width = layout->columns[L_DAYS_IDX + selected_day].width;
int input_pos_x = 1 + layout->columns[L_TITLE_IDX].width;
@@ -1483,37 +1497,34 @@ int main(int argc, char *argv[]) {
}
input_pos_x++;
- // Get time delta.
+ // Get input string.
read_input_to_string_buffer(selected_task_row, input_pos_x, action_style, input_width);
-
- // TODO Review code
- /* Check if parsed OK. For that, I need to read the manual to know what strtoX returns.
- // It seems that the float parsing may return INF or NAN. Take special care with those.
- // Once I know the parse was OK, I'll check the remaining of the string for multipliers:
- // s/S - second (default if none is found)
- // m/M - minute
- // h/H - hour
- // d/D - day
- // y/Y - year
- */
-
char *input = string_buffer;
-
+
+ // Abort if input if empty.
if (is_empty_string(input) == true) {
break;
}
-
+
+ // Search for assign '=' operator and discard everything before (if found).
char *assign_str = strchr(input, '=');
bool is_assign = assign_str != NULL;
if (is_assign == true) {
input = assign_str + 1;
}
-
+
+ // Try to parse a number and abort if it fails.
char *parser;
long double input_float = strtold(input, &parser);
+ if (parser == input) {
+ break;
+ }
+ input = parser;
+
+ // Try to parse a character representing the time multiplier.
long double multiplier = 1.0;
- for (int i=0; i < strlen(parser); i++) {
- char ch = parser[i];
+ for (int i=0; i < strlen(input); i++) {
+ char ch = input[i];
if (ch == 'm' || ch == 'M') {
multiplier = SECONDS_IN_MINUTE;
break;
@@ -1531,35 +1542,24 @@ int main(int argc, char *argv[]) {
break;
}
}
-
- long double result = input_float * multiplier;
- int64_t seconds = result;
- bool is_result_valid = (result >= (long double)INT64_MIN && result <= (long double)INT64_MAX);
- char action = is_assign ? '=': result >= 0 ? '+' : '-';
-
- // TODO TEST
-// fprintf(stderr, "%c : %Lf x %Lf = %Lf\n", action, input_float, multiplier, result);
-// fprintf(stderr, "[%20" PRId64 "\n", INT64_MIN);
-// fprintf(stderr, " %20.0Lf\n", result);
-// fprintf(stderr, " %20" PRId64 " is %s\n", seconds, is_result_valid ? "valid" : "INVALID");
-// fprintf(stderr, " %+20" PRId64 "]\n", INT64_MAX);
+ // Process input and check if it's valid.
+ long double input_time = input_float * multiplier;
+ bool is_result_valid = (input_time >= (long double)INT64_MIN && input_time <= (long double)INT64_MAX);
if (is_result_valid == false) {
break;
}
- // Make sure we sync before applying the changes.
- update_times(db);
-
+ // Apply changes.
+ int64_t time = input_time;
int day = (selected_day + FIRST_DAY_OF_WEEK) % NUM_WEEK_DAYS;
- int64_t time = selected_task->times[day];
- time = (action == '=' ? 0 : time) + seconds;
-
- // Adust time.
- set_task_time(db, selected_task, day, time);
-
+ if (is_assign == true) {
+ set_task_time(db, selected_task, day, time);
+ }
+ else {
+ add_task_time(db, selected_task, day, time);
+ }
trigger_autosave();
-
break;
}
diff --git a/readme.md b/readme.md
index 59a43d2..6a6d47e 100644
--- a/readme.md
+++ b/readme.md
@@ -72,5 +72,5 @@ Task Time Tracker
- By having each column-print job decoulpled, we avoid havint to measure and compensate lengths of UTF8 strings;
- [x] Review all code for bugs related to auto-cast on ptrdiff_t (signed/unsigned);
- [x] Review all code for bugs related to auto-cast on size_t (signed/unsigned);
+- [x] Go over all `TODO` items;
- [ ] Hide stderr messages from app screen.
-- [ ] Go over all `TODO` items;