From 07773514820777867fc661064d55fb830f20f7ed Mon Sep 17 00:00:00 2001 From: Brendan Punsky Date: Tue, 12 Feb 2019 17:27:54 -0500 Subject: [PATCH 01/10] Added path lib "core:path", as well as single- and multiple-splitset string split proc variants to "core:string" --- core/path/path.odin | 200 ++++++++++++++++++++++++++++++++++++ core/path/path_linux.odin | 7 ++ core/path/path_windows.odin | 121 ++++++++++++++++++++++ 3 files changed, 328 insertions(+) create mode 100644 core/path/path.odin create mode 100644 core/path/path_linux.odin create mode 100644 core/path/path_windows.odin diff --git a/core/path/path.odin b/core/path/path.odin new file mode 100644 index 000000000..7824e849e --- /dev/null +++ b/core/path/path.odin @@ -0,0 +1,200 @@ +package path + +import "core:strings" +import "core:unicode/utf8" + + +// returns everything preceding the last path element +dir :: proc(path: string, new := false, allocator := context.allocator) -> string { + if path == "" do return ""; + + for i := len(path) - 1; i >= 0; i -= 1 { + if path[i] == '/' || path[i] == '\\' { + if path[:i] == "" { + // path is root + return new ? strings.new_string(SEPARATOR_STRING, allocator) : SEPARATOR_STRING; + } else { + return new ? strings.new_string(path[:i], allocator) : path[:i]; + } + } + } + + // path doesn't contain any folder structure + return ""; +} + +// returns the final path element +base :: proc(path: string, new := false, allocator := context.allocator) -> string { + if path == "" do return ""; + + end := len(path) - 1; + + for i := end; i >= 0; i -= 1 { + switch path[i] { + case '/', '\\': + if i != end { + return new ? strings.new_string(path[i+1:], allocator) : path[i+1:]; + } else { + end = i; // we don't want trailing slashes + } + } + } + + // path doesn't contain any folder structure, return entire path + return new ? strings.new_string(path, allocator) : path; +} + +// returns the final path element, excluding the file extension if there is one +name :: proc(path: string, new := false, allocator := context.allocator) -> string { + if path == "" do return ""; + + dot := len(path); + end := dot - 1; + + for i := end; i >= 0; i -= 1 { + switch path[i] { + case '.': dot = (dot == end ? i : dot); + case '/', '\\': return new ? strings.new_string(path[i+1:dot], allocator) : path[i+1:dot]; + } + } + + // path doesn't contain any folder structure or file extensions; assumed to be a valid file name + return new ? strings.new_string(path, allocator) : path; +} + +// returns the file extension, if there is one +ext :: proc(path: string, new := false, allocator := context.allocator) -> string { + if path == "" do return ""; + + for i := len(path)-1; i >= 0; i -= 1 { + switch path[i] { + case '/', '\\': return ""; + case '.': return new ? strings.new_string(path[i+1:], allocator) : path[i+1:]; + } + } + + // path does not include a file extension + return ""; +} + + +rel :: proc{rel_between, rel_current}; + +// returns the relative path from one path to another +rel_between :: proc(from, to: string, allocator := context.temp_allocator) -> string { + if from == "" || to == "" do return ""; + + from = full(from, context.temp_allocator); + to = full(to, context.temp_allocator); + + from_is_dir := is_dir(from); + to_is_dir := is_dir(to); + + index, slash := 0, 0; + + for { + if index >= len(from) { + if index >= len(to) || (from_is_dir && index < len(to) && (to[index] == '/' || to[index] == '\\')) { + slash = index; + } + + break; + } + else if index >= len(to) { + if index >= len(from) || (to_is_dir && index < len(from) && (from[index] == '/' || from[index] == '\\')) { + slash = index; + } + + break; + } + + lchar, skip := utf8.decode_rune_in_string(from[index:]); + rchar, _ := utf8.decode_rune_in_string(to[index:]); + + if (lchar == '/' || lchar == '\\') && (rchar == '/' || lchar == '\\') { + slash = index; + } + else if lchar != rchar { + break; + } + + index += skip; + } + + if slash < 1 { + // there is no common path, use the absolute `to` path + return strings.new_string(to, allocator); + } + + from_slashes, to_slashes := 0, 0; + + if slash < len(from) { + from = from[slash+1:]; + + if from_is_dir { + from_slashes += 1; + } + } + else { + from = ""; + } + + if slash < len(to) { + to = to[slash+1:]; + + if to_is_dir { + to_slashes += 1; + } + } + else { + to = ""; + } + + for char in from { + if char == '/' || char == '\\' { + from_slashes += 1; + } + } + + for char in to { + if char == '/' || char == '\\' { + to_slashes += 1; + } + } + + if from_slashes == 0 { + buffer := make([]byte, 2 + len(to), allocator); + + buffer[0] = '.'; + buffer[1] = SEPARATOR; + copy(buffer[2:], ([]byte)(to)); + + return string(buffer); + } + else { + buffer := make([]byte, from_slashes*3 + len(to), allocator); + + for i in 0..from_slashes-1 { + buffer[i*3+0] = '.'; + buffer[i*3+1] = '.'; + buffer[i*3+2] = SEPARATOR; + } + + copy(buffer[from_slashes*3:], ([]byte)(to)); + + return string(buffer); + } + + return ""; +} + +// returns the relative path from the current directory to another path +rel_current :: proc(to: string, allocator := context.temp_allocator) -> string { + return inline rel_between(current(allocator), to); +} + + +// splits the path elements into slices of the original path string +split :: proc(s: string, allocator := context.temp_allocator) -> []string #no_bounds_check { + return inline strings.split(s, []string{"\\", "/"}, true, allocator); +} diff --git a/core/path/path_linux.odin b/core/path/path_linux.odin new file mode 100644 index 000000000..c78b29e57 --- /dev/null +++ b/core/path/path_linux.odin @@ -0,0 +1,7 @@ +package path + + +SEPARATOR :: '/'; +SEPARATOR_STRING :: "/"; + +#compile_assert("Linux is not yet supported"); diff --git a/core/path/path_windows.odin b/core/path/path_windows.odin new file mode 100644 index 000000000..dc3e0ab99 --- /dev/null +++ b/core/path/path_windows.odin @@ -0,0 +1,121 @@ +package path + +foreign import "system:kernel32.lib" + +import "core:strings" +import "core:sys/win32" + + +SEPARATOR :: '\\'; +SEPARATOR_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); + length := GetLongPathNameW(c_path, nil, 0); + + if length > 0 { + buf := make([]u16, length-1, context.temp_allocator); + + GetLongPathNameW(c_path, win32.Wstring(&buf[0]), length); + + return win32.ucs2_to_utf8(buf[:length-1], allocator); + } + + return ""; +} + +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); + length := GetShortPathNameW(c_path, nil, 0); + + if length > 0 { + buf := make([]u16, length-1, context.temp_allocator); + + GetShortPathNameW(c_path, win32.Wstring(&buf[0]), length); + + return win32.ucs2_to_utf8(buf[:length-1], allocator); + } + + return ""; +} + +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); + length := GetFullPathNameW(c_path, 0, nil, nil); + + if length > 0 { + buf := make([]u16, length, context.temp_allocator); + + GetFullPathNameW(c_path, length, win32.Wstring(&buf[0]), nil); + + return win32.ucs2_to_utf8(buf[:len(buf)-1], allocator); + } + + return ""; +} + +current :: proc(allocator := context.temp_allocator) -> string { + foreign kernel32 { + GetCurrentDirectoryW :: proc "std" (buffer_length: u32, buffer: win32.Wstring) -> u32 ---; + } + + length := GetCurrentDirectoryW(0, nil); + + if length > 0 { + buf := make([]u16, length, context.temp_allocator); + + GetCurrentDirectoryW(length, win32.Wstring(&buf[0])); + + return win32.ucs2_to_utf8(buf[:length-1], allocator); + } + + return ""; +} + + +exists :: proc(path: string) -> bool { + c_path := win32.utf8_to_wstring(path, context.temp_allocator); + attribs := win32.get_file_attributes_w(c_path); + + return i32(attribs) != win32.INVALID_FILE_ATTRIBUTES; +} + +is_dir :: proc(path: string) -> bool { + c_path := win32.utf8_to_wstring(path, context.temp_allocator); + attribs := win32.get_file_attributes_w(c_path); + + return (i32(attribs) != win32.INVALID_FILE_ATTRIBUTES) && (attribs & win32.FILE_ATTRIBUTE_DIRECTORY == win32.FILE_ATTRIBUTE_DIRECTORY); +} + +is_file :: proc(path: string) -> bool { + c_path := win32.utf8_to_wstring(path, context.temp_allocator); + attribs := win32.get_file_attributes_w(c_path); + + return (i32(attribs) != win32.INVALID_FILE_ATTRIBUTES) && (attribs & win32.FILE_ATTRIBUTE_DIRECTORY != win32.FILE_ATTRIBUTE_DIRECTORY); +} + + +drive :: proc(path: string, new := false, allocator := context.allocator) -> string { + if len(path) >= 3 { + letter := path[:2]; + + if path[1] == ':' && (path[2] == '\\' || path[2] == '/') { + return new ? strings.new_string(path[:2], allocator) : path[:2]; + } + } + + return ""; +} From 772dc47f553cedc2d1a0f6781cff6ca0f0eb81c9 Mon Sep 17 00:00:00 2001 From: Brendan Punsky Date: Tue, 12 Feb 2019 17:30:46 -0500 Subject: [PATCH 02/10] Oops, I forgot to stage core/strings/strings.odin --- core/strings/strings.odin | 149 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 149 insertions(+) diff --git a/core/strings/strings.odin b/core/strings/strings.odin index c8c8e560c..efc5f2a6d 100644 --- a/core/strings/strings.odin +++ b/core/strings/strings.odin @@ -476,3 +476,152 @@ trim_right_space :: proc(s: string) -> string { trim_space :: proc(s: string) -> string { return trim_right_space(trim_left_space(s)); } + + +// returns a slice of sub-strings into `s` +// `allocator` is used only for the slice +// `skip_empty=true` does not return zero-length substrings +split :: proc{split_single, split_multi}; + +split_single :: proc(s, substr: string, skip_empty := false, allocator := context.temp_allocator) -> []string #no_bounds_check { + if s == "" || substr == "" do return nil; + + sublen := len(substr); + shared := len(s) - sublen; + + if shared <= 0 { + return nil; + } + + // number, index, last + n, i, l := 0, 0, 0; + + // count results + first_pass: for i <= shared { + if s[i:i+sublen] == substr { + if !skip_empty || i - l > 0 { + n += 1; + } + + i += sublen; + l = i; + } else { + _, skip := utf8.decode_rune_in_string(s[i:]); + i += skip; + } + } + + if !skip_empty || len(s) - l > 0 { + n += 1; + } + + if n < 1 { + // no results + return nil; + } + + buf := make([]string, n, allocator); + + n, i, l = 0, 0, 0; + + // slice results + second_pass: for i <= shared { + if s[i:i+sublen] == substr { + if !skip_empty || i - l > 0 { + buf[n] = s[l:i]; + n += 1; + } + + i += sublen; + l = i; + } else { + _, skip := utf8.decode_rune_in_string(s[i:]); + i += skip; + } + } + + if !skip_empty || len(s) - l > 0 { + buf[n] = s[l:]; + } + + return buf; +} + +split_multi :: proc(s: string, substrs: []string, skip_empty := false, allocator := context.temp_allocator) -> []string #no_bounds_check { + if s == "" || len(substrs) <= 0 { + return nil; + } + + sublen := len(substrs[0]); + + for substr in substrs[1:] { + sublen = min(sublen, len(substr)); + } + + shared := len(s) - sublen; + + if shared <= 0 { + return nil; + } + + // number, index, last + n, i, l := 0, 0, 0; + + // count results + first_pass: for i <= shared { + for substr in substrs { + if s[i:i+sublen] == substr { + if !skip_empty || i - l > 0 { + n += 1; + } + + i += sublen; + l = i; + + continue first_pass; + } + } + + _, skip := utf8.decode_rune_in_string(s[i:]); + i += skip; + } + + if !skip_empty || len(s) - l > 0 { + n += 1; + } + + if n < 1 { + // no results + return nil; + } + + buf := make([]string, n, allocator); + + n, i, l = 0, 0, 0; + + // slice results + second_pass: for i <= shared { + for substr in substrs { + if s[i:i+sublen] == substr { + if !skip_empty || i - l > 0 { + buf[n] = s[l:i]; + n += 1; + } + + i += sublen; + l = i; + + continue second_pass; + } + } + + _, skip := utf8.decode_rune_in_string(s[i:]); + i += skip; + } + + if !skip_empty || len(s) - l > 0 { + buf[n] = s[l:]; + } + + return buf; +} From 222e5c8ae9cb9e41f532cf7a7fdf130b2861eec4 Mon Sep 17 00:00:00 2001 From: Brendan Punsky Date: Thu, 14 Feb 2019 20:25:13 -0500 Subject: [PATCH 03/10] Delete path_linux.odin Delete useless path_linux.odin stub --- core/path/path_linux.odin | 7 ------- 1 file changed, 7 deletions(-) delete mode 100644 core/path/path_linux.odin diff --git a/core/path/path_linux.odin b/core/path/path_linux.odin deleted file mode 100644 index c78b29e57..000000000 --- a/core/path/path_linux.odin +++ /dev/null @@ -1,7 +0,0 @@ -package path - - -SEPARATOR :: '/'; -SEPARATOR_STRING :: "/"; - -#compile_assert("Linux is not yet supported"); From da26e14959a96ba3ee26dbb6925390b503190d32 Mon Sep 17 00:00:00 2001 From: Brendan Punsky Date: Wed, 13 Mar 2019 16:38:02 -0400 Subject: [PATCH 04/10] Update path.odin Fix allocator usage for `rel_current` and `rel_between` --- core/path/path.odin | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/core/path/path.odin b/core/path/path.odin index 7824e849e..fb26eb229 100644 --- a/core/path/path.odin +++ b/core/path/path.odin @@ -81,7 +81,7 @@ ext :: proc(path: string, new := false, allocator := context.allocator) -> strin rel :: proc{rel_between, rel_current}; // returns the relative path from one path to another -rel_between :: proc(from, to: string, allocator := context.temp_allocator) -> string { +rel_between :: proc(from, to: string, allocator := context.allocator) -> string { if from == "" || to == "" do return ""; from = full(from, context.temp_allocator); @@ -190,7 +190,7 @@ rel_between :: proc(from, to: string, allocator := context.temp_allocator) -> st // returns the relative path from the current directory to another path rel_current :: proc(to: string, allocator := context.temp_allocator) -> string { - return inline rel_between(current(allocator), to); + return inline rel_between(current(context.temp_allocator), to, allocator); } From 775b544326de3e95e0d785af5838d96b25e48bf6 Mon Sep 17 00:00:00 2001 From: Brendan Punsky Date: Wed, 13 Mar 2019 16:40:26 -0400 Subject: [PATCH 05/10] 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 --- core/path/path_windows.odin | 64 ++++++++++++++----------------------- 1 file changed, 24 insertions(+), 40 deletions(-) diff --git a/core/path/path_windows.odin b/core/path/path_windows.odin index dc3e0ab99..7fcc0c28e 100644 --- a/core/path/path_windows.odin +++ b/core/path/path_windows.odin @@ -11,78 +11,62 @@ SEPARATOR_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); length := GetLongPathNameW(c_path, nil, 0); - if length > 0 { - buf := make([]u16, length-1, context.temp_allocator); + if length == 0 do return ""; - 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 { - foreign kernel32 { - GetShortPathNameW :: proc "std" (long, short: win32.Wstring, len: u32) -> u32 ---; - } - c_path := win32.utf8_to_wstring(path, context.temp_allocator); length := GetShortPathNameW(c_path, nil, 0); - if length > 0 { - buf := make([]u16, length-1, context.temp_allocator); + if length == 0 do return ""; + + 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 { - 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); length := GetFullPathNameW(c_path, 0, nil, nil); - if length > 0 { - buf := make([]u16, length, context.temp_allocator); + if length == 0 do return ""; - 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 { - foreign kernel32 { - GetCurrentDirectoryW :: proc "std" (buffer_length: u32, buffer: win32.Wstring) -> u32 ---; - } - length := GetCurrentDirectoryW(0, nil); - if length > 0 { - buf := make([]u16, length, context.temp_allocator); + if length == 0 do return ""; - 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); } From 9d7e1c17cc4a9b0d6cfd4c741c800b5732eb9948 Mon Sep 17 00:00:00 2001 From: Brendan Punsky Date: Wed, 13 Mar 2019 16:44:11 -0400 Subject: [PATCH 06/10] Update kernel32.odin Add Win32 path functions --- core/sys/win32/kernel32.odin | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/core/sys/win32/kernel32.odin b/core/sys/win32/kernel32.odin index a41fe32de..56a150a5c 100644 --- a/core/sys/win32/kernel32.odin +++ b/core/sys/win32/kernel32.odin @@ -167,4 +167,13 @@ foreign kernel32 { @(link_name="FreeLibrary") free_library :: proc(h: Hmodule) ---; @(link_name="GetProcAddress") get_proc_address :: proc(h: Hmodule, c_str: cstring) -> rawptr ---; + @(link_name="GetFullPathNameA") get_full_path_name_a :: proc(filename: cstring, buffer_length: u32, buffer: cstring, file_part: ^Wstring) -> u32 ---; + @(link_name="GetFullPathNameW") get_full_path_name_w :: proc(filename: Wstring, buffer_length: u32, buffer: Wstring, file_part: ^Wstring) -> u32 ---; + @(link_name="GetLongPathNameA") get_long_path_name_a :: proc(short, long: cstring, len: u32) -> u32 ---; + @(link_name="GetLongPathNameW") get_long_path_name_w :: proc(short, long: Wstring, len: u32) -> u32 ---; + @(link_name="GetShortPathNameA") get_short_path_name_a :: proc(long, short: cstring, len: u32) -> u32 ---; + @(link_name="GetShortPathNameW") get_short_path_name_w :: proc(long, short: Wstring, len: u32) -> u32 ---; + + @(link_name="GetCurrentDirectorya") get_current_directory_a :: proc(buffer_length: u32, buffer: cstring) -> u32 ---; + @(link_name="GetCurrentDirectoryW") get_current_directory_w :: proc(buffer_length: u32, buffer: Wstring) -> u32 ---; } From e398c074dbbfe716ec2735e6e2e948bea9395fad Mon Sep 17 00:00:00 2001 From: Brendan Punsky Date: Wed, 13 Mar 2019 18:37:01 -0400 Subject: [PATCH 07/10] Fix typo --- core/sys/win32/kernel32.odin | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/sys/win32/kernel32.odin b/core/sys/win32/kernel32.odin index 6e9c752fb..26fe8d296 100644 --- a/core/sys/win32/kernel32.odin +++ b/core/sys/win32/kernel32.odin @@ -174,7 +174,7 @@ foreign kernel32 { @(link_name="GetShortPathNameA") get_short_path_name_a :: proc(long, short: cstring, len: u32) -> u32 ---; @(link_name="GetShortPathNameW") get_short_path_name_w :: proc(long, short: Wstring, len: u32) -> u32 ---; - @(link_name="GetCurrentDirectorya") get_current_directory_a :: proc(buffer_length: u32, buffer: cstring) -> u32 ---; + @(link_name="GetCurrentDirectoryA") get_current_directory_a :: proc(buffer_length: u32, buffer: cstring) -> u32 ---; @(link_name="GetCurrentDirectoryW") get_current_directory_w :: proc(buffer_length: u32, buffer: Wstring) -> u32 ---; } From 299c299dffb0fda13544083f1991ad9a1d89c1c0 Mon Sep 17 00:00:00 2001 From: Brendan Punsky Date: Mon, 18 Mar 2019 17:07:36 -0400 Subject: [PATCH 08/10] Add linux support --- core/path/path_linux.odin | 80 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 core/path/path_linux.odin diff --git a/core/path/path_linux.odin b/core/path/path_linux.odin new file mode 100644 index 000000000..fab3bffbc --- /dev/null +++ b/core/path/path_linux.odin @@ -0,0 +1,80 @@ +package path + +foreign import libc "system:c" + +import "core:os" +import "core:strings" + + +MAX :: 4096; // @note(bp): apparently PATH_MAX is bullshit + +SEPARATOR :: '/'; +SEPARATOR_STRING :: "/"; + + +@(private) +null_term :: proc(str: string) -> string { + for c, i in str { + if c == '\x00' { + return str[:i]; + } + } + return str; +} + + +full :: proc(path: string, allocator := context.allocator) -> string { + cpath := strings.clone_to_cstring(path, context.temp_allocator); + + foreign libc { + realpath :: proc(path: cstring, resolved_path: ^u8) -> cstring ---; + } + + buf := make([dynamic]u8, MAX, MAX, allocator); + + cstr := realpath(cpath, &buf[0]); + for cstr == nil && os.get_last_error() == int(os.ENAMETOOLONG) { + resize(&buf, len(buf) + MAX); + cstr = realpath(cpath, &buf[0]); + } + + return null_term(string(buf[:])); +} + +current :: proc(allocator := context.allocator) -> string { + foreign libc{ + getcwd :: proc(buf: ^u8, size: int) -> cstring ---; + } + + buf := make([dynamic]u8, MAX, MAX, allocator); + + cstr := getcwd(&buf[0], len(buf)); + for cstr == nil && os.get_last_error() == int(os.ENAMETOOLONG) { + resize(&buf, len(buf) + MAX); + cstr = getcwd(&buf[0], len(buf)); + } + + return null_term(string(buf[:])); +} + + +exists :: proc(path: string) -> bool { + if _, err := os.stat(path); err != os.ERROR_NONE { + return true; + } + return false; +} + +is_dir :: proc(path: string) -> bool { + if stat, err := os.stat(path); err == os.ERROR_NONE { + return os.S_ISDIR(stat.mode); + } + return false; +} + +is_file :: proc(path: string) -> bool { + if stat, err := os.stat(path); err == os.ERROR_NONE { + return os.S_ISREG(stat.mode); + } + return false; +} From 7426f3b09238118239009bb35aa968db868687fe Mon Sep 17 00:00:00 2001 From: Brendan Punsky Date: Mon, 18 Mar 2019 17:11:17 -0400 Subject: [PATCH 09/10] 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); From b6ea7b741831f7cceaf43eaf116995d6ff386fb5 Mon Sep 17 00:00:00 2001 From: Brendan Punsky Date: Mon, 18 Mar 2019 17:19:21 -0400 Subject: [PATCH 10/10] Fix temp allocation on linux --- core/path/path_linux.odin | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/core/path/path_linux.odin b/core/path/path_linux.odin index fab3bffbc..83783f857 100644 --- a/core/path/path_linux.odin +++ b/core/path/path_linux.odin @@ -23,7 +23,7 @@ null_term :: proc(str: string) -> string { } -full :: proc(path: string, allocator := context.allocator) -> string { +full :: proc(path: string, allocator := context.temp_allocator) -> string { cpath := strings.clone_to_cstring(path, context.temp_allocator); foreign libc { @@ -41,7 +41,7 @@ full :: proc(path: string, allocator := context.allocator) -> string { return null_term(string(buf[:])); } -current :: proc(allocator := context.allocator) -> string { +current :: proc(allocator := context.temp_allocator) -> string { foreign libc{ getcwd :: proc(buf: ^u8, size: int) -> cstring ---; }