diff options
Diffstat (limited to 'src/utils/xml.cpp')
-rw-r--r-- | src/utils/xml.cpp | 51 |
1 files changed, 46 insertions, 5 deletions
diff --git a/src/utils/xml.cpp b/src/utils/xml.cpp index 341f34c0..8d444fab 100644 --- a/src/utils/xml.cpp +++ b/src/utils/xml.cpp @@ -25,14 +25,30 @@ #include "resources/resourcemanager.h" -#include <iostream> +#include <libxml/parser.h> +#include <libxml/xmlerror.h> + #include <fstream> +#include <iostream> namespace XML { + static void xmlLogger(void *ctx, xmlErrorPtr error); + + struct XMLContext + { + std::string file; + bool resman; + }; + Document::Document(const std::string &filename, bool useResman): mDoc(0) { + XMLContext *ctx = new XMLContext(); + ctx->file = filename; + ctx->resman = useResman; + xmlSetStructuredErrorFunc(ctx, xmlLogger); + int size; char *data = NULL; if (useResman) @@ -75,11 +91,8 @@ namespace XML { logger->log("Error loading %s", filename.c_str()); } - } - Document::Document(const char *data, int size) - { - mDoc = xmlParseMemory(data, size); + xmlSetStructuredErrorFunc(NULL, xmlLogger); } Document::~Document() @@ -144,4 +157,32 @@ namespace XML return NULL; } + static void xmlLogger(void *ctx, xmlErrorPtr error) + { + XMLContext *context = static_cast<XMLContext*>(ctx); + + if (context) + logger->log("Error in XML file '%s' on line %d", + context->file.c_str(), error->line); + else + logger->log("Error in unknown xml file on line %d", + error->line); + + logger->log(error->message); + + // No need to keep errors around + xmlCtxtResetLastError(error->ctxt); + } + + void init() + { + // Initialize libxml2 and check for potential ABI mismatches between + // compiled version and the shared library actually used. + xmlInitParser(); + LIBXML_TEST_VERSION; + + // Handle libxml2 error messages + xmlSetStructuredErrorFunc(NULL, xmlLogger); + } + } // namespace XML |