Update path_windows.odin

Updated `long`, `short`, `full`, `current` to be cleaner, use `win32` for platform procs, and `strings.trim_null` to clean resulting strings
This commit is contained in:
Brendan Punsky
2019-03-13 16:40:26 -04:00
committed by GitHub
parent da26e14959
commit 775b544326
+24 -40
View File
@@ -11,78 +11,62 @@ SEPARATOR_STRING :: "\\";
long :: proc(path: string, allocator := context.temp_allocator) -> string { long :: proc(path: string, allocator := context.temp_allocator) -> string {
foreign kernel32 {
GetLongPathNameW :: proc "std" (short, long: win32.Wstring, len: u32) -> u32 ---;
}
c_path := win32.utf8_to_wstring(path, context.temp_allocator); c_path := win32.utf8_to_wstring(path, context.temp_allocator);
length := GetLongPathNameW(c_path, nil, 0); length := GetLongPathNameW(c_path, nil, 0);
if length > 0 { if length == 0 do return "";
buf := make([]u16, length-1, context.temp_allocator);
GetLongPathNameW(c_path, win32.Wstring(&buf[0]), length); buf := make([]u16, length, context.temp_allocator);
return win32.ucs2_to_utf8(buf[:length-1], allocator); GetLongPathNameW(c_path, win32.Wstring(&buf[0]), length);
}
return ""; res := win32.ucs2_to_utf8(buf[:length], allocator);
return strings.trim_null(res);
} }
short :: proc(path: string, allocator := context.temp_allocator) -> string { short :: proc(path: string, allocator := context.temp_allocator) -> string {
foreign kernel32 {
GetShortPathNameW :: proc "std" (long, short: win32.Wstring, len: u32) -> u32 ---;
}
c_path := win32.utf8_to_wstring(path, context.temp_allocator); c_path := win32.utf8_to_wstring(path, context.temp_allocator);
length := GetShortPathNameW(c_path, nil, 0); length := GetShortPathNameW(c_path, nil, 0);
if length > 0 { if length == 0 do return "";
buf := make([]u16, length-1, context.temp_allocator);
buf := make([]u16, length, context.temp_allocator);
GetShortPathNameW(c_path, win32.Wstring(&buf[0]), length); GetShortPathNameW(c_path, win32.Wstring(&buf[0]), length);
return win32.ucs2_to_utf8(buf[:length-1], allocator); res := win32.ucs2_to_utf8(buf[:length], allocator);
}
return ""; return strings.trim_null(res);
} }
full :: proc(path: string, allocator := context.temp_allocator) -> string { full :: proc(path: string, allocator := context.temp_allocator) -> string {
foreign kernel32 {
GetFullPathNameW :: proc "std" (filename: win32.Wstring, buffer_length: u32, buffer: win32.Wstring, file_part: ^win32.Wstring) -> u32 ---;
}
c_path := win32.utf8_to_wstring(path, context.temp_allocator); c_path := win32.utf8_to_wstring(path, context.temp_allocator);
length := GetFullPathNameW(c_path, 0, nil, nil); length := GetFullPathNameW(c_path, 0, nil, nil);
if length > 0 { if length == 0 do return "";
buf := make([]u16, length, context.temp_allocator);
GetFullPathNameW(c_path, length, win32.Wstring(&buf[0]), nil); buf := make([]u16, length, context.temp_allocator);
return win32.ucs2_to_utf8(buf[:len(buf)-1], allocator); GetFullPathNameW(c_path, length, win32.Wstring(&buf[0]), nil);
}
return ""; res := win32.ucs2_to_utf8(buf[:length], allocator);
return strings.trim_null(res);
} }
current :: proc(allocator := context.temp_allocator) -> string { current :: proc(allocator := context.temp_allocator) -> string {
foreign kernel32 {
GetCurrentDirectoryW :: proc "std" (buffer_length: u32, buffer: win32.Wstring) -> u32 ---;
}
length := GetCurrentDirectoryW(0, nil); length := GetCurrentDirectoryW(0, nil);
if length > 0 { if length == 0 do return "";
buf := make([]u16, length, context.temp_allocator);
GetCurrentDirectoryW(length, win32.Wstring(&buf[0])); buf := make([]u16, length, context.temp_allocator);
return win32.ucs2_to_utf8(buf[:length-1], allocator); GetCurrentDirectoryW(length, win32.Wstring(&buf[0]));
}
return ""; res := win32.ucs2_to_utf8(buf[:length], allocator);
return strings.trim_null(res);
} }