From adef1ef38e52fbaed58865a3c939380f3f2dd1cc Mon Sep 17 00:00:00 2001 From: Philipp Schafft Date: Wed, 7 Oct 2020 16:43:58 +0000 Subject: [PATCH] Feature: Added support for a container object holding a list of XML nodes --- src/xml2json.c | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) diff --git a/src/xml2json.c b/src/xml2json.c index 5183073b..ac9e014b 100644 --- a/src/xml2json.c +++ b/src/xml2json.c @@ -15,6 +15,7 @@ #endif #include +#include #include "xml2json.h" #include "json.h" @@ -40,6 +41,63 @@ struct nodelist { static void render_node(json_renderer_t *renderer, xmlDocPtr doc, xmlNodePtr node, xmlNodePtr parent, struct xml2json_cache *cache); static void render_node_generic(json_renderer_t *renderer, xmlDocPtr doc, xmlNodePtr node, xmlNodePtr parent, struct xml2json_cache *cache); +static void nodelist_init(struct nodelist *list) +{ + memset(list, 0, sizeof(*list)); +} + +static void nodelist_free(struct nodelist *list) +{ + free(list->nodes); + memset(list, 0, sizeof(*list)); +} + +static void nodelist_push(struct nodelist *list, xmlNodePtr node) +{ + if (list->fill == list->len) { + xmlNodePtr *n = realloc(list->nodes, sizeof(xmlNodePtr)*(list->len + 16)); + if (!n) { + ICECAST_LOG_ERROR("Can not allocate memory for node list. BAD."); + return; + } + + list->nodes = n; + list->len += 16; + } + + list->nodes[list->fill++] = node; +} + +static xmlNodePtr nodelist_get(struct nodelist *list, size_t idx) +{ + if (idx >= list->fill) + return NULL; + return list->nodes[idx]; +} + +static void nodelist_unset(struct nodelist *list, size_t idx) +{ + if (idx >= list->fill) + return; + list->nodes[idx] = NULL; +} + +static size_t nodelist_fill(struct nodelist *list) +{ + return list->fill; +} + +static int nodelist_is_empty(struct nodelist *list) +{ + size_t i; + + for (i = 0; i < list->fill; i++) + if (list->nodes[i]) + return 0; + + return 1; +} + static void handle_textchildnode(json_renderer_t *renderer, xmlDocPtr doc, xmlNodePtr node, xmlNodePtr parent, struct xml2json_cache *cache) { xmlChar *value = xmlNodeListGetString(doc, node->xmlChildrenNode, 1);