[PATCH 1/3] added function notmuch_talloc_g_key_file_get_string_list()

Subject:[PATCH 1/3] added function notmuch_talloc_g_key_file_get_string_list()

Date:Wed, 14 Sep 2011 00:32:02 +0300

To:notmuch@notmuchmail.org

Cc:Tomi Ollila

From:tomi.ollila@iki.fi


From: Tomi Ollila <tomi.ollila@iki.fi>

The function notmuch_talloc_g_key_file_get_string_list() wraps
call to g_key_file_get_string_list() in a way that the returned
string array is copied to talloc'd memory area. The returned
pointer is itself also a talloc context, child of the context
given as first argument.
---
 notmuch-config.c |   44 +++++++++++++++++++++++++++++++++++++++++---
 1 files changed, 41 insertions(+), 3 deletions(-)

diff --git a/notmuch-config.c b/notmuch-config.c
index 485fa72..706f481 100644
--- a/notmuch-config.c
+++ b/notmuch-config.c
@@ -170,6 +170,44 @@ get_username_from_passwd_file (void *ctx)
     return name;
 }
 
+/** XXX move to (not-yet-existent) notmuch-talloc.c, or somewhere */
+static char **
+notmuch_talloc_g_key_file_get_string_list (const void * ctx,
+					   GKeyFile *key_file,
+					   const gchar *group_name,
+					   const gchar *key,
+					   gsize *length,
+					   GError **error)
+{
+    char ** newlist;
+    gchar ** strlist = g_key_file_get_string_list (key_file, group_name, key,
+						   length, error);
+    if (strlist) {
+	int i;
+	int l = *length;
+
+	newlist = talloc_array (ctx, char *, l + 1);
+	if (newlist == NULL)
+	    goto fail1;
+	for (i = 0; i < l; i++) {
+	    if ( (newlist[i] = talloc_strdup (newlist, strlist[i])) == NULL)
+		goto fail2;
+	}
+	newlist[i] = NULL;
+	g_strfreev (strlist);
+
+	return newlist;
+    }
+    return NULL;
+
+fail2:
+    talloc_free (newlist);
+fail1:
+    g_strfreev (strlist);
+    *length = 0; /* like in g_key_file_get_string_list () */
+    return NULL;
+}
+
 /* Open the named notmuch configuration file. If the filename is NULL,
  * the value of the environment variable $NOTMUCH_CONFIG will be used.
  * If $NOTMUCH_CONFIG is unset, the default configuration file
@@ -229,7 +267,7 @@ notmuch_config_open (void *ctx,
 	fprintf (stderr, "Out of memory.\n");
 	return NULL;
     }
-    
+
     talloc_set_destructor (config, notmuch_config_destructor);
 
     if (filename) {
@@ -393,7 +431,7 @@ notmuch_config_open (void *ctx,
 }
 
 /* Close the given notmuch_config_t object, freeing all resources.
- * 
+ *
  * Note: Any changes made to the configuration are *not* saved by this
  * function. To save changes, call notmuch_config_save before
  * notmuch_config_close.
@@ -653,7 +691,7 @@ notmuch_config_command_get (void *ctx, char *item)
     } else if (strcmp(item, "user.other_email") == 0) {
 	const char **other_email;
 	size_t i, length;
-	
+
 	other_email = notmuch_config_get_user_other_email (config, &length);
 	for (i = 0; i < length; i++)
 	    printf ("%s\n", other_email[i]);
-- 
1.7.3.4


Thread: