diff options
author | sanine <sanine.not@pm.me> | 2023-02-04 12:41:32 -0600 |
---|---|---|
committer | sanine <sanine.not@pm.me> | 2023-02-04 12:41:32 -0600 |
commit | db4aaea68f40f43e10ce73852630c94227001235 (patch) | |
tree | d8693709372cc8b9930c68f9fdd4abffc3f6c8d1 /src/xml | |
parent | 059a70686c49d591fd7a193493745a7bc172cbca (diff) |
add kai_tag_num_children()
Diffstat (limited to 'src/xml')
-rw-r--r-- | src/xml/xml.c | 12 | ||||
-rw-r--r-- | src/xml/xml.h | 4 | ||||
-rw-r--r-- | src/xml/xml.test.c | 35 |
3 files changed, 51 insertions, 0 deletions
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( + "<no_kids />" + ); + int child_count = kai_tag_num_children(t); + CHECK_EQ(child_count, 0, "%d"); + kai_tag_destroy(t); + + t = kai_parse_string( + "<one_child>" + " <child />" + "</one_child>" + ); + child_count = kai_tag_num_children(t); + CHECK_EQ(child_count, 1, "%d"); + kai_tag_destroy(t); + + t = kai_parse_string( + "<six_kids>" + " <child />" + " <child />" + " <child />" + " <child />" + " <child />" + " <child />" + "</six_kids>" + ); + 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() |