diff help2html/buffer.c @ 0:13d2b8934445

Import AnaGram (near-)release tree into Mercurial.
author David A. Holland
date Sat, 22 Dec 2007 17:52:45 -0500
parents
children
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/help2html/buffer.c	Sat Dec 22 17:52:45 2007 -0500
@@ -0,0 +1,58 @@
+#include <stdlib.h>
+#include <string.h>
+#include <assert.h>
+
+#include "must.h"
+#include "buffer.h"
+
+static void buffer_provide(struct buffer *b, size_t needed) {
+  if (needed <= b->allocsize) {
+    return;
+  }
+  size_t newalloc = b->allocsize*2;
+  if (newalloc < 16) {
+    newalloc = 16;
+  }
+  while (newalloc < needed) {
+    newalloc *= 2;
+  }
+
+  b->text = must_realloc(b->text, newalloc);
+  b->allocsize = newalloc;
+}
+
+void buffer_clear(struct buffer *b) {
+  b->len = 0;
+  b->text[b->len] = 0;
+}
+
+void buffer_init(struct buffer *b) {
+  b->allocsize = 16;
+  b->text = must_malloc(b->allocsize);
+  buffer_clear(b);
+}
+
+void buffer_cleanup(struct buffer *b) {
+  free(b->text);
+  b->text = NULL;
+  b->len = 0;
+  b->allocsize = 0;
+}
+
+void buffer_append(struct buffer *b, const char *t) {
+  size_t tlen = strlen(t);
+  buffer_provide(b, b->len + tlen + 1);
+  strcpy(b->text + b->len, t);
+  b->len += tlen;
+}
+
+void buffer_add(struct buffer *b, int ch) {
+  buffer_provide(b, b->len + 2);
+  b->text[b->len++] = ch;
+  b->text[b->len] = 0;
+}
+
+void buffer_start(struct buffer *b, int ch) {
+  buffer_clear(b);
+  buffer_add(b, ch);
+}