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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
|
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.clone(SEPARATOR_STRING, allocator) : SEPARATOR_STRING;
} else {
return new ? strings.clone(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.clone(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.clone(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 "";
end := len(path) - 1;
dot := end;
for i := end; i >= 0; i -= 1 {
switch path[i] {
case '.': dot = (dot == end ? i : 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
return new ? strings.clone(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.clone(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.allocator) -> string {
if from == "" || to == "" do return "";
from, to := from, to;
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.clone(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:], to);
return string(buffer);
}
else {
buffer := make([]byte, from_slashes*3 + len(to), allocator);
for i in 0..<from_slashes {
buffer[i*3+0] = '.';
buffer[i*3+1] = '.';
buffer[i*3+2] = SEPARATOR;
}
copy(buffer[from_slashes*3:], to);
return string(buffer);
}
return "";
}
// returns the relative path from the current directory to another path
rel_current :: proc(to: string, allocator := context.allocator) -> string {
return rel_between(current(context.allocator), to, allocator);
}
// splits the path elements into slices of the original path string
split :: proc(s: string, allocator := context.allocator) -> []string {
return strings.split_multi(s, []string{"\\", "/"}, true, allocator);
}
|