summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKyle Gunger <kgunger12@gmail.com>2022-12-05 00:59:19 -0500
committerKyle Gunger <kgunger12@gmail.com>2022-12-05 00:59:19 -0500
commit0b5b8b0be6f3128df6453ea6667db9d35d396f94 (patch)
tree6347be98a67933335158a73cd2cbd3ea64f2bb69
parent24c4683a5b4a85e6a5b12ae879decf746904e114 (diff)
Path handling
-rw-r--r--tnslc/paths.tnsl80
1 files changed, 80 insertions, 0 deletions
diff --git a/tnslc/paths.tnsl b/tnslc/paths.tnsl
new file mode 100644
index 0000000..93c14f5
--- /dev/null
+++ b/tnslc/paths.tnsl
@@ -0,0 +1,80 @@
+# Requires util.tnsl
+;struct Path {
+ {}{}charp dirs,
+ {}charp file
+}
+
+/; method Path
+ /; rel_file ({}charp rpath) [Path]
+ ;{}{}charp 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 [{}charp]
+ ;{}charp out = join((self.dirs), '/')
+ ;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())
+ ;/
+
+;/
+
+/; split({}charp str, charp c) [{}{}charp]
+ ;{}{}charp out = {}
+ ;{}charp tmp = ""
+
+ /; loop (int i = 0; i < len str) [i++]
+ /; if (str{i} == c)
+ ;out.append(tmp)
+ ;{}charp tmp = ""
+ ;true
+ ;; else
+ ;tmp.append(str{i})
+ ;/
+ ;/
+ ;out.append(tmp)
+
+ ;return out
+;/
+
+/; join ({}{}charp s, charp j) [{}charp]
+ ;{}charp 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(j)
+ ;/
+ ;/
+ ;return out
+;/
+
+/; path_from_str ({}charp f) [Path]
+ ;{}{}charp 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
+;/