[PATCH 3/4] Utility function to seek in MIME trees in depth-first order.

Subject: [PATCH 3/4] Utility function to seek in MIME trees in depth-first order.

Date: Sun, 27 Nov 2011 21:21:10 -0500

To: notmuch@notmuchmail.org

Cc: dkg@fifthhorseman.net

From: Austin Clements


This function matches how we number parts for the --part argument to
show.  It will allow us to jump directly to the desired part, rather
than traversing the entire tree and carefully tracking whether or not
we're "in the zone".
---
 mime-node.c      |   25 +++++++++++++++++++++++++
 notmuch-client.h |    5 +++++
 2 files changed, 30 insertions(+), 0 deletions(-)

diff --git a/mime-node.c b/mime-node.c
index 942738b..40fff75 100644
--- a/mime-node.c
+++ b/mime-node.c
@@ -232,3 +232,28 @@ mime_node_child (const mime_node_t *parent, int child)
 			g_type_name (G_OBJECT_TYPE (parent->part)));
     }
 }
+
+static mime_node_t *
+_mime_node_seek_dfs_walk (mime_node_t *node, int *n)
+{
+    mime_node_t *ret = NULL;
+    int i;
+
+    if (*n <= 0)
+	return node;
+
+    *n = *n - 1;
+    for (i = 0; i < node->children && !ret; i++) {
+	mime_node_t *child = mime_node_child (node, i);
+	ret = _mime_node_seek_dfs_walk (child, n);
+	if (!ret)
+	    talloc_free (child);
+    }
+    return ret;
+}
+
+mime_node_t *
+mime_node_seek_dfs (mime_node_t *node, int n)
+{
+    return _mime_node_seek_dfs_walk (node, &n);
+}
diff --git a/notmuch-client.h b/notmuch-client.h
index 58bd21c..8b1454f 100644
--- a/notmuch-client.h
+++ b/notmuch-client.h
@@ -318,4 +318,9 @@ mime_node_open (const void *ctx, notmuch_message_t *message,
 mime_node_t *
 mime_node_child (const mime_node_t *parent, int child);
 
+/* Return the nth child of node in a depth-first traversal.  If n is
+ * 0, returns node itself.  Returns NULL if there is no such part. */
+mime_node_t *
+mime_node_seek_dfs (mime_node_t *node, int n);
+
 #endif
-- 
1.7.5.4


Thread: