summaryrefslogtreecommitdiff
path: root/tnslc/paths.tnsl
blob: 17719a037d19f628be8523b2292e83111ef7d6a8 (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
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
# Requires util.tnsl
;struct Path {
    {}{}uint8 dirs,
    {}uint8 file
}

/; method Path
    /; rel_file ({}uint8 rpath) [Path]
        ;{}{}uint8 spl = split(rpath, '/')
        ;Path out = {{}, ""}

        /; loop (int i = 0; i < len (self.dirs)) [i++]
            ;out.dirs.append(self.dirs{i})
        ;/
        
        /; loop (int i = 0; i < len spl - 1) [i++]
            ;out.dirs.append(spl{i})
        ;/
        
        ;out.file = spl{len spl - 1}
        ;return out
    ;/

    /; full_path [{}uint8]
        ;{}uint8 out = join((self.dirs), '/')
        /; if (len (self.dirs) > 0)
            ;out.append('/')
        ;/
        ;add_strings(~out, ~(self.file))
        ;return out
    ;/

    /; open_w [tnsl.io.File]
        ;return tnsl.io.writeFile(self.full_path())
    ;/

    /; open_r [tnsl.io.File]
        ;return tnsl.io.readFile(self.full_path())
    ;/

    /; extension_is ({}uint8 chk) [bool]
        ;{}uint8 ext = ""
        ;int dot = -1
        /; loop (int i = len (self.file) - 1; i > 0) [i = i - 1]
            /; if (self.file{i} == '.')
                ;dot = i
                ;break
            ;/
        ;/

        /; if (dot > 0)
            ;dot++
            /; loop (dot < len (self.file)) [dot++]
                ;ext.append(self.file{dot})
            ;/
        ;/

        ;return string_equate(chk, ext)
    ;/

;/

/; split({}uint8 str, uint8 c) [{}{}uint8]
    ;{}{}uint8 out = {}
    ;{}uint8 tmp = ""

    /; loop (int i = 0; i < len str) [i++]
        /; if (str{i} == c)
            ;out.append(tmp)
            ;{}uint8 tmp = ""
            ;true # work around for interpreter bug
        ;; else
            ;tmp.append(str{i})
        ;/
    ;/
    ;out.append(tmp)
    
    ;return out
;/

/; join ({}{}uint8 s, uint8 jc) [{}uint8]
    ;{}uint8 out = ""
    /; loop (int i = 0; i < len s) [i++]
        /; loop (int j = 0; j < len s{i}) [j++]
            ;out.append(s{i}{j})
        ;/
        /; if (i < len s - 1)
            ;out.append(jc)
        ;/
    ;/
    
    ;return out
;/

/; path_from_str ({}uint8 f) [Path]
    ;{}{}uint8 spl = split(f, '/')
    ;Path out = {{}, ""}
    /; loop (int i = 0; i < len spl - 1) [i++]
        ;out.dirs.append(spl{i})
    ;/
    ;out.file = spl{len spl - 1}
    ;return out
;/