From patchwork Thu Jun 16 20:29:53 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Daniel Lezcano X-Patchwork-Id: 2008 Return-Path: X-Original-To: patchwork@peony.canonical.com Delivered-To: patchwork@peony.canonical.com Received: from fiordland.canonical.com (fiordland.canonical.com [91.189.94.145]) by peony.canonical.com (Postfix) with ESMTP id 5D7A723F3F for ; Thu, 16 Jun 2011 20:31:56 +0000 (UTC) Received: from mail-vx0-f180.google.com (mail-vx0-f180.google.com [209.85.220.180]) by fiordland.canonical.com (Postfix) with ESMTP id 2D45CA188FF for ; Thu, 16 Jun 2011 20:31:56 +0000 (UTC) Received: by mail-vx0-f180.google.com with SMTP id 12so2092646vxk.11 for ; Thu, 16 Jun 2011 13:31:56 -0700 (PDT) Received: by 10.52.100.72 with SMTP id ew8mr1841118vdb.247.1308256315979; Thu, 16 Jun 2011 13:31:55 -0700 (PDT) X-Forwarded-To: linaro-patchwork@canonical.com X-Forwarded-For: patch@linaro.org linaro-patchwork@canonical.com Delivered-To: patches@linaro.org Received: by 10.52.183.130 with SMTP id em2cs211227vdc; Thu, 16 Jun 2011 13:31:55 -0700 (PDT) Received: by 10.216.14.199 with SMTP id d49mr68261wed.65.1308256309876; Thu, 16 Jun 2011 13:31:49 -0700 (PDT) Received: from smtp.smtpout.orange.fr (smtp08.smtpout.orange.fr [80.12.242.130]) by mx.google.com with ESMTP id t53si5055168weq.138.2011.06.16.13.31.49; Thu, 16 Jun 2011 13:31:49 -0700 (PDT) Received-SPF: neutral (google.com: 80.12.242.130 is neither permitted nor denied by best guess record for domain of daniel.lezcano@linaro.org) client-ip=80.12.242.130; Authentication-Results: mx.google.com; spf=neutral (google.com: 80.12.242.130 is neither permitted nor denied by best guess record for domain of daniel.lezcano@linaro.org) smtp.mail=daniel.lezcano@linaro.org Received: from monster.dhcp.lxc ([92.134.76.78]) by mwinf5d16 with ME id wkXf1g0031hMfSL03kXoKW; Thu, 16 Jun 2011 22:31:49 +0200 From: Daniel Lezcano To: patches@linaro.org Subject: [PATCH 24/28] add a tree function to return a list of elements Date: Thu, 16 Jun 2011 22:29:53 +0200 Message-Id: <1308256197-29155-24-git-send-email-daniel.lezcano@linaro.org> X-Mailer: git-send-email 1.7.1 In-Reply-To: <1308256197-29155-1-git-send-email-daniel.lezcano@linaro.org> References: <1308256197-29155-1-git-send-email-daniel.lezcano@linaro.org> This patch provides a function returning all the node of the tree matching the substring passed a parameter. Signed-off-by: Daniel Lezcano --- tree.c | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ tree.h | 2 ++ 2 files changed, 50 insertions(+), 0 deletions(-) diff --git a/tree.c b/tree.c index 516e832..8f546a9 100644 --- a/tree.c +++ b/tree.c @@ -265,3 +265,51 @@ struct tree *tree_find(struct tree *tree, const char *name) return tree_find(tree->next, name); } + +struct struct_find { + int nr; + const char *name; + struct tree ***ptree; +}; + +static int tree_finds_cb(struct tree *tree, void *data) +{ + struct struct_find *sf = data; + + if (strncmp(sf->name, tree->name, strlen(sf->name))) + return 0; + + if (sf->ptree) + (*(sf->ptree))[sf->nr] = tree; + + sf->nr++; + + return 0; +} + +int tree_finds(struct tree *tree, const char *name, struct tree ***ptr) +{ + struct struct_find sf = { .nr = 0, .ptree = NULL, .name = name }; + int nmatch; + + /* first pass : count # of matching nodes */ + tree_for_each(tree, tree_finds_cb, &sf); + + /* no match */ + if (!sf.nr) + return 0; + + *ptr = malloc(sizeof(struct tree *) * sf.nr); + if (!*ptr) + return -1; + + /* store the result as it will be overwritten by the next call */ + nmatch = sf.nr; + sf.nr = 0; + sf.ptree = ptr; + + /* second pass : fill with the matching nodes */ + tree_for_each(tree, tree_finds_cb, &sf); + + return nmatch; +} diff --git a/tree.h b/tree.h index 2af4a5a..88d4a19 100644 --- a/tree.h +++ b/tree.h @@ -49,3 +49,5 @@ extern int tree_for_each(struct tree *tree, tree_cb_t cb, void *data); extern int tree_for_each_reverse(struct tree *tree, tree_cb_t cb, void *data); extern int tree_for_each_parent(struct tree *tree, tree_cb_t cb, void *data); + +extern int tree_finds(struct tree *tree, const char *name, struct tree ***ptr);