forked from GaijinEntertainment/daScript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fs.das
72 lines (58 loc) · 1.82 KB
/
fs.das
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
options indenting = 4
module fs shared
require fio
require strings
require uriparser
def private ends_with_separator(str: string): bool
for c in "\\/"
if ends_with(str, to_char(c))
return true
return false
def private starts_with_separator(str: string): bool
for c in "\\/"
if starts_with(str, to_char(c))
return true
return false
def private trim_path(path: string): string
if path == "."
return ""
if path |> starts_with("./") || path |> starts_with(".\\")
return path |> slice(2)
return path
def join_path(a, path_b: string): string
let b = trim_path(path_b)
if length(a) == 0
return fix_path(b)
if length(b) == 0
return fix_path(a)
var res = build_string() <| $(builder)
builder |> write(a)
let ends = ends_with_separator(a)
let starts = starts_with_separator(b)
if ends && starts
builder |> write(slice(b, 1))
elif !ends && !starts
builder |> write("/")
builder |> write(b)
else
builder |> write(b)
return fix_path(res)
def fix_path(path: string): string
return path |> trim_path() |> file_name_to_uri() |> normalize_uri() |> uri_to_file_name()
def scan_dir(path: string; var cache: table<string; void?>; var res: array<string>; suffix = ".das"): bool
let dirStat = stat(path)
if !dirStat.is_dir
return false
fio::dir(path) <| $(n)
if n == "." || n == ".."
return
let f = path |> join_path(n)
let fStat = stat(f)
if !fStat.is_valid
return
if fStat.is_dir
f |> scan_dir(cache, res)
elif fStat.is_reg && f |> ends_with(suffix) && !cache |> key_exists(f)
cache[f] = null
res |> push(f)
return true