45
ConfigTree::~ConfigTree()
47
// erase every new ItemMap (by CreateSection)
48
SectionMap::iterator iter = sections_.begin();
50
while (iter != sections_.end()) {
57
45
void ConfigTree::addDefaultValue(const std::pair<std::string, std::string>& token, std::string section)
59
47
defaultValueMap_.insert(token);
82
70
// if we doesn't find the item, create it
83
71
if (sections_.find(section) == sections_.end())
84
sections_[section] = new ItemMap;
72
sections_[section] = ItemMap();
100
88
/** Retrieve the sections as an array */
102
ConfigTree::getSections()
89
std::list<std::string>
90
ConfigTree::getSections() const
106
SectionMap::iterator iter = sections_.begin();
108
while (iter != sections_.end()) {
109
// add to token list the: iter->second;
92
std::list<std::string> sections;
94
for (SectionMap::const_iterator iter = sections_.begin(); iter != sections_.end(); ++iter)
110
95
sections.push_back(iter->first);
125
108
SectionMap::iterator iter = sections_.find(section);
127
110
if (iter == sections_.end()) {
128
sections_[section] = new ItemMap;
111
sections_[section] = ItemMap();
129
112
iter = sections_.find(section);
133
116
if (iter != sections_.end()) {
134
117
std::string name(item.getName());
136
if (iter->second->find(name) == iter->second->end()) {
137
(*(iter->second))[name] = item;
119
if (iter->second.find(name) == iter->second.end())
120
iter->second[name] = item;
164
146
ConfigTree::getConfigTreeItemBoolValue(const std::string& section, const std::string& itemName) const
166
std::string configItem = getConfigTreeItemValue(section, itemName);
168
if (configItem == "true") {
148
return getConfigTreeItemValue(section, itemName) == "true";
176
ConfigTree::getConfigTreeItemToken(const std::string& section, const std::string& itemName, TokenList& arg) const
152
ConfigTree::getConfigTreeItemToken(const std::string& section, const std::string& itemName, std::list<std::string>& arg) const
178
154
const ConfigTreeItem *item = getConfigTreeItem(section, itemName);
201
177
if (iter == sections_.end())
204
ItemMap::const_iterator iterItem = iter->second->find(itemName);
180
ItemMap::const_iterator iterItem = iter->second.find(itemName);
206
if (iterItem == iter->second->end())
182
if (iterItem == iter->second.end())
209
185
return & (iterItem->second);
215
191
* @todo Élimier les 45,000 classes qui servent à rien pour Conf.
216
192
* The true/false logic is useless here.
219
ConfigTree::setConfigTreeItem(const std::string& section,
194
void ConfigTree::setConfigTreeItem(const std::string& section,
220
195
const std::string& itemName,
221
196
const std::string& value)
224
198
SectionMap::iterator iter = sections_.find(section);
226
200
if (iter == sections_.end()) {
227
201
// Not found, create section
228
sections_[section] = new ItemMap;
202
sections_[section] = ItemMap();
229
203
iter = sections_.find(section);
232
ItemMap::iterator iterItem = iter->second->find(itemName);
206
ItemMap::iterator iterItem = iter->second.find(itemName);
234
if (iterItem == iter->second->end()) {
208
if (iterItem == iter->second.end()) {
235
209
// If not found, search in our default list to find
236
210
// something that would fit.
237
211
std::string defaultValue = getDefaultValue(itemName);
238
212
addConfigTreeItem(section, ConfigTreeItem(itemName, value, defaultValue));
242
216
// Use default value if the value is empty.
243
if (value.empty() == true) {
244
218
iterItem->second.setValue(getDefaultValue(itemName));
248
222
iterItem->second.setValue(value);
253
226
// Save config to a file (ini format)
254
227
// return false if empty, no config, or enable to open
255
228
// return true if everything is ok
257
ConfigTree::saveConfigTree(const std::string& fileName)
230
ConfigTree::saveConfigTree(const std::string& fileName) const
259
232
DEBUG("ConfigTree: Save %s", fileName.c_str());
273
246
// for each section, for each item...
274
SectionMap::iterator iter = sections_.begin();
276
while (iter != sections_.end()) {
247
for (SectionMap::const_iterator iter = sections_.begin(); iter != sections_.end(); ++iter) {
277
248
file << "[" << iter->first << "]" << std::endl;
278
ItemMap::iterator iterItem = iter->second->begin();
280
while (iterItem != iter->second->end()) {
249
for (ItemMap::const_iterator iterItem = iter->second.begin(); iterItem != iter->second.end(); ++iterItem)
281
250
file << iterItem->first << "=" << iterItem->second.getValue() << std::endl;
285
252
file << std::endl;
368
331
if (chmod(fileName.c_str(), S_IRUSR | S_IWUSR))
369
332
DEBUG("Failed to set permission on configuration file because: %m");
375
ConfigTreeIterator::begin()
337
std::list<std::string>
338
ConfigTreeIterator::begin() const
340
std::list<std::string> tk;
378
341
iter_ = tree_->sections_.begin();
380
if (iter_!=tree_->sections_.end()) {
381
iterItem_ = iter_->second->begin();
343
if (iter_ != tree_->sections_.end()) {
344
iterItem_ = iter_->second.begin();
383
if (iterItem_!=iter_->second->end()) {
346
if (iterItem_ != iter_->second.end()) {
384
347
tk.push_back(iter_->first);
385
348
tk.push_back(iterItem_->first);
386
349
tk.push_back(iterItem_->second.getType());
358
std::list<std::string>
396
359
ConfigTreeIterator::next()
361
std::list<std::string> tk;
399
362
// we return tk empty if we are at the end of the list...
401
364
if (iter_ == tree_->sections_.end())
404
if (iterItem_ != iter_->second->end())
367
if (iterItem_ != iter_->second.end())
407
if (iterItem_ == iter_->second->end()) {
370
if (iterItem_ == iter_->second.end()) {
408
371
// if we increment, and we are at the end of a section
411
374
if (iter_ != tree_->sections_.end()) {
412
iterItem_ = iter_->second->begin();
375
iterItem_ = iter_->second.begin();
414
if (iterItem_ != iter_->second->end()) {
377
if (iterItem_ != iter_->second.end()) {
415
378
tk.push_back(iter_->first);
416
379
tk.push_back(iterItem_->first);
417
380
tk.push_back(iterItem_->second.getType());