|
| 1 | +import std/[ |
| 2 | + strutils, |
| 3 | + strformat, |
| 4 | + os |
| 5 | +] |
| 6 | + |
| 7 | +import common |
| 8 | + |
| 9 | + |
| 10 | +let lib* = newDict(0) |
| 11 | + |
| 12 | +template addV(name, doc: string, item: Value) = |
| 13 | + addV(lib, name, doc, item) |
| 14 | + |
| 15 | +template addF(name, doc: string, args: ProcArgs, body: untyped) = |
| 16 | + addF(lib, name, doc, args, body) |
| 17 | + |
| 18 | +template addS(name, doc: string, args: ProcArgs, body: string) = |
| 19 | + addS(lib, "io.pg", name, doc, args, body) |
| 20 | + |
| 21 | + |
| 22 | +func newPgFile*(f: File, path: string): Value = |
| 23 | + result = newExtitem(cast[pointer](f)) |
| 24 | + |
| 25 | + let fname = fmt"File object at '{path}'" |
| 26 | + |
| 27 | + result.id = "File" |
| 28 | + result.fmtf = func(_: pointer): string = fname |
| 29 | + |
| 30 | + |
| 31 | +addF("f-open", |
| 32 | +""" |
| 33 | +'f-open' |
| 34 | +P M -> F |
| 35 | +Opens a filepath P with the specified mode symbol M and returns its file object F. |
| 36 | +The valid modes are: |
| 37 | +- /r (read); opens the file for reading, throws an error if the file doesn't exist. |
| 38 | +- /w (write); opens the file for writing, throws an error if the file doesn't exist. |
| 39 | +- /rw (readwrite); opens the file for reading and writing, throws an error if the file doesn't exist. |
| 40 | +- /a (append); opens the file for writing and appends to the end of the file when written to, throws an error if the file doesn't exist. |
| 41 | +- /c (create); opens the file for reading and writing, creates the file if it doesn't exist. |
| 42 | +""", @[("P", tString), ("M", tSymbol)]): |
| 43 | + let |
| 44 | + modeName = s.pop().strv |
| 45 | + path = s.pop().strv |
| 46 | + |
| 47 | + var |
| 48 | + mode: FileMode |
| 49 | + f: File |
| 50 | + |
| 51 | + case modeName |
| 52 | + of "r": |
| 53 | + mode = fmRead |
| 54 | + of "w": |
| 55 | + mode = fmWrite |
| 56 | + else: |
| 57 | + raise newPgError(fmt"Invalid file mode '{modeName}'") |
| 58 | + |
| 59 | + if not path.fileExists() and mode != fmReadWrite: |
| 60 | + raise newPgError(fmt"File '{path}' does not exist") |
| 61 | + |
| 62 | + if not f.open(path, mode): |
| 63 | + raise newPgError(fmt"File '{path}' could not be opened") |
| 64 | + |
| 65 | + s.push(f.newPgFile(path.absolutePath())) |
| 66 | + |
| 67 | +addF("f-close", |
| 68 | +""" |
| 69 | +'f-close' |
| 70 | +F -> |
| 71 | +Closes a file object F. |
| 72 | +Trying to use a file object after it was closed is undefined behavior. |
| 73 | +""", @[("F", tExtitem)]): |
| 74 | + let fobj = s.pop() |
| 75 | + |
| 76 | + if fobj isnot "File": |
| 77 | + raise newPgError("Given object is not a File object") |
| 78 | + |
| 79 | + let f = cast[File](fobj.dat) |
| 80 | + |
| 81 | + f.close() |
| 82 | + |
| 83 | +addF("f-read", |
| 84 | +""" |
| 85 | +'f-readall' |
| 86 | +F -> S |
| 87 | +Reads everything from a file object F and returns the contents S. |
| 88 | +""", @[("F", tExtitem)]): |
| 89 | + let fobj = s.pop() |
| 90 | + |
| 91 | + if fobj isnot "File": |
| 92 | + raise newPgError("Given object is not a File object") |
| 93 | + |
| 94 | + let f = cast[File](fobj.dat) |
| 95 | + |
| 96 | + var content: string |
| 97 | + |
| 98 | + try: |
| 99 | + content = f.readAll() |
| 100 | + except IoError as e: |
| 101 | + raise newPgError(e.msg) |
| 102 | + |
| 103 | + s.push(newString(content)) |
| 104 | + |
| 105 | +addF("f-write", |
| 106 | +""" |
| 107 | +'f-write' |
| 108 | +F S -> WS |
| 109 | +Writes a string S to a file object F and returns the amount written WS. |
| 110 | +""", @[("F", tExtitem), ("S", tString)]): |
| 111 | + let |
| 112 | + str = s.pop().strv |
| 113 | + fobj = s.pop() |
| 114 | + |
| 115 | + if fobj isnot "File": |
| 116 | + raise newPgError("Given object is not a File object") |
| 117 | + |
| 118 | + let f = cast[File](fobj.dat) |
| 119 | + |
| 120 | + try: |
| 121 | + f.write(str) |
| 122 | + except IoError as e: |
| 123 | + raise newPgError(e.msg) |
0 commit comments