From 4fbf50ddfeeb0e9d99d271e31dd35d545fb42ab6 Mon Sep 17 00:00:00 2001 From: Nicholas Noll Date: Tue, 21 Apr 2020 10:40:03 -0700 Subject: feat: added wrapper for io --- sys/libn/bufio.c | 33 ++++++++++++++++++++++++++ sys/libn/coro.c | 16 ++++++------- sys/libn/io.c | 71 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 112 insertions(+), 8 deletions(-) create mode 100644 sys/libn/bufio.c create mode 100644 sys/libn/io.c (limited to 'sys') diff --git a/sys/libn/bufio.c b/sys/libn/bufio.c new file mode 100644 index 0000000..6b15760 --- /dev/null +++ b/sys/libn/bufio.c @@ -0,0 +1,33 @@ +#include +#include + +enum +{ + BUF·size = 8 * 2048, + BUF·ungets = 8, + + BUF·bad = -2, + BUF·eof = -1, + + BUF·inactive = 0, + BUF·rdractive, + BUF·wtractive, + + BUF·END, +} bmode; + +typedef struct Buffer +{ + uint8 state; + vlong off; + vlong size; + + byte *bbuf, *ebuf; + byte b[BUF·size + BUF·ungets]; +} Buffer; + +struct bufio·Stream +{ + Stream *s; + Buffer buf; +}; diff --git a/sys/libn/coro.c b/sys/libn/coro.c index 2e50164..2ad629b 100644 --- a/sys/libn/coro.c +++ b/sys/libn/coro.c @@ -4,8 +4,8 @@ // ----------------------------------------------------------------------- // Assembly routines -extern void _newcoro(coro *co, uintptr (*func)(coro*, uintptr), void *stk); -extern uintptr _coroyield(coro *co, uintptr arg); +extern void _newcoro(Coro *co, uintptr (*func)(Coro*, uintptr), void *stk); +extern uintptr _coroyield(Coro *co, uintptr arg); // ----------------------------------------------------------------------- // Globals @@ -16,7 +16,7 @@ extern uintptr _coroyield(coro *co, uintptr arg); // C interface /* Co-routine context */ -struct coro +struct Coro { void* sp; void* bp; @@ -24,14 +24,14 @@ struct coro void* user; }; -coro* -coro·new(uintptr stk, uintptr (*func)(coro*, uintptr)) +Coro* +coro·new(uintptr stk, uintptr (*func)(Coro*, uintptr)) { if (!func) return nil; if (stk == 0) stk = 8192; byte *block = malloc(stk); - coro *co = (coro*)&block[stk - sizeof(coro)]; + Coro *co = (Coro*)&block[stk - sizeof(Coro)]; co->bp = block; co->size = stk; @@ -40,7 +40,7 @@ coro·new(uintptr stk, uintptr (*func)(coro*, uintptr)) } error -coro·free(coro *co) +coro·free(Coro *co) { enum { @@ -60,7 +60,7 @@ coro·free(coro *co) } uintptr -coro·yield(coro *c, uintptr arg) +coro·yield(Coro *c, uintptr arg) { return _coroyield(c, arg); } diff --git a/sys/libn/io.c b/sys/libn/io.c new file mode 100644 index 0000000..789bc7b --- /dev/null +++ b/sys/libn/io.c @@ -0,0 +1,71 @@ +#include +#include + +// ----------------------------------------------------------------------- +// Open/Close + +Stream* +io·open(byte *name, byte *mode) +{ + return fopen(name, mode); +} + +error +io·close(Stream *s) +{ + return fclose(s); +} + +// ----------------------------------------------------------------------- +// Reading + +byte +io·getbyte(Stream *s) +{ + return fgetc(s); +} + +vlong +io·read(Stream *s, int sz, int n, void *buf) +{ + return fread(buf, sz, n, s); +} + +int +io·readln(Stream *s, int n, byte* buf) +{ + byte* b; + b = fgets(buf, n+1, s); + + return strlen(buf); +} + +// ----------------------------------------------------------------------- +// Writing + +error +io·putbyte(Stream *s, byte c) +{ + return fputc(c, s); +} + +int +io·putstring(Stream *s, string str) +{ + return fputs(str, s); +} + +vlong +io·write(Stream *s, int sz, int n, void *buf) +{ + return fwrite(buf, sz, n, s); +} + +// ----------------------------------------------------------------------- +// Seek + +int +io·seek(Stream *s, long off, enum SeekPos origin) +{ + return fseek(s, off, origin); +} -- cgit v1.2.1