Use GSList for buffer entries

This commit is contained in:
James Booth
2014-07-18 23:07:08 +01:00
parent 2a435cd3de
commit 1e6b70943a
3 changed files with 42 additions and 57 deletions

View File

@@ -15,56 +15,39 @@
#include "ui/window.h" #include "ui/window.h"
#include "ui/buffer.h" #include "ui/buffer.h"
ProfBuff* struct prof_buff_t {
GSList *entries;
};
static void _free_entry(ProfBuffEntry *entry);
ProfBuff
buffer_create() buffer_create()
{ {
ProfBuff* new_buff = malloc(sizeof(struct prof_buff_t)); ProfBuff new_buff = malloc(sizeof(struct prof_buff_t));
new_buff->wrap = 0; new_buff->entries = NULL;
new_buff->current = 0;
return new_buff; return new_buff;
} }
int int
buffer_size(ProfBuff* buffer) buffer_size(ProfBuff buffer)
{ {
if(buffer->wrap == 1) { return g_slist_length(buffer->entries);
return BUFF_SIZE;
} else {
return buffer->current;
}
} }
void void
buffer_free(ProfBuff* buffer) buffer_free(ProfBuff buffer)
{ {
int i = 0; g_slist_free_full(buffer->entries, (GDestroyNotify)_free_entry);
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);
}
free(buffer); free(buffer);
buffer = NULL;
} }
void 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 flags, int attrs, const char * const from, const char * const message)
{ {
int *current = &(buffer->current); ProfBuffEntry *e = malloc(sizeof(struct prof_buff_entry_t));
ProfBuffEntry* e = &(buffer->entry[buffer->current]);
if (buffer->wrap == 1) {
free(e->message);
free(e->from);
}
e->show_char = show_char; e->show_char = show_char;
e->flags = flags; e->flags = flags;
e->attrs = attrs; 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); e->message = malloc(strlen(message)+1);
strcpy(e->message, message); strcpy(e->message, message);
*current += 1; if (g_slist_length(buffer->entries) == BUFF_SIZE) {
if (*current >= BUFF_SIZE) { _free_entry(buffer->entries->data);
*current = 0; buffer->entries = g_slist_delete_link(buffer->entries, buffer->entries);
buffer->wrap = 1;
} }
buffer->entries = g_slist_append(buffer->entries, e);
} }
ProfBuffEntry ProfBuffEntry
buffer_yield_entry(ProfBuff* buffer, int entry) buffer_yield_entry(ProfBuff buffer, int entry)
{ {
return (buffer->entry)[entry]; GSList *node = g_slist_nth(buffer->entries, entry);
ProfBuffEntry *buff_entry = node->data;
return *buff_entry;
}
static void
_free_entry(ProfBuffEntry *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)];
}
}

View File

@@ -14,15 +14,11 @@ typedef struct prof_buff_entry_t {
char *message; char *message;
} ProfBuffEntry; } ProfBuffEntry;
typedef struct prof_buff_t { typedef struct prof_buff_t *ProfBuff;
ProfBuffEntry entry[BUFF_SIZE];
int wrap;
int current;
} ProfBuff;
ProfBuff* buffer_create(); ProfBuff buffer_create();
void buffer_free(ProfBuff* buffer); 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); 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); int buffer_size(ProfBuff buffer);
ProfBuffEntry buffer_yield_entry(ProfBuff* buffer, int entry); ProfBuffEntry buffer_yield_entry(ProfBuff buffer, int entry);
#endif #endif

View File

@@ -54,7 +54,7 @@ typedef enum {
typedef struct prof_win_t { typedef struct prof_win_t {
char *from; char *from;
WINDOW *win; WINDOW *win;
ProfBuff *buffer; ProfBuff buffer;
win_type_t type; win_type_t type;
gboolean is_otr; gboolean is_otr;
gboolean is_trusted; gboolean is_trusted;