From 86e5f80837ad55932f2469d79d9e6b6bb07cf5ed Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Mon, 28 Sep 2009 19:09:22 +0200 Subject: Implemented new ConfigManager --- src/Core/ConfigEntry.cpp | 94 ++++++++++++++++++++++++++++++++++-------------- 1 file changed, 68 insertions(+), 26 deletions(-) (limited to 'src/Core/ConfigEntry.cpp') diff --git a/src/Core/ConfigEntry.cpp b/src/Core/ConfigEntry.cpp index 04ed08f..ec716b6 100644 --- a/src/Core/ConfigEntry.cpp +++ b/src/Core/ConfigEntry.cpp @@ -22,43 +22,85 @@ namespace Mad { namespace Core { -String& ConfigEntry::Entry::operator[] (std::size_t i) { - try { - return value.at(i); - } - catch(std::out_of_range &e) { - zero = String(); - return zero; - } +void ConfigEntry::addChild(const String &key, boost::shared_ptr entry) { + String lowerKey(key); + lowerKey.toLower(); + + EntryMap::iterator child = children.find(lowerKey); + + if(child == children.end()) + child = children.insert(std::make_pair(lowerKey, EntryVector())).first; + + child->second.push_back(entry); } -const String& ConfigEntry::Entry::operator[] (std::size_t i) const { - try { - return value.at(i); - } - catch(std::out_of_range &e) { - return constZero; +const ConfigEntry* ConfigEntry::getEntry(const String &key) const { + String childKey(key); + childKey.toLower(); + + int32_t dotIndex = key.indexOf('.'); + if(dotIndex >= 0) { + childKey.remove(dotIndex); } -} -ConfigEntry::Entry& ConfigEntry::operator[] (std::size_t i) { - try { - return entries.at(i); + EntryMap::const_iterator child = children.find(childKey); + if(child == children.end()) { + return 0; } - catch(std::out_of_range &e) { - zero = Entry(); - return zero; + else { + if(dotIndex >= 0) { + return child->second.back()->getEntry(key.substr(dotIndex+1)); + } + else { + return child->second.back().get(); + } } } -const ConfigEntry::Entry& ConfigEntry::operator[] (std::size_t i) const { - try { - return entries.at(i); +std::vector ConfigEntry::getEntries(const String &key) const { + String childKey(key); + childKey.toLower(); + + int32_t dotIndex = key.indexOf('.'); + if(dotIndex >= 0) { + childKey.remove(dotIndex); } - catch(std::out_of_range &e) { - return constZero; + + EntryMap::const_iterator child = children.find(childKey); + if(child == children.end()) { + return std::vector(); + } + else { + if(dotIndex >= 0) { + return child->second.back()->getEntries(key.substr(dotIndex+1)); + } + else { + std::vector ret; + + for(EntryVector::const_iterator entry = child->second.begin(); entry != child->second.end(); ++entry) { + ret.push_back(entry->get()); + } + + return ret; + } } } +std::vector ConfigEntry::getAll(const String &key) const { + const ConfigEntry *entry = getEntry(key); + if(!entry) + return std::vector(); + else + return entry->getValues(); +} + +String ConfigEntry::get(const String &key, const String &defaultValue) const { + const ConfigEntry *entry = getEntry(key); + if(!entry) + return defaultValue; + else + return entry->getValue(defaultValue); +} + } } -- cgit v1.2.3