From 2080a61663e3a919d6a1eb1c31e7d0333148292e Mon Sep 17 00:00:00 2001 From: James Booth Date: Sun, 20 May 2012 00:54:17 +0100 Subject: [PATCH] Autcomplete defaults to string data p_obj_autocomplete_new() can be used to create an autocompleter that stores arbitrary data structures passing memory management functions to get a string, copy and free --- contact_list.c | 2 +- prof_autocomplete.c | 7 ++++++- prof_autocomplete.h | 3 ++- test_prof_autocomplete.c | 37 +++++++++++++++++++------------------ 4 files changed, 28 insertions(+), 21 deletions(-) diff --git a/contact_list.c b/contact_list.c index 22122f4a..3e0aca7e 100644 --- a/contact_list.c +++ b/contact_list.c @@ -34,7 +34,7 @@ static PAutocomplete ac; void contact_list_init(void) { - ac = p_autocomplete_new((PStrFunc)p_contact_name, + ac = p_obj_autocomplete_new((PStrFunc)p_contact_name, (PCopyFunc)p_contact_copy, (GDestroyNotify)p_contact_free); } diff --git a/prof_autocomplete.c b/prof_autocomplete.c index 6996f4fd..8ee0fbad 100644 --- a/prof_autocomplete.c +++ b/prof_autocomplete.c @@ -41,7 +41,12 @@ static gchar * _search_from(PAutocomplete ac, GSList *curr); static const char *_str_func_default(const char *orig); static const char *_copy_func_default(const char *orig); -PAutocomplete p_autocomplete_new(PStrFunc str_func, PCopyFunc copy_func, +PAutocomplete p_autocomplete_new(void) +{ + return p_obj_autocomplete_new(NULL, NULL, NULL); +} + +PAutocomplete p_obj_autocomplete_new(PStrFunc str_func, PCopyFunc copy_func, GDestroyNotify free_func) { PAutocomplete new = malloc(sizeof(struct p_autocomplete_t)); diff --git a/prof_autocomplete.h b/prof_autocomplete.h index 5f89d207..02a1bdc1 100644 --- a/prof_autocomplete.h +++ b/prof_autocomplete.h @@ -29,7 +29,8 @@ typedef struct p_autocomplete_t *PAutocomplete; typedef const char * (*PStrFunc)(const void *obj); typedef void * (*PCopyFunc)(const void *obj); -PAutocomplete p_autocomplete_new(PStrFunc str_func, PCopyFunc copy_func, +PAutocomplete p_autocomplete_new(void); +PAutocomplete p_obj_autocomplete_new(PStrFunc str_func, PCopyFunc copy_func, GDestroyNotify free_func); void p_autocomplete_clear(PAutocomplete ac); void p_autocomplete_reset(PAutocomplete ac); diff --git a/test_prof_autocomplete.c b/test_prof_autocomplete.c index 82e57eae..807b7681 100644 --- a/test_prof_autocomplete.c +++ b/test_prof_autocomplete.c @@ -9,33 +9,34 @@ static void clear_empty(void) { - PAutocomplete ac = p_autocomplete_new(NULL, NULL, NULL); + PAutocomplete ac = p_autocomplete_new(); p_autocomplete_clear(ac); } static void clear_empty_with_free_func(void) { - PAutocomplete ac = p_autocomplete_new(NULL, NULL, (GDestroyNotify)p_contact_free); + PAutocomplete ac = p_obj_autocomplete_new(NULL, NULL, + (GDestroyNotify)p_contact_free); p_autocomplete_clear(ac); } static void reset_after_create(void) { - PAutocomplete ac = p_autocomplete_new(NULL, NULL, NULL); + PAutocomplete ac = p_autocomplete_new(); p_autocomplete_reset(ac); p_autocomplete_clear(ac); } static void find_after_create(void) { - PAutocomplete ac = p_autocomplete_new(NULL, NULL, NULL); + PAutocomplete ac = p_autocomplete_new(); p_autocomplete_complete(ac, "hello"); p_autocomplete_clear(ac); } static void get_after_create_returns_null(void) { - PAutocomplete ac = p_autocomplete_new(NULL, NULL, NULL); + PAutocomplete ac = p_autocomplete_new(); GSList *result = p_autocomplete_get_list(ac); assert_is_null(result); @@ -45,7 +46,7 @@ static void get_after_create_returns_null(void) static void get_after_create_with_copy_func_returns_null(void) { - PAutocomplete ac = p_autocomplete_new(NULL, (PCopyFunc)p_contact_copy, + PAutocomplete ac = p_obj_autocomplete_new(NULL, (PCopyFunc)p_contact_copy, (GDestroyNotify)p_contact_free); GSList *result = p_autocomplete_get_list(ac); @@ -57,7 +58,7 @@ static void get_after_create_with_copy_func_returns_null(void) static void add_one_and_complete(void) { char *item = strdup("Hello"); - PAutocomplete ac = p_autocomplete_new(NULL, NULL, NULL); + PAutocomplete ac = p_autocomplete_new(); p_autocomplete_add(ac, item); char *result = p_autocomplete_complete(ac, "Hel"); @@ -69,7 +70,7 @@ static void add_one_and_complete(void) static void add_one_and_complete_with_funcs(void) { PContact contact = p_contact_new("James", "Online", "I'm here"); - PAutocomplete ac = p_autocomplete_new((PStrFunc)p_contact_name, NULL, + PAutocomplete ac = p_obj_autocomplete_new((PStrFunc)p_contact_name, NULL, (GDestroyNotify)p_contact_free); p_autocomplete_add(ac, contact); char *result = p_autocomplete_complete(ac, "Jam"); @@ -83,7 +84,7 @@ static void add_two_and_complete_returns_first(void) { char *item1 = strdup("Hello"); char *item2 = strdup("Help"); - PAutocomplete ac = p_autocomplete_new(NULL, NULL, NULL); + PAutocomplete ac = p_autocomplete_new(); p_autocomplete_add(ac, item1); p_autocomplete_add(ac, item2); char *result = p_autocomplete_complete(ac, "Hel"); @@ -97,7 +98,7 @@ static void add_two_and_complete_returns_first_with_funcs(void) { PContact contact1 = p_contact_new("James", "Online", "I'm here"); PContact contact2 = p_contact_new("Jamie", "Away", "Out to lunch"); - PAutocomplete ac = p_autocomplete_new((PStrFunc)p_contact_name, NULL, + PAutocomplete ac = p_obj_autocomplete_new((PStrFunc)p_contact_name, NULL, (GDestroyNotify)p_contact_free); p_autocomplete_add(ac, contact1); p_autocomplete_add(ac, contact2); @@ -112,7 +113,7 @@ static void add_two_and_complete_returns_second(void) { char *item1 = strdup("Hello"); char *item2 = strdup("Help"); - PAutocomplete ac = p_autocomplete_new(NULL, NULL, NULL); + PAutocomplete ac = p_autocomplete_new(); p_autocomplete_add(ac, item1); p_autocomplete_add(ac, item2); char *result1 = p_autocomplete_complete(ac, "Hel"); @@ -127,7 +128,7 @@ static void add_two_and_complete_returns_second_with_funcs(void) { PContact contact1 = p_contact_new("James", "Online", "I'm here"); PContact contact2 = p_contact_new("Jamie", "Away", "Out to lunch"); - PAutocomplete ac = p_autocomplete_new((PStrFunc)p_contact_name, NULL, + PAutocomplete ac = p_obj_autocomplete_new((PStrFunc)p_contact_name, NULL, (GDestroyNotify)p_contact_free); p_autocomplete_add(ac, contact1); p_autocomplete_add(ac, contact2); @@ -143,7 +144,7 @@ static void add_two_adds_two(void) { char *item1 = strdup("Hello"); char *item2 = strdup("Help"); - PAutocomplete ac = p_autocomplete_new(NULL, NULL, NULL); + PAutocomplete ac = p_autocomplete_new(); p_autocomplete_add(ac, item1); p_autocomplete_add(ac, item2); GSList *result = p_autocomplete_get_list(ac); @@ -157,7 +158,7 @@ static void add_two_adds_two_with_funcs(void) { PContact contact1 = p_contact_new("James", "Online", "I'm here"); PContact contact2 = p_contact_new("Jamie", "Away", "Out to lunch"); - PAutocomplete ac = p_autocomplete_new((PStrFunc)p_contact_name, + PAutocomplete ac = p_obj_autocomplete_new((PStrFunc)p_contact_name, (PCopyFunc)p_contact_copy, (GDestroyNotify)p_contact_free); p_autocomplete_add(ac, contact1); p_autocomplete_add(ac, contact2); @@ -172,7 +173,7 @@ static void add_two_same_adds_one(void) { char *item1 = strdup("Hello"); char *item2 = strdup("Hello"); - PAutocomplete ac = p_autocomplete_new(NULL, NULL, NULL); + PAutocomplete ac = p_autocomplete_new(); p_autocomplete_add(ac, item1); p_autocomplete_add(ac, item2); GSList *result = p_autocomplete_get_list(ac); @@ -186,7 +187,7 @@ static void add_two_same_adds_one_with_funcs(void) { PContact contact1 = p_contact_new("James", "Online", "I'm here"); PContact contact2 = p_contact_new("James", "Away", "Out to lunch"); - PAutocomplete ac = p_autocomplete_new((PStrFunc)p_contact_name, + PAutocomplete ac = p_obj_autocomplete_new((PStrFunc)p_contact_name, (PCopyFunc)p_contact_copy, (GDestroyNotify)p_contact_free); p_autocomplete_add(ac, contact1); p_autocomplete_add(ac, contact2); @@ -201,7 +202,7 @@ static void add_two_same_updates(void) { char *item1 = strdup("Hello"); char *item2 = strdup("Hello"); - PAutocomplete ac = p_autocomplete_new(NULL, NULL, NULL); + PAutocomplete ac = p_autocomplete_new(); p_autocomplete_add(ac, item1); p_autocomplete_add(ac, item2); GSList *result = p_autocomplete_get_list(ac); @@ -219,7 +220,7 @@ static void add_two_same_updates_with_funcs(void) { PContact contact1 = p_contact_new("James", "Online", "I'm here"); PContact contact2 = p_contact_new("James", "Away", "Out to lunch"); - PAutocomplete ac = p_autocomplete_new((PStrFunc)p_contact_name, + PAutocomplete ac = p_obj_autocomplete_new((PStrFunc)p_contact_name, (PCopyFunc)p_contact_copy, (GDestroyNotify)p_contact_free); p_autocomplete_add(ac, contact1); p_autocomplete_add(ac, contact2);