From ff85006e7159aa808c23bd02f9fb83d24b277da0 Mon Sep 17 00:00:00 2001 From: scrawl Date: Fri, 30 Mar 2012 18:38:33 +0200 Subject: [PATCH] added untested interface --- components/CMakeLists.txt | 4 ++ components/settings/settings.cpp | 64 ++++++++++++++++++++++++++++++++ components/settings/settings.hpp | 35 +++++++++++++++++ 3 files changed, 103 insertions(+) create mode 100644 components/settings/settings.cpp create mode 100644 components/settings/settings.hpp diff --git a/components/CMakeLists.txt b/components/CMakeLists.txt index c95efb37d..b48c50640 100644 --- a/components/CMakeLists.txt +++ b/components/CMakeLists.txt @@ -2,6 +2,10 @@ project (Components) # source files +add_component_dir (settings + settings + ) + add_component_dir (bsa bsa_archive bsa_file ) diff --git a/components/settings/settings.cpp b/components/settings/settings.cpp new file mode 100644 index 000000000..157de0b4d --- /dev/null +++ b/components/settings/settings.cpp @@ -0,0 +1,64 @@ +#include "settings.hpp" + +#include + +#include +#include + +using namespace Settings; + +Ogre::ConfigFile Manager::mFile = Ogre::ConfigFile(); +Ogre::ConfigFile Manager::mDefaultFile = Ogre::ConfigFile(); + +void Manager::load(const std::string& file) +{ + mFile.load(file); +} + +void Manager::loadDefault(const std::string& file) +{ + mDefaultFile.load(file); +} + +void Manager::save(const std::string& file) +{ + std::fstream fout(file.c_str(), std::ios::out); + + Ogre::ConfigFile::SectionIterator seci = mFile.getSectionIterator(); + + while (seci.hasMoreElements()) + { + Ogre::String sectionName = seci.peekNextKey(); + + if (sectionName.length() > 0) + fout << '\n' << '[' << seci.peekNextKey() << ']' << '\n'; + + Ogre::ConfigFile::SettingsMultiMap *settings = seci.getNext(); + Ogre::ConfigFile::SettingsMultiMap::iterator i; + for (i = settings->begin(); i != settings->end(); ++i) + { + fout << i->first.c_str() << '=' << i->second.c_str() << '\n'; + } + } +} + +const std::string Manager::getString (const std::string& setting, const std::string& category) +{ + std::string defaultval = mDefaultFile.getSetting(setting, category); + return mFile.getSetting(setting, category, defaultval); +} + +const float Manager::getFloat (const std::string& setting, const std::string& category) +{ + return Ogre::StringConverter::parseReal( getString(setting, category) ); +} + +const int Manager::getInt (const std::string& setting, const std::string& category) +{ + return Ogre::StringConverter::parseInt( getString(setting, category) ); +} + +const bool Manager::getBool (const std::string& setting, const std::string& category) +{ + return Ogre::StringConverter::parseBool( getString(setting, category) ); +} diff --git a/components/settings/settings.hpp b/components/settings/settings.hpp new file mode 100644 index 000000000..ae6f1a9de --- /dev/null +++ b/components/settings/settings.hpp @@ -0,0 +1,35 @@ +#ifndef _COMPONENTS_SETTINGS_H +#define _COMPONENTS_SETTINGS_H + +#include + +namespace Settings +{ + + /// + /// \brief Settings management (can change during runtime) + /// + class Manager + { + public: + static Ogre::ConfigFile mFile; + static Ogre::ConfigFile mDefaultFile; + + void loadDefault (const std::string& file); + ///< load file as the default settings (can be overridden by user settings) + + void load (const std::string& file); + ///< load file as user settings + + void save (const std::string& file); + ///< save to file + + static const int getInt (const std::string& setting, const std::string& category); + static const float getFloat (const std::string& setting, const std::string& category); + static const std::string getString (const std::string& setting, const std::string& category); + static const bool getBool (const std::string& setting, const std::string& category); + }; + +} + +#endif // _COMPONENTS_SETTINGS_H