diff options
author | Thomas Voss <mail@thomasvoss.com> | 2024-03-31 17:22:46 +0200 |
---|---|---|
committer | Thomas Voss <mail@thomasvoss.com> | 2024-03-31 17:22:51 +0200 |
commit | d79a16a7d172ece3b01bf78ab7c6db02dc9cf267 (patch) | |
tree | 2fafeac48408d23825f79c7ccfe6d65a3d784eeb | |
parent | d11440309c97c02a9ab70d6b9367cbfdac53a211 (diff) |
Add mbio.h and freadrune()
-rw-r--r-- | README | 1 | ||||
-rw-r--r-- | include/mbio.h | 16 | ||||
-rw-r--r-- | lib/mbio/freadrune.c | 23 |
3 files changed, 40 insertions, 0 deletions
@@ -14,6 +14,7 @@ The headers as of now are: • dynarr.h — dynamic array implementation • errors.h — err.h-inspired diagnostics functions • macros.h — miscellaneous utility macros (MIN/MAX/lengthof/etc.) + • mbio.h — multibyte file I/O • mbstring.h — multibyte-strings • optparse.h — option parsing functions • rune.h — inttypes.h but for runes diff --git a/include/mbio.h b/include/mbio.h new file mode 100644 index 0000000..ef2fcd5 --- /dev/null +++ b/include/mbio.h @@ -0,0 +1,16 @@ +#ifndef MLIB_MBIOO_H +#define MLIB_MBIOO_H + +#include <stdio.h> + +#include "__charN_t.h" +#include "__rune.h" + +typedef struct { + char8_t _buf[4]; + int _fill; +} u8_io_state; + +int freadrune(rune *, u8_io_state *, FILE *); + +#endif /* !MLIB_MBIOO_H */ diff --git a/lib/mbio/freadrune.c b/lib/mbio/freadrune.c new file mode 100644 index 0000000..efa42b3 --- /dev/null +++ b/lib/mbio/freadrune.c @@ -0,0 +1,23 @@ +#include <macros.h> +#include <stdio.h> +#include <string.h> + +#include "mbio.h" +#include "mbstring.h" + +int +freadrune(rune *ch, u8_io_state *st, FILE *stream) +{ + size_t n, need; + need = lengthof(st->_buf) - st->_fill; + st->_fill += n = fread(st->_buf + st->_fill, 1, need, stream); + if (n < need && ferror(stream)) + return -1; + if (st->_fill == 0) + return 0; + + int w = u8tor(ch, st->_buf); + memmove(st->_buf, st->_buf + w, lengthof(st->_buf) - w); + st->_fill -= w; + return w; +} |