From e000cdf9d31433c4e90a8b59415de5ac8377005d Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Tue, 8 Jul 2008 04:54:10 +0200 Subject: Verschachtelte Konfigurationen (D?mon-Liste) --- src/Common/ConfigManager.cpp | 62 ++++++++++++++++++++++++++++++++++++-------- src/Common/ConfigManager.h | 5 ++-- 2 files changed, 53 insertions(+), 14 deletions(-) (limited to 'src/Common') 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 +#include namespace Mad { namespace Common { bool ConfigManager::loadFile(const std::string &filename) { std::ifstream file(filename.c_str()); + std::vector 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; } diff --git a/src/Common/ConfigManager.h b/src/Common/ConfigManager.h index ccbfbaa..68669d3 100644 --- a/src/Common/ConfigManager.h +++ b/src/Common/ConfigManager.h @@ -21,6 +21,7 @@ #define MAD_COMMON_CONFIGMANAGER_H_ #include +#include namespace Mad { namespace Common { @@ -29,14 +30,12 @@ class ConfigManager { protected: ConfigManager() {} - virtual bool parseLine(const std::string &key, const std::string &value = std::string()) = 0; + virtual bool parseLine(const std::vector §ion, const std::string &key, const std::string &value = std::string()) = 0; bool loadFile(const std::string &filename); public: virtual ~ConfigManager() {} - - //virtual const std::string& getValue(const std::string &value) const = 0; }; } -- cgit v1.2.3