[evolution-data-server/openismus-work: 100/122] libedata-book: Simplify signature of sexp helpers



commit b6a4db8911a5d23e9ec19c4890cea008e41cf863
Author: Mathias Hasselmann <mathias openismus com>
Date:   Thu Dec 6 22:18:23 2012 +0100

    libedata-book: Simplify signature of sexp helpers
    
    The compare() and list_compare() functions are used as plain boolean
    predicates. Still the function signature required the helpers to return
    a fancy string without actually describing the requirements for that
    string. Let's stop that now.

 addressbook/libedata-book/e-book-backend-sexp.c |  121 +++++++++-------------
 1 files changed, 50 insertions(+), 71 deletions(-)
---
diff --git a/addressbook/libedata-book/e-book-backend-sexp.c b/addressbook/libedata-book/e-book-backend-sexp.c
index 30ca998..a6c578e 100644
--- a/addressbook/libedata-book/e-book-backend-sexp.c
+++ b/addressbook/libedata-book/e-book-backend-sexp.c
@@ -29,6 +29,7 @@
 G_DEFINE_TYPE (EBookBackendSExp, e_book_backend_sexp, G_TYPE_OBJECT)
 
 typedef struct _SearchContext SearchContext;
+typedef gboolean (*CompareFunc) (const gchar *, const gchar *);
 
 struct _EBookBackendSExpPrivate {
 	ESExp *search_sexp;
@@ -42,8 +43,7 @@ struct _SearchContext {
 static gboolean
 compare_im (EContact *contact,
             const gchar *str,
-            gchar * (*compare) (const gchar *,
-                                const gchar *),
+            CompareFunc compare,
             EContactField im_field)
 {
 	GList    *aims, *l;
@@ -68,8 +68,7 @@ compare_im (EContact *contact,
 static gboolean
 compare_im_aim (EContact *contact,
                 const gchar *str,
-                gchar * (*compare) (const gchar *,
-                                    const gchar *))
+                CompareFunc compare)
 {
 	return compare_im (contact, str, compare, E_CONTACT_IM_AIM);
 }
@@ -77,8 +76,7 @@ compare_im_aim (EContact *contact,
 static gboolean
 compare_im_msn (EContact *contact,
                 const gchar *str,
-                gchar * (*compare) (const gchar *,
-                                    const gchar *))
+                CompareFunc compare)
 {
 	return compare_im (contact, str, compare, E_CONTACT_IM_MSN);
 }
@@ -86,8 +84,7 @@ compare_im_msn (EContact *contact,
 static gboolean
 compare_im_skype (EContact *contact,
                   const gchar *str,
-                  gchar * (*compare) (const gchar *,
-                                      const gchar *))
+                  CompareFunc compare)
 {
 	return compare_im (contact, str, compare, E_CONTACT_IM_SKYPE);
 }
@@ -95,8 +92,7 @@ compare_im_skype (EContact *contact,
 static gboolean
 compare_im_google_talk (EContact *contact,
                         const gchar *str,
-                        gchar * (*compare) (const gchar *,
-                                            const gchar *))
+                        CompareFunc compare)
 {
 	return compare_im (contact, str, compare, E_CONTACT_IM_GOOGLE_TALK);
 }
@@ -104,8 +100,7 @@ compare_im_google_talk (EContact *contact,
 static gboolean
 compare_im_icq (EContact *contact,
                 const gchar *str,
-                gchar * (*compare) (const gchar *,
-                                    const gchar *))
+                CompareFunc compare)
 {
 	return compare_im (contact, str, compare, E_CONTACT_IM_ICQ);
 }
@@ -113,8 +108,7 @@ compare_im_icq (EContact *contact,
 static gboolean
 compare_im_yahoo (EContact *contact,
                   const gchar *str,
-                  gchar *(*compare) (const gchar *,
-                                     const gchar *))
+                  CompareFunc compare)
 {
 	return compare_im (contact, str, compare, E_CONTACT_IM_YAHOO);
 }
@@ -122,8 +116,7 @@ compare_im_yahoo (EContact *contact,
 static gboolean
 compare_im_gadugadu (EContact *contact,
                      const gchar *str,
-                     gchar * (*compare) (const gchar *,
-                                         const gchar *))
+                     CompareFunc compare)
 {
 	return compare_im (contact, str, compare, E_CONTACT_IM_GADUGADU);
 }
@@ -131,8 +124,7 @@ compare_im_gadugadu (EContact *contact,
 static gboolean
 compare_im_jabber (EContact *contact,
                    const gchar *str,
-                   gchar * (*compare) (const gchar *,
-                                       const gchar *))
+                   CompareFunc compare)
 {
 	return compare_im (contact, str, compare, E_CONTACT_IM_JABBER);
 }
@@ -140,8 +132,7 @@ compare_im_jabber (EContact *contact,
 static gboolean
 compare_im_groupwise (EContact *contact,
                       const gchar *str,
-                      gchar * (*compare) (const gchar *,
-                                          const gchar *))
+                      CompareFunc compare)
 {
 	return compare_im (contact, str, compare, E_CONTACT_IM_GROUPWISE);
 }
@@ -149,8 +140,7 @@ compare_im_groupwise (EContact *contact,
 static gboolean
 compare_email (EContact *contact,
                const gchar *str,
-               gchar * (*compare) (const gchar *,
-                                   const gchar *))
+               CompareFunc compare)
 {
 	gboolean rv = FALSE;
 	GList *list, *l;
@@ -174,8 +164,7 @@ compare_email (EContact *contact,
 static gboolean
 compare_phone (EContact *contact,
                const gchar *str,
-               gchar * (*compare) (const gchar *,
-                                   const gchar *))
+               CompareFunc compare)
 {
 	GList *list, *l;
 	gboolean rv = FALSE;
@@ -199,8 +188,7 @@ compare_phone (EContact *contact,
 static gboolean
 compare_name (EContact *contact,
               const gchar *str,
-              gchar * (*compare) (const gchar *,
-                                  const gchar *))
+              CompareFunc compare)
 {
 	const gchar *name;
 
@@ -226,8 +214,7 @@ compare_name (EContact *contact,
 static gboolean
 compare_photo_uri (EContact *contact,
                    const gchar *str,
-                   gchar * (*compare) (const gchar *,
-                                       const gchar *))
+                   CompareFunc compare)
 {
 	EContactPhoto *photo;
 	gboolean ret_val = FALSE;
@@ -248,8 +235,7 @@ compare_photo_uri (EContact *contact,
 static gboolean
 compare_address (EContact *contact,
                  const gchar *str,
-                 gchar * (*compare) (const gchar *,
-                                     const gchar *))
+                 CompareFunc compare)
 {
 
 	gint i;
@@ -280,8 +266,7 @@ compare_address (EContact *contact,
 static gboolean
 compare_category (EContact *contact,
                   const gchar *str,
-                  gchar * (*compare) (const gchar *,
-                                      const gchar *))
+                  CompareFunc compare)
 {
 	GList *categories;
 	GList *iterator;
@@ -307,8 +292,7 @@ compare_category (EContact *contact,
 static gboolean
 compare_date (EContactDate *date,
               const gchar *str,
-              gchar * (*compare) (const gchar *,
-                                  const gchar *))
+              CompareFunc compare)
 {
 	gchar *date_str = e_contact_date_to_string (date);
 	gboolean ret_val = FALSE;
@@ -334,8 +318,7 @@ static struct prop_info {
 	enum prop_type prop_type;
 	gboolean (*list_compare) (EContact *contact,
 				  const gchar *str,
-				  gchar * (*compare) (const gchar *,
-						      const gchar *));
+				  CompareFunc compare);
 
 } prop_info_table[] = {
 #define NORMAL_PROP(f,q) {f, q, PROP_TYPE_NORMAL, NULL}
@@ -389,8 +372,7 @@ entry_compare (SearchContext *ctx,
                struct _ESExp *f,
                gint argc,
                struct _ESExpResult **argv,
-               gchar * (*compare) (const gchar *,
-                                   const gchar *))
+               CompareFunc compare)
 {
 	ESExpResult *r;
 	gint truth = FALSE;
@@ -517,7 +499,7 @@ contains_helper_free_word (gpointer data,
 	}
 }
 
-static gchar *
+static gboolean
 try_contains_word (const gchar *s1,
                    GSList *word)
 {
@@ -526,11 +508,11 @@ try_contains_word (const gchar *s1,
 	GString *w;
 
 	if (s1 == NULL)
-		return NULL;
+		return FALSE;
 	if (word == NULL)
-		return (gchar *) s1; /* previous was last word */
+		return TRUE; /* previous was last word */
 	if (word->data == NULL)
-		return NULL; /* illegal structure */
+		return FALSE; /* illegal structure */
 
 	w = word->data;
 	first_w_char = g_utf8_get_char (w->str);
@@ -555,20 +537,20 @@ try_contains_word (const gchar *s1,
 				/* we read whole word and no illegal character has been found */
 				if (word->next == NULL ||
 				    try_contains_word (e_util_unicode_get_utf8 (o, &unival), word->next)) {
-					return (gchar *) o;
+					return TRUE;
 				}
 			}
 		}
 		o = p;
 	}
 
-	return NULL;
+	return FALSE;
 }
 
 /* first space between words is treated as wildcard character;
  * we are looking for s2 in s1, so s2 will be breaked into words
 */
-static gchar *
+static gboolean
 contains_helper (const gchar *s1,
                  const gchar *s2)
 {
@@ -579,25 +561,25 @@ contains_helper (const gchar *s1,
 	gboolean have_nonspace;
 	gboolean have_space;
 	GString *last_word, *w;
-	gchar *res = NULL;
+	gboolean res;
 	gunichar unich;
 	glong len1, len2;
 
 	if (!s2)
-		return NULL;
+		return FALSE;
 
 	/* the initial word contains an empty string for sure */
 	if (!*s2)
-		return (gchar *) s1;
+		return TRUE;
 
 	s1uni = e_util_utf8_normalize (s1);
 	if (s1uni == NULL)
-		return NULL;
+		return FALSE;
 
 	s2uni = e_util_utf8_normalize (s2);
 	if (s2uni == NULL) {
 		g_free (s1uni);
-		return NULL;
+		return FALSE;
 	}
 
 	len1 = g_utf8_strlen (s1uni, -1);
@@ -608,9 +590,9 @@ contains_helper (const gchar *s1,
 
 		/* both are empty strings */
 		if (len1 == len2)
-			return (gchar *) s1;
+			return TRUE;
 
-		return NULL;
+		return FALSE;
 	}
 
 	/* breaking s2 into words */
@@ -671,19 +653,18 @@ func_contains (struct _ESExp *f,
 	return entry_compare (ctx, f, argc, argv, contains_helper);
 }
 
-static gchar *
+static gboolean
 is_helper (const gchar *ps1,
            const gchar *ps2)
 {
-	gchar *s1, *s2, *res;
+	gchar *s1, *s2;
+	gboolean res = FALSE;
 
 	s1 = e_util_utf8_remove_accents (ps1);
 	s2 = e_util_utf8_remove_accents (ps2);
 
 	if (!e_util_utf8_strcasecmp (s1, s2))
-		res = (gchar *) ps1;
-	else
-		res = NULL;
+		res = TRUE;
 
 	g_free (s1);
 	g_free (s2);
@@ -702,20 +683,18 @@ func_is (struct _ESExp *f,
 	return entry_compare (ctx, f, argc, argv, is_helper);
 }
 
-static gchar *
+static gboolean
 endswith_helper (const gchar *ps1,
                  const gchar *ps2)
 {
 	gchar *s1 = e_util_utf8_remove_accents (ps1);
 	gchar *s2 = e_util_utf8_remove_accents (ps2);
-	gchar *res;
+	gboolean res = FALSE;
 	glong s1len = g_utf8_strlen (s1, -1);
 	glong s2len = g_utf8_strlen (s2, -1);
 
-	if (s1len < s2len)
-		res = NULL;
-	else
-		res = (gchar *) e_util_utf8_strstrcase (g_utf8_offset_to_pointer (s1, s1len - s2len), s2);
+	if (s1len >= s2len)
+		res = TRUE;
 
 	g_free (s1);
 	g_free (s2);
@@ -734,19 +713,18 @@ func_endswith (struct _ESExp *f,
 	return entry_compare (ctx, f, argc, argv, endswith_helper);
 }
 
-static gchar *
+static gboolean
 beginswith_helper (const gchar *ps1,
                    const gchar *ps2)
 {
-	gchar *p, *res;
+	gchar *p;
+	gboolean res = FALSE;
 	gchar *s1 = e_util_utf8_remove_accents (ps1);
 	gchar *s2 = e_util_utf8_remove_accents (ps2);
 
 	if ((p = (gchar *) e_util_utf8_strstrcase (s1, s2))
 	    && (p == s1))
-		res = (gchar *) ps1;
-	else
-		res = NULL;
+		res = TRUE;
 
 	g_free (s1);
 	g_free (s2);
@@ -765,15 +743,16 @@ func_beginswith (struct _ESExp *f,
 	return entry_compare (ctx, f, argc, argv, beginswith_helper);
 }
 
-static gchar *
+static gboolean
 exists_helper (const gchar *ps1,
                const gchar *ps2)
 {
-	gchar *res;
+	gboolean res = FALSE;
 	gchar *s1 = e_util_utf8_remove_accents (ps1);
 	gchar *s2 = e_util_utf8_remove_accents (ps2);
 
-	res = (gchar *) e_util_utf8_strstrcase (s1, s2);
+	if (e_util_utf8_strstrcase (s1, s2))
+		res = TRUE;
 
 	g_free (s1);
 	g_free (s2);



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]