Fix new package path

This commit is contained in:
gingerBill
2020-07-10 08:42:22 +01:00
parent f65fa0e4a6
commit 642afa4f88
5 changed files with 250 additions and 391 deletions
+132 -131
View File
@@ -6,75 +6,75 @@ import "core:unicode/utf8"
// returns everything preceding the last path element // returns everything preceding the last path element
dir :: proc(path: string, new := false, allocator := context.allocator) -> string { dir :: proc(path: string, new := false, allocator := context.allocator) -> string {
if path == "" do return ""; if path == "" do return "";
for i := len(path) - 1; i >= 0; i -= 1 { for i := len(path) - 1; i >= 0; i -= 1 {
if path[i] == '/' || path[i] == '\\' { if path[i] == '/' || path[i] == '\\' {
if path[:i] == "" { if path[:i] == "" {
// path is root // path is root
return new ? strings.new_string(SEPARATOR_STRING, allocator) : SEPARATOR_STRING; return new ? strings.clone(SEPARATOR_STRING, allocator) : SEPARATOR_STRING;
} else { } else {
return new ? strings.new_string(path[:i], allocator) : path[:i]; return new ? strings.clone(path[:i], allocator) : path[:i];
} }
} }
} }
// path doesn't contain any folder structure // path doesn't contain any folder structure
return ""; return "";
} }
// returns the final path element // returns the final path element
base :: proc(path: string, new := false, allocator := context.allocator) -> string { base :: proc(path: string, new := false, allocator := context.allocator) -> string {
if path == "" do return ""; if path == "" do return "";
end := len(path) - 1; end := len(path) - 1;
for i := end; i >= 0; i -= 1 { for i := end; i >= 0; i -= 1 {
switch path[i] { switch path[i] {
case '/', '\\': case '/', '\\':
if i != end { if i != end {
return new ? strings.new_string(path[i+1:], allocator) : path[i+1:]; return new ? strings.clone(path[i+1:], allocator) : path[i+1:];
} else { } else {
end = i; // we don't want trailing slashes end = i; // we don't want trailing slashes
} }
} }
} }
// path doesn't contain any folder structure, return entire path // path doesn't contain any folder structure, return entire path
return new ? strings.new_string(path, allocator) : path; return new ? strings.clone(path, allocator) : path;
} }
// returns the final path element, excluding the file extension if there is one // returns the final path element, excluding the file extension if there is one
name :: proc(path: string, new := false, allocator := context.allocator) -> string { name :: proc(path: string, new := false, allocator := context.allocator) -> string {
if path == "" do return ""; if path == "" do return "";
dot := len(path); dot := len(path);
end := dot - 1; end := dot - 1;
for i := end; i >= 0; i -= 1 { for i := end; i >= 0; i -= 1 {
switch path[i] { switch path[i] {
case '.': dot = (dot == end ? i : dot); case '.': dot = (dot == end ? i : dot);
case '/', '\\': return new ? strings.new_string(path[i+1:dot], allocator) : path[i+1:dot]; case '/', '\\': return new ? strings.clone(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 // 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; return new ? strings.clone(path, allocator) : path;
} }
// returns the file extension, if there is one // returns the file extension, if there is one
ext :: proc(path: string, new := false, allocator := context.allocator) -> string { ext :: proc(path: string, new := false, allocator := context.allocator) -> string {
if path == "" do return ""; if path == "" do return "";
for i := len(path)-1; i >= 0; i -= 1 { for i := len(path)-1; i >= 0; i -= 1 {
switch path[i] { switch path[i] {
case '/', '\\': return ""; case '/', '\\': return "";
case '.': return new ? strings.new_string(path[i+1:], allocator) : path[i+1:]; case '.': return new ? strings.clone(path[i+1:], allocator) : path[i+1:];
} }
} }
// path does not include a file extension // path does not include a file extension
return ""; return "";
} }
@@ -82,119 +82,120 @@ rel :: proc{rel_between, rel_current};
// returns the relative path from one path to another // returns the relative path from one path to another
rel_between :: proc(from, to: string, allocator := context.allocator) -> string { rel_between :: proc(from, to: string, allocator := context.allocator) -> string {
if from == "" || to == "" do return ""; if from == "" || to == "" do return "";
from = full(from, context.temp_allocator); from, to := from, to;
to = full(to, context.temp_allocator); from = full(from, context.temp_allocator);
to = full(to, context.temp_allocator);
from_is_dir := is_dir(from); from_is_dir := is_dir(from);
to_is_dir := is_dir(to); to_is_dir := is_dir(to);
index, slash := 0, 0; index, slash := 0, 0;
for { for {
if index >= len(from) { if index >= len(from) {
if index >= len(to) || (from_is_dir && index < len(to) && (to[index] == '/' || to[index] == '\\')) { if index >= len(to) || (from_is_dir && index < len(to) && (to[index] == '/' || to[index] == '\\')) {
slash = index; slash = index;
} }
break; break;
} }
else if index >= len(to) { else if index >= len(to) {
if index >= len(from) || (to_is_dir && index < len(from) && (from[index] == '/' || from[index] == '\\')) { if index >= len(from) || (to_is_dir && index < len(from) && (from[index] == '/' || from[index] == '\\')) {
slash = index; slash = index;
} }
break; break;
} }
lchar, skip := utf8.decode_rune_in_string(from[index:]); lchar, skip := utf8.decode_rune_in_string(from[index:]);
rchar, _ := utf8.decode_rune_in_string(to[index:]); rchar, _ := utf8.decode_rune_in_string(to[index:]);
if (lchar == '/' || lchar == '\\') && (rchar == '/' || lchar == '\\') { if (lchar == '/' || lchar == '\\') && (rchar == '/' || lchar == '\\') {
slash = index; slash = index;
} }
else if lchar != rchar { else if lchar != rchar {
break; break;
} }
index += skip; index += skip;
} }
if slash < 1 { if slash < 1 {
// there is no common path, use the absolute `to` path // there is no common path, use the absolute `to` path
return strings.new_string(to, allocator); return strings.clone(to, allocator);
} }
from_slashes, to_slashes := 0, 0; from_slashes, to_slashes := 0, 0;
if slash < len(from) { if slash < len(from) {
from = from[slash+1:]; from = from[slash+1:];
if from_is_dir { if from_is_dir {
from_slashes += 1; from_slashes += 1;
} }
} }
else { else {
from = ""; from = "";
} }
if slash < len(to) { if slash < len(to) {
to = to[slash+1:]; to = to[slash+1:];
if to_is_dir { if to_is_dir {
to_slashes += 1; to_slashes += 1;
} }
} }
else { else {
to = ""; to = "";
} }
for char in from { for char in from {
if char == '/' || char == '\\' { if char == '/' || char == '\\' {
from_slashes += 1; from_slashes += 1;
} }
} }
for char in to { for char in to {
if char == '/' || char == '\\' { if char == '/' || char == '\\' {
to_slashes += 1; to_slashes += 1;
} }
} }
if from_slashes == 0 { if from_slashes == 0 {
buffer := make([]byte, 2 + len(to), allocator); buffer := make([]byte, 2 + len(to), allocator);
buffer[0] = '.'; buffer[0] = '.';
buffer[1] = SEPARATOR; buffer[1] = SEPARATOR;
copy(buffer[2:], ([]byte)(to)); copy(buffer[2:], to);
return string(buffer); return string(buffer);
} }
else { else {
buffer := make([]byte, from_slashes*3 + len(to), allocator); buffer := make([]byte, from_slashes*3 + len(to), allocator);
for i in 0..from_slashes-1 { for i in 0..<from_slashes {
buffer[i*3+0] = '.'; buffer[i*3+0] = '.';
buffer[i*3+1] = '.'; buffer[i*3+1] = '.';
buffer[i*3+2] = SEPARATOR; buffer[i*3+2] = SEPARATOR;
} }
copy(buffer[from_slashes*3:], ([]byte)(to)); copy(buffer[from_slashes*3:], to);
return string(buffer); return string(buffer);
} }
return ""; return "";
} }
// returns the relative path from the current directory to another path // returns the relative path from the current directory to another path
rel_current :: proc(to: string, allocator := context.temp_allocator) -> string { rel_current :: proc(to: string, allocator := context.allocator) -> string {
return inline rel_between(current(context.temp_allocator), to, allocator); return rel_between(current(context.allocator), to, allocator);
} }
// splits the path elements into slices of the original path string // splits the path elements into slices of the original path string
split :: proc(s: string, allocator := context.temp_allocator) -> []string #no_bounds_check { split :: proc(s: string, allocator := context.allocator) -> []string {
return inline strings.split(s, []string{"\\", "/"}, true, allocator); return strings.split_multi(s, []string{"\\", "/"}, true, allocator);
} }
-80
View File
@@ -1,80 +0,0 @@
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.temp_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.temp_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;
}
+51 -51
View File
@@ -1,7 +1,7 @@
package path package path
import "core:strings" import "core:strings"
import "core:sys/win32" import win32 "core:sys/windows"
SEPARATOR :: '\\'; SEPARATOR :: '\\';
@@ -9,106 +9,106 @@ SEPARATOR_STRING :: "\\";
@(private) @(private)
null_term :: proc"contextless"(str: string) -> string { null_term :: proc "contextless" (str: string) -> string {
for c, i in str { for c, i in str {
if c == '\x00' { if c == '\x00' {
return str[:i]; return str[:i];
} }
} }
return str; 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 := win32.get_long_path_name_w(c_path, nil, 0); length := win32.GetLongPathNameW(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);
win32.get_long_path_name_w(c_path, win32.Wstring(&buf[0]), length); win32.GetLongPathNameW(c_path, win32.LPCWSTR(&buf[0]), length);
res := win32.ucs2_to_utf8(buf[:length], allocator); res := win32.utf16_to_utf8(buf[:length], allocator);
return null_term(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 := win32.get_short_path_name_w(c_path, nil, 0); length := win32.GetShortPathNameW(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);
win32.get_short_path_name_w(c_path, win32.Wstring(&buf[0]), length); win32.GetShortPathNameW(c_path, win32.LPCWSTR(&buf[0]), length);
res := win32.ucs2_to_utf8(buf[:length], allocator); res := win32.utf16_to_utf8(buf[:length], allocator);
return null_term(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 := win32.get_full_path_name_w(c_path, 0, nil, nil); length := win32.GetFullPathNameW(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);
win32.get_full_path_name_w(c_path, length, win32.Wstring(&buf[0]), nil); win32.GetFullPathNameW(c_path, length, win32.LPCWSTR(&buf[0]), nil);
res := win32.ucs2_to_utf8(buf[:length], allocator); res := win32.utf16_to_utf8(buf[:length], allocator);
return null_term(res); return null_term(res);
} }
current :: proc(allocator := context.temp_allocator) -> string { current :: proc(allocator := context.temp_allocator) -> string {
length := win32.get_current_directory_w(0, nil); length := win32.GetCurrentDirectoryW(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);
win32.get_current_directory_w(length, win32.Wstring(&buf[0])); win32.GetCurrentDirectoryW(length, win32.LPCWSTR(&buf[0]));
res := win32.ucs2_to_utf8(buf[:length], allocator); res := win32.utf16_to_utf8(buf[:length], allocator);
return strings.trim_null(res); return strings.trim_null(res);
} }
exists :: proc(path: string) -> bool { exists :: proc(path: string) -> bool {
c_path := win32.utf8_to_wstring(path, context.temp_allocator); c_path := win32.utf8_to_wstring(path, context.temp_allocator);
attribs := win32.get_file_attributes_w(c_path); attribs := win32.GetFileAttributesW(c_path);
return i32(attribs) != win32.INVALID_FILE_ATTRIBUTES; return i32(attribs) != win32.INVALID_FILE_ATTRIBUTES;
} }
is_dir :: proc(path: string) -> bool { is_dir :: proc(path: string) -> bool {
c_path := win32.utf8_to_wstring(path, context.temp_allocator); c_path := win32.utf8_to_wstring(path, context.temp_allocator);
attribs := win32.get_file_attributes_w(c_path); attribs := win32.GetFileAttributesW(c_path);
return (i32(attribs) != win32.INVALID_FILE_ATTRIBUTES) && (attribs & win32.FILE_ATTRIBUTE_DIRECTORY == win32.FILE_ATTRIBUTE_DIRECTORY); return (i32(attribs) != win32.INVALID_FILE_ATTRIBUTES) && (attribs & win32.FILE_ATTRIBUTE_DIRECTORY == win32.FILE_ATTRIBUTE_DIRECTORY);
} }
is_file :: proc(path: string) -> bool { is_file :: proc(path: string) -> bool {
c_path := win32.utf8_to_wstring(path, context.temp_allocator); c_path := win32.utf8_to_wstring(path, context.temp_allocator);
attribs := win32.get_file_attributes_w(c_path); attribs := win32.GetFileAttributesW(c_path);
return (i32(attribs) != win32.INVALID_FILE_ATTRIBUTES) && (attribs & win32.FILE_ATTRIBUTE_DIRECTORY != win32.FILE_ATTRIBUTE_DIRECTORY); 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 { drive :: proc(path: string, new := false, allocator := context.allocator) -> string {
if len(path) >= 3 { if len(path) >= 3 {
letter := path[:2]; letter := path[:2];
if path[1] == ':' && (path[2] == '\\' || path[2] == '/') { if path[1] == ':' && (path[2] == '\\' || path[2] == '/') {
return new ? strings.new_string(path[:2], allocator) : path[:2]; return new ? strings.clone(path[:2], allocator) : path[:2];
} }
} }
return ""; return "";
} }
+58 -127
View File
@@ -655,152 +655,83 @@ trim_null :: proc(s: string) -> string {
} }
// returns a slice of sub-strings into `s` split_multi :: proc(s: string, substrs: []string, skip_empty := false, allocator := context.allocator) -> []string #no_bounds_check {
// `allocator` is used only for the slice if s == "" || len(substrs) <= 0 {
// `skip_empty=true` does not return zero-length substrings return nil;
split :: proc{split_single, split_multi}; }
split_single :: proc(s, substr: string, skip_empty := false, allocator := context.temp_allocator) -> []string #no_bounds_check { sublen := len(substrs[0]);
if s == "" || substr == "" do return nil;
sublen := len(substr); for substr in substrs[1:] {
shared := len(s) - sublen; sublen = min(sublen, len(substr));
}
if shared <= 0 { shared := len(s) - sublen;
return nil;
}
// number, index, last if shared <= 0 {
n, i, l := 0, 0, 0; return nil;
}
// count results // number, index, last
n, i, l := 0, 0, 0;
// count results
first_pass: for i <= shared { first_pass: for i <= shared {
if s[i:i+sublen] == substr { for substr in substrs {
if !skip_empty || i - l > 0 { if s[i:i+sublen] == substr {
n += 1; if !skip_empty || i - l > 0 {
} n += 1;
}
i += sublen; i += sublen;
l = i; l = i;
} else {
_, skip := utf8.decode_rune_in_string(s[i:]);
i += skip;
}
}
if !skip_empty || len(s) - l > 0 { continue first_pass;
n += 1; }
} }
if n < 1 { _, skip := utf8.decode_rune_in_string(s[i:]);
// no results i += skip;
return nil; }
}
buf := make([]string, n, allocator); if !skip_empty || len(s) - l > 0 {
n += 1;
}
n, i, l = 0, 0, 0; if n < 1 {
// no results
return nil;
}
// slice results buf := make([]string, n, allocator);
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; n, i, l = 0, 0, 0;
l = i;
} else {
_, skip := utf8.decode_rune_in_string(s[i:]);
i += skip;
}
}
if !skip_empty || len(s) - l > 0 { // slice results
buf[n] = s[l:]; 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;
}
return buf; i += sublen;
} l = i;
split_multi :: proc(s: string, substrs: []string, skip_empty := false, allocator := context.temp_allocator) -> []string #no_bounds_check { continue second_pass;
if s == "" || len(substrs) <= 0 { }
return nil; }
}
sublen := len(substrs[0]); _, skip := utf8.decode_rune_in_string(s[i:]);
i += skip;
}
for substr in substrs[1:] { if !skip_empty || len(s) - l > 0 {
sublen = min(sublen, len(substr)); buf[n] = s[l:];
} }
shared := len(s) - sublen; return buf;
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;
} }
// scrub scruvs invalid utf-8 characters and replaces them with the replacement string // scrub scruvs invalid utf-8 characters and replaces them with the replacement string
+7
View File
@@ -259,6 +259,7 @@ foreign kernel32 {
GetFileType :: proc(file_handle: HANDLE) -> DWORD --- GetFileType :: proc(file_handle: HANDLE) -> DWORD ---
SetFilePointer :: proc(file_handle: HANDLE, distance_to_move: LONG, distance_to_move_high: ^LONG, move_method: DWORD) -> DWORD --- SetFilePointer :: proc(file_handle: HANDLE, distance_to_move: LONG, distance_to_move_high: ^LONG, move_method: DWORD) -> DWORD ---
GetFileSizeEx :: proc(file_handle: HANDLE, file_size: ^LARGE_INTEGER) -> BOOL --- GetFileSizeEx :: proc(file_handle: HANDLE, file_size: ^LARGE_INTEGER) -> BOOL ---
GetFileAttributesW :: proc(lpFileName: LPCWSTR) -> DWORD ---
GetFileAttributesExW :: proc(lpFileName: LPCWSTR, fInfoLevelId: GET_FILEEX_INFO_LEVELS, lpFileInformation: LPVOID) -> BOOL --- GetFileAttributesExW :: proc(lpFileName: LPCWSTR, fInfoLevelId: GET_FILEEX_INFO_LEVELS, lpFileInformation: LPVOID) -> BOOL ---
GetSystemInfo :: proc(system_info: ^SYSTEM_INFO) --- GetSystemInfo :: proc(system_info: ^SYSTEM_INFO) ---
GetVersionExW :: proc(osvi: ^OSVERSIONINFOEXW) --- GetVersionExW :: proc(osvi: ^OSVERSIONINFOEXW) ---
@@ -266,4 +267,10 @@ foreign kernel32 {
LoadLibraryW :: proc(c_str: LPCWSTR) -> HMODULE --- LoadLibraryW :: proc(c_str: LPCWSTR) -> HMODULE ---
FreeLibrary :: proc(h: HMODULE) -> BOOL --- FreeLibrary :: proc(h: HMODULE) -> BOOL ---
GetProcAddress :: proc(h: HMODULE, c_str: LPCSTR) -> rawptr --- GetProcAddress :: proc(h: HMODULE, c_str: LPCSTR) -> rawptr ---
GetFullPathNameW :: proc(filename: LPCWSTR, buffer_length: DWORD, buffer: LPCWSTR, file_part: ^LPCWSTR) -> DWORD ---
GetLongPathNameW :: proc(short, long: LPCWSTR, len: DWORD) -> DWORD ---
GetShortPathNameW :: proc(long, short: LPCWSTR, len: DWORD) -> DWORD ---
} }