about summary refs log tree commit diff stats
path: root/src/ui/buffer.c
diff options
context:
space:
mode:
authorImmae <ismael.bouya@normalesup.org>2014-06-23 00:44:35 +0200
committerJames Booth <boothj5@gmail.com>2014-06-23 21:05:46 +0100
commit6a9e19303eed29d5985d0e8fb00ef48c9d6b25e1 (patch)
treeb506f485265f6481853f4f204a4adab0a482943b /src/ui/buffer.c
parent66ad23b35b3df519e45e2bf46741d8b874a18e74 (diff)
downloadprofani-tty-6a9e19303eed29d5985d0e8fb00ef48c9d6b25e1.tar.gz
Added buffer handling. Buffer are initialized, pushed, and freed. No use is made of them so far
Signed-off-by: James Booth <boothj5@gmail.com>
Diffstat (limited to 'src/ui/buffer.c')
-rw-r--r--src/ui/buffer.c106
1 files changed, 106 insertions, 0 deletions
diff --git a/src/ui/buffer.c b/src/ui/buffer.c
new file mode 100644
index 00000000..0c0d4d90
--- /dev/null
+++ b/src/ui/buffer.c
@@ -0,0 +1,106 @@
+#include "config.h"
+
+#include <stdlib.h>
+#include <string.h>
+#include <assert.h>
+#include <stdlib.h>
+
+#include <glib.h>
+#ifdef HAVE_NCURSESW_NCURSES_H
+#include <ncursesw/ncurses.h>
+#elif HAVE_NCURSES_H
+#include <ncurses.h>
+#endif
+
+#include "ui/window.h"
+#include "ui/buffer.h"
+
+ProfBuff*
+buffer_create() {
+  ProfBuff* new_buff = malloc(sizeof(struct prof_buff_t));
+  new_buff->wrap = 0;
+  new_buff->current = 0;
+  return new_buff;
+}
+
+void
+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);
+}
+
+void
+buffer_push(ProfBuff* buffer, const char show_char, GTimeVal *tstamp, int flags, int attrs, const char * const from, const char * const message) {
+  int *current = &(buffer->current);
+  ProfBuffEntry e = buffer->entry[*current];
+
+  if(buffer->wrap == 1) {
+    free(e.message);
+    free(e.from);
+  }
+  e.show_char = show_char;
+  e.tstamp = *tstamp;
+  e.flags = flags;
+  e.attrs = attrs;
+
+  e.from = malloc(strlen(from)+1);
+  strcpy(e.from, from);
+
+  e.message = malloc(strlen(message)+1);
+  strcpy(e.message, message);
+
+  *current += 1;
+  if(*current >= BUFF_SIZE) {
+    *current = 0;
+    buffer->wrap = 1;
+  }
+}
+
+int
+buffer_yield(ProfBuff* buffer, int line, ProfBuffEntry** list) {
+  //Returns the (line+1)-th last line
+  //e.g. if line == 0, returns the last line
+  //To correct for splitted lines...
+  int current = buffer->current;
+  int imax = current;
+  if(buffer->wrap == 1) {
+    imax = BUFF_SIZE;
+  }
+  int i = 1;
+  int j = 0;
+  while(i <= imax) {
+    ProfBuffEntry e = buffer->entry[(current - i) % BUFF_SIZE];
+    if(j == line && (e.flags & NO_EOL) == 0) {
+      //We found our line and we are at its last entry
+      int nb_entries = 1;
+      while(i + nb_entries <= imax) {
+        e = buffer->entry[(current - i - nb_entries) % BUFF_SIZE];
+        if((e.flags & NO_EOL) == 0) {
+          break;
+        }
+        nb_entries += 1;
+      }
+      if((buffer->wrap == 1) && (i + nb_entries > imax)) {
+        //The line is at the top of the buffer, we don't know if it's complete
+        return 0;
+      }
+      else {
+        *list = &(buffer->entry[(current - i - nb_entries + 1) % BUFF_SIZE]);
+        return nb_entries;
+      }
+    }
+    if((e.flags & NO_EOL) == 0) {
+      j++;
+    }
+    i++;
+  }
+  return 0;
+}