mirror of
https://github.com/Ed94/Odin.git
synced 2026-08-06 23:58:50 +00:00
Fix null termination detection, win32 namespacing
This commit is contained in:
+22
-13
@@ -1,7 +1,5 @@
|
|||||||
package path
|
package path
|
||||||
|
|
||||||
foreign import "system:kernel32.lib"
|
|
||||||
|
|
||||||
import "core:strings"
|
import "core:strings"
|
||||||
import "core:sys/win32"
|
import "core:sys/win32"
|
||||||
|
|
||||||
@@ -10,59 +8,70 @@ SEPARATOR :: '\\';
|
|||||||
SEPARATOR_STRING :: "\\";
|
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 {
|
long :: proc(path: string, allocator := context.temp_allocator) -> string {
|
||||||
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 := win32.get_long_path_name_w(c_path, nil, 0);
|
||||||
|
|
||||||
if length == 0 do return "";
|
if length == 0 do return "";
|
||||||
|
|
||||||
buf := make([]u16, length, context.temp_allocator);
|
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);
|
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 {
|
short :: proc(path: string, allocator := context.temp_allocator) -> string {
|
||||||
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 := win32.get_short_path_name_w(c_path, nil, 0);
|
||||||
|
|
||||||
if length == 0 do return "";
|
if length == 0 do return "";
|
||||||
|
|
||||||
buf := make([]u16, length, context.temp_allocator);
|
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);
|
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 {
|
full :: proc(path: string, allocator := context.temp_allocator) -> string {
|
||||||
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 := win32.get_full_path_name_w(c_path, 0, nil, nil);
|
||||||
|
|
||||||
if length == 0 do return "";
|
if length == 0 do return "";
|
||||||
|
|
||||||
buf := make([]u16, length, context.temp_allocator);
|
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);
|
res := win32.ucs2_to_utf8(buf[:length], allocator);
|
||||||
|
|
||||||
return strings.trim_null(res);
|
return null_term(res);
|
||||||
}
|
}
|
||||||
|
|
||||||
current :: proc(allocator := context.temp_allocator) -> string {
|
current :: proc(allocator := context.temp_allocator) -> string {
|
||||||
length := GetCurrentDirectoryW(0, nil);
|
length := win32.get_current_directory_w(0, nil);
|
||||||
|
|
||||||
if length == 0 do return "";
|
if length == 0 do return "";
|
||||||
|
|
||||||
buf := make([]u16, length, context.temp_allocator);
|
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);
|
res := win32.ucs2_to_utf8(buf[:length], allocator);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user