From: Jani Nikula Date: Sun, 18 Aug 2013 15:20:04 +0000 (+0300) Subject: [PATCH 1/2] cli: abstract common config get/set code X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=b97cb25ca8f3c0ad4c42a35ae128753bd73ecbc8;p=notmuch-archives.git [PATCH 1/2] cli: abstract common config get/set code --- diff --git a/4e/064edad52986af8010252ef91986c7fd01ddcc b/4e/064edad52986af8010252ef91986c7fd01ddcc new file mode 100644 index 000000000..bb9cd1d41 --- /dev/null +++ b/4e/064edad52986af8010252ef91986c7fd01ddcc @@ -0,0 +1,196 @@ +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 578C7421161 + for ; Sun, 18 Aug 2013 08:20:09 -0700 (PDT) +X-Virus-Scanned: Debian amavisd-new at olra.theworths.org +X-Spam-Flag: NO +X-Spam-Score: -0.7 +X-Spam-Level: +X-Spam-Status: No, score=-0.7 tagged_above=-999 required=5 + tests=[RCVD_IN_DNSWL_LOW=-0.7] 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 tF2-jhiNVfu0 for ; + Sun, 18 Aug 2013 08:20:03 -0700 (PDT) +Received: from mail-bk0-f42.google.com (mail-bk0-f42.google.com + [209.85.214.42]) (using TLSv1 with cipher RC4-SHA (128/128 bits)) + (No client certificate requested) + by olra.theworths.org (Postfix) with ESMTPS id A2643431FDB + for ; Sun, 18 Aug 2013 08:20:03 -0700 (PDT) +Received: by mail-bk0-f42.google.com with SMTP id my10so1149927bkb.15 + for ; Sun, 18 Aug 2013 08:20:02 -0700 (PDT) +X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; + d=google.com; s=20120113; + h=x-gm-message-state:from:to:cc:subject:date:message-id; + bh=TBmMXK6A1uOEclkvE4Y9Fkd7wwtTW/kPnPShADis+z8=; + b=JtkT3tlft7sSpM59+W98hXJnLltIhEa5mgmqhPev/bjyTB4WQLbBcS1SMdy2vtilZF + KAKJBDs9wkkVnTaizV7lYBhOQ0srVIGDp6IKB0nqWC2rWLefESO0A4gGrivzFJGeEbVD + gukAmvNMN0gJHXky6jLOHHyeRPhH/VpB6jyzUBeH0b0J7svxZvPVkZhAJ1d2BHhCA/yY + a0n+xSWplbYyvy3EkU0Xi0fYGxqJZedyU8DvDLMCZJb1hAfUiO2z8wYT1D2Qb2K8wWic + IMjFPVMTPFOFHLtn9vs5tIGV9r2Pnp3UJcmyCldqIMsHIDe9MlhM2BShC1rKEsbeyU5Y + F07g== +X-Gm-Message-State: + ALoCoQk9uEvZlGyq8kGQuSmrzA36DauMGDLM7WUaZ2C/0jZV302hIuNSpgXJGHpLnNNO45LpLWTC +X-Received: by 10.204.70.1 with SMTP id b1mr4827999bkj.3.1376839202312; + Sun, 18 Aug 2013 08:20:02 -0700 (PDT) +Received: from localhost (dsl-hkibrasgw2-58c36f-91.dhcp.inet.fi. + [88.195.111.91]) + by mx.google.com with ESMTPSA id ku9sm951140bkb.1.1969.12.31.16.00.00 + (version=TLSv1.2 cipher=RC4-SHA bits=128/128); + Sun, 18 Aug 2013 08:20:01 -0700 (PDT) +From: Jani Nikula +To: notmuch@notmuchmail.org +Subject: [PATCH 1/2] cli: abstract common config get/set code +Date: Sun, 18 Aug 2013 18:20:04 +0300 +Message-Id: <1376839205-5115-1-git-send-email-jani@nikula.org> +X-Mailer: git-send-email 1.7.10.4 +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: Sun, 18 Aug 2013 15:20:09 -0000 + +Pretty straightforward abstraction similar to get/set list. +--- + notmuch-config.c | 80 ++++++++++++++++++++---------------------------------- + 1 file changed, 29 insertions(+), 51 deletions(-) + +diff --git a/notmuch-config.c b/notmuch-config.c +index befe9b5..305d213 100644 +--- a/notmuch-config.c ++++ b/notmuch-config.c +@@ -496,6 +496,29 @@ notmuch_config_is_new (notmuch_config_t *config) + return config->is_new; + } + ++static const char * ++_config_get (notmuch_config_t *config, char **field, ++ const char *group, const char *key) ++{ ++ if (*field == NULL) { ++ char *value; ++ value = g_key_file_get_string (config->key_file, group, key, NULL); ++ if (value) { ++ *field = talloc_strdup (config, value); ++ free (value); ++ } ++ } ++ return *field; ++} ++ ++static void ++_config_set (notmuch_config_t *config, char **field, ++ const char *group, const char *key, const char *value) ++{ ++ g_key_file_set_string (config->key_file, group, key, value); ++ talloc_free (*field); ++ *field = NULL; ++} + + static const char ** + _config_get_list (notmuch_config_t *config, +@@ -542,85 +565,40 @@ _config_set_list (notmuch_config_t *config, + const char * + notmuch_config_get_database_path (notmuch_config_t *config) + { +- char *path; +- +- if (config->database_path == NULL) { +- path = g_key_file_get_string (config->key_file, +- "database", "path", NULL); +- if (path) { +- config->database_path = talloc_strdup (config, path); +- free (path); +- } +- } +- +- return config->database_path; ++ return _config_get (config, &config->database_path, "database", "path"); + } + + void + notmuch_config_set_database_path (notmuch_config_t *config, + const char *database_path) + { +- g_key_file_set_string (config->key_file, +- "database", "path", database_path); +- +- talloc_free (config->database_path); +- config->database_path = NULL; ++ _config_set (config, &config->database_path, "database", "path", database_path); + } + + const char * + notmuch_config_get_user_name (notmuch_config_t *config) + { +- char *name; +- +- if (config->user_name == NULL) { +- name = g_key_file_get_string (config->key_file, +- "user", "name", NULL); +- if (name) { +- config->user_name = talloc_strdup (config, name); +- free (name); +- } +- } +- +- return config->user_name; ++ return _config_get (config, &config->user_name, "user", "name"); + } + + void + notmuch_config_set_user_name (notmuch_config_t *config, + const char *user_name) + { +- g_key_file_set_string (config->key_file, +- "user", "name", user_name); +- +- talloc_free (config->user_name); +- config->user_name = NULL; ++ _config_set (config, &config->user_name, "user", "name", user_name); + } + + const char * + notmuch_config_get_user_primary_email (notmuch_config_t *config) + { +- char *email; +- +- if (config->user_primary_email == NULL) { +- email = g_key_file_get_string (config->key_file, +- "user", "primary_email", NULL); +- if (email) { +- config->user_primary_email = talloc_strdup (config, email); +- free (email); +- } +- } +- +- return config->user_primary_email; ++ return _config_get (config, &config->user_primary_email, "user", "primary_email"); + } + + void + notmuch_config_set_user_primary_email (notmuch_config_t *config, + const char *primary_email) + { +- g_key_file_set_string (config->key_file, +- "user", "primary_email", primary_email); +- +- talloc_free (config->user_primary_email); +- config->user_primary_email = NULL; ++ _config_set (config, &config->user_primary_email, "user", "primary_email", primary_email); + } + + const char ** +-- +1.7.10.4 +