Support for config files to reference config variables (i.e %someConfigName%)

This commit is contained in:
emb 2015-01-17 09:51:40 -06:00
parent db1a284bb5
commit 50f8375a0b
6 changed files with 70 additions and 12 deletions

View File

@ -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;
}

View File

@ -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;
};

View File

@ -34,6 +34,7 @@ CollectionDatabase *InitializeCollectionDatabase(DB &db, Configuration &config);
int main(int argc, char *argv[])
{
Configuration::Initialize();
Configuration config;

View File

@ -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<Item *> *coll = new std::vector<Item *>();
Page * page = pb.BuildPage();
Config.SetCurrentCollection("");
page->SetItems(coll);
page->Start();
PageChain.push_back(page);

View File

@ -21,6 +21,8 @@
#include <sstream>
#include <fstream>
#include <dirent.h>
#include <locale>
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<std::string> &extensions, std::string &file)
{

View File

@ -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<std::string> &extensions, std::string &file);
static std::string ToLower(std::string str);
private:
Utils();
virtual ~Utils();