Return-Path: X-Original-To: notmuch@notmuchmail.org Delivered-To: notmuch@notmuchmail.org Received: from localhost (localhost [127.0.0.1]) by olra.theworths.org (Postfix) with ESMTP id 0110D429E3F for ; Fri, 23 Dec 2011 19:46:07 -0800 (PST) X-Virus-Scanned: Debian amavisd-new at olra.theworths.org X-Spam-Flag: NO X-Spam-Score: -0.7 X-Spam-Level: X-Spam-Status: No, score=-0.7 tagged_above=-999 required=5 tests=[RCVD_IN_DNSWL_LOW=-0.7] autolearn=disabled Received: from olra.theworths.org ([127.0.0.1]) by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id tI3wPvrh2uxG for ; Fri, 23 Dec 2011 19:46:06 -0800 (PST) Received: from dmz-mailsec-scanner-1.mit.edu (DMZ-MAILSEC-SCANNER-1.MIT.EDU [18.9.25.12]) by olra.theworths.org (Postfix) with ESMTP id 63668429E30 for ; Fri, 23 Dec 2011 19:46:06 -0800 (PST) X-AuditID: 1209190c-b7fad6d000000920-43-4ef54afd2be6 Received: from mailhub-auth-1.mit.edu ( [18.9.21.35]) by dmz-mailsec-scanner-1.mit.edu (Symantec Messaging Gateway) with SMTP id AF.2C.02336.DFA45FE4; Fri, 23 Dec 2011 22:46:05 -0500 (EST) Received: from outgoing.mit.edu (OUTGOING-AUTH.MIT.EDU [18.7.22.103]) by mailhub-auth-1.mit.edu (8.13.8/8.9.2) with ESMTP id pBO3k5Zf003762; Fri, 23 Dec 2011 22:46:05 -0500 Received: from drake.mit.edu (c-76-21-105-205.hsd1.ca.comcast.net [76.21.105.205]) (authenticated bits=0) (User authenticated as amdragon@ATHENA.MIT.EDU) by outgoing.mit.edu (8.13.6/8.12.4) with ESMTP id pBO3k3Uf017818 (version=TLSv1/SSLv3 cipher=AES256-SHA bits=256 verify=NOT); Fri, 23 Dec 2011 22:46:05 -0500 (EST) Received: from amthrax by drake.mit.edu with local (Exim 4.77) (envelope-from ) id 1ReIYd-0007IO-HM; Fri, 23 Dec 2011 22:46:03 -0500 From: Austin Clements To: notmuch@notmuchmail.org Subject: [PATCH v4 3/4] Utility function to seek in MIME trees in depth-first order. Date: Fri, 23 Dec 2011 22:45:47 -0500 Message-Id: <1324698348-27620-4-git-send-email-amdragon@mit.edu> X-Mailer: git-send-email 1.7.7.3 In-Reply-To: <1324698348-27620-1-git-send-email-amdragon@mit.edu> References: <1323460468-4030-1-git-send-email-amdragon@mit.edu> <1324698348-27620-1-git-send-email-amdragon@mit.edu> X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFtrLIsWRmVeSWpSXmKPExsUixCmqrPvP66ufwT5+i+s3ZzI7MHo8W3WL OYAxissmJTUnsyy1SN8ugStj/sHVrAXXeCsmH5nL1MB4n6uLkZNDQsBE4v79NjYIW0ziwr31 QDYXh5DAPkaJN303GSGcDYwSD6eeYIJwzjFJ7D90Fiozn1FiydnnLCD9bAIaEtv2L2cEsUUE pCV23p3N2sXIwcEsoCbxp0sFJCwsECLxYtcEsBIWAVWJ5l8LwFp5BRwk5rbOZoY4Q0Hi3Opz 7CA2p4CjxNat7xlBxggJlEk0nwmawMi/gJFhFaNsSm6Vbm5iZk5xarJucXJiXl5qka6hXm5m iV5qSukmRnDISPLsYHxzUOkQowAHoxIPb+PSL35CrIllxZW5hxglOZiURHmvun31E+JLyk+p zEgszogvKs1JLT7EKMHBrCTCq5kEVM6bklhZlVqUD5OS5mBREudV0XrnJySQnliSmp2aWpBa BJOV4eBQkuB94gk0VLAoNT21Ii0zpwQhzcTBCTKcB2g4NzDGhHiLCxJzizPTIfKnGHU5Fq/a cJZRiCUvPy9VSpz3HcggAZCijNI8uDmwWH/FKA70ljBvD8goHmCagJv0CmgJE9CSGCOQD4pL EhFSUg2MHkGChX/lShM9d7z/W5Y1wyxxorFl4vmZsxmDE/ZFbApffZ/PSE3s1Px12vVGHPse 1Euc/Tr9++NtRa9e6fd3Pr3zfvZNnsnOq/c0l15f+ljr15fnv24bqxTc+fYhRPOIVO+tPTlv AvU7FyTqxJ9dcNvqlF750udS0W8rRTgrWF57tnww9X3FocRSnJFoqMVcVJwIAGsxrEXQAgAA X-BeenThere: notmuch@notmuchmail.org X-Mailman-Version: 2.1.13 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: Sat, 24 Dec 2011 03:46:07 -0000 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 | 27 +++++++++++++++++++++++++++ notmuch-client.h | 5 +++++ 2 files changed, 32 insertions(+), 0 deletions(-) diff --git a/mime-node.c b/mime-node.c index fd8e754..1d32823 100644 --- a/mime-node.c +++ b/mime-node.c @@ -236,3 +236,30 @@ mime_node_child (const mime_node_t *parent, int child) } 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); +} diff --git a/notmuch-client.h b/notmuch-client.h index e23b51c..2f36519 100644 --- a/notmuch-client.h +++ b/notmuch-client.h @@ -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); +/* 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 -- 1.7.7.3