From 0c8c4d490893907237facd931d1ad8e651089f7d Mon Sep 17 00:00:00 2001 From: Bjørn Lindeijer Date: Tue, 1 Mar 2005 19:49:51 +0000 Subject: Changed tmw.ini to config.xml which is read/written using libxml2. --- src/configuration.cpp | 93 +++++++++++++++++++++++++++++---------------------- src/gui/chat.cpp | 6 ++-- 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 #include #include +#include + +// 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::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::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 #include #include +#include #ifdef USE_OPENGL #include @@ -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 -- cgit v1.2.3-70-g09d2