From 50f8375a0bbf09f5f225d526d8da4140047a6537 Mon Sep 17 00:00:00 2001 From: emb <> Date: Sat, 17 Jan 2015 09:51:40 -0600 Subject: [PATCH] Support for config files to reference config variables (i.e %someConfigName%) --- RetroFE/Source/Database/Configuration.cpp | 59 +++++++++++++++++++---- RetroFE/Source/Database/Configuration.h | 7 ++- RetroFE/Source/Main.cpp | 1 + RetroFE/Source/RetroFE.cpp | 2 + RetroFE/Source/Utility/Utils.cpp | 12 +++++ RetroFE/Source/Utility/Utils.h | 1 + 6 files changed, 70 insertions(+), 12 deletions(-) diff --git a/RetroFE/Source/Database/Configuration.cpp b/RetroFE/Source/Database/Configuration.cpp index d6ae685..4ca1575 100644 --- a/RetroFE/Source/Database/Configuration.cpp +++ b/RetroFE/Source/Database/Configuration.cpp @@ -70,6 +70,16 @@ void Configuration::Initialize() } } +void Configuration::SetCurrentCollection(std::string collection) +{ + CurrentCollection = collection; +} + +std::string Configuration::GetCurrentCollection() +{ + return CurrentCollection; +} + bool Configuration::Import(std::string keyPrefix, std::string file) { bool retVal = true; @@ -133,7 +143,6 @@ bool Configuration::ParseLine(std::string keyPrefix, std::string line, int lineC value = line.substr(position + delimiter.length(), line.length()); value = TrimEnds(value); - Properties.insert(PropertiesPair(key, value)); std::stringstream ss; @@ -171,6 +180,7 @@ std::string Configuration::TrimEnds(std::string str) bool Configuration::GetProperty(std::string key, std::string &value) { bool retVal = false; + if(Properties.find(key) != Properties.end()) { value = Properties[key]; @@ -182,6 +192,7 @@ bool Configuration::GetProperty(std::string key, std::string &value) Logger::Write(Logger::ZONE_DEBUG, "Configuration", "Missing property " + key); } + value = Translate(value); return retVal; } @@ -210,15 +221,6 @@ bool Configuration::GetProperty(std::string key, bool &value) if(retVal) { - std::stringstream ss; - ss << strValue; - - for(unsigned int i=0; i < strValue.length(); ++i) - { - std::locale loc; - strValue[i] = std::tolower(strValue[i], loc); - } - if(!strValue.compare("yes") || !strValue.compare("true")) { value = true; @@ -342,4 +344,41 @@ void Configuration::SetVerbose(bool verbose) this->Verbose = verbose; } +std::string Configuration::Translate(std::string str) +{ + std::string translated; + std::size_t startIndex = 0; + + while(str.find("%") != std::string::npos) + { + std::size_t startIndex = str.find("%"); + std::string var = str.substr(startIndex + 1); + + // copy everything before the first % + translated += str.substr(0, startIndex); + + str = var; // discard the old unprocessed data up until the first % + + std::size_t endIndex = var.find("%"); + var = var.substr(0, endIndex); + str = str.substr(endIndex + 1); + + std::string result; + + if(var == "collectionName") + { + result = GetCurrentCollection(); + } + else + { + GetProperty(var, result); + } + + translated += result; + } + + //copy the remaining string + translated += str; + return translated; +} diff --git a/RetroFE/Source/Database/Configuration.h b/RetroFE/Source/Database/Configuration.h index 2918ed5..ecad4e6 100644 --- a/RetroFE/Source/Database/Configuration.h +++ b/RetroFE/Source/Database/Configuration.h @@ -19,7 +19,8 @@ public: // gets the global configuration bool Import(std::string keyPrefix, std::string file); - + void SetCurrentCollection(std::string collection); + std::string GetCurrentCollection(); bool GetProperty(std::string key, std::string &value); bool GetProperty(std::string key, int &value); bool GetProperty(std::string key, bool &value); @@ -30,7 +31,7 @@ public: bool GetPropertyAbsolutePath(std::string key, std::string &value); bool IsVerbose() const; void SetVerbose(bool verbose); - bool IsRequiredPropertiesSet(); + std::string Translate(std::string str); private: bool ParseLine(std::string keyPrefix, std::string line, int lineCount); @@ -40,5 +41,7 @@ private: bool Verbose; static std::string AbsolutePath; + std::string CurrentCollection; PropertiesType Properties; + }; diff --git a/RetroFE/Source/Main.cpp b/RetroFE/Source/Main.cpp index 204a52c..722be0d 100644 --- a/RetroFE/Source/Main.cpp +++ b/RetroFE/Source/Main.cpp @@ -34,6 +34,7 @@ CollectionDatabase *InitializeCollectionDatabase(DB &db, Configuration &config); int main(int argc, char *argv[]) { + Configuration::Initialize(); Configuration config; diff --git a/RetroFE/Source/RetroFE.cpp b/RetroFE/Source/RetroFE.cpp index 3067cdc..bbed359 100644 --- a/RetroFE/Source/RetroFE.cpp +++ b/RetroFE/Source/RetroFE.cpp @@ -514,6 +514,7 @@ Page *RetroFE::LoadPage(std::string collectionName) } else { + Config.SetCurrentCollection(collectionName); page->SetItems(collection); page->Start(); @@ -527,6 +528,7 @@ Page *RetroFE::LoadSplashPage() PageBuilder pb("Splash", "", Config, &FC); std::vector *coll = new std::vector(); Page * page = pb.BuildPage(); + Config.SetCurrentCollection(""); page->SetItems(coll); page->Start(); PageChain.push_back(page); diff --git a/RetroFE/Source/Utility/Utils.cpp b/RetroFE/Source/Utility/Utils.cpp index d5b76f6..cd59b94 100644 --- a/RetroFE/Source/Utility/Utils.cpp +++ b/RetroFE/Source/Utility/Utils.cpp @@ -21,6 +21,8 @@ #include #include #include +#include + Utils::Utils() { @@ -30,6 +32,16 @@ Utils::~Utils() { } +std::string Utils::ToLower(std::string str) +{ + for(unsigned int i=0; i < str.length(); ++i) + { + std::locale loc; + str[i] = std::tolower(str[i], loc); + } + + return str; +} bool Utils::FindMatchingFile(std::string prefix, std::vector &extensions, std::string &file) { diff --git a/RetroFE/Source/Utility/Utils.h b/RetroFE/Source/Utility/Utils.h index 7917ed4..75e78c1 100644 --- a/RetroFE/Source/Utility/Utils.h +++ b/RetroFE/Source/Utility/Utils.h @@ -19,6 +19,7 @@ public: static std::string GetParentDirectory(std::string filePath); static std::string GetFileName(std::string filePath); static bool FindMatchingFile(std::string prefix, std::vector &extensions, std::string &file); + static std::string ToLower(std::string str); private: Utils(); virtual ~Utils();