/* * ConfigEntry.cpp * * Copyright (C) 2008 Matthias Schiffer * * This program is free software: you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as published by the * Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * See the GNU Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License along * with this program. If not, see . */ #include "ConfigEntry.h" namespace Mad { namespace Core { 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 ConfigEntry* ConfigEntry::getEntry(const String &key) const { String childKey(key); childKey.toLower(); int32_t dotIndex = key.indexOf('.'); if(dotIndex >= 0) { childKey.remove(dotIndex); } EntryMap::const_iterator child = children.find(childKey); if(child == children.end()) { return 0; } else { if(dotIndex >= 0) { return child->second.back()->getEntry(key.substr(dotIndex+1)); } else { return child->second.back().get(); } } } std::vector ConfigEntry::getEntries(const String &key) const { String childKey(key); childKey.toLower(); int32_t dotIndex = key.indexOf('.'); if(dotIndex >= 0) { childKey.remove(dotIndex); } 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); } } }