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 ""; +}