From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mp2 ([2001:41d0:2:4a6f::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by ms11 with LMTPS id UHIgMcdHHWDvOAAA0tVLHw (envelope-from ) for ; Fri, 05 Feb 2021 13:27:35 +0000 Received: from aspmx1.migadu.com ([2001:41d0:2:4a6f::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by mp2 with LMTPS id ON/oLMdHHWDyOwAAB5/wlQ (envelope-from ) for ; Fri, 05 Feb 2021 13:27:35 +0000 Received: from mail.notmuchmail.org (nmbug.tethera.net [144.217.243.247]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) server-signature RSA-PSS (2048 bits)) (No client certificate requested) by aspmx1.migadu.com (Postfix) with ESMTPS id 289A8940309 for ; Fri, 5 Feb 2021 13:27:35 +0000 (UTC) Received: from nmbug.tethera.net (localhost [127.0.0.1]) by mail.notmuchmail.org (Postfix) with ESMTP id 0E97F1FBB3; Fri, 5 Feb 2021 08:27:11 -0500 (EST) Received: from fethera.tethera.net (fethera.tethera.net [IPv6:2607:5300:60:c5::1]) by mail.notmuchmail.org (Postfix) with ESMTP id 9E5DB1FBA9 for ; Fri, 5 Feb 2021 08:27:05 -0500 (EST) Received: by fethera.tethera.net (Postfix, from userid 1001) id 827C4606D9; Fri, 5 Feb 2021 08:27:05 -0500 (EST) Received: (nullmailer pid 3258396 invoked by uid 1000); Fri, 05 Feb 2021 13:27:02 -0000 From: David Bremner To: notmuch@notmuchmail.org Cc: David Bremner Subject: [PATCH 11/39] lib/config: make values iterators restartable Date: Fri, 5 Feb 2021 09:26:26 -0400 Message-Id: <20210205132654.3258292-12-david@tethera.net> X-Mailer: git-send-email 2.30.0 In-Reply-To: <20210205132654.3258292-1-david@tethera.net> References: <20210205132654.3258292-1-david@tethera.net> MIME-Version: 1.0 Message-ID-Hash: OROTR33AWLE5CWM2OUH5L2JU4EIW33R3 X-Message-ID-Hash: OROTR33AWLE5CWM2OUH5L2JU4EIW33R3 X-MailFrom: bremner@tethera.net X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; emergency; loop; banned-address; member-moderation; header-match-notmuch.notmuchmail.org-0; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; suspicious-header X-Mailman-Version: 3.2.1 Precedence: list List-Id: "Use and development of the notmuch mail system." List-Help: List-Post: List-Subscribe: List-Unsubscribe: Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit X-Migadu-Flow: FLOW_IN X-Migadu-Spam-Score: -0.94 Authentication-Results: aspmx1.migadu.com; dkim=none; dmarc=none; spf=pass (aspmx1.migadu.com: domain of notmuch-bounces@notmuchmail.org designates 144.217.243.247 as permitted sender) smtp.mailfrom=notmuch-bounces@notmuchmail.org X-Migadu-Queue-Id: 289A8940309 X-Spam-Score: -0.94 X-Migadu-Scanner: scn1.migadu.com X-TUID: lGGCC5avKnFq This is relatively cheap, and makes it easier to transform existing code which uses arrays of pointers to store configuration lists. --- lib/config.cc | 43 +++++++++++++++++++++++++++++++++--------- lib/notmuch.h | 12 ++++++++++++ test/T590-libconfig.sh | 32 +++++++++++++++++++++++++++++++ 3 files changed, 78 insertions(+), 9 deletions(-) diff --git a/lib/config.cc b/lib/config.cc index 0fe9a268..b2957f0c 100644 --- a/lib/config.cc +++ b/lib/config.cc @@ -34,6 +34,8 @@ struct _notmuch_config_list { struct _notmuch_config_values { const char *iterator; size_t tok_len; + const char *string; + void *children; /* talloc_context */ }; static const char * _notmuch_config_key_to_string (notmuch_config_key_t key); @@ -256,23 +258,33 @@ _notmuch_config_load_from_database (notmuch_database_t *notmuch) notmuch_config_values_t * notmuch_config_get_values (notmuch_database_t *notmuch, notmuch_config_key_t key) { - notmuch_config_values_t *values; + notmuch_config_values_t *values = NULL; + bool ok = false; - const char *str; const char *key_str = _notmuch_config_key_to_string (key); if (! key_str) - return NULL; - - str = _notmuch_string_map_get (notmuch->config, key_str); - if (! str) - return NULL; + goto DONE; values = talloc (notmuch, notmuch_config_values_t); if (unlikely(! values)) - return NULL; + goto DONE; + + values->children = talloc_new (values); + + values->string = _notmuch_string_map_get (notmuch->config, key_str); + if (! values->string) + goto DONE; + + values->iterator = strsplit_len (values->string, ';', &(values->tok_len)); + ok = true; - values->iterator = strsplit_len (str, ';', &(values->tok_len)); + DONE: + if (!ok) { + if (values) + talloc_free(values); + return NULL; + } return values; } @@ -289,6 +301,19 @@ notmuch_config_values_get (notmuch_config_values_t *values) { return talloc_strndup (values, values->iterator, values->tok_len); } +void +notmuch_config_values_start (notmuch_config_values_t *values) { + if (values == NULL) + return; + if (values->children) { + talloc_free (values->children); + } + + values->children = talloc_new (values); + + values->iterator = strsplit_len (values->string, ';', &(values->tok_len)); +} + void notmuch_config_values_move_to_next (notmuch_config_values_t *values) { values->iterator += values->tok_len; diff --git a/lib/notmuch.h b/lib/notmuch.h index 7aeff567..39f39423 100644 --- a/lib/notmuch.h +++ b/lib/notmuch.h @@ -2509,6 +2509,18 @@ notmuch_config_values_get (notmuch_config_values_t *values); void notmuch_config_values_move_to_next (notmuch_config_values_t *values); + +/** + * reset the 'values' iterator to the first element + * + * @param[in,out] values iterator. A NULL value is ignored. + * + * @since libnotmuch 5.4 (notmuch 0.32) + * + */ +void +notmuch_config_values_start (notmuch_config_values_t *values); + /** * Destroy a config values iterator, along with any associated * resources. diff --git a/test/T590-libconfig.sh b/test/T590-libconfig.sh index efbee61d..ab439078 100755 --- a/test/T590-libconfig.sh +++ b/test/T590-libconfig.sh @@ -224,6 +224,38 @@ EOF test_expect_equal_file EXPECTED OUTPUT restore_database +test_begin_subtest "notmuch_config_get_values (restart)" +cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR} ${NOTMUCH_CONFIG} %NULL% +{ + notmuch_config_values_t *values; + EXPECT0(notmuch_config_set (db, NOTMUCH_CONFIG_NEW_TAGS, "a;b;c")); + for (values = notmuch_config_get_values (db, NOTMUCH_CONFIG_NEW_TAGS); + notmuch_config_values_valid (values); + notmuch_config_values_move_to_next (values)) + { + puts (notmuch_config_values_get (values)); + } + for (notmuch_config_values_start (values); + notmuch_config_values_valid (values); + notmuch_config_values_move_to_next (values)) + { + puts (notmuch_config_values_get (values)); + } +} +EOF +cat <<'EOF' >EXPECTED +== stdout == +a +b +c +a +b +c +== stderr == +EOF +test_expect_equal_file EXPECTED OUTPUT +restore_database + backup_database test_begin_subtest "notmuch_config_get_values, trailing ;" cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR} ${NOTMUCH_CONFIG} %NULL% -- 2.30.0