diff options
Diffstat (limited to 'common/eurephia_xml.c')
-rw-r--r-- | common/eurephia_xml.c | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/common/eurephia_xml.c b/common/eurephia_xml.c index c3389ae..cbf7ec4 100644 --- a/common/eurephia_xml.c +++ b/common/eurephia_xml.c @@ -19,7 +19,13 @@ */ #ifdef HAVE_LIBXML2 +#include <assert.h> + #include <libxml/tree.h> +#include <libxml/xmlstring.h> + +#include <eurephia_nullsafe.h> +#include <eurephia_log.h> char *xmlGetAttrValue(xmlAttr *attr, const char *key) { xmlAttr *aptr; @@ -46,4 +52,54 @@ xmlNode *xmlFindNode(xmlNode *node, const char *key) { return NULL; } + +int eurephiaXML_CreateDoc(eurephiaCTX *ctx, int format, const char *eurephiaRoot, + xmlDoc **doc, xmlNode **root_n) +{ + char tmp[34]; + + // Create a new XML document + *doc = xmlNewDoc((xmlChar *)"1.0"); + assert(*doc != NULL); + + // Set the XML root to be <eurephia/> + *root_n = xmlNewNode(NULL, (xmlChar *)"eurephia"); + assert(*root_n != NULL); + + // Add eurephia XML document format version id + snprintf(tmp, 33, "%i%c", format, '\0'); + xmlNewProp(*root_n, (xmlChar *)"format", (xmlChar *)tmp); + xmlDocSetRootElement(*doc, *root_n); + + // Add the eurephia XML root (always inside the <eurephia/> tags) + *root_n = xmlNewChild(*root_n, NULL, (xmlChar*)eurephiaRoot, NULL); + + return 1; +} + + +xmlNode *eurephiaXML_getRoot(eurephiaCTX *ctx, xmlDoc *doc, const char *nodeset, int req_format) { + xmlNode *root = NULL; + char *xmlformat_str = NULL; + int xmlformat = 0; + + root = xmlDocGetRootElement(doc); + if( xmlStrcmp(root->name, (xmlChar *)"eurephia") != 0 ) { + eurephia_log(ctx, LOG_FATAL, 0, "Could not find eurephia XML root element. " + "Not a valid eurephia XML document."); + printf("-%s-\n", root->name); + return NULL; + } + + xmlformat_str = xmlGetAttrValue(root->properties, "format"); + xmlformat = atoi_nullsafe(xmlformat_str); + if( xmlformat < req_format ) { + eurephia_log(ctx, LOG_ERROR, 0, "eurephia XML document format is not supported. " + "The XML document uses '%s', while we need minimum '%i'", xmlformat_str, req_format); + return NULL; + } + + return xmlFindNode(root, nodeset); +} + #endif |