Using GSList for contact list

This commit is contained in:
James Booth
2012-05-10 00:30:03 +01:00
parent b2385010b5
commit a336148cd2
6 changed files with 123 additions and 190 deletions

View File

@@ -24,52 +24,32 @@
#include <stdlib.h>
#include <string.h>
#include <glib.h>
#include "contact.h"
#include "contact_list.h"
// internal contact list
static struct contact_node_t * _contact_list = NULL;
static GSList * _contact_list = NULL;
// state of current tab completion, currrent node
static struct contact_node_t * _last_found = NULL;
static GSList * _last_found = NULL;
// and current search pattern
static char * _search_str = NULL;
static char * _search_contact_list_from(struct contact_node_t * curr);
static struct contact_node_t * _make_contact_node(const char * const name,
const char * const show, const char * const status);
static struct contact_node_t * _copy_contact_list(struct contact_node_t *node);
static void _insert_contact(struct contact_node_t *curr,
struct contact_node_t *prev, const char * const name,
const char * const show, const char * const status);
static char * _search_contact_list_from(GSList * curr);
void contact_list_clear(void)
{
struct contact_node_t *curr = _contact_list;
if (curr) {
while(curr) {
PContact contact = curr->contact;
p_contact_free(contact);
curr = curr->next;
}
free(_contact_list);
_contact_list = NULL;
g_slist_free_full(_contact_list, (GDestroyNotify)p_contact_free);
_contact_list = NULL;
reset_search_attempts();
}
reset_search_attempts();
}
void destroy_list(struct contact_node_t *list)
void destroy_list(GSList *list)
{
while(list) {
PContact contact = list->contact;
p_contact_free(contact);
list = list->next;
}
free(list);
g_slist_free_full(list, (GDestroyNotify)p_contact_free);
list = NULL;
}
@@ -84,34 +64,26 @@ void reset_search_attempts(void)
int contact_list_remove(const char * const name)
{
// reset last found if it points at the node to be removed
if (_last_found != NULL)
if (strcmp(p_contact_name(_last_found->data), name) == 0)
_last_found = NULL;
if (!_contact_list) {
return 0;
} else {
struct contact_node_t *curr = _contact_list;
struct contact_node_t *prev = NULL;
GSList *curr = _contact_list;
while(curr) {
PContact contact = curr->contact;
PContact contact = curr->data;
if (strcmp(p_contact_name(contact), name) == 0) {
if (prev)
prev->next = curr->next;
else
_contact_list = curr->next;
// reset last found if it points at the node to be removed
if (_last_found != NULL)
if (strcmp(p_contact_name(_last_found->contact),
p_contact_name(contact)) == 0)
_last_found = NULL;
_contact_list = g_slist_remove(_contact_list, contact);
p_contact_free(contact);
free(curr);
return 1;
}
prev = curr;
curr = curr->next;
curr = g_slist_next(curr);
}
return 0;
@@ -122,69 +94,60 @@ int contact_list_add(const char * const name, const char * const show,
const char * const status)
{
// empty list, create new
if (!_contact_list) {
_contact_list = _make_contact_node(name, show, status);
_contact_list = g_slist_append(_contact_list,
p_contact_new(name, show, status));
return 1;
} else {
struct contact_node_t *curr = _contact_list;
struct contact_node_t *prev = NULL;
GSList *curr = _contact_list;
while(curr) {
PContact curr_contact = curr->contact;
PContact curr_contact = curr->data;
// insert
if (strcmp(p_contact_name(curr_contact), name) > 0) {
_insert_contact(curr, prev, name, show, status);
_contact_list = g_slist_insert_before(_contact_list,
curr, p_contact_new(name, show, status));
return 0;
// update
} else if (strcmp(p_contact_name(curr_contact), name) == 0) {
p_contact_free(curr->contact);
curr->contact = p_contact_new(name, show, status);
p_contact_free(curr->data);
curr->data = p_contact_new(name, show, status);
return 0;
}
// move on
prev = curr;
curr = curr->next;
curr = g_slist_next(curr);
}
curr = _make_contact_node(name, show, status);
// hit end, append
_contact_list = g_slist_append(_contact_list,
p_contact_new(name, show, status));
if (prev)
prev->next = curr;
return 1;
}
}
struct contact_node_t * get_contact_list(void)
GSList * get_contact_list(void)
{
struct contact_node_t *copy = NULL;
struct contact_node_t *curr = _contact_list;
copy = _copy_contact_list(curr);
GSList *copy = NULL;
GSList *curr = _contact_list;
while(curr) {
PContact curr_contact = curr->data;
copy = g_slist_append(copy,
p_contact_new(p_contact_name(curr_contact),
p_contact_show(curr_contact),
p_contact_status(curr_contact)));
curr = g_slist_next(curr);
}
return copy;
}
struct contact_node_t * _copy_contact_list(struct contact_node_t *node)
{
if (node == NULL) {
return NULL;
} else {
PContact curr_contact = node->contact;
struct contact_node_t *copy =
_make_contact_node(p_contact_name(curr_contact),
p_contact_show(curr_contact),
p_contact_status(curr_contact));
copy->next = _copy_contact_list(node->next);
return copy;
}
}
char * find_contact(char *search_str)
{
char *found = NULL;
@@ -204,7 +167,7 @@ char * find_contact(char *search_str)
// subsequent search attempt
} else {
// search from here+1 to end
found = _search_contact_list_from(_last_found->next);
found = _search_contact_list_from(g_slist_next(_last_found));
if (found != NULL)
return found;
@@ -219,22 +182,15 @@ char * find_contact(char *search_str)
}
}
int get_size(struct contact_node_t *list)
int get_size(GSList *list)
{
int size = 0;
while(list) {
size++;
list = list->next;
}
return size;
return g_slist_length(list);
}
static char * _search_contact_list_from(struct contact_node_t * curr)
static char * _search_contact_list_from(GSList * curr)
{
while(curr) {
PContact curr_contact = curr->contact;
PContact curr_contact = curr->data;
// match found
if (strncmp(p_contact_name(curr_contact),
@@ -252,35 +208,8 @@ static char * _search_contact_list_from(struct contact_node_t * curr)
return result;
}
curr = curr->next;
curr = g_slist_next(curr);
}
return NULL;
}
static struct contact_node_t * _make_contact_node(const char * const name,
const char * const show, const char * const status)
{
struct contact_node_t *new =
(struct contact_node_t *) malloc(sizeof(struct contact_node_t));
new->contact = p_contact_new(name, show, status);
new->next = NULL;
return new;
}
static void _insert_contact(struct contact_node_t *curr,
struct contact_node_t *prev, const char * const name,
const char * const show, const char * const status)
{
if (prev) {
struct contact_node_t *new = _make_contact_node(name, show, status);
new->next = curr;
prev->next = new;
} else {
struct contact_node_t *new = _make_contact_node(name, show, status);
new->next = _contact_list;
_contact_list = new;
}
}