]> git.neil.brown.name Git - edlib.git/commitdiff
Add 'misc' core code: text accumulation buffer.
authorNeilBrown <neil@brown.name>
Wed, 18 Nov 2015 02:46:02 +0000 (13:46 +1100)
committerNeilBrown <neil@brown.name>
Wed, 18 Nov 2015 02:46:02 +0000 (13:46 +1100)
This allows us to easily build strings for temporary uses.

Signed-off-by: NeilBrown <neil@brown.name>
Makefile
core-misc.c [new file with mode: 0644]
misc.h [new file with mode: 0644]

index 6eee64bc54077e3c68312e055750c1cc6b721640..0e7a2d4f2e1b380c6128faba73524eea2db7f9b7 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -11,7 +11,7 @@ all: dirs edlib checksym lib shared
 
 OBJ = O/mainloop.o
 LIBOBJ = O/core-mark.o O/core-doc.o O/core-editor.o O/core-attr.o \
-       O/core-keymap.o O/core-pane.o
+       O/core-keymap.o O/core-pane.o O/core-misc.o
 SHOBJ = O/doc-text.o O/doc-dir.o \
        O/render-text.o O/render-hex.o O/render-dir.o \
        O/lib-view.o O/lib-tile.o O/lib-popup.o O/lib-line-count.o O/lib-keymap.o \
@@ -21,7 +21,7 @@ SHOBJ = O/doc-text.o O/doc-dir.o \
 XOBJ = O/rexel.o O/emacs-search.o
 
 SO = $(patsubst O/%.o,lib/edlib-%.so,$(SHOBJ))
-H = list.h core.h
+H = list.h core.h misc.h
 edlib: $(OBJ) lib/libedlib.so
        $(CC) $(CPPFLAGS) $(CFLAGS) -rdynamic -Wl,--disable-new-dtags -o edlib $(OBJ) -Llib -Wl,-rpath=`pwd`/lib -ledlib $(LDLIBS)
 
diff --git a/core-misc.c b/core-misc.c
new file mode 100644 (file)
index 0000000..75572d0
--- /dev/null
@@ -0,0 +1,52 @@
+/*
+ * Copyright Neil Brown <neil@brown.name> 2015
+ * May be distributed under terms of GPLv2 - see file:COPYING
+ *
+ * Assorted utility functions used by edlib
+ *
+ */
+#include <stdlib.h>
+#include <string.h>
+#include <wchar.h>
+
+#include "misc.h"
+
+void buf_init(struct buf *b)
+{
+       b->b = NULL;
+       b->len = 0;
+       b->size = 0;
+}
+
+void buf_concat_len(struct buf *b, char *s, int l)
+{
+
+       if (b->len + l >= b->size) {
+               while (b->len + l >= b->size)
+                       b->size += 128;
+               b->b = realloc(b->b, b->size);
+       }
+       strncpy(b->b + b->len, s, l);
+       b->len += l;
+       b->b[b->len] = 0;
+}
+
+void buf_concat(struct buf *b, char *s)
+{
+       int l = strlen(s);
+       buf_concat_len(b, s, l);
+}
+
+void buf_append(struct buf *b, wchar_t wch)
+{
+       char t[5];
+       mbstate_t ps = {0};
+       size_t l;
+
+       if (wch <= 0x7f) {
+               t[0] = wch;
+               l = 1;
+       } else
+               l = wcrtomb(t, wch, &ps);
+       buf_concat_len(b, t, l);
+}
diff --git a/misc.h b/misc.h
new file mode 100644 (file)
index 0000000..62a114e
--- /dev/null
+++ b/misc.h
@@ -0,0 +1,24 @@
+/*
+ * Copyright Neil Brown <neil@brown.name> 2015
+ * May be distributed under terms of GPLv2 - see file:COPYING
+ *
+ * Assorted utility functions used by edlib
+ *
+ */
+
+struct buf {
+       char *b;
+       int len;
+       int size;
+};
+
+void buf_init(struct buf *b);
+void buf_concat(struct buf *b, char *s);
+void buf_concat_len(struct buf *b, char *s, int l);
+void buf_append(struct buf *b, wchar_t wch);
+static inline char *buf_final(struct buf *b)
+{
+       if (b->b)
+               b->b[b->len] = 0;
+       return b->b;
+}