[WIP patch 8/9] lib: make string representation of metadata class public

David Bremner david at tethera.net
Sat Jan 9 18:51:40 PST 2016


This is needed for e.g. notmuch-dump
---
 lib/metadata.cc | 34 +++++++++++++++++++++++-----------
 lib/notmuch.h   |  5 +++++
 2 files changed, 28 insertions(+), 11 deletions(-)

diff --git a/lib/metadata.cc b/lib/metadata.cc
index 79a0a9e..34b46e0 100644
--- a/lib/metadata.cc
+++ b/lib/metadata.cc
@@ -34,16 +34,28 @@ _notmuch_metadata_destroy (notmuch_metadata_t *list) {
     delete list->iterator;
     return 0;
 }
-static
+
+typedef struct prefix {
+    notmuch_metadata_class_t mclass;
+    const char *prefix;
+} prefix_t;
+
+static prefix_t METADATA_PREFIX[] = {
+    {NOTMUCH_METADATA_CONFIG, "C"},
+};
+
+#define ARRAY_SIZE(arr) (sizeof (arr) / sizeof (arr[0]))
+
 const char *
-_find_metadata_prefix (notmuch_metadata_class_t mclass)
+notmuch_metadata_prefix_string (notmuch_metadata_class_t mclass)
 {
-    switch (mclass) {
-    case NOTMUCH_METADATA_CONFIG:
-	return "C";
-    default:
-	return NULL;
+    unsigned int i;
+
+    for (i=0; i < ARRAY_SIZE(METADATA_PREFIX); i++) {
+	if (METADATA_PREFIX[0].mclass == mclass)
+	    return METADATA_PREFIX[i].prefix;
     }
+    return NULL;
 }
 
 notmuch_status_t _make_key(void *ctx, notmuch_metadata_class_t mclass,
@@ -55,7 +67,7 @@ notmuch_status_t _make_key(void *ctx, notmuch_metadata_class_t mclass,
     if (!out)
 	return NOTMUCH_STATUS_NULL_POINTER;
 
-    prefix = _find_metadata_prefix(mclass);
+    prefix = notmuch_metadata_prefix_string(mclass);
     if (!prefix)
 	return NOTMUCH_STATUS_UNSUPPORTED_OPERATION;
 
@@ -169,7 +181,7 @@ notmuch_database_get_all_metadata (notmuch_database_t *notmuch,
     notmuch_metadata_t *list = NULL;
     notmuch_status_t status = NOTMUCH_STATUS_SUCCESS;
 
-    const char *prefix = _find_metadata_prefix (mclass);
+    const char *prefix = notmuch_metadata_prefix_string (mclass);
 
     list = talloc (notmuch, notmuch_metadata_t);
     if (!list) {
@@ -207,7 +219,7 @@ notmuch_database_get_all_metadata (notmuch_database_t *notmuch,
 notmuch_bool_t
 notmuch_metadata_valid (notmuch_metadata_t *metadata)
 {
-    const char *prefix = _find_metadata_prefix (metadata->mclass);
+    const char *prefix = notmuch_metadata_prefix_string (metadata->mclass);
     if (*(metadata->iterator) == metadata->notmuch->xapian_db->metadata_keys_end())
 	return FALSE;
 
@@ -217,7 +229,7 @@ notmuch_metadata_valid (notmuch_metadata_t *metadata)
 const char *
 notmuch_metadata_key (notmuch_metadata_t *metadata)
 {
-    const char *prefix = _find_metadata_prefix (metadata->mclass);
+    const char *prefix = notmuch_metadata_prefix_string (metadata->mclass);
 
     if (metadata->current_key)
 	talloc_free (metadata->current_key);
diff --git a/lib/notmuch.h b/lib/notmuch.h
index 6773253..657a7d6 100644
--- a/lib/notmuch.h
+++ b/lib/notmuch.h
@@ -1897,6 +1897,11 @@ notmuch_metadata_move_to_next (notmuch_metadata_t *metadata);
 void
 notmuch_metadata_destroy (notmuch_metadata_t * metadata);
 
+/**
+ * convert enum to string
+ */
+const char *
+notmuch_metadata_prefix_string (notmuch_metadata_class_t mclass);
 /* @} */
 
 NOTMUCH_END_DECLS
-- 
2.6.4



More information about the notmuch mailing list