From 7426f3b09238118239009bb35aa968db868687fe Mon Sep 17 00:00:00 2001 From: Brendan Punsky Date: Mon, 18 Mar 2019 17:11:17 -0400 Subject: [PATCH] Fix null termination detection, win32 namespacing --- core/path/path_windows.odin | 35 ++++++++++++++++++++++------------- 1 file changed, 22 insertions(+), 13 deletions(-) diff --git a/core/path/path_windows.odin b/core/path/path_windows.odin index 7fcc0c28e..460e0bf27 100644 --- a/core/path/path_windows.odin +++ b/core/path/path_windows.odin @@ -1,7 +1,5 @@ package path -foreign import "system:kernel32.lib" - import "core:strings" import "core:sys/win32" @@ -10,59 +8,70 @@ SEPARATOR :: '\\'; SEPARATOR_STRING :: "\\"; +@(private) +null_term :: proc"contextless"(str: string) -> string { + for c, i in str { + if c == '\x00' { + return str[:i]; + } + } + return str; +} + + long :: proc(path: string, allocator := context.temp_allocator) -> string { c_path := win32.utf8_to_wstring(path, context.temp_allocator); - length := GetLongPathNameW(c_path, nil, 0); + length := win32.get_long_path_name_w(c_path, nil, 0); if length == 0 do return ""; buf := make([]u16, length, context.temp_allocator); - GetLongPathNameW(c_path, win32.Wstring(&buf[0]), length); + win32.get_long_path_name_w(c_path, win32.Wstring(&buf[0]), length); res := win32.ucs2_to_utf8(buf[:length], allocator); - return strings.trim_null(res); + return null_term(res); } short :: proc(path: string, allocator := context.temp_allocator) -> string { c_path := win32.utf8_to_wstring(path, context.temp_allocator); - length := GetShortPathNameW(c_path, nil, 0); + length := win32.get_short_path_name_w(c_path, nil, 0); if length == 0 do return ""; buf := make([]u16, length, context.temp_allocator); - GetShortPathNameW(c_path, win32.Wstring(&buf[0]), length); + win32.get_short_path_name_w(c_path, win32.Wstring(&buf[0]), length); res := win32.ucs2_to_utf8(buf[:length], allocator); - return strings.trim_null(res); + return null_term(res); } full :: proc(path: string, allocator := context.temp_allocator) -> string { c_path := win32.utf8_to_wstring(path, context.temp_allocator); - length := GetFullPathNameW(c_path, 0, nil, nil); + length := win32.get_full_path_name_w(c_path, 0, nil, nil); if length == 0 do return ""; buf := make([]u16, length, context.temp_allocator); - GetFullPathNameW(c_path, length, win32.Wstring(&buf[0]), nil); + win32.get_full_path_name_w(c_path, length, win32.Wstring(&buf[0]), nil); res := win32.ucs2_to_utf8(buf[:length], allocator); - return strings.trim_null(res); + return null_term(res); } current :: proc(allocator := context.temp_allocator) -> string { - length := GetCurrentDirectoryW(0, nil); + length := win32.get_current_directory_w(0, nil); if length == 0 do return ""; buf := make([]u16, length, context.temp_allocator); - GetCurrentDirectoryW(length, win32.Wstring(&buf[0])); + win32.get_current_directory_w(length, win32.Wstring(&buf[0])); res := win32.ucs2_to_utf8(buf[:length], allocator);