diff mbox

[16/28] add 'find' function for the tree

Message ID 1308256197-29155-16-git-send-email-daniel.lezcano@linaro.org
State Accepted
Headers show

Commit Message

Daniel Lezcano June 16, 2011, 8:29 p.m. UTC
That will be useful to search for a specific node name.

Signed-off-by: Daniel Lezcano <daniel.lezcano@linaro.org>
---
 tree.c |   17 +++++++++++++++++
 tree.h |    2 ++
 2 files changed, 19 insertions(+), 0 deletions(-)
diff mbox

Patch

diff --git a/tree.c b/tree.c
index e681f4f..7466e31 100644
--- a/tree.c
+++ b/tree.c
@@ -223,3 +223,20 @@  int tree_for_each(struct tree *tree, tree_cb_t cb, void *data)
 
 	return tree_for_each(tree->next, cb, data);
 }
+
+struct tree *tree_find(struct tree *tree, const char *name)
+{
+	struct tree *t;
+
+	if (!tree)
+		return NULL;
+
+	if (!strcmp(tree->name, name))
+		return tree;
+
+	t = tree_find(tree->child, name);
+	if (t)
+		return t;
+
+	return tree_find(tree->next, name);
+}
diff --git a/tree.h b/tree.h
index 57c70f8..7be3fc4 100644
--- a/tree.h
+++ b/tree.h
@@ -42,4 +42,6 @@  typedef int (*tree_filter_t)(const char *name);
 
 extern struct tree *tree_load(const char *path, tree_filter_t filter);
 
+extern struct tree *tree_find(struct tree *tree, const char *name);
+
 extern int tree_for_each(struct tree *tree, tree_cb_t cb, void *data);