1
0
mirror of https://github.com/profanity-im/profanity.git synced 2024-09-22 19:45:54 -04:00

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/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)
{
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;
} 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
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

View File

@ -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;