summaryrefslogtreecommitdiffstats
path: root/src/Core/ConfigEntry.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/Core/ConfigEntry.cpp')
-rw-r--r--src/Core/ConfigEntry.cpp94
1 files changed, 68 insertions, 26 deletions
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<ConfigEntry> 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<const ConfigEntry*> 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<const ConfigEntry*>();
+ }
+ else {
+ if(dotIndex >= 0) {
+ return child->second.back()->getEntries(key.substr(dotIndex+1));
+ }
+ else {
+ std::vector<const ConfigEntry*> ret;
+
+ for(EntryVector::const_iterator entry = child->second.begin(); entry != child->second.end(); ++entry) {
+ ret.push_back(entry->get());
+ }
+
+ return ret;
+ }
}
}
+std::vector<String> ConfigEntry::getAll(const String &key) const {
+ const ConfigEntry *entry = getEntry(key);
+ if(!entry)
+ return std::vector<String>();
+ 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);
+}
+
}
}