|
| 1 | +;; this is loaded as !(import &self fileio) |
| 2 | + |
| 3 | +; This add ther type to the type system |
| 4 | +(: FileHandle Type) |
| 5 | +; the system level get-type is nondetermninistic therefore returns all willing participants |
| 6 | +(= (get-type $obj) |
| 7 | + (call-for! FileHandle ; call for returns the first argument |
| 8 | + (atomic $obj) ; stream might return an error if obj is not atomic |
| 9 | + (stream $obj))) ; if obj is a stream we are calling it a filehandle since prolog streams already support this api |
| 10 | + |
| 11 | +(= (parse-mode $chars) |
| 12 | + (call-for! $mode |
| 13 | + (or |
| 14 | + (and (memberchk r $chars) (memberchk w $chars) (= $mode update)) |
| 15 | + (and (memberchk a $chars) (= $mode append)) |
| 16 | + (and (memberchk w $chars) (= $mode write)) |
| 17 | + (and (memberchk r $chars) (= $mode read)) |
| 18 | + (= $mode read)))) |
| 19 | + |
| 20 | +(= (handle-create-options $path $chars) |
| 21 | + (call-unit |
| 22 | + (if-then (and (memberchk n $chars) (exists_file $path)) |
| 23 | + (throw (error (file_exists_error $path)))) |
| 24 | + (if-then (and (memberchk c $chars) (not (exists_file $path))) |
| 25 | + (setup_call_cleanup (open $path write $s) (close $s) true)) |
| 26 | + (if-then (and (memberchk t $chars) (exists_file $path)) |
| 27 | + (setup_call_cleanup (open $path write $s) (close $s) true)))) |
| 28 | + |
| 29 | +(@doc file-open! |
| 30 | + (@desc "Function takes path to the file and open options (r, w, c, a, t) both in form of string, creates filehandle and |
| 31 | + returns it") |
| 32 | + (@params ( |
| 33 | + (@param "Filepath (string atom)") |
| 34 | + (@param "Open options (string atom), r - read, w - write, c - create if file doesn't exist, a - append to file, |
| 35 | + t - truncate file"))) |
| 36 | + (@return "Filehandle or error if combination of path and open options is wrong (e.g. file doesn't exist and no 'c' |
| 37 | + in options; or 'rc' option provided, since 'c' demands for 'w')")) |
| 38 | +(: file-open! (-> String String FileHandle)) |
| 39 | +; tells the compiler to return only the first successful clause |
| 40 | +(iz file-open! Deterministic) |
| 41 | +(= (file-open! $fpath $opt) |
| 42 | + (call-for! $stream |
| 43 | + (string_chars $opt $chars) |
| 44 | + (any_to_atom $fpath $path) |
| 45 | + (= $mode (parse-mode $chars)) |
| 46 | + (handle-create-options $path $chars) |
| 47 | + (open $path $mode $stream [ (type text) ]))) |
| 48 | +(= (file-open-err! $path $opt) |
| 49 | + (call-for! $_err |
| 50 | + (format (string $msg) |
| 51 | + "Failed to open file with provided path=~w and options=~w" |
| 52 | + [$path $opt]) |
| 53 | + (throw (error (file_open_error $msg))))) |
| 54 | + |
| 55 | +(@doc file-read-to-string! |
| 56 | + (@desc "Function takes filehandle provided by file-open! reads its content from current cursor place till the end of |
| 57 | + file and returns content in form of string.") |
| 58 | + (@params ( |
| 59 | + (@param "Filehandle"))) |
| 60 | + (@return "File's content")) |
| 61 | +(: file-read-to-string! (-> FileHandle String)) |
| 62 | +(= (file-read-to-string! $stream) |
| 63 | + (call-fn read_string $stream $_ )) |
| 64 | + |
| 65 | +(@doc file-write! |
| 66 | + (@desc "Function takes filehandle provided by file-open!, content to be written (string atom) and puts content into |
| 67 | + file associated with filehandle") |
| 68 | + (@params ( |
| 69 | + (@param "Filehandle") |
| 70 | + (@param "Content (string atom)"))) |
| 71 | + (@return "Unit atom")) |
| 72 | +(: file-write! (-> FileHandle String Unit)) |
| 73 | +(= (file-write! $stream $content) |
| 74 | + (call-unit |
| 75 | + (write $stream $content) |
| 76 | + (flush_output $stream))) |
| 77 | + |
| 78 | +(@doc file-seek! |
| 79 | + (@desc "Function takes filehandle provided by file-open! and desired cursor position (number) and sets cursor to |
| 80 | + provided position") |
| 81 | + (@params ( |
| 82 | + (@param "Filehandle") |
| 83 | + (@param "Desired cursor position (number)"))) |
| 84 | + (@return "Unit atom")) |
| 85 | +(: file-seek! (-> FileHandle Number Unit)) |
| 86 | +(= (file-seek! $stream $offset) |
| 87 | + (call-unit |
| 88 | + (seek $stream $offset bof $_))) |
| 89 | + |
| 90 | +(@doc file-read-exact! |
| 91 | + (@desc "Function takes filehandle provided by file-open! and desired number of bytes to read (number), reads content |
| 92 | + of file from current cursor position (number of read bytes <= input number of bytes to read) and returns it in form of |
| 93 | + string") |
| 94 | + (@params ( |
| 95 | + (@param "Filehandle") |
| 96 | + (@param "Number of bytes to read"))) |
| 97 | + (@return "File's content")) |
| 98 | +(: file-read-exact! (-> FileHandle Number String)) |
| 99 | +(= (file-read-exact! $stream $bytes) |
| 100 | + (call-for! $content ; runs this code returning the binding of $content |
| 101 | + (read_string $stream $bytes $content))) |
| 102 | + |
| 103 | +(@doc file-get-size! |
| 104 | + (@desc "Function takes filehandle provided by file-open! and returns size of file") |
| 105 | + (@params ( |
| 106 | + (@param "Filehandle"))) |
| 107 | + (@return "Size of file")) |
| 108 | +(: file-get-size! (-> FileHandle Number)) |
| 109 | +(= (file-get-size! $stream) |
| 110 | + (call-for! $size ; runs this code returning the binding of $size |
| 111 | + (stream_property $stream (file_name $file)) |
| 112 | + (size_file $file $size))) |
| 113 | + |
| 114 | +(@doc file-close! |
| 115 | + (@desc "Function takes filehandle provided by file-open! and closes it") |
| 116 | + (@params ( |
| 117 | + (@param "Filehandle"))) |
| 118 | + (@return "Unit atom")) |
| 119 | +(: file-close! (-> FileHandle Unit)) |
| 120 | +(= (file-close! $stream) |
| 121 | + (call-unit |
| 122 | + (close $stream))) |
| 123 | + |
| 124 | +; Load system libs (not required .. but more here for reference) |
| 125 | +;!(call-unit |
| 126 | +; (use_module (library apply)) |
| 127 | +; (use_module (library filesex))) |
| 128 | + |
| 129 | + |
0 commit comments