about summary refs log tree commit diff stats
diff options
context:
space:
mode:
-rw-r--r--src/ui/buffer.c79
-rw-r--r--src/ui/buffer.h18
-rw-r--r--src/ui/window.h2
3 files changed, 42 insertions, 57 deletions
diff --git a/src/ui/buffer.c b/src/ui/buffer.c
index 16919c08..60ee91dc 100644
--- a/src/ui/buffer.c
+++ b/src/ui/buffer.c
@@ -15,56 +15,39 @@
 #include "ui/window.h"
 #include "ui/buffer.h"
 
-ProfBuff*
+struct prof_buff_t {
+    GSList *entries;
+};
+
+static void _free_entry(ProfBuffEntry *entry);
+
+ProfBuff
 buffer_create()
 {
-    ProfBuff* new_buff = malloc(sizeof(struct prof_buff_t));
-    new_buff->wrap = 0;
-    new_buff->current = 0;
+    ProfBuff new_buff = malloc(sizeof(struct prof_buff_t));
+    new_buff->entries = NULL;
     return new_buff;
 }
 
 int
-buffer_size(ProfBuff* buffer)
+buffer_size(ProfBuff buffer)
 {
-    if(buffer->wrap == 1) {
-        return BUFF_SIZE;
-    } else {
-        return buffer->current;
-    }
+    return g_slist_length(buffer->entries);
 }
 
 void
-buffer_free(ProfBuff* buffer)
+buffer_free(ProfBuff buffer)
 {
-    int i = 0;
-    int imax = buffer->current;
-
-    if (buffer->wrap == 1) {
-        imax = BUFF_SIZE;
-    }
-
-    for (i = 0; i < imax; i++) {
-        free(buffer->entry[i].message);
-        free(buffer->entry[i].from);
-        free(buffer->entry[i].date_fmt);
-    }
-
+    g_slist_free_full(buffer->entries, (GDestroyNotify)_free_entry);
     free(buffer);
+    buffer = NULL;
 }
 
 void
-buffer_push(ProfBuff* buffer, const char show_char, const char * const date_fmt,
+buffer_push(ProfBuff buffer, const char show_char, const char * const date_fmt,
     int flags, int attrs, const char * const from, const char * const message)
 {
-    int *current = &(buffer->current);
-    ProfBuffEntry* e = &(buffer->entry[buffer->current]);
-
-    if (buffer->wrap == 1) {
-        free(e->message);
-        free(e->from);
-    }
-
+    ProfBuffEntry *e = malloc(sizeof(struct prof_buff_entry_t));
     e->show_char = show_char;
     e->flags = flags;
     e->attrs = attrs;
@@ -78,21 +61,27 @@ buffer_push(ProfBuff* buffer, const char show_char, const char * const date_fmt,
     e->message = malloc(strlen(message)+1);
     strcpy(e->message, message);
 
-    *current += 1;
-    if (*current >= BUFF_SIZE) {
-        *current = 0;
-        buffer->wrap = 1;
+    if (g_slist_length(buffer->entries) == BUFF_SIZE) {
+        _free_entry(buffer->entries->data);
+        buffer->entries = g_slist_delete_link(buffer->entries, buffer->entries);
     }
+
+    buffer->entries = g_slist_append(buffer->entries, e);
 }
 
 ProfBuffEntry
-buffer_yield_entry(ProfBuff* buffer, int entry)
+buffer_yield_entry(ProfBuff buffer, int entry)
+{
+    GSList *node = g_slist_nth(buffer->entries, entry);
+    ProfBuffEntry *buff_entry = node->data;
+    return *buff_entry;
+}
+
+static void
+_free_entry(ProfBuffEntry *entry)
 {
-    return (buffer->entry)[entry];
+    free(entry->message);
+    free(entry->from);
+    free(entry->date_fmt);
+}
 
-    if (buffer->wrap == 1) {
-        return buffer->entry[(buffer->current + entry) % BUFF_SIZE];
-    } else {
-        return buffer->entry[entry % (buffer->current)];
-    }
-}
\ No newline at end of file
diff --git a/src/ui/buffer.h b/src/ui/buffer.h
index b8828c14..fec46a36 100644
--- a/src/ui/buffer.h
+++ b/src/ui/buffer.h
@@ -14,15 +14,11 @@ typedef struct prof_buff_entry_t {
     char *message;
 } ProfBuffEntry;
 
-typedef struct prof_buff_t {
-    ProfBuffEntry entry[BUFF_SIZE];
-    int wrap;
-    int current;
-} ProfBuff;
+typedef struct prof_buff_t *ProfBuff;
 
-ProfBuff* buffer_create();
-void buffer_free(ProfBuff* buffer);
-void buffer_push(ProfBuff* buffer, const char show_char, const char * const date_fmt, int flags, int attrs, const char * const from, const char * const message);
-int buffer_size(ProfBuff* buffer);
-ProfBuffEntry buffer_yield_entry(ProfBuff* buffer, int entry);
-#endif
\ No newline at end of file
+ProfBuff buffer_create();
+void buffer_free(ProfBuff buffer);
+void buffer_push(ProfBuff buffer, const char show_char, const char * const date_fmt, int flags, int attrs, const char * const from, const char * const message);
+int buffer_size(ProfBuff buffer);
+ProfBuffEntry buffer_yield_entry(ProfBuff buffer, int entry);
+#endif
diff --git a/src/ui/window.h b/src/ui/window.h
index 632c635b..a4046d71 100644
--- a/src/ui/window.h
+++ b/src/ui/window.h
@@ -54,7 +54,7 @@ typedef enum {
 typedef struct prof_win_t {
     char *from;
     WINDOW *win;
-    ProfBuff *buffer;
+    ProfBuff buffer;
     win_type_t type;
     gboolean is_otr;
     gboolean is_trusted;