Skip to contentSkip to conversation

libfdt: Add and use a node iteration helper function.

PATCH New Jerry Van Baren 1 participant 1 message Feb 16, 2008

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 libfdt: Add and use a node iteration helper function.

This patch adds an fdt_next_node() function which can be used to iterate through nodes of the tree while keeping track of depth. This function is used to simplify the iteration code in a lot of other functions, and is also exported for use by library users.

---
 libfdt/fdt.c    |   41 +++++++++
 libfdt/fdt_ro.c |  258 ++++++++++++++++++-------------------------------------
 libfdt/libfdt.h |    6 ++
 3 files changed, 131 insertions(+), 174 deletions(-)
libfdt/fdt.c +41 -0
index 586a361..c61fb53 100644
@@ -129,6 +129,47 @@ uint32_t fdt_next_tag(const void *fdt, int offset, int *nextoffset)
 	return tag;
 }
 
+int fdt_next_node(const void *fdt, int offset, int *depth)
+{
+	int nextoffset = 0;
+	uint32_t tag;
+
+	if (offset >= 0) {
+		tag = fdt_next_tag(fdt, offset, &nextoffset);
+		if (tag != FDT_BEGIN_NODE)
+			return -FDT_ERR_BADOFFSET;
+	}
+
+	do {
+		offset = nextoffset;
+		tag = fdt_next_tag(fdt, offset, &nextoffset);
+
+		switch (tag) {
+		case FDT_PROP:
+		case FDT_NOP:
+			break;
+
+		case FDT_BEGIN_NODE:
+			if (depth)
+				(*depth)++;
+			break;
+
+		case FDT_END_NODE:
+			if (depth)
+				(*depth)--;
+			break;
+
+		case FDT_END:
+			return -FDT_ERR_NOTFOUND;
+
+		default:
+			return -FDT_ERR_BADSTRUCTURE;
+		}
+	} while (tag != FDT_BEGIN_NODE);
+
+	return offset;
+}
+
 const char *_fdt_find_string(const char *strtab, int tabsize, const char *s)
 {
 	int len = strlen(s) + 1;
libfdt/fdt_ro.c +84 -174

337 lines not on this page · raw message

libfdt/libfdt.h +6 -0
index d053689..8645de0 100644
@@ -131,6 +131,12 @@ static inline void *fdt_offset_ptr_w(void *fdt, int offset, int checklen)
 uint32_t fdt_next_tag(const void *fdt, int offset, int *nextoffset);
 
 /**********************************************************************/
+/* Traversal functions                                                */
+/**********************************************************************/
+
+int fdt_next_node(const void *fdt, int offset, int *depth);
+
+/**********************************************************************/
 /* General functions                                                  */
 /**********************************************************************/
 
Signature · 1 line
1.5.3.8