Issue #168 - Configuration cleanup

Corrected tokens processing. If directory exist then tokens
shall be replaced by correct path, otherwise they are silently
removed from path container.

Signed-off-by: Lukasz Gromanowski <lgromanowski@gmail.com>
This commit is contained in:
Lukasz Gromanowski 2012-01-28 09:49:09 +01:00
parent f4b6caac59
commit c5dee2c4fb
3 changed files with 17 additions and 42 deletions

View file

@ -55,10 +55,10 @@ ConfigurationManager::~ConfigurationManager()
void ConfigurationManager::setupTokensMapping() void ConfigurationManager::setupTokensMapping()
{ {
mTokensMapping.insert(std::make_pair(mwDataToken, &FixedPath<>::setInstallPath)); mTokensMapping.insert(std::make_pair(mwDataToken, &FixedPath<>::getInstallPath));
mTokensMapping.insert(std::make_pair(localDataToken, &FixedPath<>::setLocalDataPath)); mTokensMapping.insert(std::make_pair(localDataToken, &FixedPath<>::getLocalDataPath));
mTokensMapping.insert(std::make_pair(userDataToken, &FixedPath<>::setUserDataPath)); mTokensMapping.insert(std::make_pair(userDataToken, &FixedPath<>::getUserDataPath));
mTokensMapping.insert(std::make_pair(globalDataToken, &FixedPath<>::setGlobalDataPath)); mTokensMapping.insert(std::make_pair(globalDataToken, &FixedPath<>::getGlobalDataPath));
} }
void ConfigurationManager::readConfiguration(boost::program_options::variables_map& variables, void ConfigurationManager::readConfiguration(boost::program_options::variables_map& variables,
@ -87,21 +87,17 @@ void ConfigurationManager::processPaths(Files::PathContainer& dataDirs)
for (Files::PathContainer::iterator it = dataDirs.begin(); it != dataDirs.end(); ++it) for (Files::PathContainer::iterator it = dataDirs.begin(); it != dataDirs.end(); ++it)
{ {
const std::string& path = it->string(); const std::string& path = it->string();
if (!path.empty() && path[0] == '?')
{
std::string::size_type pos = path.find('?', 1);
if (pos != std::string::npos)
{
++pos;
TokensMappingContainer::iterator tokenIt = mTokensMapping.find(path.substr(0, pos));
// Check if path contains a token
if (!path.empty() && *path.begin() == '?' && *path.rbegin() == '?')
{
TokensMappingContainer::iterator tokenIt = mTokensMapping.find(path);
if (tokenIt != mTokensMapping.end()) if (tokenIt != mTokensMapping.end())
{ {
boost::filesystem::path tempPath(path.substr(pos, path.length() - pos)); boost::filesystem::path tempPath(((mFixedPath).*(tokenIt->second))());
if (boost::filesystem::is_directory(tempPath)) if (boost::filesystem::is_directory(tempPath))
{ {
((mFixedPath).*(tokenIt->second))(tempPath);
(*it) = tempPath; (*it) = tempPath;
} }
else else
@ -111,7 +107,6 @@ void ConfigurationManager::processPaths(Files::PathContainer& dataDirs)
} }
} }
} }
}
dataDirs.erase(std::remove_if(dataDirs.begin(), dataDirs.end(), EmptyPath()), dataDirs.end()); dataDirs.erase(std::remove_if(dataDirs.begin(), dataDirs.end(), EmptyPath()), dataDirs.end());
} }

View file

@ -44,7 +44,7 @@ struct ConfigurationManager
private: private:
typedef Files::FixedPath<> FixedPathType; typedef Files::FixedPath<> FixedPathType;
typedef void (FixedPathType::*path_type_f)(const boost::filesystem::path&); typedef const boost::filesystem::path& (FixedPathType::*path_type_f)() const;
typedef std::tr1::unordered_map<std::string, path_type_f> TokensMappingContainer; typedef std::tr1::unordered_map<std::string, path_type_f> TokensMappingContainer;
void loadConfig(const boost::filesystem::path& path, void loadConfig(const boost::filesystem::path& path,

View file

@ -125,41 +125,21 @@ struct FixedPath
return mInstallPath; return mInstallPath;
} }
void setInstallPath(const boost::filesystem::path& path)
{
mInstallPath = path;
}
const boost::filesystem::path& getGlobalDataPath() const const boost::filesystem::path& getGlobalDataPath() const
{ {
return mGlobalDataPath; return mGlobalDataPath;
} }
void setGlobalDataPath(const boost::filesystem::path& path)
{
mGlobalDataPath = path;
}
const boost::filesystem::path& getUserDataPath() const const boost::filesystem::path& getUserDataPath() const
{ {
return mUserDataPath; return mUserDataPath;
} }
void setUserDataPath(const boost::filesystem::path& path)
{
mUserDataPath = path;
}
const boost::filesystem::path& getLocalDataPath() const const boost::filesystem::path& getLocalDataPath() const
{ {
return mLocalDataPath; return mLocalDataPath;
} }
void setLocalDataPath(const boost::filesystem::path& path)
{
mLocalDataPath = path;
}
private: private:
PathType mPath; PathType mPath;