From add7e06a641a13382b22e2ebfa7d5f20e810dffc Mon Sep 17 00:00:00 2001 From: Alexander Bock Date: Sat, 27 Sep 2014 18:54:10 +0200 Subject: [PATCH] Cleaning up ConfigurationManager --- .../openspace/engine/configurationmanager.h | 4 - src/engine/configurationmanager.cpp | 78 +++++++------------ 2 files changed, 27 insertions(+), 55 deletions(-) diff --git a/include/openspace/engine/configurationmanager.h b/include/openspace/engine/configurationmanager.h index 6cbd98780a..15e29ed8bf 100644 --- a/include/openspace/engine/configurationmanager.h +++ b/include/openspace/engine/configurationmanager.h @@ -32,10 +32,6 @@ namespace openspace { class ConfigurationManager : public ghoul::Dictionary { public: bool loadFromFile(const std::string& filename); - -private: - bool registerPaths(); - bool registerBasePathFromConfigurationFile(const std::string& filename); }; } // namespace openspace diff --git a/src/engine/configurationmanager.cpp b/src/engine/configurationmanager.cpp index 7ddda9ab00..a21ea3619c 100644 --- a/src/engine/configurationmanager.cpp +++ b/src/engine/configurationmanager.cpp @@ -36,80 +36,56 @@ namespace { namespace openspace { bool ConfigurationManager::loadFromFile(const std::string& filename) { - const bool basePathSuccess = registerBasePathFromConfigurationFile(filename); - if (!basePathSuccess) { - LERROR("Registering Base Path failed"); - return false; + using ghoul::filesystem::FileSystem; + using constants::configurationmanager::keyPaths; + if (!FileSys.fileExists(filename)) { + LERROR("Could not find file '" << filename << "'"); + return false; } - + + // ${BASE_PATH} + std::string&& basePathToken = FileSystem::TokenOpeningBraces + + constants::configurationmanager::paths::keyBasePath + + FileSystem::TokenClosingBraces; + + // Retrieving the directory in which the configuration file lies + std::string absolutePath = FileSys.absolutePath(filename); + std::string basePath = ghoul::filesystem::File(absolutePath).directoryName(); + FileSys.registerPathToken(basePathToken, basePath); + + // Loading the configuration file into ourselves const bool loadingSuccess = ghoul::lua::loadDictionaryFromFile(filename, *this); if (!loadingSuccess) { LERROR("Loading dictionary from file failed"); return false; } - const bool registerSuccess = registerPaths(); - if (!registerSuccess) { - LERROR("Registering paths failed"); - return false; - } - - return true; -} - -bool ConfigurationManager::registerPaths() { + // Register all the paths ghoul::Dictionary dictionary; - const bool success = getValueSafe(constants::configurationmanager::keyPaths, dictionary); + const bool success = getValueSafe(keyPaths, dictionary); if (!success) { - LERROR("Configuration does not contain the key '" << - constants::configurationmanager::keyPaths << "'"); + LERROR("Configuration does not contain the key '" << keyPaths << "'"); return false; } - std::vector&& pathKeys = dictionary.keys(); - for (const std::string& key : pathKeys) { + std::vector pathKeys = dictionary.keys(); + for (std::string key : pathKeys) { std::string p; if (dictionary.getValue(key, p)) { - const std::string fullKey - = ghoul::filesystem::FileSystem::TokenOpeningBraces + key - + ghoul::filesystem::FileSystem::TokenClosingBraces; + std::string fullKey + = FileSystem::TokenOpeningBraces + key + + FileSystem::TokenClosingBraces; LDEBUG("Registering path " << fullKey << ": " << p); - std::string&& basePath = ghoul::filesystem::FileSystem::TokenOpeningBraces + - constants::configurationmanager::paths::keyBasePath - + ghoul::filesystem::FileSystem::TokenClosingBraces; - - bool override = (basePath == fullKey); + bool override = (basePathToken == fullKey); if (override) LINFO("Overriding base path with '" << p << "'"); - FileSys.registerPathToken(fullKey, p, override); + FileSys.registerPathToken(std::move(fullKey), std::move(p), override); } } return true; } -bool ConfigurationManager::registerBasePathFromConfigurationFile(const std::string& filename) -{ - if (!FileSys.fileExists(filename)) - return false; - - const std::string absolutePath = FileSys.absolutePath(filename); - - std::string::size_type last - = absolutePath.find_last_of(ghoul::filesystem::FileSystem::PathSeparator); - if (last == std::string::npos) - return false; - - std::string basePath = absolutePath.substr(0, last); - - std::string&& basePathToken = ghoul::filesystem::FileSystem::TokenOpeningBraces + - constants::configurationmanager::paths::keyBasePath - + ghoul::filesystem::FileSystem::TokenClosingBraces; - - FileSys.registerPathToken(basePathToken, basePath); - - return true; -} } // namespace openspace