diff options
author | David Sommerseth <dazo@users.sourceforge.net> | 2009-04-02 00:39:49 +0200 |
---|---|---|
committer | David Sommerseth <dazo@users.sourceforge.net> | 2009-04-02 00:39:49 +0200 |
commit | 9ef8665fa5a8c91fed6a8dfa24b8312cefc9a59d (patch) | |
tree | 9fe028b0098dea5436bd38f447b0639bda179796 /common | |
parent | ef24b50ee95a855bf84bcaefd4e8ae1954e45fe8 (diff) | |
download | eurephia-9ef8665fa5a8c91fed6a8dfa24b8312cefc9a59d.tar.gz eurephia-9ef8665fa5a8c91fed6a8dfa24b8312cefc9a59d.tar.xz eurephia-9ef8665fa5a8c91fed6a8dfa24b8312cefc9a59d.zip |
Added simple function for returning results and errors as XML
Diffstat (limited to 'common')
-rw-r--r-- | common/eurephia_xml.c | 38 | ||||
-rw-r--r-- | common/eurephia_xml.h | 6 |
2 files changed, 44 insertions, 0 deletions
diff --git a/common/eurephia_xml.c b/common/eurephia_xml.c index fe09bde..fdca7f2 100644 --- a/common/eurephia_xml.c +++ b/common/eurephia_xml.c @@ -20,6 +20,8 @@ */ #ifdef HAVE_LIBXML2 +#include <stdarg.h> +#include <string.h> #include <assert.h> #include <libxml/tree.h> @@ -27,6 +29,7 @@ #include <eurephia_nullsafe.h> #include <eurephia_log.h> +#include <eurephia_xml.h> char *xmlGetAttrValue(xmlAttr *attr, const char *key) { xmlAttr *aptr; @@ -116,6 +119,41 @@ xmlNode *eurephiaXML_getRoot(eurephiaCTX *ctx, xmlDoc *doc, const char *nodeset, } +xmlDoc *eurephiaXML_ResultMsg(eurephiaCTX *ctx, exmlResultType type, const char *fmt, ... ) { + va_list ap; + xmlChar msg[2050], *xmlfmt = NULL; + xmlDoc *msgdoc = NULL; + xmlNode *msg_n = NULL; + + memset(&msg, 0, 2050); + xmlfmt = xmlCharStrdup(fmt); + assert( xmlfmt != NULL ); + + va_start(ap, fmt); + xmlStrVPrintf(msg, 2048, xmlfmt, ap); + va_end(ap); + free_nullsafe(xmlfmt); + + switch( type ) { + case exmlRESULT: + eurephiaXML_CreateDoc(ctx, 1, "Result", &msgdoc, &msg_n); + break; + + case exmlERROR: + eurephiaXML_CreateDoc(ctx, 1, "Error", &msgdoc, &msg_n); + break; + + default: + eurephia_log(ctx, LOG_ERROR, 0, "Wrong XML result message type (%i)", type); + return NULL; + } + assert( (msgdoc != NULL) && (msg_n != NULL) ); + xmlAddChild(msg_n, xmlNewText(msg)); + + return msgdoc; +} + + inline char *xmlExtractContent(xmlNode *n) { // FIXME: Should find better way how to return UTF-8 data return (char *) (((n != NULL) && (n->children != NULL)) ? n->children->content : NULL); diff --git a/common/eurephia_xml.h b/common/eurephia_xml.h index 26cbd33..57b9073 100644 --- a/common/eurephia_xml.h +++ b/common/eurephia_xml.h @@ -22,6 +22,10 @@ #ifndef EUREPHIA_XML_H_ # define EUREPHIA_XML_H_ +typedef enum _exmlResultType { exmlRESULT = 1, exmlERROR } exmlResultType; + +#include <stdarg.h> + #ifdef HAVE_LIBXML2 #include <libxml/tree.h> @@ -31,6 +35,8 @@ 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); +xmlDoc *eurephiaXML_ResultMsg(eurephiaCTX *ctx, exmlResultType type, const char *fmt, ... ); + inline char *xmlExtractContent(xmlNode *n); inline char *xmlGetNodeContent(xmlNode *node, const char *key); |