mirror of
https://github.com/Ed94/Odin.git
synced 2026-07-31 20:00:09 +00:00
Remove unneeded semicolons from the core library
This commit is contained in:
+120
-120
@@ -33,181 +33,181 @@ Match_Error :: enum {
|
||||
// NOTE(bill): This is effectively the shell pattern matching system found
|
||||
//
|
||||
match :: proc(pattern, name: string) -> (matched: bool, err: Match_Error) {
|
||||
pattern, name := pattern, name;
|
||||
pattern, name := pattern, name
|
||||
pattern_loop: for len(pattern) > 0 {
|
||||
star: bool;
|
||||
chunk: string;
|
||||
star, chunk, pattern = scan_chunk(pattern);
|
||||
star: bool
|
||||
chunk: string
|
||||
star, chunk, pattern = scan_chunk(pattern)
|
||||
if star && chunk == "" {
|
||||
return !strings.contains(name, SEPARATOR_STRING), .None;
|
||||
return !strings.contains(name, SEPARATOR_STRING), .None
|
||||
}
|
||||
|
||||
t: string;
|
||||
ok: bool;
|
||||
t, ok, err = match_chunk(chunk, name);
|
||||
t: string
|
||||
ok: bool
|
||||
t, ok, err = match_chunk(chunk, name)
|
||||
|
||||
if ok && (len(t) == 0 || len(pattern) > 0) {
|
||||
name = t;
|
||||
continue;
|
||||
name = t
|
||||
continue
|
||||
}
|
||||
if err != .None {
|
||||
return;
|
||||
return
|
||||
}
|
||||
if star {
|
||||
for i := 0; i < len(name) && name[i] != SEPARATOR; i += 1 {
|
||||
t, ok, err = match_chunk(chunk, name[i+1:]);
|
||||
t, ok, err = match_chunk(chunk, name[i+1:])
|
||||
if ok {
|
||||
if len(pattern) == 0 && len(t) > 0 {
|
||||
continue;
|
||||
continue
|
||||
}
|
||||
name = t;
|
||||
continue pattern_loop;
|
||||
name = t
|
||||
continue pattern_loop
|
||||
}
|
||||
if err != .None {
|
||||
return;
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return false, .None;
|
||||
return false, .None
|
||||
}
|
||||
|
||||
return len(name) == 0, .None;
|
||||
return len(name) == 0, .None
|
||||
}
|
||||
|
||||
|
||||
@(private="file")
|
||||
scan_chunk :: proc(pattern: string) -> (star: bool, chunk, rest: string) {
|
||||
pattern := pattern;
|
||||
pattern := pattern
|
||||
for len(pattern) > 0 && pattern[0] == '*' {
|
||||
pattern = pattern[1:];
|
||||
star = true;
|
||||
pattern = pattern[1:]
|
||||
star = true
|
||||
}
|
||||
|
||||
in_range, i := false, 0;
|
||||
in_range, i := false, 0
|
||||
|
||||
scan_loop: for i = 0; i < len(pattern); i += 1 {
|
||||
switch pattern[i] {
|
||||
case '\\':
|
||||
when ODIN_OS != "windows" {
|
||||
if i+1 < len(pattern) {
|
||||
i += 1;
|
||||
i += 1
|
||||
}
|
||||
}
|
||||
case '[':
|
||||
in_range = true;
|
||||
in_range = true
|
||||
case ']':
|
||||
in_range = false;
|
||||
in_range = false
|
||||
case '*':
|
||||
if !in_range {
|
||||
break scan_loop;
|
||||
break scan_loop
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
return star, pattern[:i], pattern[i:];
|
||||
return star, pattern[:i], pattern[i:]
|
||||
}
|
||||
|
||||
@(private="file")
|
||||
match_chunk :: proc(chunk, s: string) -> (rest: string, ok: bool, err: Match_Error) {
|
||||
chunk, s := chunk, s;
|
||||
chunk, s := chunk, s
|
||||
for len(chunk) > 0 {
|
||||
if len(s) == 0 {
|
||||
return;
|
||||
return
|
||||
}
|
||||
switch chunk[0] {
|
||||
case '[':
|
||||
r, w := utf8.decode_rune_in_string(s);
|
||||
s = s[w:];
|
||||
chunk = chunk[1:];
|
||||
is_negated := false;
|
||||
r, w := utf8.decode_rune_in_string(s)
|
||||
s = s[w:]
|
||||
chunk = chunk[1:]
|
||||
is_negated := false
|
||||
if len(chunk) > 0 && chunk[0] == '^' {
|
||||
is_negated = true;
|
||||
chunk = chunk[1:];
|
||||
is_negated = true
|
||||
chunk = chunk[1:]
|
||||
}
|
||||
match := false;
|
||||
range_count := 0;
|
||||
match := false
|
||||
range_count := 0
|
||||
for {
|
||||
if len(chunk) > 0 && chunk[0] == ']' && range_count > 0 {
|
||||
chunk = chunk[1:];
|
||||
break;
|
||||
chunk = chunk[1:]
|
||||
break
|
||||
}
|
||||
lo, hi: rune;
|
||||
lo, hi: rune
|
||||
if lo, chunk, err = get_escape(chunk); err != .None {
|
||||
return;
|
||||
return
|
||||
}
|
||||
hi = lo;
|
||||
hi = lo
|
||||
if chunk[0] == '-' {
|
||||
if hi, chunk, err = get_escape(chunk[1:]); err != .None {
|
||||
return;
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
if lo <= r && r <= hi {
|
||||
match = true;
|
||||
match = true
|
||||
}
|
||||
range_count += 1;
|
||||
range_count += 1
|
||||
}
|
||||
if match == is_negated {
|
||||
return;
|
||||
return
|
||||
}
|
||||
|
||||
case '?':
|
||||
if s[0] == SEPARATOR {
|
||||
return;
|
||||
return
|
||||
}
|
||||
_, w := utf8.decode_rune_in_string(s);
|
||||
s = s[w:];
|
||||
chunk = chunk[1:];
|
||||
_, w := utf8.decode_rune_in_string(s)
|
||||
s = s[w:]
|
||||
chunk = chunk[1:]
|
||||
|
||||
case '\\':
|
||||
when ODIN_OS != "windows" {
|
||||
chunk = chunk[1:];
|
||||
chunk = chunk[1:]
|
||||
if len(chunk) == 0 {
|
||||
err = .Syntax_Error;
|
||||
return;
|
||||
err = .Syntax_Error
|
||||
return
|
||||
}
|
||||
}
|
||||
fallthrough;
|
||||
fallthrough
|
||||
case:
|
||||
if chunk[0] != s[0] {
|
||||
return;
|
||||
return
|
||||
}
|
||||
s = s[1:];
|
||||
chunk = chunk[1:];
|
||||
s = s[1:]
|
||||
chunk = chunk[1:]
|
||||
|
||||
}
|
||||
}
|
||||
return s, true, .None;
|
||||
return s, true, .None
|
||||
}
|
||||
|
||||
@(private="file")
|
||||
get_escape :: proc(chunk: string) -> (r: rune, next_chunk: string, err: Match_Error) {
|
||||
if len(chunk) == 0 || chunk[0] == '-' || chunk[0] == ']' {
|
||||
err = .Syntax_Error;
|
||||
return;
|
||||
err = .Syntax_Error
|
||||
return
|
||||
}
|
||||
chunk := chunk;
|
||||
chunk := chunk
|
||||
if chunk[0] == '\\' && ODIN_OS != "windows" {
|
||||
chunk = chunk[1:];
|
||||
chunk = chunk[1:]
|
||||
if len(chunk) == 0 {
|
||||
err = .Syntax_Error;
|
||||
return;
|
||||
err = .Syntax_Error
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
w: int;
|
||||
r, w = utf8.decode_rune_in_string(chunk);
|
||||
w: int
|
||||
r, w = utf8.decode_rune_in_string(chunk)
|
||||
if r == utf8.RUNE_ERROR && w == 1 {
|
||||
err = .Syntax_Error;
|
||||
err = .Syntax_Error
|
||||
}
|
||||
|
||||
next_chunk = chunk[w:];
|
||||
next_chunk = chunk[w:]
|
||||
if len(next_chunk) == 0 {
|
||||
err = .Syntax_Error;
|
||||
err = .Syntax_Error
|
||||
}
|
||||
|
||||
return;
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
@@ -222,128 +222,128 @@ get_escape :: proc(chunk: string) -> (r: rune, next_chunk: string, err: Match_Er
|
||||
glob :: proc(pattern: string, allocator := context.allocator) -> (matches: []string, err: Match_Error) {
|
||||
if !has_meta(pattern) {
|
||||
// TODO(bill): os.lstat on here to check for error
|
||||
m := make([]string, 1, allocator);
|
||||
m[0] = pattern;
|
||||
return m[:], .None;
|
||||
m := make([]string, 1, allocator)
|
||||
m[0] = pattern
|
||||
return m[:], .None
|
||||
}
|
||||
|
||||
temp_buf: [8]byte;
|
||||
temp_buf: [8]byte
|
||||
|
||||
dir, file := split(pattern);
|
||||
volume_len := 0;
|
||||
dir, file := split(pattern)
|
||||
volume_len := 0
|
||||
when ODIN_OS == "windows" {
|
||||
volume_len, dir = clean_glob_path_windows(dir, temp_buf[:]);
|
||||
volume_len, dir = clean_glob_path_windows(dir, temp_buf[:])
|
||||
} else {
|
||||
dir = clean_glob_path(dir);
|
||||
dir = clean_glob_path(dir)
|
||||
}
|
||||
|
||||
if !has_meta(dir[volume_len:]) {
|
||||
m, e := _glob(dir, file, nil);
|
||||
return m[:], e;
|
||||
m, e := _glob(dir, file, nil)
|
||||
return m[:], e
|
||||
}
|
||||
|
||||
m: []string;
|
||||
m, err = glob(dir);
|
||||
m: []string
|
||||
m, err = glob(dir)
|
||||
if err != .None {
|
||||
return;
|
||||
return
|
||||
}
|
||||
dmatches := make([dynamic]string, 0, 0, allocator);
|
||||
dmatches := make([dynamic]string, 0, 0, allocator)
|
||||
for d in m {
|
||||
dmatches, err = _glob(d, file, &dmatches);
|
||||
dmatches, err = _glob(d, file, &dmatches)
|
||||
if err != .None {
|
||||
break;
|
||||
break
|
||||
}
|
||||
}
|
||||
if len(dmatches) > 0 {
|
||||
matches = dmatches[:];
|
||||
matches = dmatches[:]
|
||||
}
|
||||
return;
|
||||
return
|
||||
}
|
||||
_glob :: proc(dir, pattern: string, matches: ^[dynamic]string) -> (m: [dynamic]string, e: Match_Error) {
|
||||
if matches != nil {
|
||||
m = matches^;
|
||||
m = matches^
|
||||
} else {
|
||||
m = make([dynamic]string, 0, 0, context.allocator);
|
||||
m = make([dynamic]string, 0, 0, context.allocator)
|
||||
}
|
||||
|
||||
|
||||
d, derr := os.open(dir);
|
||||
d, derr := os.open(dir)
|
||||
if derr != 0 {
|
||||
return;
|
||||
return
|
||||
}
|
||||
defer os.close(d);
|
||||
defer os.close(d)
|
||||
|
||||
{
|
||||
file_info, ferr := os.fstat(d);
|
||||
defer os.file_info_delete(file_info);
|
||||
file_info, ferr := os.fstat(d)
|
||||
defer os.file_info_delete(file_info)
|
||||
if ferr != 0 {
|
||||
return;
|
||||
return
|
||||
}
|
||||
if !file_info.is_dir {
|
||||
return;
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
fis, _ := os.read_dir(d, -1);
|
||||
fis, _ := os.read_dir(d, -1)
|
||||
slice.sort_by(fis, proc(a, b: os.File_Info) -> bool {
|
||||
return a.name < b.name;
|
||||
});
|
||||
return a.name < b.name
|
||||
})
|
||||
defer {
|
||||
for fi in fis {
|
||||
os.file_info_delete(fi);
|
||||
os.file_info_delete(fi)
|
||||
}
|
||||
delete(fis);
|
||||
delete(fis)
|
||||
}
|
||||
|
||||
for fi in fis {
|
||||
n := fi.name;
|
||||
matched := match(pattern, n) or_return;
|
||||
n := fi.name
|
||||
matched := match(pattern, n) or_return
|
||||
if matched {
|
||||
append(&m, join(dir, n));
|
||||
append(&m, join(dir, n))
|
||||
}
|
||||
}
|
||||
return;
|
||||
return
|
||||
}
|
||||
|
||||
@(private)
|
||||
has_meta :: proc(path: string) -> bool {
|
||||
when ODIN_OS == "windows" {
|
||||
CHARS :: `*?[`;
|
||||
CHARS :: `*?[`
|
||||
} else {
|
||||
CHARS :: `*?[\`;
|
||||
CHARS :: `*?[\`
|
||||
}
|
||||
return strings.contains_any(path, CHARS);
|
||||
return strings.contains_any(path, CHARS)
|
||||
}
|
||||
|
||||
@(private)
|
||||
clean_glob_path :: proc(path: string) -> string {
|
||||
switch path {
|
||||
case "":
|
||||
return ".";
|
||||
return "."
|
||||
case SEPARATOR_STRING:
|
||||
return path;
|
||||
return path
|
||||
}
|
||||
return path[:len(path)-1];
|
||||
return path[:len(path)-1]
|
||||
}
|
||||
|
||||
|
||||
@(private)
|
||||
clean_glob_path_windows :: proc(path: string, temp_buf: []byte) -> (prefix_len: int, cleaned: string) {
|
||||
vol_len := volume_name_len(path);
|
||||
vol_len := volume_name_len(path)
|
||||
switch {
|
||||
case path == "":
|
||||
return 0, ".";
|
||||
return 0, "."
|
||||
case vol_len+1 == len(path) && is_separator(path[len(path)-1]): // /, \, C:\, C:/
|
||||
return vol_len+1, path;
|
||||
return vol_len+1, path
|
||||
case vol_len == len(path) && len(path) == 2: // C:
|
||||
copy(temp_buf[:], path);
|
||||
temp_buf[2] = '.';
|
||||
return vol_len, string(temp_buf[:3]);
|
||||
copy(temp_buf[:], path)
|
||||
temp_buf[2] = '.'
|
||||
return vol_len, string(temp_buf[:3])
|
||||
}
|
||||
|
||||
if vol_len >= len(path) {
|
||||
vol_len = len(path) -1;
|
||||
vol_len = len(path) -1
|
||||
}
|
||||
return vol_len, path[:len(path)-1];
|
||||
return vol_len, path[:len(path)-1]
|
||||
}
|
||||
|
||||
+145
-145
@@ -7,40 +7,40 @@ import "core:strings"
|
||||
// is_separator checks whether the byte is a valid separator character
|
||||
is_separator :: proc(c: byte) -> bool {
|
||||
switch c {
|
||||
case '/': return true;
|
||||
case '\\': return ODIN_OS == "windows";
|
||||
case '/': return true
|
||||
case '\\': return ODIN_OS == "windows"
|
||||
}
|
||||
return false;
|
||||
return false
|
||||
}
|
||||
|
||||
@(private)
|
||||
is_slash :: proc(c: byte) -> bool {
|
||||
return c == '\\' || c == '/';
|
||||
return c == '\\' || c == '/'
|
||||
}
|
||||
|
||||
split :: proc(path: string) -> (dir, file: string) {
|
||||
vol := volume_name(path);
|
||||
i := len(path) - 1;
|
||||
vol := volume_name(path)
|
||||
i := len(path) - 1
|
||||
for i >= len(vol) && !is_separator(path[i]) {
|
||||
i -= 1;
|
||||
i -= 1
|
||||
}
|
||||
return path[:i+1], path[i+1:];
|
||||
return path[:i+1], path[i+1:]
|
||||
}
|
||||
|
||||
volume_name :: proc(path: string) -> string {
|
||||
return path[:volume_name_len(path)];
|
||||
return path[:volume_name_len(path)]
|
||||
}
|
||||
|
||||
volume_name_len :: proc(path: string) -> int {
|
||||
if ODIN_OS == "windows" {
|
||||
if len(path) < 2 {
|
||||
return 0;
|
||||
return 0
|
||||
}
|
||||
c := path[0];
|
||||
c := path[0]
|
||||
if path[1] == ':' {
|
||||
switch c {
|
||||
case 'a'..='z', 'A'..='Z':
|
||||
return 2;
|
||||
return 2
|
||||
}
|
||||
}
|
||||
|
||||
@@ -49,153 +49,153 @@ volume_name_len :: proc(path: string) -> int {
|
||||
!is_slash(path[2]) && path[2] != '.' {
|
||||
for n := 3; n < l-1; n += 1 {
|
||||
if is_slash(path[n]) {
|
||||
n += 1;
|
||||
n += 1
|
||||
if !is_slash(path[n]) {
|
||||
if path[n] == '.' {
|
||||
break;
|
||||
break
|
||||
}
|
||||
}
|
||||
for ; n < l; n += 1 {
|
||||
if is_slash(path[n]) {
|
||||
break;
|
||||
break
|
||||
}
|
||||
}
|
||||
return n;
|
||||
return n
|
||||
}
|
||||
break;
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
return 0
|
||||
}
|
||||
|
||||
base :: proc(path: string) -> string {
|
||||
if path == "" {
|
||||
return ".";
|
||||
return "."
|
||||
}
|
||||
|
||||
path := path;
|
||||
path := path
|
||||
for len(path) > 0 && is_separator(path[len(path)-1]) {
|
||||
path = path[:len(path)-1];
|
||||
path = path[:len(path)-1]
|
||||
}
|
||||
|
||||
path = path[volume_name_len(path):];
|
||||
path = path[volume_name_len(path):]
|
||||
|
||||
i := len(path)-1;
|
||||
i := len(path)-1
|
||||
for i >= 0 && !is_separator(path[i]) {
|
||||
i -= 1;
|
||||
i -= 1
|
||||
}
|
||||
if i >= 0 {
|
||||
path = path[i+1:];
|
||||
path = path[i+1:]
|
||||
}
|
||||
if path == "" {
|
||||
return SEPARATOR_STRING;
|
||||
return SEPARATOR_STRING
|
||||
}
|
||||
return path;
|
||||
return path
|
||||
}
|
||||
|
||||
|
||||
clean :: proc(path: string, allocator := context.allocator) -> string {
|
||||
context.allocator = allocator;
|
||||
context.allocator = allocator
|
||||
|
||||
path := path;
|
||||
original_path := path;
|
||||
vol_len := volume_name_len(path);
|
||||
path = path[vol_len:];
|
||||
path := path
|
||||
original_path := path
|
||||
vol_len := volume_name_len(path)
|
||||
path = path[vol_len:]
|
||||
|
||||
if path == "" {
|
||||
if vol_len > 1 && original_path[1] != ':' {
|
||||
s, ok := from_slash(original_path);
|
||||
s, ok := from_slash(original_path)
|
||||
if !ok {
|
||||
s = strings.clone(s);
|
||||
s = strings.clone(s)
|
||||
}
|
||||
return s;
|
||||
return s
|
||||
}
|
||||
return strings.concatenate({original_path, "."});
|
||||
return strings.concatenate({original_path, "."})
|
||||
}
|
||||
|
||||
rooted := is_separator(path[0]);
|
||||
rooted := is_separator(path[0])
|
||||
|
||||
n := len(path);
|
||||
n := len(path)
|
||||
out := &Lazy_Buffer{
|
||||
s = path,
|
||||
vol_and_path = original_path,
|
||||
vol_len = vol_len,
|
||||
};
|
||||
}
|
||||
|
||||
r, dot_dot := 0, 0;
|
||||
r, dot_dot := 0, 0
|
||||
if rooted {
|
||||
lazy_buffer_append(out, SEPARATOR);
|
||||
r, dot_dot = 1, 1;
|
||||
lazy_buffer_append(out, SEPARATOR)
|
||||
r, dot_dot = 1, 1
|
||||
}
|
||||
|
||||
for r < n {
|
||||
switch {
|
||||
case is_separator(path[r]):
|
||||
r += 1;
|
||||
r += 1
|
||||
case path[r] == '.' && (r+1 == n || is_separator(path[r+1])):
|
||||
r += 1;
|
||||
r += 1
|
||||
case path[r] == '.' && path[r+1] == '.' && (r+2 == n || is_separator(path[r+2])):
|
||||
r += 2;
|
||||
r += 2
|
||||
switch {
|
||||
case out.w > dot_dot:
|
||||
out.w -= 1;
|
||||
out.w -= 1
|
||||
for out.w > dot_dot && !is_separator(lazy_buffer_index(out, out.w)) {
|
||||
out.w -= 1;
|
||||
out.w -= 1
|
||||
}
|
||||
case !rooted:
|
||||
if out.w > 0 {
|
||||
lazy_buffer_append(out, SEPARATOR);
|
||||
lazy_buffer_append(out, SEPARATOR)
|
||||
}
|
||||
lazy_buffer_append(out, '.');
|
||||
lazy_buffer_append(out, '.');
|
||||
dot_dot = out.w;
|
||||
lazy_buffer_append(out, '.')
|
||||
lazy_buffer_append(out, '.')
|
||||
dot_dot = out.w
|
||||
}
|
||||
case:
|
||||
if rooted && out.w != 1 || !rooted && out.w != 0 {
|
||||
lazy_buffer_append(out, SEPARATOR);
|
||||
lazy_buffer_append(out, SEPARATOR)
|
||||
}
|
||||
for ; r < n && !is_separator(path[r]); r += 1 {
|
||||
lazy_buffer_append(out, path[r]);
|
||||
lazy_buffer_append(out, path[r])
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
if out.w == 0 {
|
||||
lazy_buffer_append(out, '.');
|
||||
lazy_buffer_append(out, '.')
|
||||
}
|
||||
|
||||
s := lazy_buffer_string(out);
|
||||
cleaned, new_allocation := from_slash(s);
|
||||
s := lazy_buffer_string(out)
|
||||
cleaned, new_allocation := from_slash(s)
|
||||
if new_allocation {
|
||||
delete(s);
|
||||
lazy_buffer_destroy(out);
|
||||
delete(s)
|
||||
lazy_buffer_destroy(out)
|
||||
}
|
||||
return cleaned;
|
||||
return cleaned
|
||||
}
|
||||
|
||||
from_slash :: proc(path: string, allocator := context.allocator) -> (new_path: string, new_allocation: bool) {
|
||||
if SEPARATOR == '/' {
|
||||
return path, false;
|
||||
return path, false
|
||||
}
|
||||
return strings.replace_all(path, "/", SEPARATOR_STRING, allocator);
|
||||
return strings.replace_all(path, "/", SEPARATOR_STRING, allocator)
|
||||
}
|
||||
|
||||
to_slash :: proc(path: string, allocator := context.allocator) -> (new_path: string, new_allocation: bool) {
|
||||
if SEPARATOR == '/' {
|
||||
return path, false;
|
||||
return path, false
|
||||
}
|
||||
return strings.replace_all(path, SEPARATOR_STRING, "/", allocator);
|
||||
return strings.replace_all(path, SEPARATOR_STRING, "/", allocator)
|
||||
}
|
||||
|
||||
ext :: proc(path: string) -> string {
|
||||
for i := len(path)-1; i >= 0 && !is_separator(path[i]); i -= 1 {
|
||||
if path[i] == '.' {
|
||||
return path[i:];
|
||||
return path[i:]
|
||||
}
|
||||
}
|
||||
return "";
|
||||
return ""
|
||||
}
|
||||
|
||||
|
||||
@@ -206,145 +206,145 @@ Relative_Error :: enum {
|
||||
}
|
||||
|
||||
rel :: proc(base_path, target_path: string, allocator := context.allocator) -> (string, Relative_Error) {
|
||||
context.allocator = allocator;
|
||||
base_clean, target_clean := clean(base_path), clean(target_path);
|
||||
context.allocator = allocator
|
||||
base_clean, target_clean := clean(base_path), clean(target_path)
|
||||
|
||||
delete_target := true;
|
||||
delete_target := true
|
||||
defer {
|
||||
if delete_target {
|
||||
delete(target_clean);
|
||||
delete(target_clean)
|
||||
}
|
||||
delete(base_clean);
|
||||
delete(base_clean)
|
||||
}
|
||||
|
||||
if strings.equal_fold(target_clean, base_clean) {
|
||||
return strings.clone("."), .None;
|
||||
return strings.clone("."), .None
|
||||
}
|
||||
|
||||
base_vol, target_vol := volume_name(base_path), volume_name(target_path);
|
||||
base := base_clean[len(base_vol):];
|
||||
target := target_clean[len(target_vol):];
|
||||
base_vol, target_vol := volume_name(base_path), volume_name(target_path)
|
||||
base := base_clean[len(base_vol):]
|
||||
target := target_clean[len(target_vol):]
|
||||
if base == "." {
|
||||
base = "";
|
||||
base = ""
|
||||
}
|
||||
|
||||
base_slashed := len(base) > 0 && base[0] == SEPARATOR;
|
||||
target_slashed := len(target) > 0 && target[0] == SEPARATOR;
|
||||
base_slashed := len(base) > 0 && base[0] == SEPARATOR
|
||||
target_slashed := len(target) > 0 && target[0] == SEPARATOR
|
||||
if base_slashed != target_slashed || !strings.equal_fold(base_vol, target_vol) {
|
||||
return "", .Cannot_Relate;
|
||||
return "", .Cannot_Relate
|
||||
}
|
||||
|
||||
bl, tl := len(base), len(target);
|
||||
b0, bi, t0, ti: int;
|
||||
bl, tl := len(base), len(target)
|
||||
b0, bi, t0, ti: int
|
||||
for {
|
||||
for bi < bl && base[bi] != SEPARATOR {
|
||||
bi += 1;
|
||||
bi += 1
|
||||
}
|
||||
for ti < tl && target[ti] != SEPARATOR {
|
||||
ti += 1;
|
||||
ti += 1
|
||||
}
|
||||
if !strings.equal_fold(target[t0:ti], base[b0:bi]) {
|
||||
break;
|
||||
break
|
||||
}
|
||||
if bi < bl {
|
||||
bi += 1;
|
||||
bi += 1
|
||||
}
|
||||
if ti < tl {
|
||||
ti += 1;
|
||||
ti += 1
|
||||
}
|
||||
b0, t0 = bi, ti;
|
||||
b0, t0 = bi, ti
|
||||
}
|
||||
|
||||
if base[b0:bi] == ".." {
|
||||
return "", .Cannot_Relate;
|
||||
return "", .Cannot_Relate
|
||||
}
|
||||
|
||||
if b0 != bl {
|
||||
seps := strings.count(base[b0:bl], SEPARATOR_STRING);
|
||||
size := 2 + seps*3;
|
||||
seps := strings.count(base[b0:bl], SEPARATOR_STRING)
|
||||
size := 2 + seps*3
|
||||
if tl != t0 {
|
||||
size += 1 + tl - t0;
|
||||
size += 1 + tl - t0
|
||||
}
|
||||
buf := make([]byte, size);
|
||||
n := copy(buf, "..");
|
||||
buf := make([]byte, size)
|
||||
n := copy(buf, "..")
|
||||
for in 0..<seps {
|
||||
buf[n] = SEPARATOR;
|
||||
copy(buf[n+1:], "..");
|
||||
n += 3;
|
||||
buf[n] = SEPARATOR
|
||||
copy(buf[n+1:], "..")
|
||||
n += 3
|
||||
}
|
||||
if t0 != tl {
|
||||
buf[n] = SEPARATOR;
|
||||
copy(buf[n+1:], target[t0:]);
|
||||
buf[n] = SEPARATOR
|
||||
copy(buf[n+1:], target[t0:])
|
||||
}
|
||||
return string(buf), .None;
|
||||
return string(buf), .None
|
||||
}
|
||||
|
||||
delete_target = false;
|
||||
return target[t0:], .None;
|
||||
delete_target = false
|
||||
return target[t0:], .None
|
||||
}
|
||||
|
||||
dir :: proc(path: string, allocator := context.allocator) -> string {
|
||||
vol := volume_name(path);
|
||||
i := len(path) - 1;
|
||||
vol := volume_name(path)
|
||||
i := len(path) - 1
|
||||
for i >= len(vol) && !is_separator(path[i]) {
|
||||
i -= 1;
|
||||
i -= 1
|
||||
}
|
||||
dir := clean(path[len(vol) : i+1], allocator);
|
||||
defer delete(dir, allocator);
|
||||
dir := clean(path[len(vol) : i+1], allocator)
|
||||
defer delete(dir, allocator)
|
||||
if dir == "." && len(vol) > 2 {
|
||||
return strings.clone(vol);
|
||||
return strings.clone(vol)
|
||||
}
|
||||
return strings.concatenate({vol, dir});
|
||||
return strings.concatenate({vol, dir})
|
||||
}
|
||||
|
||||
|
||||
|
||||
split_list :: proc(path: string, allocator := context.allocator) -> []string {
|
||||
if path == "" {
|
||||
return nil;
|
||||
return nil
|
||||
}
|
||||
|
||||
start: int;
|
||||
quote: bool;
|
||||
start: int
|
||||
quote: bool
|
||||
|
||||
start, quote = 0, false;
|
||||
count := 0;
|
||||
start, quote = 0, false
|
||||
count := 0
|
||||
|
||||
for i := 0; i < len(path); i += 1 {
|
||||
c := path[i];
|
||||
c := path[i]
|
||||
switch {
|
||||
case c == '"':
|
||||
quote = !quote;
|
||||
quote = !quote
|
||||
case c == LIST_SEPARATOR && !quote:
|
||||
count += 1;
|
||||
count += 1
|
||||
}
|
||||
}
|
||||
|
||||
start, quote = 0, false;
|
||||
list := make([]string, count, allocator);
|
||||
index := 0;
|
||||
start, quote = 0, false
|
||||
list := make([]string, count, allocator)
|
||||
index := 0
|
||||
for i := 0; i < len(path); i += 1 {
|
||||
c := path[i];
|
||||
c := path[i]
|
||||
switch {
|
||||
case c == '"':
|
||||
quote = !quote;
|
||||
quote = !quote
|
||||
case c == LIST_SEPARATOR && !quote:
|
||||
list[index] = path[start:i];
|
||||
index += 1;
|
||||
start = i + 1;
|
||||
list[index] = path[start:i]
|
||||
index += 1
|
||||
start = i + 1
|
||||
}
|
||||
}
|
||||
assert(index == count);
|
||||
assert(index == count)
|
||||
|
||||
for s0, i in list {
|
||||
s, new := strings.replace_all(s0, `"`, ``, allocator);
|
||||
s, new := strings.replace_all(s0, `"`, ``, allocator)
|
||||
if !new {
|
||||
s = strings.clone(s, allocator);
|
||||
s = strings.clone(s, allocator)
|
||||
}
|
||||
list[i] = s;
|
||||
list[i] = s
|
||||
}
|
||||
|
||||
return list;
|
||||
return list
|
||||
}
|
||||
|
||||
|
||||
@@ -366,38 +366,38 @@ Lazy_Buffer :: struct {
|
||||
@(private)
|
||||
lazy_buffer_index :: proc(lb: ^Lazy_Buffer, i: int) -> byte {
|
||||
if lb.b != nil {
|
||||
return lb.b[i];
|
||||
return lb.b[i]
|
||||
}
|
||||
return lb.s[i];
|
||||
return lb.s[i]
|
||||
}
|
||||
@(private)
|
||||
lazy_buffer_append :: proc(lb: ^Lazy_Buffer, c: byte) {
|
||||
if lb.b == nil {
|
||||
if lb.w < len(lb.s) && lb.s[lb.w] == c {
|
||||
lb.w += 1;
|
||||
return;
|
||||
lb.w += 1
|
||||
return
|
||||
}
|
||||
lb.b = make([]byte, len(lb.s));
|
||||
copy(lb.b, lb.s[:lb.w]);
|
||||
lb.b = make([]byte, len(lb.s))
|
||||
copy(lb.b, lb.s[:lb.w])
|
||||
}
|
||||
lb.b[lb.w] = c;
|
||||
lb.w += 1;
|
||||
lb.b[lb.w] = c
|
||||
lb.w += 1
|
||||
}
|
||||
@(private)
|
||||
lazy_buffer_string :: proc(lb: ^Lazy_Buffer) -> string {
|
||||
if lb.b == nil {
|
||||
return strings.clone(lb.vol_and_path[:lb.vol_len+lb.w]);
|
||||
return strings.clone(lb.vol_and_path[:lb.vol_len+lb.w])
|
||||
}
|
||||
|
||||
x := lb.vol_and_path[:lb.vol_len];
|
||||
y := string(lb.b[:lb.w]);
|
||||
z := make([]byte, len(x)+len(y));
|
||||
copy(z, x);
|
||||
copy(z[len(x):], y);
|
||||
return string(z);
|
||||
x := lb.vol_and_path[:lb.vol_len]
|
||||
y := string(lb.b[:lb.w])
|
||||
z := make([]byte, len(x)+len(y))
|
||||
copy(z, x)
|
||||
copy(z[len(x):], y)
|
||||
return string(z)
|
||||
}
|
||||
@(private)
|
||||
lazy_buffer_destroy :: proc(lb: ^Lazy_Buffer) {
|
||||
delete(lb.b);
|
||||
lb^ = {};
|
||||
delete(lb.b)
|
||||
lb^ = {}
|
||||
}
|
||||
|
||||
@@ -4,128 +4,128 @@ import "core:strings"
|
||||
import "core:os"
|
||||
import win32 "core:sys/windows"
|
||||
|
||||
SEPARATOR :: '\\';
|
||||
SEPARATOR_STRING :: `\`;
|
||||
LIST_SEPARATOR :: ';';
|
||||
SEPARATOR :: '\\'
|
||||
SEPARATOR_STRING :: `\`
|
||||
LIST_SEPARATOR :: ';'
|
||||
|
||||
@(private)
|
||||
reserved_names := [?]string{
|
||||
"CON", "PRN", "AUX", "NUL",
|
||||
"COM1", "COM2", "COM3", "COM4", "COM5", "COM6", "COM7", "COM8", "COM9",
|
||||
"LPT1", "LPT2", "LPT3", "LPT4", "LPT5", "LPT6", "LPT7", "LPT8", "LPT9",
|
||||
};
|
||||
}
|
||||
|
||||
is_reserved_name :: proc(path: string) -> bool {
|
||||
if len(path) == 0 {
|
||||
return false;
|
||||
return false
|
||||
}
|
||||
for reserved in reserved_names {
|
||||
if strings.equal_fold(path, reserved) {
|
||||
return true;
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false;
|
||||
return false
|
||||
}
|
||||
|
||||
is_UNC :: proc(path: string) -> bool {
|
||||
return volume_name_len(path) > 2;
|
||||
return volume_name_len(path) > 2
|
||||
}
|
||||
|
||||
|
||||
is_abs :: proc(path: string) -> bool {
|
||||
if is_reserved_name(path) {
|
||||
return true;
|
||||
return true
|
||||
}
|
||||
l := volume_name_len(path);
|
||||
l := volume_name_len(path)
|
||||
if l == 0 {
|
||||
return false;
|
||||
return false
|
||||
}
|
||||
|
||||
path := path;
|
||||
path = path[l:];
|
||||
path := path
|
||||
path = path[l:]
|
||||
if path == "" {
|
||||
return false;
|
||||
return false
|
||||
}
|
||||
return is_slash(path[0]);
|
||||
return is_slash(path[0])
|
||||
}
|
||||
|
||||
|
||||
@(private)
|
||||
temp_full_path :: proc(name: string) -> (path: string, err: os.Errno) {
|
||||
ta := context.temp_allocator;
|
||||
ta := context.temp_allocator
|
||||
|
||||
name := name;
|
||||
name := name
|
||||
if name == "" {
|
||||
name = ".";
|
||||
name = "."
|
||||
}
|
||||
|
||||
p := win32.utf8_to_utf16(name, ta);
|
||||
buf := make([dynamic]u16, 100, ta);
|
||||
p := win32.utf8_to_utf16(name, ta)
|
||||
buf := make([dynamic]u16, 100, ta)
|
||||
for {
|
||||
n := win32.GetFullPathNameW(raw_data(p), u32(len(buf)), raw_data(buf), nil);
|
||||
n := win32.GetFullPathNameW(raw_data(p), u32(len(buf)), raw_data(buf), nil)
|
||||
if n == 0 {
|
||||
delete(buf);
|
||||
return "", os.Errno(win32.GetLastError());
|
||||
delete(buf)
|
||||
return "", os.Errno(win32.GetLastError())
|
||||
}
|
||||
if n <= u32(len(buf)) {
|
||||
return win32.utf16_to_utf8(buf[:n], ta), os.ERROR_NONE;
|
||||
return win32.utf16_to_utf8(buf[:n], ta), os.ERROR_NONE
|
||||
}
|
||||
resize(&buf, len(buf)*2);
|
||||
resize(&buf, len(buf)*2)
|
||||
}
|
||||
|
||||
return;
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
|
||||
abs :: proc(path: string, allocator := context.allocator) -> (string, bool) {
|
||||
full_path, err := temp_full_path(path);
|
||||
full_path, err := temp_full_path(path)
|
||||
if err != 0 {
|
||||
return "", false;
|
||||
return "", false
|
||||
}
|
||||
p := clean(full_path, allocator);
|
||||
return p, true;
|
||||
p := clean(full_path, allocator)
|
||||
return p, true
|
||||
}
|
||||
|
||||
|
||||
join :: proc(elems: ..string, allocator := context.allocator) -> string {
|
||||
for e, i in elems {
|
||||
if e != "" {
|
||||
return join_non_empty(elems[i:]);
|
||||
return join_non_empty(elems[i:])
|
||||
}
|
||||
}
|
||||
return "";
|
||||
return ""
|
||||
}
|
||||
|
||||
join_non_empty :: proc(elems: []string) -> string {
|
||||
if len(elems[0]) == 2 && elems[0][1] == ':' {
|
||||
i := 1;
|
||||
i := 1
|
||||
for ; i < len(elems); i += 1 {
|
||||
if elems[i] != "" {
|
||||
break;
|
||||
break
|
||||
}
|
||||
}
|
||||
s := strings.join(elems[i:], SEPARATOR_STRING, context.temp_allocator);
|
||||
s = strings.concatenate({elems[0], s}, context.temp_allocator);
|
||||
return clean(s);
|
||||
s := strings.join(elems[i:], SEPARATOR_STRING, context.temp_allocator)
|
||||
s = strings.concatenate({elems[0], s}, context.temp_allocator)
|
||||
return clean(s)
|
||||
}
|
||||
|
||||
s := strings.join(elems, SEPARATOR_STRING, context.temp_allocator);
|
||||
p := clean(s);
|
||||
s := strings.join(elems, SEPARATOR_STRING, context.temp_allocator)
|
||||
p := clean(s)
|
||||
if !is_UNC(p) {
|
||||
return p;
|
||||
return p
|
||||
}
|
||||
|
||||
head := clean(elems[0], context.temp_allocator);
|
||||
head := clean(elems[0], context.temp_allocator)
|
||||
if is_UNC(head) {
|
||||
return p;
|
||||
return p
|
||||
}
|
||||
delete(p); // It is not needed now
|
||||
delete(p) // It is not needed now
|
||||
|
||||
tail := clean(strings.join(elems[1:], SEPARATOR_STRING, context.temp_allocator), context.temp_allocator);
|
||||
tail := clean(strings.join(elems[1:], SEPARATOR_STRING, context.temp_allocator), context.temp_allocator)
|
||||
if head[len(head)-1] == SEPARATOR {
|
||||
return strings.concatenate({head, tail});
|
||||
return strings.concatenate({head, tail})
|
||||
}
|
||||
|
||||
return strings.concatenate({head, SEPARATOR_STRING, tail});
|
||||
return strings.concatenate({head, SEPARATOR_STRING, tail})
|
||||
}
|
||||
|
||||
@@ -14,7 +14,7 @@ import "core:slice"
|
||||
// The sole exception is if 'skip_dir' is returned as true:
|
||||
// when 'skip_dir' is invoked on a directory. 'walk' skips directory contents
|
||||
// when 'skip_dir' is invoked on a non-directory. 'walk' skips the remaining files in the containing directory
|
||||
Walk_Proc :: #type proc(info: os.File_Info, in_err: os.Errno) -> (err: os.Errno, skip_dir: bool);
|
||||
Walk_Proc :: #type proc(info: os.File_Info, in_err: os.Errno) -> (err: os.Errno, skip_dir: bool)
|
||||
|
||||
// walk walks the file tree rooted at 'root', calling 'walk_proc' for each file or directory in the tree, including 'root'
|
||||
// All errors that happen visiting files and directories are filtered by walk_proc
|
||||
@@ -23,16 +23,16 @@ Walk_Proc :: #type proc(info: os.File_Info, in_err: os.Errno) -> (err: os.Errno,
|
||||
// NOTE: walk does not follow symbolic links
|
||||
// NOTE: os.File_Info uses the 'context.temp_allocator' to allocate, and will delete when it is done
|
||||
walk :: proc(root: string, walk_proc: Walk_Proc) -> os.Errno {
|
||||
info, err := os.lstat(root, context.temp_allocator);
|
||||
defer os.file_info_delete(info, context.temp_allocator);
|
||||
info, err := os.lstat(root, context.temp_allocator)
|
||||
defer os.file_info_delete(info, context.temp_allocator)
|
||||
|
||||
skip_dir: bool;
|
||||
skip_dir: bool
|
||||
if err != 0 {
|
||||
err, skip_dir = walk_proc(info, err);
|
||||
err, skip_dir = walk_proc(info, err)
|
||||
} else {
|
||||
err, skip_dir = _walk(info, walk_proc);
|
||||
err, skip_dir = _walk(info, walk_proc)
|
||||
}
|
||||
return 0 if skip_dir else err;
|
||||
return 0 if skip_dir else err
|
||||
}
|
||||
|
||||
|
||||
@@ -41,48 +41,48 @@ _walk :: proc(info: os.File_Info, walk_proc: Walk_Proc) -> (err: os.Errno, skip_
|
||||
if !info.is_dir {
|
||||
if info.fullpath == "" && info.name == "" {
|
||||
// ignore empty things
|
||||
return;
|
||||
return
|
||||
}
|
||||
return walk_proc(info, 0);
|
||||
return walk_proc(info, 0)
|
||||
}
|
||||
|
||||
fis: []os.File_Info;
|
||||
err1: os.Errno;
|
||||
fis, err = read_dir(info.fullpath, context.temp_allocator);
|
||||
defer os.file_info_slice_delete(fis, context.temp_allocator);
|
||||
fis: []os.File_Info
|
||||
err1: os.Errno
|
||||
fis, err = read_dir(info.fullpath, context.temp_allocator)
|
||||
defer os.file_info_slice_delete(fis, context.temp_allocator)
|
||||
|
||||
err1, skip_dir = walk_proc(info, err);
|
||||
err1, skip_dir = walk_proc(info, err)
|
||||
if err != 0 || err1 != 0 || skip_dir {
|
||||
err = err1;
|
||||
return;
|
||||
err = err1
|
||||
return
|
||||
}
|
||||
|
||||
for fi in fis {
|
||||
err, skip_dir = _walk(fi, walk_proc);
|
||||
err, skip_dir = _walk(fi, walk_proc)
|
||||
if err != 0 || skip_dir {
|
||||
if !fi.is_dir || !skip_dir {
|
||||
return;
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return;
|
||||
return
|
||||
}
|
||||
|
||||
@(private)
|
||||
read_dir :: proc(dir_name: string, allocator := context.temp_allocator) -> ([]os.File_Info, os.Errno) {
|
||||
f, err := os.open(dir_name);
|
||||
f, err := os.open(dir_name)
|
||||
if err != 0 {
|
||||
return nil, err;
|
||||
return nil, err
|
||||
}
|
||||
fis: []os.File_Info;
|
||||
fis, err = os.read_dir(f, -1, allocator);
|
||||
os.close(f);
|
||||
fis: []os.File_Info
|
||||
fis, err = os.read_dir(f, -1, allocator)
|
||||
os.close(f)
|
||||
if err != 0 {
|
||||
return nil, err;
|
||||
return nil, err
|
||||
}
|
||||
slice.sort_by(fis, proc(a, b: os.File_Info) -> bool {
|
||||
return a.name < b.name;
|
||||
});
|
||||
return fis, 0;
|
||||
return a.name < b.name
|
||||
})
|
||||
return fis, 0
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user