From 73705dadf6036323bbbe79e2da0dbe7c52f7991d Mon Sep 17 00:00:00 2001 From: Sebastian Wick Date: Tue, 3 Apr 2012 02:14:39 +0200 Subject: [PATCH] parse fallback with boost program option custom validators --- apps/openmw/engine.cpp | 6 ++--- apps/openmw/engine.hpp | 4 +-- apps/openmw/main.cpp | 46 ++++++++++++++++++----------------- apps/openmw/mwworld/world.cpp | 24 ++++-------------- apps/openmw/mwworld/world.hpp | 4 +-- 5 files changed, 36 insertions(+), 48 deletions(-) diff --git a/apps/openmw/engine.cpp b/apps/openmw/engine.cpp index c6684593e..dae5aaff1 100644 --- a/apps/openmw/engine.cpp +++ b/apps/openmw/engine.cpp @@ -341,7 +341,7 @@ void OMW::Engine::go() // Create the world mEnvironment.mWorld = new MWWorld::World (*mOgre, mFileCollections, mMaster, - mResDir, mNewGame, mEnvironment, mEncoding, mFallbackPairs); + mResDir, mNewGame, mEnvironment, mEncoding, mFallbackMap); // Create window manager - this manages all the MW-specific GUI windows MWScript::registerExtensions (mExtensions); @@ -509,7 +509,7 @@ void OMW::Engine::setEncoding(const std::string& encoding) mEncoding = encoding; } -void OMW::Engine::setFallbackValues(std::vector pairs) +void OMW::Engine::setFallbackValues(std::map fallbackMap) { - mFallbackPairs = pairs; + mFallbackMap = fallbackMap; } diff --git a/apps/openmw/engine.hpp b/apps/openmw/engine.hpp index f1df98f4c..6eae20cc0 100644 --- a/apps/openmw/engine.hpp +++ b/apps/openmw/engine.hpp @@ -76,7 +76,7 @@ namespace OMW bool mReportFocus; float mFocusTDiff; std::string mFocusName; - std::vector mFallbackPairs; + std::map mFallbackMap; MWWorld::Environment mEnvironment; Compiler::Extensions mExtensions; @@ -164,7 +164,7 @@ namespace OMW void setAnimationVerbose(bool animverbose); - void setFallbackValues(std::vector pairs); + void setFallbackValues(std::map map); private: Files::ConfigurationManager& mCfgMgr; diff --git a/apps/openmw/main.cpp b/apps/openmw/main.cpp index e1ddb03e7..b541ea0d3 100644 --- a/apps/openmw/main.cpp +++ b/apps/openmw/main.cpp @@ -56,31 +56,33 @@ using namespace std; struct FallbackMap { std::map mMap; +}; - static void - validate(boost::any &v, std::vector const &tokens) +void validate(boost::any &v, std::vector const &tokens, FallbackMap*, int) +{ + if(v.empty()) { - if(v.empty()) - { - v = boost::any(FallbackMap()); - } - - FallbackMap *map = boost::any_cast(&v); - - std::map::iterator mapIt; - for(std::vector::const_iterator it=tokens.begin(); it != tokens.end(); it++) + v = boost::any(FallbackMap()); + } + + FallbackMap *map = boost::any_cast(&v); + + std::map::iterator mapIt; + for(std::vector::const_iterator it=tokens.begin(); it != tokens.end(); it++) + { + int sep = it->find(","); + if(sep < 1) + continue; + + std::string key(it->substr(0,sep)); + std::string value(it->substr(sep+1)); + + if((mapIt = map->mMap.find(key)) == map->mMap.end()) { - int sep = it->find(","); - std::string key(it->substr(0,sep-1)); - std::string value(it->substr(sep)); - - if((mapIt = map->mMap.find(key)) == map->mMap.end()) - { - map->mMap.insert(std::make_pair(key,value)); - } + map->mMap.insert(std::make_pair(key,value)); } - } -}; + } +} /** @@ -265,7 +267,7 @@ bool parseOptions (int argc, char** argv, OMW::Engine& engine, Files::Configurat engine.setCompileAll(variables["script-all"].as()); engine.setReportFocus(variables["report-focus"].as()); engine.setAnimationVerbose(variables["anim-verbose"].as()); - //engine.setFallbackValues(variables["fallback"].as >()); + engine.setFallbackValues(variables["fallback"].as().mMap); return true; } diff --git a/apps/openmw/mwworld/world.cpp b/apps/openmw/mwworld/world.cpp index 093c3fef0..596adcf6b 100644 --- a/apps/openmw/mwworld/world.cpp +++ b/apps/openmw/mwworld/world.cpp @@ -152,24 +152,9 @@ namespace MWWorld mRendering->skyDisable(); } - void World::setFallbackValues(std::vector pairs) + void World::setFallbackValues(std::map fallbackMap) { - for(std::vector::iterator it = pairs.begin(); it != pairs.end(); it++) - { - std::string kv = *it; - int seperator = kv.find("="); - if(seperator < 1 || seperator == (kv.length()-1)) - { - continue; - } - - std::string key = it->substr(0,seperator); - std::string value = it->substr(seperator+1); - if(mFallback.find(key) == mFallback.end()) { - std::cout << "insert " << key << ":" << value << std::endl; - mFallback.insert(std::make_pair(key,value)); - } - } + mFallback = fallbackMap; } std::string World::getFallback(std::string key) @@ -185,7 +170,7 @@ namespace MWWorld World::World (OEngine::Render::OgreRenderer& renderer, const Files::Collections& fileCollections, const std::string& master, const boost::filesystem::path& resDir, - bool newGame, Environment& environment, const std::string& encoding, std::vector fallbackPairs) + bool newGame, Environment& environment, const std::string& encoding, std::map fallbackMap) : mPlayer (0), mLocalScripts (mStore), mGlobalVariables (0), mSky (true), mEnvironment (environment), mNextDynamicRecord (0), mCells (mStore, mEsm, *this), mNumFacing(0) @@ -221,7 +206,8 @@ namespace MWWorld mWorldScene = new Scene(environment, this, *mRendering, mPhysics); - setFallbackValues(fallbackPairs); + setFallbackValues(fallbackMap); + std::cout << "Weather_Sunrise_Time=" << getFallback("Weather_Sunrise_Time") << std::endl; } diff --git a/apps/openmw/mwworld/world.hpp b/apps/openmw/mwworld/world.hpp index d6b422141..7f18762f5 100644 --- a/apps/openmw/mwworld/world.hpp +++ b/apps/openmw/mwworld/world.hpp @@ -111,7 +111,7 @@ namespace MWWorld World (OEngine::Render::OgreRenderer& renderer, const Files::Collections& fileCollections, const std::string& master, const boost::filesystem::path& resDir, bool newGame, - Environment& environment, const std::string& encoding, std::vector fallbackPairs); + Environment& environment, const std::string& encoding, std::map fallbackMap); ~World(); @@ -126,7 +126,7 @@ namespace MWWorld void adjustSky(); - void setFallbackValues(std::vector pairs); + void setFallbackValues(std::map fallbackMap); std::string getFallback(std::string key);