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 ACEEF6DE0C42 for ; Thu, 10 May 2018 23:58:15 -0700 (PDT) X-Virus-Scanned: Debian amavisd-new at cworth.org X-Spam-Flag: NO X-Spam-Score: -0.018 X-Spam-Level: X-Spam-Status: No, score=-0.018 tagged_above=-999 required=5 tests=[AWL=-0.018, RCVD_IN_DNSWL_NONE=-0.0001] 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 RniMxQ_2Ez1w for ; Thu, 10 May 2018 23:58:14 -0700 (PDT) Received: from che.mayfirst.org (che.mayfirst.org [162.247.75.118]) by arlo.cworth.org (Postfix) with ESMTPS id 524F26DE0B0A for ; Thu, 10 May 2018 23:58:12 -0700 (PDT) Received: from fifthhorseman.net (unknown [38.109.115.130]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by che.mayfirst.org (Postfix) with ESMTPSA id A8C17F9A1 for ; Fri, 11 May 2018 02:58:08 -0400 (EDT) Received: by fifthhorseman.net (Postfix, from userid 1000) id 6008220C51; Fri, 11 May 2018 02:58:03 -0400 (EDT) From: Daniel Kahn Gillmor To: Notmuch Mail Subject: [PATCH v3 2/8] properties: add notmuch_message_count_properties Date: Fri, 11 May 2018 02:57:53 -0400 Message-Id: <20180511065759.8556-3-dkg@fifthhorseman.net> X-Mailer: git-send-email 2.17.0 In-Reply-To: <20180511065759.8556-1-dkg@fifthhorseman.net> References: <20180511065759.8556-1-dkg@fifthhorseman.net> X-BeenThere: notmuch@notmuchmail.org X-Mailman-Version: 2.1.26 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: Fri, 11 May 2018 06:58:15 -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 210a15cc..710ba046 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 06842517..df8f534a 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.2 (notmuch 0.27) + */ +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.17.0