Return-Path: X-Original-To: notmuch@notmuchmail.org Delivered-To: notmuch@notmuchmail.org Received: from localhost (localhost [127.0.0.1]) by olra.theworths.org (Postfix) with ESMTP id 8753C429E25 for ; Sat, 10 Dec 2011 09:50:48 -0800 (PST) X-Virus-Scanned: Debian amavisd-new at olra.theworths.org X-Spam-Flag: NO X-Spam-Score: -2.3 X-Spam-Level: X-Spam-Status: No, score=-2.3 tagged_above=-999 required=5 tests=[RCVD_IN_DNSWL_MED=-2.3] autolearn=disabled Received: from olra.theworths.org ([127.0.0.1]) by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id 0oQl0tXpRyNe for ; Sat, 10 Dec 2011 09:50:48 -0800 (PST) Received: from tempo.its.unb.ca (tempo.its.unb.ca [131.202.1.21]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) by olra.theworths.org (Postfix) with ESMTPS id D06D9431FB6 for ; Sat, 10 Dec 2011 09:50:47 -0800 (PST) Received: from zancas.localnet (fctnnbsc36w-156034079193.pppoe-dynamic.High-Speed.nb.bellaliant.net [156.34.79.193]) (authenticated bits=0) by tempo.its.unb.ca (8.13.8/8.13.8) with ESMTP id pBAHoeqc002191 (version=TLSv1/SSLv3 cipher=AES256-SHA bits=256 verify=NO); Sat, 10 Dec 2011 13:50:43 -0400 Received: from bremner by zancas.localnet with local (Exim 4.77) (envelope-from ) id 1RZR4K-00048b-GR; Sat, 10 Dec 2011 13:50:40 -0400 From: David Bremner To: notmuch@notmuchmail.org Subject: [RFC PATCH] cli: factor out config handling code to get/set lists. Date: Sat, 10 Dec 2011 13:50:38 -0400 Message-Id: <1323539438-15869-1-git-send-email-david@tethera.net> X-Mailer: git-send-email 1.7.7.3 Cc: David Bremner X-BeenThere: notmuch@notmuchmail.org X-Mailman-Version: 2.1.13 Precedence: list List-Id: "Use and development of the notmuch mail system." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sat, 10 Dec 2011 17:50:48 -0000 From: David Bremner 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 . return the * component group and key. If any error occurs, print a message on -- 1.7.7.3