|
From: <otp...@us...> - 2006-08-10 14:12:50
|
Revision: 97 Author: otpetrik Date: 2006-08-10 07:12:30 -0700 (Thu, 10 Aug 2006) ViewCVS: http://svn.sourceforge.net/dunelegacy/?rev=97&view=rev Log Message: ----------- - basic settings load & save Modified Paths: -------------- branches/dunks/include/ConfigFile.h branches/dunks/include/Settings.h branches/dunks/include/Strings.h branches/dunks/src/Application.cpp branches/dunks/src/ConfigFile.cpp branches/dunks/src/Settings.cpp branches/dunks/src/Strings.cpp Added Paths: ----------- branches/dunks/config.txt Added: branches/dunks/config.txt =================================================================== --- branches/dunks/config.txt (rev 0) +++ branches/dunks/config.txt 2006-08-10 14:12:30 UTC (rev 97) @@ -0,0 +1,10 @@ +{ + data_dir = "paks/"; + debug = false; + graphics = { + double_buffer = false; + fullscreen = false; + height = 600; + width = 800; + }; +} Modified: branches/dunks/include/ConfigFile.h =================================================================== --- branches/dunks/include/ConfigFile.h 2006-08-10 09:14:20 UTC (rev 96) +++ branches/dunks/include/ConfigFile.h 2006-08-10 14:12:30 UTC (rev 97) @@ -306,7 +306,12 @@ //------------------------------------------------------------------------------ //! Value node /*! - Leaf node containg a string + Leaf node containg a string. + + @note + The string cannot contain any of the following characters: '\n', '\t', ' ', '/', '*', ',', ')', ';' + String containing those characters can be escaped by ' or ", but escaped string cannot contain '"' ! + */ class ValueNode : public Node { Modified: branches/dunks/include/Settings.h =================================================================== --- branches/dunks/include/Settings.h 2006-08-10 09:14:20 UTC (rev 96) +++ branches/dunks/include/Settings.h 2006-08-10 14:12:30 UTC (rev 97) @@ -55,6 +55,8 @@ #include "singleton.h" +#include "ConfigFile.h" + class Settings: public Singleton<Settings> { friend class Singleton<Settings>; @@ -70,8 +72,13 @@ bool m_doubleBuffered; std::string m_dataDir; + + ConfigFile::NodePtr configFile; public: + void load(); + void save(); + void ParseFile(char* fn); void ParseOptions(int argc, char* argv[]); Modified: branches/dunks/include/Strings.h =================================================================== --- branches/dunks/include/Strings.h 2006-08-10 09:14:20 UTC (rev 96) +++ branches/dunks/include/Strings.h 2006-08-10 14:12:30 UTC (rev 97) @@ -29,12 +29,22 @@ Tries to convert value to string (uses std::ostringstream). */ template <typename T> -String toString(const T &something) +inline String toString(const T &something) { std::ostringstream s; s << something; return s.str(); }; +template <> +inline String toString(const bool &something) +{ + std::ostringstream s; + if (something) + s << "true"; + else + s << "false"; + return s.str(); +}; //! Convert string to something /* @@ -46,13 +56,24 @@ @endcode */ template <typename T> -T fromString(ConstString string) +inline T fromString(ConstString string) { T something; std::istringstream s(string); s >> something; return something; }; +template <> +inline bool fromString(ConstString string) +{ + String something; + std::istringstream s(string); + s >> something; + if (something == "true") + return true; + else + return false; +}; //@} Modified: branches/dunks/src/Application.cpp =================================================================== --- branches/dunks/src/Application.cpp 2006-08-10 09:14:20 UTC (rev 96) +++ branches/dunks/src/Application.cpp 2006-08-10 14:12:30 UTC (rev 97) @@ -46,6 +46,9 @@ Application::~Application() { + // TODO: not sure where this belongs... (otpetrik) + Settings::Instance()->save(); + delete m_rootState; delete m_rootWidget; @@ -283,13 +286,8 @@ ResMan::Instance()->addRes("XTRE"); printf("done loading resources\n"); - // example of reading the config file - remove me - std::string config = ResMan::Instance()->readText("CONFIG:config.txt"); - printf("%s\n", config.c_str()); - - config += "newline\n"; - ResMan::Instance()->writeText("CONFIG:config.txt", config); - + Settings::Instance()->load(); + SetPalette(); int len; Modified: branches/dunks/src/ConfigFile.cpp =================================================================== --- branches/dunks/src/ConfigFile.cpp 2006-08-10 09:14:20 UTC (rev 96) +++ branches/dunks/src/ConfigFile.cpp 2006-08-10 14:12:30 UTC (rev 97) @@ -439,6 +439,8 @@ { if (binder != NULL) value = binder->getString(); + if (value.find_first_of("\n\t /*,);") != String::npos) + value = "\"" + value + "\""; cache.add(value); }; @@ -505,7 +507,10 @@ assert(root != NULL); root->save(c); + // file should end with a newline + c.add("\n"); + return c.getString(); }; - + } // namespace ConfigFile Modified: branches/dunks/src/Settings.cpp =================================================================== --- branches/dunks/src/Settings.cpp 2006-08-10 09:14:20 UTC (rev 96) +++ branches/dunks/src/Settings.cpp 2006-08-10 14:12:30 UTC (rev 97) @@ -1,6 +1,9 @@ #include "Settings.h" #include <fstream> +#include "Strings.h" +#include "ResMan.h" + SETTINGSTYPE settings; Settings::Settings() @@ -18,6 +21,32 @@ }; +void Settings::load() +{ + String configText = ResMan::Instance()->readText("CONFIG:config.txt"); + configFile = ConfigFile::loadFile(configText.c_str()); + ConfigFile::bind("graphics.width", configFile, m_width, 640); + ConfigFile::bind("graphics.height", configFile, m_height, 480); + ConfigFile::bind("graphics.fullscreen", configFile, m_fullscreen, false); + ConfigFile::bind("graphics.double_buffer", configFile, m_doubleBuffered, false); + ConfigFile::bind("debug", configFile, m_debug, true); + ConfigFile::bind("data_dir", configFile, m_dataDir, String("paks/")); + +/* root = ConfigFile::loadFile((const char *)data); + std::string config = ResMan::Instance()->readText("CONFIG:config.txt"); + printf("%s\n", config.c_str()); + + config += "newline\n"; + ResMan::Instance()->writeText("CONFIG:config.txt", config); + +*/ +}; +void Settings::save() +{ + String configText = ConfigFile::saveFile(configFile); + ResMan::Instance()->writeText("CONFIG:config.txt", configText); +}; + void Settings::ParseFile(char* fn) { /* Modified: branches/dunks/src/Strings.cpp =================================================================== --- branches/dunks/src/Strings.cpp 2006-08-10 09:14:20 UTC (rev 96) +++ branches/dunks/src/Strings.cpp 2006-08-10 14:12:30 UTC (rev 97) @@ -196,7 +196,7 @@ { data += str.substr(index, nlIndex-index); data += '\n'; - for (int i = 0; i <= indentLevel; i++) + for (int i = 0; i < indentLevel; i++) data += " "; // 4 spaces for one indent level // start next search after the newline index = nlIndex+1; This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |