From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from localhost (localhost [127.0.0.1]) by arlo.cworth.org (Postfix) with ESMTP id 8F37D6DE0941 for ; Mon, 11 Dec 2017 20:49:14 -0800 (PST) X-Virus-Scanned: Debian amavisd-new at cworth.org X-Spam-Flag: NO X-Spam-Score: -0.027 X-Spam-Level: X-Spam-Status: No, score=-0.027 tagged_above=-999 required=5 tests=[AWL=-0.027] autolearn=disabled Received: from arlo.cworth.org ([127.0.0.1]) by localhost (arlo.cworth.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id xdjWw0sEdljn for ; Mon, 11 Dec 2017 20:49:13 -0800 (PST) Received: from che.mayfirst.org (che.mayfirst.org [162.247.75.118]) by arlo.cworth.org (Postfix) with ESMTPS id C732A6DE0243 for ; Mon, 11 Dec 2017 20:49:13 -0800 (PST) Received: from fifthhorseman.net (unknown [38.109.115.130]) by che.mayfirst.org (Postfix) with ESMTPSA id DB170F99B for ; Mon, 11 Dec 2017 23:49:10 -0500 (EST) Received: by fifthhorseman.net (Postfix, from userid 1000) id 173DD20B95; Mon, 11 Dec 2017 23:49:07 -0500 (EST) From: Daniel Kahn Gillmor To: Notmuch Mail Subject: [PATCH v2] properties: add notmuch_message_count_properties Date: Mon, 11 Dec 2017 23:49:07 -0500 Message-Id: <20171212044907.15988-1-dkg@fifthhorseman.net> X-Mailer: git-send-email 2.15.1 In-Reply-To: <20171212025225.11854-3-dkg@fifthhorseman.net> References: <20171212025225.11854-3-dkg@fifthhorseman.net> X-BeenThere: notmuch@notmuchmail.org X-Mailman-Version: 2.1.23 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: Tue, 12 Dec 2017 04:49:14 -0000 The user can already do this manually, of course, but (a) it's nice to have a convenience function, and (b) exposing this interface means that someone more clever with a _notmuch_string_map_t than i am can write a more efficient version if they like, and it will just accelerate the users of the convenience function. --- lib/message-property.cc | 25 +++++++++++++++++++++++++ lib/notmuch.h | 16 ++++++++++++++++ 2 files changed, 41 insertions(+) diff --git a/lib/message-property.cc b/lib/message-property.cc index 2e44a386..7894016c 100644 --- a/lib/message-property.cc +++ b/lib/message-property.cc @@ -36,6 +36,31 @@ notmuch_message_get_property (notmuch_message_t *message, const char *key, const return NOTMUCH_STATUS_SUCCESS; } +notmuch_status_t +notmuch_message_count_properties (notmuch_message_t *message, const char *key, unsigned int *count) +{ + if (! count || ! key || ! message) + return NOTMUCH_STATUS_NULL_POINTER; + + notmuch_string_map_t *map; + map = _notmuch_message_property_map (message); + if (! map) + return NOTMUCH_STATUS_NULL_POINTER; + + notmuch_string_map_iterator_t *matcher = _notmuch_string_map_iterator_create (map, key, true); + if (! matcher) + return NOTMUCH_STATUS_OUT_OF_MEMORY; + + *count = 0; + while (_notmuch_string_map_iterator_valid (matcher)) { + (*count)++; + _notmuch_string_map_iterator_move_to_next (matcher); + } + + _notmuch_string_map_iterator_destroy (matcher); + return NOTMUCH_STATUS_SUCCESS; +} + static notmuch_status_t _notmuch_message_modify_property (notmuch_message_t *message, const char *key, const char *value, bool delete_it) diff --git a/lib/notmuch.h b/lib/notmuch.h index bcf9b68a..141425ee 100644 --- a/lib/notmuch.h +++ b/lib/notmuch.h @@ -1890,6 +1890,22 @@ typedef struct _notmuch_string_map_iterator notmuch_message_properties_t; notmuch_message_properties_t * notmuch_message_get_properties (notmuch_message_t *message, const char *key, notmuch_bool_t exact); +/** + * Return the number of properties named "key" belonging to the specific message. + * + * @param[in] message The message to examine + * @param[in] key key to count + * @param[out] count The number of matching properties associated with this message. + * + * @returns + * + * NOTMUCH_STATUS_SUCCESS: successful count, possibly some other error. + * + * @since libnotmuch 5.1 (notmuch 0.26) + */ +notmuch_status_t +notmuch_message_count_properties (notmuch_message_t *message, const char *key, unsigned int *count); + /** * Is the given *properties* iterator pointing at a valid (key,value) * pair. -- 2.15.1