diff options
Diffstat (limited to 'src/Common/ConfigManager.cpp')
-rw-r--r-- | src/Common/ConfigManager.cpp | 62 |
1 files changed, 51 insertions, 11 deletions
diff --git a/src/Common/ConfigManager.cpp b/src/Common/ConfigManager.cpp index 2159c6e..4d0e476 100644 --- a/src/Common/ConfigManager.cpp +++ b/src/Common/ConfigManager.cpp @@ -20,33 +20,73 @@ #include "ConfigManager.h" #include "Util.h" #include <fstream> +#include <stdexcept> namespace Mad { namespace Common { bool ConfigManager::loadFile(const std::string &filename) { std::ifstream file(filename.c_str()); + std::vector<std::string> section; + std::string line, key; if(!file.good()) return false; - while(!file.eof()) { - std::string line; + while(!(file.eof() && line.empty())) { + std::string nextLine; + char bracket = 0; - std::getline(file, line); - line = Util::trim(line); + if(line.empty()) { + std::getline(file, line); - if(line.empty() || line[0] == '#') - continue; + size_t pos = line.find_first_of('#'); + if(pos != std::string::npos) + line[pos] = 0; - size_t pos = line.find_first_of(" \t"); + line = Util::trim(line); - if(pos == std::string::npos) - parseLine(line); - else - parseLine(line.substr(0, pos), Util::trim(line.substr(pos))); + if(line.empty()) + continue; + } + + size_t pos = line.find_first_of("{}"); + + if(pos != std::string::npos) { + bracket = line[pos]; + line = Util::trim(line.substr(0, pos)); + try { + nextLine = Util::trim(line.substr(pos+1)); + } + catch(std::out_of_range& e) {} + } + + if(!line.empty()) { + pos = line.find_first_of(" \t"); + + if(pos == std::string::npos) { + key = line; + parseLine(section, key); + } + else { + key = line.substr(0, pos); + parseLine(section, key, Util::trim(line.substr(pos))); + } + } + + switch(bracket) { + case '{': + section.push_back(key); + break; + case '}': + section.pop_back(); + } + + line = nextLine; } + // TODO Depth check + return true; } |