diff options
author | Kartik Agaram <vc@akkartik.com> | 2020-06-05 14:09:07 -0700 |
---|---|---|
committer | Kartik Agaram <vc@akkartik.com> | 2020-06-05 14:09:07 -0700 |
commit | fbb9b0489321e5dca0ee91c355b5839e133def90 (patch) | |
tree | e947d7f761872b398db5fbb8a3eff805b5b0c25a /prototypes/browse/20/file-state.mu | |
parent | da9f0082dbd64590bfa37c1d2f77d0b871b207bb (diff) | |
download | mu-fbb9b0489321e5dca0ee91c355b5839e133def90.tar.gz |
6471
Diffstat (limited to 'prototypes/browse/20/file-state.mu')
-rw-r--r-- | prototypes/browse/20/file-state.mu | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/prototypes/browse/20/file-state.mu b/prototypes/browse/20/file-state.mu new file mode 100644 index 00000000..35f40229 --- /dev/null +++ b/prototypes/browse/20/file-state.mu @@ -0,0 +1,44 @@ +type file-state { + source: (handle buffered-file) + eof?: boolean +} + +fn init-file-state _self: (addr file-state), filename: (addr array byte) { + var self/eax: (addr file-state) <- copy _self + load-file self, filename + var eof/eax: (addr boolean) <- get self, eof? + copy-to *eof, 0 # false +} + +fn load-file _self: (addr file-state), filename: (addr array byte) { + var self/eax: (addr file-state) <- copy _self + var out/esi: (addr handle buffered-file) <- get self, source + open filename, 0, out # 0 = read mode +} + +fn next-char _self: (addr file-state) -> result/eax: byte { + var self/ecx: (addr file-state) <- copy _self + var source/eax: (addr handle buffered-file) <- get self, source + var in/eax: (addr buffered-file) <- lookup *source + result <- read-byte-buffered in + # if result == EOF, set eof? + compare result, 0xffffffff # EOF marker + { + var eof/ecx: (addr boolean) <- get self, eof? + copy-to *eof, 1 # true + } +} + +fn done-reading? _self: (addr file-state) -> result/eax: boolean { + var self/eax: (addr file-state) <- copy _self + var eof/eax: (addr boolean) <- get self, eof? + result <- copy *eof +} + +fn dump in: (addr buffered-file) { + var c/eax: byte <- read-byte-buffered in + compare c, 0xffffffff # EOF marker + break-if-= + print-byte c + loop +} |