From e8c8132361c114bf6d90e3fb1f46004c3e09fecc Mon Sep 17 00:00:00 2001 From: dam Date: Sat, 11 May 2024 04:11:53 +0100 Subject: Reviewed import_from_csv procedure. --- ttt.jai | 75 +++++++++++------------------------------------------------------ 1 file changed, 12 insertions(+), 63 deletions(-) diff --git a/ttt.jai b/ttt.jai index f9afd52..36271f2 100644 --- a/ttt.jai +++ b/ttt.jai @@ -627,90 +627,45 @@ export_to_csv :: (db: Database, path: string) -> success: bool #must { // Imports CSV file into database. // Returns success. import_from_csv :: (db: *Database, path: string) -> bool #must { - // TODO Review code. assert(db != null, ASSERT_NOT_NULL, "db"); assert(xx path, ASSERT_NOT_EMPTY, "path"); - error_code: s64; - - // Check file size TODO Read based on file size - //file_info: stat_t; - //error_code = sys_stat(path, *file_info); // TODO Check for error. - //size := file_info.st_size; - size := 0; - - success: bool; - map: Map_File_Info; - data: string; - is_using_map := false; - if size >= 1<<30 { - assert(false, "Parsing big files not implemented yet."); - } - else { - data, success = read_entire_file(path); - } - defer if is_using_map then map_entire_file_end(*map); else free(data.data); - csv := data; - if success == false { - log_error("Failed to read file '%' while loading database: ERROR_FROM_LOG", path); // TODO Get error from logger ?! - return false; - } - - - // TODO Helper function. advance :: inline (array: *[] $T, amount: int = 1) { assert(amount >= 0); assert(array.count >= amount); array.count -= amount; array.data += amount; } - - - // TODO Helper function. - consume_next_line :: (sp: *string) -> string, bool { - // To find the end of the line, we look for a linefeed character. - // We will trim a carriage return off the end if there is one there also. - // Thus this works on both 'dos' and 'unix'-style files. + // Taken from Text_File_Handler module. + consume_next_line :: (sp: *string) -> string, bool { s := << sp; found, result, right := split_from_left(s, 10,, temporary_allocator); if !found { - // This is the last line; there may not have been a linefeed after that, - // but we still want to handle that data, so we return true if there was - // a nonzero amount of stuff there. - << sp = ""; - return s, (s.count > 0); } - // Chop the characters we are going to return from 'sp', - // which holds the remaining file data. advance(sp, result.count + 1); if result { - if result[result.count-1] == 13 result.count -= 1; // If there's a carriage return at the end, remove it by decrementing the string's length. + // If there's a carriage return at the end, remove it by decrementing the string's length. + if result[result.count-1] == 13 result.count -= 1; } return result, true; } - //Skip header line. - consume_next_line(*csv); - - next_line :: inline (csv: *string) -> line: string, success: bool { - for 0..csv.count { - if csv.data[it] == #char "\n" { - line: string = < bool #must { auto_release_temp(); line, success := consume_next_line(*csv); - // line, success := next_line(*csv); if success == false break; task: Task; @@ -733,11 +687,6 @@ import_from_csv :: (db: *Database, path: string) -> bool #must { task.times[it_index] = string_to_int(it); add_task(db, *task); - // TODO Check this old code and remove it if not necessary. - // if context.temporary_storage.total_bytes_occupied > (100<<20) { - // print("temp: %\n", context.temporary_storage.total_bytes_occupied >> 20); - // reset_temporary_storage(); - // } } } -- cgit v1.2.3