Fix null termination detection, win32 namespacing

This commit is contained in:
Brendan Punsky
2019-03-18 17:11:17 -04:00
committed by GitHub
parent 299c299dff
commit 7426f3b092
+22 -13
View File
@@ -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);