summaryrefslogtreecommitdiff
path: root/src/configuration.cpp
diff options
context:
space:
mode:
authorBjørn Lindeijer <bjorn@lindeijer.nl>2005-03-01 19:49:51 +0000
committerBjørn Lindeijer <bjorn@lindeijer.nl>2005-03-01 19:49:51 +0000
commit0c8c4d490893907237facd931d1ad8e651089f7d (patch)
tree6750ed6f0af75860be51204f7813994914b7927e /src/configuration.cpp
parent4b1882a2c49572a108d1fd3269fe2dd71e62c7bc (diff)
downloadmana-client-0c8c4d490893907237facd931d1ad8e651089f7d.tar.gz
mana-client-0c8c4d490893907237facd931d1ad8e651089f7d.tar.bz2
mana-client-0c8c4d490893907237facd931d1ad8e651089f7d.tar.xz
mana-client-0c8c4d490893907237facd931d1ad8e651089f7d.zip
Changed tmw.ini to config.xml which is read/written using libxml2.
Diffstat (limited to 'src/configuration.cpp')
-rw-r--r--src/configuration.cpp93
1 files changed, 53 insertions, 40 deletions
diff --git a/src/configuration.cpp b/src/configuration.cpp
index 15c0827c..d843f793 100644
--- a/src/configuration.cpp
+++ b/src/configuration.cpp
@@ -31,67 +31,80 @@
#include <sstream>
#include <libxml/parser.h>
#include <libxml/tree.h>
+#include <libxml/xmlwriter.h>
+
+// MSVC libxml2 at the moment doesn't work right when using MinGW, missing this
+// function at link time.
+#ifdef WIN32
+#undef xmlFree
+#define xmlFree(x) ;
+#endif
void Configuration::init(std::string filename)
{
- //xmlDocPtr doc = xmlReadFile(filename.c_str(), NULL, 0);
- std::ifstream inFile(filename.c_str(), std::ifstream::in);
- std::string inBuffer;
- unsigned int position;
-
- options.clear();
-
- while (inFile.good())
- {
- std::getline(inFile, inBuffer, '\n');
+ xmlDocPtr doc = xmlReadFile(filename.c_str(), NULL, 0);
- if (inBuffer.substr(0, 1) != INI_COMMENTER)
- {
- // Replace spaces with void
- while (inBuffer.find(" ", 0) != std::string::npos) {
- inBuffer.replace(inBuffer.find(" ", 0), 1, "");
- }
+ if (!doc) return;
- position = inBuffer.find(INI_DELIMITER, 0);
+ xmlNodePtr node = xmlDocGetRootElement(doc);
- if (position != std::string::npos)
- {
- std::string key = inBuffer.substr(0, position);
+ if (!node || !xmlStrEqual(node->name, BAD_CAST "configuration")) {
+ log("Warning: No configuration file (%s)", filename.c_str());
+ return;
+ }
- if (inBuffer.length() > position + 1)
- {
- options[key] =
- inBuffer.substr(position + 1, inBuffer.length());
- }
+ for (node = node->xmlChildrenNode; node != NULL; node = node->next)
+ {
+ if (xmlStrEqual(node->name, BAD_CAST "option"))
+ {
+ xmlChar *name = xmlGetProp(node, BAD_CAST "name");
+ xmlChar *value = xmlGetProp(node, BAD_CAST "value");
- log("Configuration::init(%s, \"%s\")",
- key.c_str(), options[key].c_str());
+ if (name && value) {
+ options[std::string((const char*)name)] =
+ std::string((const char*)value);
}
+
+ if (name) xmlFree(name);
+ if (value) xmlFree(value);
}
}
- inFile.close();
+ xmlFreeDoc(doc);
}
bool Configuration::write(std::string filename)
{
- std::map<std::string, std::string>::iterator iter;
- std::ofstream out(filename.c_str(),
- std::ofstream::out | std::ofstream::trunc);
+ xmlTextWriterPtr writer = xmlNewTextWriterFilename(filename.c_str(), 0);
- for (iter = options.begin(); iter != options.end(); iter++)
+ if (writer)
{
- log("Configuration::write(%s, \"%s\")",
- iter->first.c_str(), iter->second.c_str());
+ xmlTextWriterSetIndent(writer, 1);
+ xmlTextWriterStartDocument(writer, NULL, NULL, NULL);
+ xmlTextWriterStartElement(writer, BAD_CAST "configuration");
+
+ std::map<std::string, std::string>::iterator iter;
+
+ for (iter = options.begin(); iter != options.end(); iter++)
+ {
+ log("Configuration::write(%s, \"%s\")",
+ iter->first.c_str(), iter->second.c_str());
+
+ xmlTextWriterStartElement(writer, BAD_CAST "option");
+ xmlTextWriterWriteAttribute(writer,
+ BAD_CAST "name", BAD_CAST iter->first.c_str());
+ xmlTextWriterWriteAttribute(writer,
+ BAD_CAST "value", BAD_CAST iter->second.c_str());
+ xmlTextWriterEndElement(writer);
+ }
+
+ xmlTextWriterEndDocument(writer);
+ xmlFreeTextWriter(writer);
- out.write(iter->first.c_str(), iter->first.length());
- out.write("=", 1);
- out.write(iter->second.c_str(), iter->second.length());
- out.write("\n", 1);
+ return true;
}
- out.close();
- return true;
+ return false;
}
void Configuration::setValue(std::string key, std::string value)