mirror of
https://github.com/OpenSpace/OpenSpace.git
synced 2026-01-01 17:20:09 -06:00
Cleaning up ConfigurationManager
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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<std::string>&& pathKeys = dictionary.keys();
|
||||
for (const std::string& key : pathKeys) {
|
||||
std::vector<std::string> 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
|
||||
|
||||
Reference in New Issue
Block a user