From 9fd76d7fcef56815f8df77144dd2cf5f27b685b6 Mon Sep 17 00:00:00 2001 From: San Jacobs Date: Fri, 6 Jun 2025 21:43:45 +0200 Subject: Fall back to os2 if walk_directory() fails with Not_Dir error --- main.odin | 68 ++++++++++++++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 57 insertions(+), 11 deletions(-) (limited to 'main.odin') diff --git a/main.odin b/main.odin index b60fddd..c7a4e40 100755 --- a/main.odin +++ b/main.odin @@ -2,6 +2,7 @@ package main import "core:fmt" import "core:os" +import "core:os/os2" import "core:path/filepath" import "core:sys/windows" import "core:strings" @@ -69,13 +70,17 @@ main :: proc() { file_list : [dynamic]string if(path_info.is_dir) { - fmt.println("Directory submitted! Walking directory...\n") + fmt.printf("Directory submitted! Walking directory...\n\n") fmt.printf("📁 {}\n", path_info.name) - walk_directory(path_info.fullpath, &file_count, &file_list, 1) + try_os2 := walk_directory(path_info.fullpath, &file_count, &file_list, 1) + if len(file_list) == 0 && try_os2 { + fmt.printf("\nNot_Dir error encountered. Trying os2 version...\n\n") + fmt.printf("📁 {}\n", path_info.name) + walk_directory_os2(path_info.fullpath, &file_count, &file_list, 1) + } } else { fmt.println("File submitted! Processing file...") append(&file_list, strings.clone(path_info.fullpath)) - file_count = 2 // Yes, I know } for file, f in file_list { @@ -87,16 +92,16 @@ main :: proc() { fmt.printf("Parse failed: {}\n", file_info.fullpath) continue } - output_name := fmt.aprintf("{}/{}_Lydrapport.html", filepath.dir(file_info.fullpath), parsed.title, allocator=context.temp_allocator) + output_name := fmt.aprintf("{}/{}_Knekt_rapport.html", filepath.dir(file_info.fullpath), parsed.title, allocator=context.temp_allocator) render(parsed, output_name) free_all(context.temp_allocator) files_done += 1 } + fmt.printf("\nCompleted {}/{} files.\n\n", files_done, len(file_list)) } else { fmt.printf("ERROR could not get path info for: {}\n", input_file_name) } - fmt.printf("\nCompleted {}/{} files.\n\n", files_done, file_count-1) } @@ -526,20 +531,21 @@ indent_by :: proc(i : int) { } } -walk_directory :: proc(path : string, file_number : ^int, file_list : ^[dynamic]string, depth : int = 0) { +walk_directory :: proc(path : string, file_number : ^int, file_list : ^[dynamic]string, depth : int = 0) -> bool { handle, ok := os.open(path) if ok != os.ERROR_NONE { indent_by(depth) fmt.printf("ERROR opening dir: %s\n", path) - return + return false } defer os.close(handle) files, okr := os.read_dir(handle, -1) if okr != os.ERROR_NONE { indent_by(depth) - fmt.printf("ERROR reading dir: %s\n", path) - return + fmt.printf("ERROR [{}] reading dir: %s\n", okr, path) + if okr == os.ERROR_FILE_IS_NOT_DIR do return true + return true } defer delete(files) @@ -547,12 +553,52 @@ walk_directory :: proc(path : string, file_number : ^int, file_list : ^[dynamic] for file in files { full_path := file.fullpath - defer delete(full_path) if file.is_dir { indent_by(depth) fmt.printf("📁 %s\n", file.name) - walk_directory(full_path, file_number, file_list, depth+1) // Recurse + return walk_directory(full_path, file_number, file_list, depth+1) // Recurse + + } else { // If file is actually a file + + extension := strings.to_lower(filepath.ext(file.name)) + defer delete(extension) + if(extension == ".csv"){ + indent_by(depth) + fmt.printf("📄 %d %s\n", file_number^, file.name) + append(file_list, strings.clone(file.fullpath)) + file_number^ += 1 + } + } + } + return false +} + +walk_directory_os2 :: proc(path : string, file_number : ^int, file_list : ^[dynamic]string, depth : int = 0) { + handle, ok := os2.open(path) + if ok != os2.ERROR_NONE { + indent_by(depth) + fmt.printf("ERROR opening dir: %s\n", path) + return + } + defer os2.close(handle) + + files, okr := os2.read_dir(handle, -1, context.temp_allocator) + if okr != os2.ERROR_NONE { + indent_by(depth) + fmt.printf("ERROR [{}] reading dir: %s\n", okr, path) + return + } + + + for file in files { + + full_path := file.fullpath + + if os.is_dir(full_path) { + indent_by(depth) + fmt.printf("📁 %s\n", file.name) + walk_directory_os2(full_path, file_number, file_list, depth+1) // Recurse } else { // If file is actually a file -- cgit v1.2.1