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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
|
package filepath
import "core:os"
import "core:slice"
import "core:strings"
import "core:unicode/utf8"
Match_Error :: enum {
None,
Syntax_Error,
}
// match states whether "name" matches the shell pattern
// Pattern syntax is:
// pattern:
// {term}
// term:
// '*' matches any sequence of non-/ characters
// '?' matches any single non-/ character
// '[' ['^'] { character-range } ']'
// character classification (cannot be empty)
// c matches character c (c != '*', '?', '\\', '[')
// '\\' c matches character c
//
// character-range
// c matches character c (c != '\\', '-', ']')
// '\\' c matches character c
// lo '-' hi matches character c for lo <= c <= hi
//
// match requires that the pattern matches the entirety of the name, not just a substring
// The only possible error returned is .Syntax_Error
//
// 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_loop: for len(pattern) > 0 {
star: bool
chunk: string
star, chunk, pattern = scan_chunk(pattern)
if star && chunk == "" {
return !strings.contains(name, SEPARATOR_STRING), .None
}
t: string
ok: bool
t, ok, err = match_chunk(chunk, name)
if ok && (len(t) == 0 || len(pattern) > 0) {
name = t
continue
}
if err != .None {
return
}
if star {
for i := 0; i < len(name) && name[i] != SEPARATOR; i += 1 {
t, ok, err = match_chunk(chunk, name[i+1:])
if ok {
if len(pattern) == 0 && len(t) > 0 {
continue
}
name = t
continue pattern_loop
}
if err != .None {
return
}
}
}
return false, .None
}
return len(name) == 0, .None
}
@(private="file")
scan_chunk :: proc(pattern: string) -> (star: bool, chunk, rest: string) {
pattern := pattern
for len(pattern) > 0 && pattern[0] == '*' {
pattern = pattern[1:]
star = true
}
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
}
}
case '[':
in_range = true
case ']':
in_range = false
case '*':
if !in_range {
break scan_loop
}
}
}
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
for len(chunk) > 0 {
if len(s) == 0 {
return
}
switch chunk[0] {
case '[':
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:]
}
match := false
range_count := 0
for {
if len(chunk) > 0 && chunk[0] == ']' && range_count > 0 {
chunk = chunk[1:]
break
}
lo, hi: rune
if lo, chunk, err = get_escape(chunk); err != .None {
return
}
hi = lo
if chunk[0] == '-' {
if hi, chunk, err = get_escape(chunk[1:]); err != .None {
return
}
}
if lo <= r && r <= hi {
match = true
}
range_count += 1
}
if match == is_negated {
return
}
case '?':
if s[0] == SEPARATOR {
return
}
_, w := utf8.decode_rune_in_string(s)
s = s[w:]
chunk = chunk[1:]
case '\\':
when ODIN_OS != .Windows {
chunk = chunk[1:]
if len(chunk) == 0 {
err = .Syntax_Error
return
}
}
fallthrough
case:
if chunk[0] != s[0] {
return
}
s = s[1:]
chunk = chunk[1:]
}
}
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
}
chunk := chunk
if chunk[0] == '\\' && ODIN_OS != .Windows {
chunk = chunk[1:]
if len(chunk) == 0 {
err = .Syntax_Error
return
}
}
w: int
r, w = utf8.decode_rune_in_string(chunk)
if r == utf8.RUNE_ERROR && w == 1 {
err = .Syntax_Error
}
next_chunk = chunk[w:]
if len(next_chunk) == 0 {
err = .Syntax_Error
}
return
}
// glob returns the names of all files matching pattern or nil if there are no matching files
// The syntax of patterns is the same as "match".
// The pattern may describe hierarchical names such as /usr/*/bin (assuming '/' is a separator)
//
// glob ignores file system errors
//
glob :: proc(pattern: string, allocator := context.allocator) -> (matches: []string, err: Match_Error) {
context.allocator = allocator
if !has_meta(pattern) {
// TODO(bill): os.lstat on here to check for error
m := make([]string, 1)
m[0] = pattern
return m[:], .None
}
dir, file := split(pattern)
volume_len := 0
when ODIN_OS == .Windows {
temp_buf: [8]byte
volume_len, dir = clean_glob_path_windows(dir, temp_buf[:])
} else {
dir = clean_glob_path(dir)
}
if !has_meta(dir[volume_len:]) {
m, e := _glob(dir, file, nil)
return m[:], e
}
m: []string
m, err = glob(dir)
if err != .None {
return
}
dmatches := make([dynamic]string, 0, 0)
for d in m {
dmatches, err = _glob(d, file, &dmatches)
if err != .None {
break
}
}
if len(dmatches) > 0 {
matches = dmatches[:]
}
return
}
_glob :: proc(dir, pattern: string, matches: ^[dynamic]string, allocator := context.allocator) -> (m: [dynamic]string, e: Match_Error) {
context.allocator = allocator
if matches != nil {
m = matches^
} else {
m = make([dynamic]string, 0, 0)
}
d, derr := os.open(dir, os.O_RDONLY)
if derr != 0 {
return
}
defer os.close(d)
{
file_info, ferr := os.fstat(d)
defer os.file_info_delete(file_info)
if ferr != 0 {
return
}
if !file_info.is_dir {
return
}
}
fis, _ := os.read_dir(d, -1)
slice.sort_by(fis, proc(a, b: os.File_Info) -> bool {
return a.name < b.name
})
defer {
for fi in fis {
os.file_info_delete(fi)
}
delete(fis)
}
for fi in fis {
n := fi.name
matched := match(pattern, n) or_return
if matched {
append(&m, join({dir, n}))
}
}
return
}
@(private)
has_meta :: proc(path: string) -> bool {
when ODIN_OS == .Windows {
CHARS :: `*?[`
} else {
CHARS :: `*?[\`
}
return strings.contains_any(path, CHARS)
}
@(private)
clean_glob_path :: proc(path: string) -> string {
switch path {
case "":
return "."
case SEPARATOR_STRING:
return path
}
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)
switch {
case path == "":
return 0, "."
case vol_len+1 == len(path) && is_separator(path[len(path)-1]): // /, \, C:\, C:/
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])
}
if vol_len >= len(path) {
vol_len = len(path) -1
}
return vol_len, path[:len(path)-1]
}
|