From 5a50d87ee2b2bd009e557ee21a61cee2af4593d5 Mon Sep 17 00:00:00 2001 From: tokyo4j Date: Sun, 3 Aug 2025 00:02:02 +0900 Subject: [PATCH] common/xml: let LAB_XML_FOR_EACH() skip first child text nodes Before this patch, first text nodes like the spaces between and below were also travered by LAB_XML_FOR_EACH(): foo --- include/common/xml.h | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/include/common/xml.h b/include/common/xml.h index 3cfa5539..8b49d0e4 100644 --- a/include/common/xml.h +++ b/include/common/xml.h @@ -35,16 +35,13 @@ bool lab_xml_get_string(xmlNode *node, const char *key, char *s, size_t len); bool lab_xml_get_int(xmlNode *node, const char *key, int *i); bool lab_xml_get_bool(xmlNode *node, const char *key, bool *b); +/* also skips other unusual nodes like comments */ static inline xmlNode * -lab_xml_get_next_child(xmlNode *child) +lab_xml_skip_text(xmlNode *child) { - if (!child) { - return NULL; - } - do { + while (child && child->type != XML_ELEMENT_NODE) { child = child->next; - } while (child && child->type != XML_ELEMENT_NODE); - + } return child; } @@ -58,11 +55,13 @@ lab_xml_get_key_and_content(xmlNode *node, char **name, char **content) } #define LAB_XML_FOR_EACH(parent, child, key, content) \ - for ((child) = (parent)->children, \ + for ((child) = lab_xml_skip_text((parent)->children), \ lab_xml_get_key_and_content((child), &(key), &(content)); \ + \ (child); \ + \ xmlFree((xmlChar *)(content)), \ - (child) = lab_xml_get_next_child(child), \ + (child) = lab_xml_skip_text((child)->next), \ lab_xml_get_key_and_content((child), &(key), &(content))) #endif /* LABWC_XML_H */