diff options
-rw-r--r-- | src/configuration.cpp | 93 | ||||
-rw-r--r-- | src/gui/chat.cpp | 6 | ||||
-rw-r--r-- | src/main.cpp | 15 |
3 files changed, 66 insertions, 48 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) diff --git a/src/gui/chat.cpp b/src/gui/chat.cpp index 34b49571..6e562017 100644 --- a/src/gui/chat.cpp +++ b/src/gui/chat.cpp @@ -78,13 +78,13 @@ void ChatBox::chat_log(std::string line, int own) // A try to get text sentences no too long... bool finished = false; - while ( !finished ) + while (!finished) { std::string tempText; - if ( line.length() > 60 ) + if (line.length() > 60) { - if ( line.length() > 60 ) + if (line.length() > 60) tempText = line.substr(0, 60); else tempText = line; diff --git a/src/main.cpp b/src/main.cpp index 4b5b745c..45d223d7 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -35,6 +35,7 @@ #include <SDL/SDL.h> #include <physfs.h> #include <libxml/xmlversion.h> +#include <libxml/parser.h> #ifdef USE_OPENGL #include <SDL_opengl.h> @@ -133,7 +134,7 @@ void init_engine() #ifndef __USE_UNIX98 // WIN32 and others - strcpy(dir, "tmw.ini"); + strcpy(dir, "config.xml"); #else // UNIX char *userHome; @@ -162,10 +163,10 @@ void init_engine() printf("%s can't be made... And doesn't exist ! Exitting ...", dir); exit(1); } - sprintf(dir, "%s/.manaworld/tmw.ini", userHome); + sprintf(dir, "%s/.manaworld/config.xml", userHome); #endif - // Checking if the tmw.ini file exists... otherwise creates it with + // Checking if the configuration file exists... otherwise creates it with // default options ! FILE *tmwFile = 0; tmwFile = fopen(dir, "r"); @@ -180,7 +181,7 @@ void init_engine() } else { fclose(tmwFile); - // Fill tmw.ini with defaults + // Fill configuration with defaults config.setValue("host", "animesites.de"); config.setValue("port", 6901); config.setValue("hwaccel", 0); @@ -195,7 +196,6 @@ void init_engine() config.setValue("chatlog", "chatlog.txt"); #endif config.setValue("remember", 1); - config.setValue("username", "Player"); config.write(dir); } @@ -315,6 +315,10 @@ void exit_engine() SAFE_DELETE_ARRAY(dir); SAFE_DELETE(gui); SAFE_DELETE(graphics); + + // Shutdown libxml + xmlCleanupParser(); + ResourceManager::deleteInstance(); } @@ -323,6 +327,7 @@ int main(int argc, char *argv[]) { // Initialize libxml2 and check for potential ABI mismatches between // compiled version and the shared library actually used. + xmlInitParser(); LIBXML_TEST_VERSION; // Initialize PhysicsFS |