about summary refs log tree commit diff stats
path: root/src
diff options
context:
space:
mode:
authorDmitry Podgorny <pasis.ua@gmail.com>2013-08-26 02:43:59 +0300
committerDmitry Podgorny <pasis.ua@gmail.com>2013-08-26 02:43:59 +0300
commit92763d2e68207b52fbe613058293de0fc8cc7f24 (patch)
tree012ea755b7a81f0314ae8fa3896c2c98b4db716c /src
parent32e18fb1a589a2273cbf210919ee7ae7de04d79a (diff)
downloadprofani-tty-92763d2e68207b52fbe613058293de0fc8cc7f24.tar.gz
refactored autocomplete_add
Now this function responsible for memory allocation
Diffstat (limited to 'src')
-rw-r--r--src/tools/autocomplete.c43
-rw-r--r--src/tools/autocomplete.h2
2 files changed, 11 insertions, 34 deletions
diff --git a/src/tools/autocomplete.c b/src/tools/autocomplete.c
index 5c70f874..afabaed2 100644
--- a/src/tools/autocomplete.c
+++ b/src/tools/autocomplete.c
@@ -83,42 +83,19 @@ autocomplete_length(Autocomplete ac)
 }
 
 gboolean
-autocomplete_add(Autocomplete ac, void *item)
+autocomplete_add(Autocomplete ac, const char *item)
 {
-    if (ac->items == NULL) {
-        ac->items = g_slist_append(ac->items, item);
-        return TRUE;
-    } else {
-        GSList *curr = ac->items;
-
-        while(curr) {
+    char *item_cpy;
+    GSList *curr = g_slist_find_custom(ac->items, item, (GCompareFunc)strcmp);
 
-            // insert
-            if (g_strcmp0(curr->data, item) > 0) {
-                ac->items = g_slist_insert_before(ac->items,
-                    curr, item);
-                return TRUE;
-
-            // update
-            } else if (g_strcmp0(curr->data, item) == 0) {
-                // only update if data different
-                if (strcmp(curr->data, item) != 0) {
-                    free(curr->data);
-                    curr->data = item;
-                    return TRUE;
-                } else {
-                    return FALSE;
-                }
-            }
-
-            curr = g_slist_next(curr);
-        }
-
-        // hit end, append
-        ac->items = g_slist_append(ac->items, item);
-
-        return TRUE;
+    // if item already exists
+    if (curr != NULL) {
+        return FALSE;
     }
+
+    item_cpy = strdup(item);
+    ac->items = g_slist_insert_sorted(ac->items, item_cpy, (GCompareFunc)strcmp);
+    return TRUE;
 }
 
 gboolean
diff --git a/src/tools/autocomplete.h b/src/tools/autocomplete.h
index a28601d0..908e273e 100644
--- a/src/tools/autocomplete.h
+++ b/src/tools/autocomplete.h
@@ -38,7 +38,7 @@ Autocomplete obj_autocomplete_new(PStrFunc str_func, PCopyFunc copy_func,
 void autocomplete_clear(Autocomplete ac);
 void autocomplete_reset(Autocomplete ac);
 void autocomplete_free(Autocomplete ac);
-gboolean autocomplete_add(Autocomplete ac, void *item);
+gboolean autocomplete_add(Autocomplete ac, const char *item);
 gboolean autocomplete_remove(Autocomplete ac, const char * const item);
 GSList * autocomplete_get_list(Autocomplete ac);
 gchar * autocomplete_complete(Autocomplete ac, gchar *search_str);