aboutsummaryrefslogtreecommitdiff
path: root/sys/libfmt/buffer.c
diff options
context:
space:
mode:
authorNicholas Noll <nbnoll@eml.cc>2021-11-11 14:49:35 -0800
committerNicholas Noll <nbnoll@eml.cc>2021-11-11 14:49:35 -0800
commitc65794b50b1bc729e7a4e940b76a973afa3030b9 (patch)
treebc435fea0f3179ac9a4b8de9fdaf993dade9e4ff /sys/libfmt/buffer.c
parentcc7d219e080263da9813e570436bfd34ab8b1bff (diff)
feat: libfmt prototype added from plan9
Diffstat (limited to 'sys/libfmt/buffer.c')
-rw-r--r--sys/libfmt/buffer.c60
1 files changed, 60 insertions, 0 deletions
diff --git a/sys/libfmt/buffer.c b/sys/libfmt/buffer.c
new file mode 100644
index 0000000..0099e72
--- /dev/null
+++ b/sys/libfmt/buffer.c
@@ -0,0 +1,60 @@
+#include "internal.h"
+
+static int
+flush(fmt·State *io)
+{
+ int n;
+ char *s;
+
+ void *heap = io->heap;
+ mem·Reallocator mem = io->mem;
+
+ if(!io->buffer.beg)
+ return 0;
+
+ n = 2*(uintptr)io->file;
+ s = io->buffer.beg;
+
+ io->buffer.beg = mem.realloc(heap, io->buffer.beg, n, 1);
+ if(!io->buffer.beg){
+ io->file = io->buffer.cur = io->buffer.end = nil;
+ mem.free(heap, s);
+ return 0;
+ }
+ io->file = (void*)(uintptr)n;
+ io->buffer.cur = io->buffer.beg + (io->buffer.cur - s);
+ io->buffer.end = io->buffer.beg + n - 1;
+
+ return 1;
+}
+
+int
+fmt·make(mem·Reallocator mem, void *heap, fmt·State *io)
+{
+ int n;
+
+ memset(io, 0, sizeof(*io));
+
+ n = 32;
+ io->buffer.beg = io->buffer.cur = mem.alloc(heap, n, 1);
+ if(!io->buffer.beg)
+ return -1;
+ io->buffer.end = io->buffer.beg + n - 1;
+
+ io->flush = flush;
+ io->file = (void*)(uintptr)n;
+ io->n = 0;
+
+ fmt·setlocale(io, nil, nil, nil);
+ return 0;
+}
+
+void
+fmt·free(fmt·State *io)
+{
+ void *heap = io->heap;
+ mem·Reallocator mem = io->mem;
+
+ mem.free(heap, io->buffer.beg);
+ io->buffer.beg = io->buffer.cur = io->buffer.end = nil;
+}