summaryrefslogtreecommitdiffstats
path: root/common
diff options
context:
space:
mode:
authorDavid Sommerseth <dazo@users.sourceforge.net>2008-12-17 09:42:46 +0100
committerDavid Sommerseth <dazo@users.sourceforge.net>2008-12-17 09:42:46 +0100
commit58619e09c5d554e66cad768a9abd6b2fff7f5a62 (patch)
treef1a81cd0ba1b5b5c526401c9ce34c93beab8b53f /common
parentb895198722a226d19bf078cabfd5395db4940021 (diff)
downloadeurephia-58619e09c5d554e66cad768a9abd6b2fff7f5a62.tar.gz
eurephia-58619e09c5d554e66cad768a9abd6b2fff7f5a62.tar.xz
eurephia-58619e09c5d554e66cad768a9abd6b2fff7f5a62.zip
Added common eurephiaXML functions for creating and reading eurephia XML documents
Diffstat (limited to 'common')
-rw-r--r--common/eurephia_xml.c56
-rw-r--r--common/eurephia_xml.h3
2 files changed, 59 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
diff --git a/common/eurephia_xml.h b/common/eurephia_xml.h
index 761ffda..f03be27 100644
--- a/common/eurephia_xml.h
+++ b/common/eurephia_xml.h
@@ -27,6 +27,9 @@
char *xmlGetAttrValue(xmlAttr *properties, const char *key);
xmlNode *xmlFindNode(xmlNode *node, const char *key);
+int eurephiaXML_CreateDoc(eurephiaCTX *ctx, int format, const char *rootname, xmlDoc **doc, xmlNode **root_n);
+xmlNode *eurephiaXML_getRoot(eurephiaCTX *ctx, xmlDoc *doc, const char *nodeset, int min_format);
+
inline char *xmlExtractContent(xmlNode *n) {
return (char *) (((n != NULL) && (n->children != NULL)) ? n->children->content : NULL);
}