about summary refs log tree commit diff stats
path: root/contact_list.c
diff options
context:
space:
mode:
authorJames Booth <boothj5@gmail.com>2012-05-10 00:30:03 +0100
committerJames Booth <boothj5@gmail.com>2012-05-10 00:30:03 +0100
commita336148cd2b0c529515e7b0d74afae9e0660cf6c (patch)
treeeae5b857545e67e84640b7816f2ced3ad1404e35 /contact_list.c
parentb2385010b5b81bd66b840802eb36b5a0945eba8b (diff)
downloadprofani-tty-a336148cd2b0c529515e7b0d74afae9e0660cf6c.tar.gz
Using GSList for contact list
Diffstat (limited to 'contact_list.c')
-rw-r--r--contact_list.c173
1 files changed, 51 insertions, 122 deletions
diff --git a/contact_list.c b/contact_list.c
index 1715e594..9b38373c 100644
--- a/contact_list.c
+++ b/contact_list.c
@@ -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,67 +94,58 @@ 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)
-{
-    struct contact_node_t *copy = NULL;
-    struct contact_node_t *curr = _contact_list;
-    
-    copy = _copy_contact_list(curr);
-
-    return copy;
-}
-
-struct contact_node_t * _copy_contact_list(struct contact_node_t *node)
+GSList * get_contact_list(void)
 {
-    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));
+    GSList *copy = NULL;
+    GSList *curr = _contact_list;
 
-        copy->next = _copy_contact_list(node->next);
+    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)));
 
-        return copy;
+        curr = g_slist_next(curr);
     }
+
+    return copy;
 }
 
 char * find_contact(char *search_str)
@@ -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;
-    }
-}
-