diff --git a/contact.c b/contact.c index cfe805b0..f9d9727e 100644 --- a/contact.c +++ b/contact.c @@ -23,6 +23,8 @@ #include #include +#include + #include "contact.h" struct p_contact_t { @@ -96,16 +98,11 @@ const char * p_contact_status(const PContact contact) return contact->status; } -int p_contact_names_equal(const PContact c1, const PContact c2) -{ - return strcmp(c1->name, c2->name); -} - int p_contacts_equal_deep(const PContact c1, const PContact c2) { - int name_eq = strcmp(c1->name, c2->name); - int show_eq = strcmp(c1->show, c2->show); - int status_eq = strcmp(c1->status, c2->status); + int name_eq = (g_strcmp0(c1->name, c2->name) == 0); + int show_eq = (g_strcmp0(c1->show, c2->show) == 0); + int status_eq = (g_strcmp0(c1->status, c2->status) == 0); return (name_eq && show_eq && status_eq); } diff --git a/contact.h b/contact.h index 63af120b..a5f96b97 100644 --- a/contact.h +++ b/contact.h @@ -32,7 +32,6 @@ void p_contact_free(PContact contact); const char * p_contact_name(PContact contact); const char * p_contact_show(PContact contact); const char * p_contact_status(PContact contact); -int p_contact_names_equal(const PContact c1, const PContact c2); int p_contacts_equal_deep(const PContact c1, const PContact c2); #endif diff --git a/contact_list.c b/contact_list.c index 3ffe91c6..acb08e63 100644 --- a/contact_list.c +++ b/contact_list.c @@ -36,6 +36,7 @@ void contact_list_init(void) { ac = p_obj_autocomplete_new((PStrFunc)p_contact_name, (PCopyFunc)p_contact_copy, + (PEqualDeepFunc)p_contacts_equal_deep, (GDestroyNotify)p_contact_free); } diff --git a/prof_autocomplete.c b/prof_autocomplete.c index b8ff1e30..db374641 100644 --- a/prof_autocomplete.c +++ b/prof_autocomplete.c @@ -34,20 +34,22 @@ struct p_autocomplete_t { gchar *search_str; PStrFunc str_func; PCopyFunc copy_func; + PEqualDeepFunc equal_deep_func; GDestroyNotify free_func; }; 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); +static int _deep_equals_func_default(const char *o1, const char *o2); PAutocomplete p_autocomplete_new(void) { - return p_obj_autocomplete_new(NULL, NULL, NULL); + return p_obj_autocomplete_new(NULL, NULL, NULL, NULL); } PAutocomplete p_obj_autocomplete_new(PStrFunc str_func, PCopyFunc copy_func, - GDestroyNotify free_func) + PEqualDeepFunc equal_deep_func, GDestroyNotify free_func) { PAutocomplete new = malloc(sizeof(struct p_autocomplete_t)); new->items = NULL; @@ -69,6 +71,11 @@ PAutocomplete p_obj_autocomplete_new(PStrFunc str_func, PCopyFunc copy_func, else new->free_func = (GDestroyNotify)free; + if (equal_deep_func) + new->equal_deep_func = equal_deep_func; + else + new->equal_deep_func = (PEqualDeepFunc)_deep_equals_func_default; + return new; } @@ -107,9 +114,14 @@ gboolean p_autocomplete_add(PAutocomplete ac, void *item) // update } else if (g_strcmp0(ac->str_func(curr->data), ac->str_func(item)) == 0) { - ac->free_func(curr->data); - curr->data = item; - return TRUE; + // only update if data different + if (!ac->equal_deep_func(curr->data, item)) { + ac->free_func(curr->data); + curr->data = item; + return TRUE; + } else { + return FALSE; + } } curr = g_slist_next(curr); @@ -233,3 +245,7 @@ static const char *_copy_func_default(const char *orig) return strdup(orig); } +static int _deep_equals_func_default(const char *o1, const char *o2) +{ + return (strcmp(o1, o2) == 0); +} diff --git a/prof_autocomplete.h b/prof_autocomplete.h index 2e0f4e86..43dc8ac9 100644 --- a/prof_autocomplete.h +++ b/prof_autocomplete.h @@ -33,7 +33,7 @@ typedef int (*PEqualDeepFunc)(const void *o1, const void *o2); PAutocomplete p_autocomplete_new(void); PAutocomplete p_obj_autocomplete_new(PStrFunc str_func, PCopyFunc copy_func, - GDestroyNotify free_func); + PEqualDeepFunc equal_deep_func, GDestroyNotify free_func); void p_autocomplete_clear(PAutocomplete ac); void p_autocomplete_reset(PAutocomplete ac); gboolean p_autocomplete_add(PAutocomplete ac, void *item); diff --git a/test_prof_autocomplete.c b/test_prof_autocomplete.c index 807b7681..d4142843 100644 --- a/test_prof_autocomplete.c +++ b/test_prof_autocomplete.c @@ -15,8 +15,11 @@ static void clear_empty(void) static void clear_empty_with_free_func(void) { - PAutocomplete ac = p_obj_autocomplete_new(NULL, NULL, - (GDestroyNotify)p_contact_free); + PAutocomplete ac = + p_obj_autocomplete_new((PStrFunc)p_contact_name, + (PCopyFunc)p_contact_copy, + (PEqualDeepFunc)p_contacts_equal_deep, + (GDestroyNotify)p_contact_free); p_autocomplete_clear(ac); } @@ -46,8 +49,11 @@ static void get_after_create_returns_null(void) static void get_after_create_with_copy_func_returns_null(void) { - PAutocomplete ac = p_obj_autocomplete_new(NULL, (PCopyFunc)p_contact_copy, - (GDestroyNotify)p_contact_free); + PAutocomplete ac = + p_obj_autocomplete_new((PStrFunc)p_contact_name, + (PCopyFunc)p_contact_copy, + (PEqualDeepFunc)p_contacts_equal_deep, + (GDestroyNotify)p_contact_free); GSList *result = p_autocomplete_get_list(ac); assert_is_null(result); @@ -70,8 +76,11 @@ 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_obj_autocomplete_new((PStrFunc)p_contact_name, NULL, - (GDestroyNotify)p_contact_free); + PAutocomplete ac = + p_obj_autocomplete_new((PStrFunc)p_contact_name, + (PCopyFunc)p_contact_copy, + (PEqualDeepFunc)p_contacts_equal_deep, + (GDestroyNotify)p_contact_free); p_autocomplete_add(ac, contact); char *result = p_autocomplete_complete(ac, "Jam"); @@ -98,8 +107,11 @@ 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_obj_autocomplete_new((PStrFunc)p_contact_name, NULL, - (GDestroyNotify)p_contact_free); + PAutocomplete ac = + p_obj_autocomplete_new((PStrFunc)p_contact_name, + (PCopyFunc)p_contact_copy, + (PEqualDeepFunc)p_contacts_equal_deep, + (GDestroyNotify)p_contact_free); p_autocomplete_add(ac, contact1); p_autocomplete_add(ac, contact2); char *result = p_autocomplete_complete(ac, "Jam"); @@ -128,8 +140,11 @@ 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_obj_autocomplete_new((PStrFunc)p_contact_name, NULL, - (GDestroyNotify)p_contact_free); + PAutocomplete ac = + p_obj_autocomplete_new((PStrFunc)p_contact_name, + (PCopyFunc)p_contact_copy, + (PEqualDeepFunc)p_contacts_equal_deep, + (GDestroyNotify)p_contact_free); p_autocomplete_add(ac, contact1); p_autocomplete_add(ac, contact2); char *result1 = p_autocomplete_complete(ac, "Jam"); @@ -158,8 +173,11 @@ 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_obj_autocomplete_new((PStrFunc)p_contact_name, - (PCopyFunc)p_contact_copy, (GDestroyNotify)p_contact_free); + PAutocomplete ac = + p_obj_autocomplete_new((PStrFunc)p_contact_name, + (PCopyFunc)p_contact_copy, + (PEqualDeepFunc)p_contacts_equal_deep, + (GDestroyNotify)p_contact_free); p_autocomplete_add(ac, contact1); p_autocomplete_add(ac, contact2); GSList *result = p_autocomplete_get_list(ac); @@ -187,8 +205,11 @@ 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_obj_autocomplete_new((PStrFunc)p_contact_name, - (PCopyFunc)p_contact_copy, (GDestroyNotify)p_contact_free); + PAutocomplete ac = + p_obj_autocomplete_new((PStrFunc)p_contact_name, + (PCopyFunc)p_contact_copy, + (PEqualDeepFunc)p_contacts_equal_deep, + (GDestroyNotify)p_contact_free); p_autocomplete_add(ac, contact1); p_autocomplete_add(ac, contact2); GSList *result = p_autocomplete_get_list(ac); @@ -220,8 +241,11 @@ 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_obj_autocomplete_new((PStrFunc)p_contact_name, - (PCopyFunc)p_contact_copy, (GDestroyNotify)p_contact_free); + PAutocomplete ac = + p_obj_autocomplete_new((PStrFunc)p_contact_name, + (PCopyFunc)p_contact_copy, + (PEqualDeepFunc)p_contacts_equal_deep, + (GDestroyNotify)p_contact_free); p_autocomplete_add(ac, contact1); p_autocomplete_add(ac, contact2); GSList *result = p_autocomplete_get_list(ac);