summaryrefslogtreecommitdiffstats
path: root/src/utils/xml.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/utils/xml.cpp')
-rw-r--r--src/utils/xml.cpp144
1 files changed, 115 insertions, 29 deletions
diff --git a/src/utils/xml.cpp b/src/utils/xml.cpp
index ab9ba77..659f928 100644
--- a/src/utils/xml.cpp
+++ b/src/utils/xml.cpp
@@ -1,6 +1,7 @@
/*
- * The Mana Server
- * Copyright (C) 2006-2010 The Mana World Development Team
+ * XML utility functions
+ * Copyright (C) 2004-2009 The Mana World Development Team
+ * Copyright (C) 2009-2010 The Mana Developers
*
* This file is part of The Mana Server.
*
@@ -18,47 +19,132 @@
* along with The Mana Server. If not, see <http://www.gnu.org/licenses/>.
*/
-#include <cstdlib>
-
#include "utils/xml.hpp"
+#include "common/resourcemanager.hpp"
+#include "utils/logger.h"
+
+#include <iostream>
+#include <fstream>
+
namespace XML
{
+ Document::Document(const std::string &filename, bool useResman):
+ mDoc(0)
+ {
+ int size;
+ char *data = NULL;
+ if (useResman)
+ {
+ data = ResourceManager::loadFile(filename, size);
+ }
+ else
+ {
+ std::ifstream file;
+ file.open(filename.c_str(), std::ios::in);
-int getProperty(xmlNodePtr node, const char *name, int def)
-{
- if (xmlChar *prop = xmlGetProp(node, BAD_CAST name))
+ if (file.is_open())
+ {
+ // Get length of file
+ file.seekg(0, std::ios::end);
+ size = file.tellg();
+ file.seekg(0, std::ios::beg);
+
+ data = (char*) malloc(size);
+
+ file.read(data, size);
+ file.close();
+ }
+ else
+ {
+ LOG_ERROR("(XML::Document) Error loading XML file: "
+ << filename);
+ }
+ }
+
+ if (data)
+ {
+ mDoc = xmlParseMemory(data, size);
+ free(data);
+
+ if (!mDoc)
+ {
+ LOG_ERROR("(XML::Document) Error parsing XML file: "
+ << filename);
+ }
+ }
+ else
+ {
+ LOG_ERROR("(XML::Document) Error loading XML file: "
+ << filename);
+ }
+ }
+
+ Document::Document(const char *data, int size)
+ {
+ mDoc = xmlParseMemory(data, size);
+ }
+
+ Document::~Document()
{
- int ret = atoi((char*)prop);
- xmlFree(prop);
+ if (mDoc)
+ xmlFreeDoc(mDoc);
+ }
+
+ xmlNodePtr Document::rootNode()
+ {
+ return mDoc ? xmlDocGetRootElement(mDoc) : 0;
+ }
+
+ int getProperty(xmlNodePtr node, const char *name, int def)
+ {
+ int &ret = def;
+
+ xmlChar *prop = xmlGetProp(node, BAD_CAST name);
+ if (prop)
+ {
+ ret = atoi((char*) prop);
+ xmlFree(prop);
+ }
+
return ret;
}
- return def;
-}
-double getFloatProperty(xmlNodePtr node, const char* name, double def)
-{
- double &ret = def;
+ double getFloatProperty(xmlNodePtr node, const char *name, double def)
+ {
+ double &ret = def;
- xmlChar *prop = xmlGetProp(node, BAD_CAST name);
- if (prop) {
- ret = atof((char*)prop);
- xmlFree(prop);
+ xmlChar *prop = xmlGetProp(node, BAD_CAST name);
+ if (prop)
+ {
+ ret = atof((char*) prop);
+ xmlFree(prop);
+ }
+
+ return ret;
}
- return ret;
-}
+ std::string getProperty(xmlNodePtr node, const char *name,
+ const std::string &def)
+ {
+ xmlChar *prop = xmlGetProp(node, BAD_CAST name);
+ if (prop)
+ {
+ std::string val = (char*) prop;
+ xmlFree(prop);
+ return val;
+ }
-std::string getProperty(xmlNodePtr node, const char *name,
- const std::string &def)
-{
- if (xmlChar *prop = xmlGetProp(node, BAD_CAST name))
+ return def;
+ }
+
+ xmlNodePtr findFirstChildByName(xmlNodePtr parent, const char *name)
{
- std::string val = (char *)prop;
- xmlFree(prop);
- return val;
+ for_each_xml_child_node(child, parent)
+ if (xmlStrEqual(child->name, BAD_CAST name))
+ return child;
+
+ return NULL;
}
- return def;
-}
} // namespace XML