From 6a9e19303eed29d5985d0e8fb00ef48c9d6b25e1 Mon Sep 17 00:00:00 2001 From: Immae Date: Mon, 23 Jun 2014 00:44:35 +0200 Subject: Added buffer handling. Buffer are initialized, pushed, and freed. No use is made of them so far Signed-off-by: James Booth --- src/ui/buffer.c | 106 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/ui/buffer.h | 29 ++++++++++++++++ src/ui/window.c | 4 ++- src/ui/window.h | 2 ++ 4 files changed, 140 insertions(+), 1 deletion(-) create mode 100644 src/ui/buffer.c create mode 100644 src/ui/buffer.h (limited to 'src/ui') 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 +#include +#include +#include + +#include +#ifdef HAVE_NCURSESW_NCURSES_H +#include +#elif HAVE_NCURSES_H +#include +#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; +} diff --git a/src/ui/buffer.h b/src/ui/buffer.h new file mode 100644 index 00000000..65bd8b8d --- /dev/null +++ b/src/ui/buffer.h @@ -0,0 +1,29 @@ +#ifndef UI_BUFFER_H +#define UI_BUFFER_H + +#include "config.h" + +//#include "ui/window.h" +#define BUFF_SIZE 1000 + +typedef struct prof_buff_entry_t { + char show_char; + GTimeVal tstamp; + int flags; + int attrs; + char *from; + char *message; +} ProfBuffEntry; + +typedef struct prof_buff_t { + ProfBuffEntry entry[BUFF_SIZE]; + int wrap; + int current; +} ProfBuff; + + +ProfBuff* buffer_create(); +void buffer_free(ProfBuff* 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 buffer_yield(ProfBuff* buffer, int line, ProfBuffEntry** list); +#endif diff --git a/src/ui/window.c b/src/ui/window.c index 3f0099b9..4131fa8a 100644 --- a/src/ui/window.c +++ b/src/ui/window.c @@ -44,6 +44,7 @@ win_create(const char * const title, int cols, win_type_t type) new_win->from = strdup(title); new_win->win = newpad(PAD_SIZE, cols); wbkgd(new_win->win, COLOUR_TEXT); + new_win->buffer = buffer_create(); new_win->y_pos = 0; new_win->x_pos = 0; new_win->paged = 0; @@ -60,6 +61,7 @@ win_create(const char * const title, int cols, win_type_t type) void win_free(ProfWin* window) { + buffer_free(window->buffer); delwin(window->win); free(window->from); free(window); @@ -357,7 +359,7 @@ void win_save_print(ProfWin *window, const char show_char, GTimeVal *tstamp, int date_fmt = g_date_time_format(time, "%H:%M:%S"); } g_date_time_unref(time); -// buffer_push(window->buffer, time, from, message, attrs); + buffer_push(window->buffer, show_char, tstamp, flags, attrs, from, message); if((flags & 2) == 0) { wattron(window->win, COLOUR_TIME); wprintw(window->win, "%s %c ", date_fmt, show_char); diff --git a/src/ui/window.h b/src/ui/window.h index 258cefe3..d585036d 100644 --- a/src/ui/window.h +++ b/src/ui/window.h @@ -32,6 +32,7 @@ #endif #include "contact.h" +#include "ui/buffer.h" #define NO_ME 1 #define NO_EOL 4 @@ -53,6 +54,7 @@ typedef enum { typedef struct prof_win_t { char *from; WINDOW *win; + ProfBuff *buffer; win_type_t type; gboolean is_otr; gboolean is_trusted; -- cgit 1.4.1-2-gfad0