From 6aff2a945f45d5d4a755a2977ef483dd3e8987ff Mon Sep 17 00:00:00 2001 From: Aldrik Ramaekers Date: Fri, 15 Mar 2024 19:42:17 +0100 Subject: import error handling --- src/export.cpp | 2 ++ src/export.h | 4 +++- src/import.cpp | 51 +++++++++++++++++++++++++++++++++++++-------------- src/import.h | 9 +++++++++ src/main.cpp | 32 +++++++++++++++++++++++++++++++- 5 files changed, 82 insertions(+), 16 deletions(-) (limited to 'src') diff --git a/src/export.cpp b/src/export.cpp index 4e316c4..5eeab36 100644 --- a/src/export.cpp +++ b/src/export.cpp @@ -3,6 +3,8 @@ #include "config.h" #include +export_result last_export_result = EXPORT_NONE; + #ifndef _WIN32 #include int fopen_s(FILE **f, const char *name, const char *mode) { diff --git a/src/export.h b/src/export.h index 0a320f8..d440997 100644 --- a/src/export.h +++ b/src/export.h @@ -3,12 +3,14 @@ #include "search.h" #include "platform.h" -typedef enum ts_export_result { +typedef enum t_export_result { EXPORT_NONE, EXPORT_NO_RESULT, EXPORT_SEARCH_ACTIVE, EXPORT_SAVE_PENDING, } export_result; +extern export_result last_export_result; + bool ts_str_has_extension(const utf8_int8_t *str, const utf8_int8_t *suffix); export_result ts_export_result(ts_search_result* result, const utf8_int8_t* path); \ No newline at end of file diff --git a/src/import.cpp b/src/import.cpp index 29f0ff9..4cb1a97 100644 --- a/src/import.cpp +++ b/src/import.cpp @@ -5,6 +5,8 @@ #include #include +import_result last_import_result = IMPORT_NONE; + #ifndef _WIN32 int fopen_s(FILE **f, const char *name, const char *mode); // defined in export.cpp #endif @@ -18,9 +20,9 @@ static utf8_int8_t* _ts_str_find(utf8_int8_t* text, utf8_int8_t token) { } #define fscanf_required(_file, _format, _expect, ...) \ - if (fscanf(_file, _format, __VA_ARGS__) != _expect) { return false; } + if (fscanf(_file, _format, __VA_ARGS__) != _expect) { return IMPORT_INVALID_DATA; } -static bool _ts_import_csv_v1(ts_search_result* result, FILE *read_file) { +static import_result _ts_import_csv_v1(ts_search_result* result, FILE *read_file) { // Read setup info. fscanf_required(read_file, "PATH,%s\n", 1, (char*)result->directory_to_search); fscanf_required(read_file, "FILTER,%s\n", 1, (char*)result->file_filter); @@ -63,9 +65,8 @@ static bool _ts_import_csv_v1(ts_search_result* result, FILE *read_file) { ts_array_push_size(&result->files, ¤t_file, sizeof(ts_found_file*)); } - // New match within current_file - if (current_file && sscanf(line_buffer, "MATCH,%u,%zu,%zu\n", &match.line_nr, &match.word_match_length, &match.word_match_offset) == 3) { + else if (current_file && sscanf(line_buffer, "MATCH,%u,%zu,%zu\n", &match.line_nr, &match.word_match_length, &match.word_match_offset) == 3) { match.file = current_file; match.file->match_count++; @@ -84,22 +85,25 @@ static bool _ts_import_csv_v1(ts_search_result* result, FILE *read_file) { match.line_info = (char *)ts_memory_bucket_reserve(&result->memory, MAX_INPUT_LENGTH); memset(match.line_info, 0, MAX_INPUT_LENGTH); } + else { + // Invalid data. skip. + } } - return true; + return IMPORT_NONE; } -static bool _ts_import_csv(ts_search_result* result, const utf8_int8_t* path) { +static import_result _ts_import_csv(ts_search_result* result, const utf8_int8_t* path) { FILE *read_file; fopen_s(&read_file, path, "rb"); - if (read_file == NULL) return false; + if (read_file == NULL) return IMPORT_FILE_ERROR; int version = -1; - bool res = false; + import_result res = IMPORT_NONE; if (fscanf(read_file, "VERSION,%d\n", &version) != 1) goto done; switch(version) { case 1: res = _ts_import_csv_v1(result, read_file); break; - default: break; + default: res = IMPORT_INVALID_VERSION; break; } done: @@ -107,18 +111,37 @@ static bool _ts_import_csv(ts_search_result* result, const utf8_int8_t* path) { return res; } +struct t_import_thread_args { + ts_search_result* result; + const utf8_int8_t* path; +}; + +static void* _ts_import_thread(void* args) { + struct t_import_thread_args* arg = (struct t_import_thread_args*)args; + + if (ts_str_has_extension(arg->path, ".csv")) { + last_import_result = _ts_import_csv(arg->result, arg->path); + } + + arg->result->done_finding_files = true; + arg->result->search_completed = true; + free(arg); + + return 0; +} + ts_search_result* ts_import_result(const utf8_int8_t* path) { ts_search_result* res = ts_create_empty_search_result(); res->done_finding_files = false; res->search_completed = false; res->cancel_search = false; - if (ts_str_has_extension(path, ".csv")) { - _ts_import_csv(res, path); - } + struct t_import_thread_args* args = (struct t_import_thread_args*)malloc(sizeof(struct t_import_thread_args)); + if (!args) exit_oom(); + args->result = res; + args->path = path; - res->done_finding_files = true; - res->search_completed = true; + ts_thread_start(_ts_import_thread, args); return res; } \ No newline at end of file diff --git a/src/import.h b/src/import.h index 979cf84..a3fedf9 100644 --- a/src/import.h +++ b/src/import.h @@ -2,4 +2,13 @@ #include "search.h" +typedef enum t_import_result { + IMPORT_NONE, + IMPORT_INVALID_DATA, + IMPORT_INVALID_VERSION, + IMPORT_FILE_ERROR, +} import_result; + +extern import_result last_import_result; + ts_search_result* ts_import_result(const utf8_int8_t* path); \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index ba087a5..c06fa2b 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -17,7 +17,6 @@ // Popups bool open_settings_window = false; bool open_about_window = false; -export_result last_export_result = EXPORT_NONE; const char* help_text = "1. Search directory\n" @@ -211,6 +210,37 @@ static int _ts_create_menu(int window_w, int window_h) { ifd::FileDialog::Instance().Close(); } + + if (last_import_result != EXPORT_NONE) { + ImGui::OpenPopup("Import Failed"); + ImGuiIO& io = ImGui::GetIO(); + ImGui::SetNextWindowPos(ImVec2(io.DisplaySize.x * 0.5f, io.DisplaySize.y * 0.5f), ImGuiCond_Always, ImVec2(0.5f,0.5f)); + } + + // import error popup + if (ImGui::BeginPopupModal("Import Failed", (bool*)&last_import_result, ImGuiWindowFlags_NoResize|ImGuiWindowFlags_NoMove)) { + ImGui::SetWindowSize({300, 0}); + + switch (last_import_result) + { + case IMPORT_INVALID_DATA: ImGui::Text("File has invalid format"); break; + case IMPORT_INVALID_VERSION: ImGui::Text("File has unknown version"); break; + case IMPORT_FILE_ERROR: ImGui::Text("Failed to open file"); break; + default: + break; + } + + ImGui::Dummy({0, 20}); + ImGui::PushStyleVar(ImGuiStyleVar_FrameBorderSize, 1.0f); + if (ImGui::Button("Close")) { + last_import_result = IMPORT_NONE; + ImGui::CloseCurrentPopup(); + } + ImGui::PopStyleVar(); + + ImGui::EndPopup(); + } + if (last_export_result != EXPORT_NONE) { ImGui::OpenPopup("Export Failed"); ImGuiIO& io = ImGui::GetIO(); -- cgit v1.2.3-70-g09d2