[PATCH v2 5/7] Introduce _notmuch_message_has_term()

Daniel Kahn Gillmor dkg at fifthhorseman.net
Sat Apr 2 07:15:39 PDT 2016


It can be useful to easily tell if a given message has a given term
associated with it.
---
 lib/message.cc        | 49 +++++++++++++++++++++++++++++++++++++++++++++++++
 lib/notmuch-private.h | 13 +++++++++++++
 2 files changed, 62 insertions(+)

diff --git a/lib/message.cc b/lib/message.cc
index e414e9c..fab70fd 100644
--- a/lib/message.cc
+++ b/lib/message.cc
@@ -1216,6 +1216,55 @@ _notmuch_message_remove_term (notmuch_message_t *message,
     return NOTMUCH_PRIVATE_STATUS_SUCCESS;
 }
 
+notmuch_bool_t
+_notmuch_message_has_term (notmuch_message_t *message,
+			   const char *prefix_name,
+			   const char *value)
+{
+    notmuch_bool_t out;
+    notmuch_private_status_t st =
+	_notmuch_message_has_term_st (message, prefix_name, value, &out);
+    if (st)
+	return FALSE;
+    return out;
+}
+
+
+notmuch_private_status_t
+_notmuch_message_has_term_st (notmuch_message_t *message,
+			      const char *prefix_name,
+			      const char *value,
+			      notmuch_bool_t *result)
+{
+    char *term;
+    notmuch_bool_t out = FALSE;
+    notmuch_private_status_t status = NOTMUCH_PRIVATE_STATUS_SUCCESS;
+
+    if (value == NULL)
+	return NOTMUCH_PRIVATE_STATUS_NULL_POINTER;
+
+    term = talloc_asprintf (message, "%s%s",
+			    _find_prefix (prefix_name), value);
+
+    if (strlen (term) > NOTMUCH_TERM_MAX)
+	return NOTMUCH_PRIVATE_STATUS_TERM_TOO_LONG;
+
+    try {
+	/* Look for the exact term */
+	Xapian::TermIterator i = message->doc.termlist_begin ();
+	i.skip_to (term);
+	if (i != message->doc.termlist_end () &&
+	    !strcmp ((*i).c_str (), term))
+	    out = TRUE;
+    } catch (Xapian::Error &error) {
+	status = NOTMUCH_PRIVATE_STATUS_XAPIAN_EXCEPTION;
+    }
+    talloc_free (term);
+
+    *result = out;
+    return status;
+}
+
 notmuch_status_t
 notmuch_message_add_tag (notmuch_message_t *message, const char *tag)
 {
diff --git a/lib/notmuch-private.h b/lib/notmuch-private.h
index cbfc144..00391a9 100644
--- a/lib/notmuch-private.h
+++ b/lib/notmuch-private.h
@@ -279,6 +279,19 @@ _notmuch_message_remove_term (notmuch_message_t *message,
 			      const char *prefix_name,
 			      const char *value);
 
+/* _notmuch_message_has_term designed to be simple: if there is an
+ * error, it will return false */
+notmuch_bool_t
+_notmuch_message_has_term (notmuch_message_t *message,
+			   const char *prefix_name,
+			   const char *value);
+
+notmuch_private_status_t
+_notmuch_message_has_term_st (notmuch_message_t *message,
+			      const char *prefix_name,
+			      const char *value,
+			      notmuch_bool_t *result);
+
 notmuch_private_status_t
 _notmuch_message_gen_terms (notmuch_message_t *message,
 			    const char *prefix_name,
-- 
2.8.0.rc3



More information about the notmuch mailing list