From fa25c8f3df6791727b9384c9b405c996ac68b8ab Mon Sep 17 00:00:00 2001 From: Nicholas Noll Date: Fri, 15 May 2020 18:11:58 -0700 Subject: feat: added buffered io to libn --- include/libn.h | 54 +++++++++++++++++++++++++++++++++++++++------- include/libn/macro/qsort.h | 2 +- 2 files changed, 47 insertions(+), 9 deletions(-) (limited to 'include') diff --git a/include/libn.h b/include/libn.h index 4efe3c1..c57868c 100644 --- a/include/libn.h +++ b/include/libn.h @@ -120,7 +120,7 @@ typedef struct str·Hdr // Perhaps break into own unit // TODO: Add to(upper|lower|title) -typedef uint32 Rune; +typedef uint32 rune; enum { @@ -134,14 +134,14 @@ enum /* utf8 helpers */ int utf8·fullrune(byte *s, int n); byte *utf8·findrune(byte *s, long i); -int utf8·chartorune(Rune *r, byte *s); -int utf8·runetochar(byte *s, Rune *r); +int utf8·chartorune(rune *r, byte *s); +int utf8·runetochar(byte *s, rune *r); int utf8·len(byte *s); -int utf8·runelen(Rune r); -int utf8·isletter(Rune r); -int utf8·isdigit(Rune r); -int utf8·isspace(Rune r); -int utf8·istitle(Rune r); +int utf8·runelen(rune r); +int utf8·isletter(rune r); +int utf8·isdigit(rune r); +int utf8·isspace(rune r); +int utf8·istitle(rune r); /* string helpers */ string str·makecap(const byte *s, vlong len, vlong cap); @@ -240,6 +240,44 @@ typedef struct io·ReadWriter io·Writer; } io·ReadWriter; +/* 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]; +}; + +error bufio·initreader(io·Buffer *buf, io·Reader rdr, void *h); +void bufio·finireader(io·Buffer *buf); +int bufio·getbyte(io·Buffer *buf); +error bufio·ungetbyte(io·Buffer *buf, byte c); +rune bufio·getrune(io·Buffer *buf); +error bufio·ungetrune(io·Buffer *buf, rune r); +int bufio·read(io·Buffer *buf, int sz, int n, void *out); + // ----------------------------------------------------------------------------- // memory mapped files diff --git a/include/libn/macro/qsort.h b/include/libn/macro/qsort.h index 2ff964a..6d0acaa 100644 --- a/include/libn/macro/qsort.h +++ b/include/libn/macro/qsort.h @@ -88,4 +88,4 @@ ENDOUTER: \ for (j = i; j > 0 && QLESS(j, j-1); j--) { \ QSWAP(j, j-1); \ } \ - } \ + } -- cgit v1.2.1