diff options
author | Nicholas <nbnoll@eml.cc> | 2021-11-20 10:53:19 -0800 |
---|---|---|
committer | Nicholas <nbnoll@eml.cc> | 2021-11-20 10:53:19 -0800 |
commit | a9bfe650038afea8b751175cac16f6027345e45f (patch) | |
tree | 9a7f9feb76a64bb3efe573036d80b7bdbf8a59a5 /include/base/io.h | |
parent | 1c8d4e69205fd875f6bec3fa3bd929c2e7f52f62 (diff) |
Chore: reorganize libutf and libfmt into base
I found the split to be arbitrary. Better to include the functionality
in the standard library. I also split the headers to allow for more
granular inclusion (but the library is still monolithic). The only
ugliness is the circular dependency introduced with libutf's generated
functions. We put explicit prereqs with the necessary object files
instead.
Diffstat (limited to 'include/base/io.h')
-rw-r--r-- | include/base/io.h | 141 |
1 files changed, 141 insertions, 0 deletions
diff --git a/include/base/io.h b/include/base/io.h new file mode 100644 index 0000000..f8c0bcc --- /dev/null +++ b/include/base/io.h @@ -0,0 +1,141 @@ +#pragma once + +enum SeekPos +{ + seek·cur = SEEK_CUR, + seek·set = SEEK_SET, + seek·end = SEEK_END +}; + +typedef struct io·Reader +{ + int (*read)(void*, int sz, int n, void *buf); +} io·Reader; +extern io·Reader sys·Reader; + +typedef struct io·Peeker +{ + byte (*get)(void*); + int (*unget)(void*, byte); +} io·Peeker; +extern io·Peeker sys·Peeker; + +typedef struct io·Seeker +{ + int (*seek)(void *skr, long off, enum SeekPos whence); + long (*tell)(void *skr); +} io·Seeker; +extern io·Seeker sys·Seeker; + +typedef struct io·SeekReader +{ + io·Seeker; + io·Reader; +} io·SeekReader; +extern io·SeekReader sys·SeekReader; + +typedef struct io·PeekReader +{ + io·Reader; + io·Peeker; +} io·PeekReader; +extern io·PeekReader sys·PeekReader; + +typedef struct io·Writer +{ + int (*write)(void*, int sz, int n, void *buf); +} io·Writer; +extern io·Writer sys·Writer; + +typedef struct io·Putter +{ + int (*put) (void*, byte); + int (*puts)(void*, string); +} io·Putter; +extern io·Putter sys·Putter; + +typedef struct io·PutWriter +{ + io·Writer; + io·Putter; +} io·PutWriter; +extern io·PutWriter sys·PutWriter; + +typedef struct io·ReadWriter +{ + io·Reader; + io·Writer; +} io·ReadWriter; +extern io·ReadWriter sys·ReadWriter; + +/* XXX: change casing */ +enum +{ + ReadOK = R_OK, + WriteOK = W_OK, + ExecOK = X_OK, +}; + +/* XXX(deprecated): file handling */ + +typedef FILE io·Stream; +typedef struct stat io·Stat; + +io·Stream *io·open(byte *name, byte *mode); +int io·fd(io·Stream *s); +int io·stat(io·Stream *s, io·Stat *buf); +int io·close(io·Stream *s); +byte io·getbyte(io·Stream *s); +int io·ungetbyte(io·Stream *s, byte c); +int io·read(io·Stream *s, int sz, int n, void *buf); +int io·readln(io·Stream *s, int n, byte *buf); +int io·putbyte(io·Stream *s, byte c); +int io·putstring(io·Stream *s, string str); +int io·write(io·Stream *s, int sz, int n, void *buf); +int io·flush(io·Stream *s); +int io·seek(io·Stream *s, long off, enum SeekPos whence); +long io·tell(io·Stream *s); + +/* basic os helpers */ + +int os·exists(byte *path, int flag); +byte *os·dirname(byte *path); +byte *os·basename(byte *path); +int os·sep(void); + +/* io interfaces */ +/* buffered i/o */ +typedef struct io·Buffer io·Buffer; + +enum +{ + bufio·size = 2*4096, + bufio·ungets = 8, + bufio·eof = -1, + bufio·err = -2, + + bufio·nil = 1 << 0, + bufio·rdr = 1 << 1, + bufio·wtr = 1 << 2, + bufio·end = 1 << 3, +}; + +struct io·Buffer +{ + int state; + int runesize; + void *h; + union { + io·Reader rdr; + io·Writer wtr; + }; + vlong size; + byte *beg, *pos, *end; + byte buf[bufio·size + bufio·ungets]; +}; + +int bufio·initreader(io·Buffer *buf, io·Reader rdr, void *h); +void bufio·finireader(io·Buffer *buf); +int bufio·getbyte(io·Buffer *buf); +int bufio·ungetbyte(io·Buffer *buf, byte c); +int bufio·read(io·Buffer *buf, int sz, int n, void *out); |