From 10045da39d6eb6aa767b360fbea43d5361383c15 Mon Sep 17 00:00:00 2001 From: dam Date: Wed, 24 Jul 2024 12:27:45 +0100 Subject: Improved robustness of CSV line parsing. --- ttt.jai | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/ttt.jai b/ttt.jai index 2bbc90d..cc6ec0d 100644 --- a/ttt.jai +++ b/ttt.jai @@ -14,7 +14,7 @@ DEBUG :: false; #import "UTF8"; TUI :: #import "TUI"(COLOR_MODE_BITS=4); -VERSION :: "3.0"; // Use only 3 chars (to fit layouts). +VERSION :: "3.1"; // Use only 3 chars (to fit layouts). YEAR :: "2024"; NUM_WEEK_DAYS :: 7; @@ -175,14 +175,6 @@ hide_processing :: () { TUI.tui_write_string(TUI.Commands.TextMode); } -// Advance to next item in array. -advance :: inline (array: *[] $T, amount: int = 1) { - assert(amount >= 0); - assert(array.count >= amount); - array.count -= amount; - array.data += amount; -} - // Advances to next line and returns it. // Taken from Text_File_Handler module. // Returns the next line and success. @@ -645,8 +637,6 @@ load_app_state :: (db: *Database, st_path: string, db_path: string) -> success: case "first_day_of_week"; first_day_of_week = abs(string_to_int(value)) % NUM_WEEK_DAYS; } - - advance(*csv_line); } // Adjust selected task. @@ -721,12 +711,20 @@ import_from_csv :: (db: *Database, path: string) -> bool #must { task: Task; csv_line := split(line, CSV_SEPARATOR,, temporary_allocator); - + + if csv_line.count != 8 { + log_error("Discarded invalid CSV entry '%' from file '%'.", line, path); + continue; + } + // Truncate and import task name. task_name := truncate(csv_line[0], task.name.count); memcpy(task.name.data, task_name.data, task_name.count); + + // Advance to next item in csv_line array. + csv_line.count -= 1; + csv_line.data += 1; - advance(*csv_line); for csv_line task.times[it_index] = string_to_int(it); -- cgit v1.2.3