Add e_util_strdup_strip().
authorMatthew Barnes <mbarnes@redhat.com>
Tue, 22 May 2012 23:55:03 +0000 (19:55 -0400)
committerMatthew Barnes <mbarnes@redhat.com>
Wed, 23 May 2012 00:01:32 +0000 (20:01 -0400)
Duplicates an input string and strips off any leading or trailing
whitespace.  The resulting string is returned unless it is empty or
NULL, in which case the function returns NULL.

Useful in "set" functions for string properties.
Helps avoid expressions like (str != NULL && *str != '\0').

docs/reference/libedataserver/libedataserver-sections.txt
libedataserver/e-data-server-util.c
libedataserver/e-data-server-util.h

index 9936e20..e666219 100644 (file)
@@ -534,6 +534,7 @@ tm
 e_get_user_cache_dir
 e_get_user_config_dir
 e_get_user_data_dir
+e_util_strdup_strip
 e_util_strstrcase
 e_util_unicode_get_utf8
 e_util_utf8_strstrcase
index 0d7e891..37f2bf7 100644 (file)
@@ -124,6 +124,37 @@ e_get_user_data_dir (void)
 }
 
 /**
+ * e_util_strdup_strip:
+ * @string: (allow-none): a string value, or %NULL
+ *
+ * Duplicates @string and strips off any leading or trailing whitespace.
+ * The resulting string is returned unless it is empty or %NULL, in which
+ * case the function returns %NULL.
+ *
+ * Free the returned string with g_free().
+ *
+ * Returns: a newly-allocated, stripped copy of @string, or %NULL
+ *
+ * Since: 3.6
+ **/
+gchar *
+e_util_strdup_strip (const gchar *string)
+{
+       gchar *duplicate;
+
+       duplicate = g_strdup (string);
+       if (duplicate != NULL) {
+               g_strstrip (duplicate);
+               if (*duplicate == '\0') {
+                       g_free (duplicate);
+                       duplicate = NULL;
+               }
+       }
+
+       return duplicate;
+}
+
+/**
  * e_util_strstrcase:
  * @haystack: The string to search in.
  * @needle: The string to search for.
index 4deb2c9..f9ba9cb 100644 (file)
@@ -33,6 +33,7 @@ const gchar * e_get_user_cache_dir            (void);
 const gchar *  e_get_user_config_dir           (void);
 const gchar *  e_get_user_data_dir             (void);
 
+gchar *                e_util_strdup_strip             (const gchar *string);
 gchar *                e_util_strstrcase               (const gchar *haystack,
                                                 const gchar *needle);
 gchar *                e_util_unicode_get_utf8         (const gchar *text,