summaryrefslogtreecommitdiff
path: root/core/readbuf.c
diff options
context:
space:
mode:
authorIan Moffett <ian@mirocom.org>2026-05-23 01:28:05 -0400
committerIan Moffett <ian@mirocom.org>2026-05-23 01:28:05 -0400
commitc5f8f95059504748f07e0d3ca9d978669622428c (patch)
treecc64c48bb1e93c2df466f4fb007912628c319c91 /core/readbuf.c
initial commit
Signed-off-by: Ian Moffett <ian@mirocom.org>
Diffstat (limited to 'core/readbuf.c')
-rw-r--r--core/readbuf.c46
1 files changed, 46 insertions, 0 deletions
diff --git a/core/readbuf.c b/core/readbuf.c
new file mode 100644
index 0000000..ee10362
--- /dev/null
+++ b/core/readbuf.c
@@ -0,0 +1,46 @@
+/*
+ * Copyright (c) 2026, Chloe M.
+ * Provided under the BSD-3 clause
+ */
+
+#include <stdint.h>
+#include <stddef.h>
+#include <errno.h>
+#include <unistd.h>
+#include "cescal/readbuf.h"
+
+int
+readbuf_init(struct readbuf *rb)
+{
+ if (rb == NULL) {
+ errno = EINVAL;
+ return -1;
+ }
+
+ rb->size = 0;
+ rb->tail = 0;
+ return 0;
+}
+
+char
+readbuf_read(struct readbuf *rb, int fd)
+{
+ ssize_t nread = -1;
+
+ if (rb == NULL || fd < 0) {
+ errno = EINVAL;
+ return '\0';
+ }
+
+#define N_RESIDUAL(rb) ((rb)->size - (rb)->tail)
+ if (N_RESIDUAL(rb) == 0 || rb->size == 0) {
+ if ((nread = read(fd, rb->buf, READBUF_CAP)) <= 0)
+ return nread;
+
+ rb->size = nread;
+ rb->tail = 0;
+ }
+#undef N_RESIDUAL
+
+ return rb->buf[rb->tail++];
+}