From db4aaea68f40f43e10ce73852630c94227001235 Mon Sep 17 00:00:00 2001 From: sanine Date: Sat, 4 Feb 2023 12:41:32 -0600 Subject: add kai_tag_num_children() --- src/xml/xml.c | 12 ++++++++++++ src/xml/xml.h | 4 ++++ src/xml/xml.test.c | 35 +++++++++++++++++++++++++++++++++++ 3 files changed, 51 insertions(+) (limited to 'src') diff --git a/src/xml/xml.c b/src/xml/xml.c index ee3c879..7f101ee 100644 --- a/src/xml/xml.c +++ b/src/xml/xml.c @@ -58,3 +58,15 @@ char *kai_tag_attr_to_dup(struct kai_tag_t *t, const char *attr) return strdup(val); } } + + +int kai_tag_num_children(struct kai_tag_t *t) +{ + int count = 0; + struct kai_tag_t *child = t->children; + while (child != NULL) { + count += 1; + child = child->next; + } + return count; +} diff --git a/src/xml/xml.h b/src/xml/xml.h index 4766e2c..41d4006 100644 --- a/src/xml/xml.h +++ b/src/xml/xml.h @@ -25,4 +25,8 @@ long kai_tag_attr_to_long( */ char *kai_tag_attr_to_dup(struct kai_tag_t *t, const char *attr); + +/* return the number of immediate children of a node */ +int kai_tag_num_children(struct kai_tag_t *t); + #endif diff --git a/src/xml/xml.test.c b/src/xml/xml.test.c index d3b8360..58784be 100644 --- a/src/xml/xml.test.c +++ b/src/xml/xml.test.c @@ -71,5 +71,40 @@ LILY_TEST("duplicate xml attributes") #include LILY_PUSH_TEST() +LILY_TEST("count xml tag children") +{ + struct kai_tag_t *t = kai_parse_string( + "" + ); + int child_count = kai_tag_num_children(t); + CHECK_EQ(child_count, 0, "%d"); + kai_tag_destroy(t); + + t = kai_parse_string( + "" + " " + "" + ); + child_count = kai_tag_num_children(t); + CHECK_EQ(child_count, 1, "%d"); + kai_tag_destroy(t); + + t = kai_parse_string( + "" + " " + " " + " " + " " + " " + " " + "" + ); + child_count = kai_tag_num_children(t); + CHECK_EQ(child_count, 6, "%d"); + kai_tag_destroy(t); +} +#include LILY_PUSH_TEST() + + #define LILY_FILE_END #include LILY_REGISTER_TESTS() -- cgit v1.2.1