commit 197736f5ab3283803a6ff9925ec9418decb746a1
parent 55128841b0b84282c27239d9b0ec920f0546a2d5
Author: Jan Klemkow <j.klemkow@wemelug.de>
Date: Fri, 23 Oct 2015 23:09:48 +0200
add slackline stub
Diffstat:
A | slackline.c | | | 60 | ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
A | slackline.h | | | 23 | +++++++++++++++++++++++ |
2 files changed, 83 insertions(+), 0 deletions(-)
diff --git a/slackline.c b/slackline.c
@@ -0,0 +1,60 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "slackline.h"
+
+struct slackline *
+sl_init(void)
+{
+ struct slackline *sl = malloc(sizeof *sl);
+
+ if (sl == NULL)
+ return NULL;
+
+ sl->bufsize = BUFSIZ;
+ sl->buf = malloc(sl->bufsize);
+ sl->buf[0] = '\0';
+ sl->len = 0;
+ sl->cur = 0;
+
+ return sl;
+}
+
+void
+sl_free(struct slackline *sl)
+{
+ free(sl->buf);
+ free(sl);
+}
+
+int
+sl_keystroke(struct slackline *sl, int key)
+{
+ if (sl == NULL || sl->len < sl->cur)
+ return -1;
+
+ /* add character to buffer */
+ if (key >= 32 && key <= 127) {
+ if (sl->cur < sl->len) {
+ memmove(sl->buf + sl->cur + 1, sl->buf + sl->cur,
+ sl->len - sl->cur);
+ sl->buf[sl->cur++] = key;
+ } else {
+ sl->buf[sl->cur++] = key;
+ sl->buf[sl->cur] = '\0';
+ }
+
+ return 0;
+ }
+
+ /* handle ctl keys */
+ switch (key) {
+ case 8: /* backspace */
+ sl->cur--;
+ sl->buf[sl->cur] = '\0';
+ break;
+ }
+
+ return 0;
+}
diff --git a/slackline.h b/slackline.h
@@ -0,0 +1,23 @@
+#ifndef SLACKLIINE_H
+#define SLACKLIINE_H
+
+/*
+ * +-+-+-+-+-+
+ * |c|c|c|0|0|
+ * +-+-+-+-+-+
+ * ^ ^
+ * len bufsize
+ */
+
+struct slackline {
+ char *buf;
+ size_t bufsize;
+ size_t len;
+ size_t cur;
+};
+
+struct slackline * sl_init(void);
+void sl_free(struct slackline *);
+int sl_keystroke(struct slackline *sl, int key);
+
+#endif