summaryrefslogtreecommitdiff
path: root/core/tokbuf.c
diff options
context:
space:
mode:
authorChloe M. <chloe@mirocom.org>2026-05-23 02:57:50 -0400
committerChloe M. <chloe@mirocom.org>2026-05-23 02:57:50 -0400
commit7ca45808939579b427f32641b0dcb9cb585b8e80 (patch)
tree8b795d1b1f97009b6b1cb3faf1d21bb0c88cd494 /core/tokbuf.c
parent74e2e8c772d0f88da6684918f782b37156f10fb3 (diff)
core: Add token buffer
Signed-off-by: Chloe M. <chloe@mirocom.org>
Diffstat (limited to 'core/tokbuf.c')
-rw-r--r--core/tokbuf.c62
1 files changed, 62 insertions, 0 deletions
diff --git a/core/tokbuf.c b/core/tokbuf.c
new file mode 100644
index 0000000..e880c15
--- /dev/null
+++ b/core/tokbuf.c
@@ -0,0 +1,62 @@
+/*
+ * Copyright (c) 2026, Chloe M.
+ * Provided under the BSD-3 clause
+ */
+
+#include <sys/types.h>
+#include <stdint.h>
+#include <stddef.h>
+#include <string.h>
+#include <errno.h>
+#include "cescal/tokbuf.h"
+
+int
+tokbuf_init(struct tokbuf *tokbuf)
+{
+ if (tokbuf == NULL) {
+ errno = EINVAL;
+ return -1;
+ }
+
+ memset(tokbuf->buf, 0, sizeof(tokbuf->buf));
+ tokbuf->head = 0;
+ return 0;
+}
+
+int
+tokbuf_push(struct tokbuf *tokbuf, struct token *tok)
+{
+ if (tokbuf == NULL || tok == NULL) {
+ return -1;
+ }
+
+ if ((tokbuf->head++) >= TOKBUF_CAP) {
+ tokbuf->head = 0;
+ }
+
+ tokbuf->buf[tokbuf->head] = *tok;
+ return 0;
+}
+
+int
+tokbuf_noff(struct tokbuf *tokbuf, size_t noff, struct token *res)
+{
+ ssize_t off;
+
+ if (tokbuf == NULL || res == NULL) {
+ errno = EINVAL;
+ return -1;
+ }
+
+ if (noff == 0) {
+ *res = tokbuf->buf[tokbuf->head + 1];
+ return 0;
+ }
+
+ if ((off = (tokbuf->head - noff + 1)) < 0) {
+ off = 0;
+ }
+
+ *res = tokbuf->buf[off];
+ return 0;
+}