This allows us to easily build strings for temporary uses.
Signed-off-by: NeilBrown <neil@brown.name>
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 \
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)
--- /dev/null
+/*
+ * 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);
+}
--- /dev/null
+/*
+ * 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;
+}