From ca2850c6c8941faecb6e07be3379127330feb04a Mon Sep 17 00:00:00 2001 From: Pavel Charvat Date: Fri, 25 Apr 2008 10:33:13 +0200 Subject: [PATCH] XML: Implemented a merger of element's contents. --- sherlock/xml/parse.c | 39 +++++++++++++++++++++++++++++++++++++++ sherlock/xml/xml.h | 6 ++++++ 2 files changed, 45 insertions(+) diff --git a/sherlock/xml/parse.c b/sherlock/xml/parse.c index bd71a615..d690ead5 100644 --- a/sherlock/xml/parse.c +++ b/sherlock/xml/parse.c @@ -1231,3 +1231,42 @@ xml_parse(struct xml_context *ctx) while (xml_next(ctx)); return ctx->err_code; } + +char * +xml_merge_chars(struct xml_context *ctx UNUSED, struct xml_node *node, struct mempool *pool) +{ + ASSERT(node->type == XML_NODE_ELEM); + char *p = mp_start_noalign(pool, 1); + XML_NODE_FOR_EACH(son, node) + if (son->type == XML_NODE_CHARS) + { + p = mp_spread(pool, p, son->len + 1); + memcpy(p, son->text, son->len); + } + *p++ = 0; + return mp_end(pool, p); +} + +static char * +xml_append_dom_chars(char *p, struct mempool *pool, struct xml_node *node) +{ + XML_NODE_FOR_EACH(son, node) + if (son->type == XML_NODE_CHARS) + { + p = mp_spread(pool, p, son->len + 1); + memcpy(p, son->text, son->len); + } + else if (son->type == XML_NODE_ELEM) + p = xml_append_dom_chars(p, pool, son); + return p; +} + +char * +xml_merge_dom_chars(struct xml_context *ctx UNUSED, struct xml_node *node, struct mempool *pool) +{ + ASSERT(node->type == XML_NODE_ELEM); + char *p = mp_start_noalign(pool, 1); + p = xml_append_dom_chars(p, pool, node); + *p++ = 0; + return mp_end(pool, p); +} diff --git a/sherlock/xml/xml.h b/sherlock/xml/xml.h index 41bf46f9..0dd405de 100644 --- a/sherlock/xml/xml.h +++ b/sherlock/xml/xml.h @@ -257,6 +257,12 @@ void xml_def_resolve_entity(struct xml_context *ctx, struct xml_dtd_entity *ent) /* Remove leading/trailing spaces and replaces sequences of spaces to a single space character (non-CDATA attribute normalization) */ uns xml_normalize_white(struct xml_context *ctx, char *value); +/* Merge character contents of a given element to a single string (not recursive) */ +char *xml_merge_chars(struct xml_context *ctx, struct xml_node *node, struct mempool *pool); + +/* Merge character contents of a given subtree to a single string */ +char *xml_merge_dom_chars(struct xml_context *ctx, struct xml_node *node, struct mempool *pool); + /* Public part of error handling */ void xml_warn(struct xml_context *ctx, const char *format, ...); void xml_error(struct xml_context *ctx, const char *format, ...); -- 2.39.2