summaryrefslogtreecommitdiffstats
path: root/src/Core/ConfigEntry.cpp
blob: ec716b66f80c4669bca95a01d77f77d912461198 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/*
 * ConfigEntry.cpp
 *
 * Copyright (C) 2008 Matthias Schiffer <matthias@gamezock.de>
 *
 * 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 <http://www.gnu.org/licenses/>.
 */

#include "ConfigEntry.h"

namespace Mad {
namespace Core {

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 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<const ConfigEntry*> 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<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);
}

}
}