aboutsummaryrefslogtreecommitdiff
path: root/core/os/path_posixfs.odin
blob: aea89c60a73281002a3567f0b2d43bc97c20d32f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#+private
#+build linux, darwin, netbsd, freebsd, openbsd, wasi
package os

// This implementation is for all systems that have POSIX-compliant filesystem paths.

_are_paths_identical :: proc(a, b: string) -> (identical: bool) {
	return a == b
}

_clean_path_handle_start :: proc(path: string, buffer: []u8) -> (rooted: bool, start: int) {
	// Preserve rooted paths.
	if _is_path_separator(path[0]) {
		rooted = true
		buffer[0] = _Path_Separator
		start = 1
	}
	return
}

_is_absolute_path :: proc(path: string) -> bool {
	return len(path) > 0 && _is_path_separator(path[0])
}

_get_relative_path_handle_start :: proc(base, target: string) -> bool {
	base_rooted   := len(base)   > 0 && _is_path_separator(base[0])
	target_rooted := len(target) > 0 && _is_path_separator(target[0])
	return base_rooted == target_rooted
}

_get_common_path_len :: proc(base, target: string) -> int {
	i := 0
	end := min(len(base), len(target))
	for j in 0..=end {
		if j == end || _is_path_separator(base[j]) {
			if base[i:j] == target[i:j] {
				i = j
			} else {
				break
			}
		}
	}
	return i
}

_split_path :: proc(path: string) -> (dir, file: string) {
	i := len(path) - 1
	for i >= 0 && !_is_path_separator(path[i]) {
		i -= 1
	}
	if i == 0 {
		return path[:i+1], path[i+1:]
	} else if i > 0 {
		return path[:i], path[i+1:]
	}
	return "", path
}