]> git.notmuchmail.org Git - notmuch/commitdiff
Utility function to seek in MIME trees in depth-first order.
authorAustin Clements <amdragon@MIT.EDU>
Sat, 24 Dec 2011 18:52:45 +0000 (13:52 -0500)
committerDavid Bremner <bremner@debian.org>
Mon, 26 Dec 2011 02:21:04 +0000 (22:21 -0400)
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
notmuch-client.h

index 760c55f63aa7fe08fa2f267fe2de34ecddea780d..d26bb445db142173dd3e38b0c4dec53d5da1341b 100644 (file)
@@ -236,3 +236,30 @@ mime_node_child (const mime_node_t *parent, int child)
     }
     return _mime_node_create (parent, sub);
 }
     }
     return _mime_node_create (parent, sub);
 }
+
+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 -= 1;
+    for (i = 0; i < node->nchildren && !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)
+{
+    if (n < 0)
+       return NULL;
+    return _mime_node_seek_dfs_walk (node, &n);
+}
index 64b255c7f019a689be1abbe136b72f12a4c0b68d..517c010a5db1dafef2e2817f753b9b4e33254398 100644 (file)
@@ -324,5 +324,10 @@ mime_node_open (const void *ctx, notmuch_message_t *message,
 mime_node_t *
 mime_node_child (const mime_node_t *parent, int child);
 
 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);
+
 #include "command-line-arguments.h"
 #endif
 #include "command-line-arguments.h"
 #endif