unofficial mirror of notmuch@notmuchmail.org
 help / color / mirror / code / Atom feed
* [RFC PATCH] cli: factor out config handling code to get/set lists.
@ 2011-12-10 17:50 David Bremner
  2011-12-10 18:24 ` Austin Clements
  0 siblings, 1 reply; 15+ messages in thread
From: David Bremner @ 2011-12-10 17:50 UTC (permalink / raw)
  To: notmuch; +Cc: David Bremner

From: David Bremner <bremner@debian.org>

The code is already duplicated once, and I want to add a third
configuration item that is also a list.
---

Mainly I am curious if people think using macros to declare these
"getters" and "setters" makes the code less maintainable.

 notmuch-config.c |  112 +++++++++++++++++++----------------------------------
 1 files changed, 40 insertions(+), 72 deletions(-)

diff --git a/notmuch-config.c b/notmuch-config.c
index 1a7ed58..33778a7 100644
--- a/notmuch-config.c
+++ b/notmuch-config.c
@@ -520,93 +520,61 @@ notmuch_config_set_user_primary_email (notmuch_config_t *config,
     config->user_primary_email = NULL;
 }
 
-const char **
-notmuch_config_get_user_other_email (notmuch_config_t *config,
-				     size_t *length)
+static void
+_config_get_list (notmuch_config_t *config,
+		  const char *section, const char *key,
+		  const char ***outlist, size_t *length)
 {
-    char **emails;
-    size_t emails_length;
+    char **inlist;
     unsigned int i;
 
-    if (config->user_other_email == NULL) {
-	emails = g_key_file_get_string_list (config->key_file,
-					     "user", "other_email",
-					     &emails_length, NULL);
-	if (emails) {
-	    config->user_other_email = talloc_size (config,
-						    sizeof (char *) *
-						    (emails_length + 1));
-	    for (i = 0; i < emails_length; i++)
-		config->user_other_email[i] = talloc_strdup (config->user_other_email,
-							     emails[i]);
-	    config->user_other_email[i] = NULL;
-
-	    g_strfreev (emails);
-
-	    config->user_other_email_length = emails_length;
+    if (*outlist == NULL) {
+	inlist = g_key_file_get_string_list (config->key_file,
+					     section, key,
+					     length, NULL);
+	if (inlist) {
+	    *outlist = talloc_size (config, sizeof (char *) *
+				    (*length + 1));
+	    for (i = 0; i < *length; i++)
+		(*outlist)[i] = talloc_strdup (*outlist, inlist[i]);
+	    (*outlist)[i] = NULL;
+
+	    g_strfreev (inlist);
+
 	}
     }
 
-    *length = config->user_other_email_length;
-    return config->user_other_email;
 }
 
-void
-notmuch_config_set_user_other_email (notmuch_config_t *config,
-				     const char *other_email[],
-				     size_t length)
-{
-    g_key_file_set_string_list (config->key_file,
-				"user", "other_email",
-				other_email, length);
-
-    talloc_free (config->user_other_email);
-    config->user_other_email = NULL;
+#define _GET_LIST(list, group, name) \
+const char ** \
+notmuch_config_get_##list (notmuch_config_t *config, size_t *length) \
+{ \
+    _config_get_list (config, group, name, &(config->list),	\
+                      &(config->list##_length));		\
+    *length = config->list##_length;				\
+    return config->list; \
 }
 
-const char **
-notmuch_config_get_new_tags (notmuch_config_t *config,
-			     size_t *length)
-{
-    char **tags;
-    size_t tags_length;
-    unsigned int i;
+_GET_LIST (user_other_email, "user", "other_email");
+_GET_LIST (new_tags, "new", "tags");
 
-    if (config->new_tags == NULL) {
-	tags = g_key_file_get_string_list (config->key_file,
-					   "new", "tags",
-					   &tags_length, NULL);
-	if (tags) {
-	    config->new_tags = talloc_size (config,
-					    sizeof (char *) *
-					    (tags_length + 1));
-	    for (i = 0; i < tags_length; i++)
-		config->new_tags[i] = talloc_strdup (config->new_tags,
-						     tags[i]);
-	    config->new_tags[i] = NULL;
-
-	    g_strfreev (tags);
-
-	    config->new_tags_length = tags_length;
-	}
-    }
+#undef _GET_LIST
 
-    *length = config->new_tags_length;
-    return config->new_tags;
+#define _SET_LIST(list, group, name) \
+void \
+notmuch_config_set_##list (notmuch_config_t *config, const char *list[], \
+			   size_t length) \
+{ \
+    g_key_file_set_string_list (config->key_file, group, name, list, length); \
+    talloc_free (config->list);                                               \
+    config->list = NULL;                                                      \
 }
 
-void
-notmuch_config_set_new_tags (notmuch_config_t *config,
-			     const char *new_tags[],
-			     size_t length)
-{
-    g_key_file_set_string_list (config->key_file,
-				"new", "tags",
-				new_tags, length);
+_SET_LIST(user_other_email, "user", "other_email");
+_SET_LIST(new_tags, "new", "tags");
 
-    talloc_free (config->new_tags);
-    config->new_tags = NULL;
-}
+#undef _SET_LIST
 
 /* Given a configuration item of the form <group>.<key> return the
  * component group and key. If any error occurs, print a message on
-- 
1.7.7.3

^ permalink raw reply related	[flat|nested] 15+ messages in thread

end of thread, other threads:[~2012-01-16 16:50 UTC | newest]

Thread overview: 15+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2011-12-10 17:50 [RFC PATCH] cli: factor out config handling code to get/set lists David Bremner
2011-12-10 18:24 ` Austin Clements
2011-12-10 20:30   ` David Bremner
2011-12-11 16:07     ` [PATCH] " David Bremner
2011-12-11 16:41       ` Dmitry Kurochkin
2011-12-12 18:26         ` David Bremner
2012-01-12 17:26           ` Pieter Praet
2012-01-12 17:30             ` [PATCH] test: cli: getting/setting/removing config values Pieter Praet
2012-01-13  3:42               ` David Bremner
2012-01-14  8:54                 ` Pieter Praet
2012-01-14  8:57                   ` Pieter Praet
2012-01-14 12:16                     ` David Bremner
2012-01-16 10:31                       ` Pieter Praet
2012-01-16 11:37                         ` David Bremner
2012-01-16 16:48                           ` Pieter Praet

Code repositories for project(s) associated with this public inbox

	https://yhetil.org/notmuch.git/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).