summaryrefslogtreecommitdiffstats
path: root/buffer.c
diff options
context:
space:
mode:
Diffstat (limited to 'buffer.c')
-rw-r--r--buffer.c127
1 files changed, 127 insertions, 0 deletions
diff --git a/buffer.c b/buffer.c
index 6ceaacf..92b10e5 100644
--- a/buffer.c
+++ b/buffer.c
@@ -819,3 +819,130 @@ valign4 (const struct buffer *buf, const char *file, const int line)
}
}
#endif
+
+/*
+ * struct buffer_list
+ */
+
+#ifdef ENABLE_BUFFER_LIST
+
+struct buffer_list *
+buffer_list_new (const int max_size)
+{
+ struct buffer_list *ret;
+ ALLOC_OBJ_CLEAR (ret, struct buffer_list);
+ ret->max_size = max_size;
+ ret->size = 0;
+ return ret;
+}
+
+void
+buffer_list_free (struct buffer_list *ol)
+{
+ buffer_list_reset (ol);
+ free (ol);
+}
+
+bool
+buffer_list_defined (const struct buffer_list *ol)
+{
+ return ol->head != NULL;
+}
+
+void
+buffer_list_reset (struct buffer_list *ol)
+{
+ struct buffer_entry *e = ol->head;
+ while (e)
+ {
+ struct buffer_entry *next = e->next;
+ free_buf (&e->buf);
+ free (e);
+ e = next;
+ }
+ ol->head = ol->tail = NULL;
+ ol->size = 0;
+}
+
+void
+buffer_list_push (struct buffer_list *ol, const unsigned char *str)
+{
+ if (!ol->max_size || ol->size < ol->max_size)
+ {
+ struct buffer_entry *e;
+ ALLOC_OBJ_CLEAR (e, struct buffer_entry);
+
+ ++ol->size;
+ if (ol->tail)
+ {
+ ASSERT (ol->head);
+ ol->tail->next = e;
+ }
+ else
+ {
+ ASSERT (!ol->head);
+ ol->head = e;
+ }
+ e->buf = string_alloc_buf ((const char *) str, NULL);
+ ol->tail = e;
+ }
+}
+
+const struct buffer *
+buffer_list_peek (struct buffer_list *ol)
+{
+ if (ol->head)
+ return &ol->head->buf;
+ else
+ return NULL;
+}
+
+static void
+buffer_list_pop (struct buffer_list *ol)
+{
+ if (ol->head)
+ {
+ struct buffer_entry *e = ol->head->next;
+ free_buf (&ol->head->buf);
+ free (ol->head);
+ ol->head = e;
+ --ol->size;
+ if (!e)
+ ol->tail = NULL;
+ }
+}
+
+void
+buffer_list_advance (struct buffer_list *ol, int n)
+{
+ if (ol->head)
+ {
+ struct buffer *buf = &ol->head->buf;
+ ASSERT (buf_advance (buf, n));
+ if (!BLEN (buf))
+ buffer_list_pop (ol);
+ }
+}
+
+struct buffer_list *
+buffer_list_file (const char *fn, int max_line_len)
+{
+ FILE *fp = fopen (fn, "r");
+ struct buffer_list *bl = NULL;
+
+ if (fp)
+ {
+ char *line = (char *) malloc (max_line_len);
+ if (line)
+ {
+ bl = buffer_list_new (0);
+ while (fgets (line, max_line_len, fp) != NULL)
+ buffer_list_push (bl, (unsigned char *)line);
+ free (line);
+ }
+ fclose (fp);
+ }
+ return bl;
+}
+
+#endif