Implement #6096
[oweals/minetest.git] / src / settings.cpp
1 /*
2 Minetest
3 Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #include "settings.h"
21 #include "irrlichttypes_bloated.h"
22 #include "exceptions.h"
23 #include "threading/mutex_auto_lock.h"
24 #include "util/strfnd.h"
25 #include <iostream>
26 #include <fstream>
27 #include <sstream>
28 #include "debug.h"
29 #include "log.h"
30 #include "util/serialize.h"
31 #include "filesys.h"
32 #include "noise.h"
33 #include <cctype>
34 #include <algorithm>
35
36 static Settings main_settings;
37 Settings *g_settings = &main_settings;
38 std::string g_settings_path;
39
40 Settings::~Settings()
41 {
42         clear();
43 }
44
45
46 Settings & Settings::operator += (const Settings &other)
47 {
48         update(other);
49
50         return *this;
51 }
52
53
54 Settings & Settings::operator = (const Settings &other)
55 {
56         if (&other == this)
57                 return *this;
58
59         MutexAutoLock lock(m_mutex);
60         MutexAutoLock lock2(other.m_mutex);
61
62         clearNoLock();
63         updateNoLock(other);
64
65         return *this;
66 }
67
68
69 bool Settings::checkNameValid(const std::string &name)
70 {
71         bool valid = name.find_first_of("=\"{}#") == std::string::npos;
72         if (valid) valid = trim(name) == name;
73         if (!valid) {
74                 errorstream << "Invalid setting name \"" << name << "\""
75                         << std::endl;
76                 return false;
77         }
78         return true;
79 }
80
81
82 bool Settings::checkValueValid(const std::string &value)
83 {
84         if (value.substr(0, 3) == "\"\"\"" ||
85                 value.find("\n\"\"\"") != std::string::npos) {
86                 errorstream << "Invalid character sequence '\"\"\"' found in"
87                         " setting value!" << std::endl;
88                 return false;
89         }
90         return true;
91 }
92
93 std::string Settings::getMultiline(std::istream &is, size_t *num_lines)
94 {
95         size_t lines = 1;
96         std::string value;
97         std::string line;
98
99         while (is.good()) {
100                 lines++;
101                 std::getline(is, line);
102                 if (line == "\"\"\"")
103                         break;
104                 value += line;
105                 value.push_back('\n');
106         }
107
108         size_t len = value.size();
109         if (len)
110                 value.erase(len - 1);
111
112         if (num_lines)
113                 *num_lines = lines;
114
115         return value;
116 }
117
118
119 bool Settings::readConfigFile(const char *filename)
120 {
121         std::ifstream is(filename);
122         if (!is.good())
123                 return false;
124
125         return parseConfigLines(is, "");
126 }
127
128
129 bool Settings::parseConfigLines(std::istream &is, const std::string &end)
130 {
131         MutexAutoLock lock(m_mutex);
132
133         std::string line, name, value;
134
135         while (is.good()) {
136                 std::getline(is, line);
137                 SettingsParseEvent event = parseConfigObject(line, end, name, value);
138
139                 switch (event) {
140                 case SPE_NONE:
141                 case SPE_INVALID:
142                 case SPE_COMMENT:
143                         break;
144                 case SPE_KVPAIR:
145                         m_settings[name] = SettingsEntry(value);
146                         break;
147                 case SPE_END:
148                         return true;
149                 case SPE_GROUP: {
150                         Settings *group = new Settings;
151                         if (!group->parseConfigLines(is, "}")) {
152                                 delete group;
153                                 return false;
154                         }
155                         m_settings[name] = SettingsEntry(group);
156                         break;
157                 }
158                 case SPE_MULTILINE:
159                         m_settings[name] = SettingsEntry(getMultiline(is));
160                         break;
161                 }
162         }
163
164         return end.empty();
165 }
166
167
168 void Settings::writeLines(std::ostream &os, u32 tab_depth) const
169 {
170         MutexAutoLock lock(m_mutex);
171
172         for (const auto &setting_it : m_settings)
173                 printEntry(os, setting_it.first, setting_it.second, tab_depth);
174 }
175
176
177 void Settings::printEntry(std::ostream &os, const std::string &name,
178         const SettingsEntry &entry, u32 tab_depth)
179 {
180         for (u32 i = 0; i != tab_depth; i++)
181                 os << "\t";
182
183         if (entry.is_group) {
184                 os << name << " = {\n";
185
186                 entry.group->writeLines(os, tab_depth + 1);
187
188                 for (u32 i = 0; i != tab_depth; i++)
189                         os << "\t";
190                 os << "}\n";
191         } else {
192                 os << name << " = ";
193
194                 if (entry.value.find('\n') != std::string::npos)
195                         os << "\"\"\"\n" << entry.value << "\n\"\"\"\n";
196                 else
197                         os << entry.value << "\n";
198         }
199 }
200
201
202 bool Settings::updateConfigObject(std::istream &is, std::ostream &os,
203         const std::string &end, u32 tab_depth)
204 {
205         SettingEntries::const_iterator it;
206         std::set<std::string> present_entries;
207         std::string line, name, value;
208         bool was_modified = false;
209         bool end_found = false;
210
211         // Add any settings that exist in the config file with the current value
212         // in the object if existing
213         while (is.good() && !end_found) {
214                 std::getline(is, line);
215                 SettingsParseEvent event = parseConfigObject(line, end, name, value);
216
217                 switch (event) {
218                 case SPE_END:
219                         os << line << (is.eof() ? "" : "\n");
220                         end_found = true;
221                         break;
222                 case SPE_MULTILINE:
223                         value = getMultiline(is);
224                         /* FALLTHROUGH */
225                 case SPE_KVPAIR:
226                         it = m_settings.find(name);
227                         if (it != m_settings.end() &&
228                                 (it->second.is_group || it->second.value != value)) {
229                                 printEntry(os, name, it->second, tab_depth);
230                                 was_modified = true;
231                         } else {
232                                 os << line << "\n";
233                                 if (event == SPE_MULTILINE)
234                                         os << value << "\n\"\"\"\n";
235                         }
236                         present_entries.insert(name);
237                         break;
238                 case SPE_GROUP:
239                         it = m_settings.find(name);
240                         if (it != m_settings.end() && it->second.is_group) {
241                                 os << line << "\n";
242                                 sanity_check(it->second.group != NULL);
243                                 was_modified |= it->second.group->updateConfigObject(is, os,
244                                         "}", tab_depth + 1);
245                         } else {
246                                 printEntry(os, name, it->second, tab_depth);
247                                 was_modified = true;
248                         }
249                         present_entries.insert(name);
250                         break;
251                 default:
252                         os << line << (is.eof() ? "" : "\n");
253                         break;
254                 }
255         }
256
257         // Add any settings in the object that don't exist in the config file yet
258         for (it = m_settings.begin(); it != m_settings.end(); ++it) {
259                 if (present_entries.find(it->first) != present_entries.end())
260                         continue;
261
262                 printEntry(os, it->first, it->second, tab_depth);
263                 was_modified = true;
264         }
265
266         return was_modified;
267 }
268
269
270 bool Settings::updateConfigFile(const char *filename)
271 {
272         MutexAutoLock lock(m_mutex);
273
274         std::ifstream is(filename);
275         std::ostringstream os(std::ios_base::binary);
276
277         bool was_modified = updateConfigObject(is, os, "");
278         is.close();
279
280         if (!was_modified)
281                 return true;
282
283         if (!fs::safeWriteToFile(filename, os.str())) {
284                 errorstream << "Error writing configuration file: \""
285                         << filename << "\"" << std::endl;
286                 return false;
287         }
288
289         return true;
290 }
291
292
293 bool Settings::parseCommandLine(int argc, char *argv[],
294                 std::map<std::string, ValueSpec> &allowed_options)
295 {
296         int nonopt_index = 0;
297         for (int i = 1; i < argc; i++) {
298                 std::string arg_name = argv[i];
299                 if (arg_name.substr(0, 2) != "--") {
300                         // If option doesn't start with -, read it in as nonoptX
301                         if (arg_name[0] != '-'){
302                                 std::string name = "nonopt";
303                                 name += itos(nonopt_index);
304                                 set(name, arg_name);
305                                 nonopt_index++;
306                                 continue;
307                         }
308                         errorstream << "Invalid command-line parameter \""
309                                         << arg_name << "\": --<option> expected." << std::endl;
310                         return false;
311                 }
312
313                 std::string name = arg_name.substr(2);
314
315                 std::map<std::string, ValueSpec>::iterator n;
316                 n = allowed_options.find(name);
317                 if (n == allowed_options.end()) {
318                         errorstream << "Unknown command-line parameter \""
319                                         << arg_name << "\"" << std::endl;
320                         return false;
321                 }
322
323                 ValueType type = n->second.type;
324
325                 std::string value;
326
327                 if (type == VALUETYPE_FLAG) {
328                         value = "true";
329                 } else {
330                         if ((i + 1) >= argc) {
331                                 errorstream << "Invalid command-line parameter \""
332                                                 << name << "\": missing value" << std::endl;
333                                 return false;
334                         }
335                         value = argv[++i];
336                 }
337
338                 set(name, value);
339         }
340
341         return true;
342 }
343
344
345
346 /***********
347  * Getters *
348  ***********/
349
350
351 const SettingsEntry &Settings::getEntry(const std::string &name) const
352 {
353         MutexAutoLock lock(m_mutex);
354
355         SettingEntries::const_iterator n;
356         if ((n = m_settings.find(name)) == m_settings.end()) {
357                 if ((n = m_defaults.find(name)) == m_defaults.end())
358                         throw SettingNotFoundException("Setting [" + name + "] not found.");
359         }
360         return n->second;
361 }
362
363
364 Settings *Settings::getGroup(const std::string &name) const
365 {
366         const SettingsEntry &entry = getEntry(name);
367         if (!entry.is_group)
368                 throw SettingNotFoundException("Setting [" + name + "] is not a group.");
369         return entry.group;
370 }
371
372
373 const std::string &Settings::get(const std::string &name) const
374 {
375         const SettingsEntry &entry = getEntry(name);
376         if (entry.is_group)
377                 throw SettingNotFoundException("Setting [" + name + "] is a group.");
378         return entry.value;
379 }
380
381
382 bool Settings::getBool(const std::string &name) const
383 {
384         return is_yes(get(name));
385 }
386
387
388 u16 Settings::getU16(const std::string &name) const
389 {
390         return stoi(get(name), 0, 65535);
391 }
392
393
394 s16 Settings::getS16(const std::string &name) const
395 {
396         return stoi(get(name), -32768, 32767);
397 }
398
399
400 u32 Settings::getU32(const std::string &name) const
401 {
402         return (u32) stoi(get(name));
403 }
404
405 s32 Settings::getS32(const std::string &name) const
406 {
407         return stoi(get(name));
408 }
409
410
411 float Settings::getFloat(const std::string &name) const
412 {
413         return stof(get(name));
414 }
415
416
417 u64 Settings::getU64(const std::string &name) const
418 {
419         u64 value = 0;
420         std::string s = get(name);
421         std::istringstream ss(s);
422         ss >> value;
423         return value;
424 }
425
426
427 v2f Settings::getV2F(const std::string &name) const
428 {
429         v2f value;
430         Strfnd f(get(name));
431         f.next("(");
432         value.X = stof(f.next(","));
433         value.Y = stof(f.next(")"));
434         return value;
435 }
436
437
438 v3f Settings::getV3F(const std::string &name) const
439 {
440         v3f value;
441         Strfnd f(get(name));
442         f.next("(");
443         value.X = stof(f.next(","));
444         value.Y = stof(f.next(","));
445         value.Z = stof(f.next(")"));
446         return value;
447 }
448
449
450 u32 Settings::getFlagStr(const std::string &name, const FlagDesc *flagdesc,
451         u32 *flagmask) const
452 {
453         std::string val = get(name);
454         return std::isdigit(val[0])
455                 ? stoi(val)
456                 : readFlagString(val, flagdesc, flagmask);
457 }
458
459
460 // N.B. if getStruct() is used to read a non-POD aggregate type,
461 // the behavior is undefined.
462 bool Settings::getStruct(const std::string &name, const std::string &format,
463         void *out, size_t olen) const
464 {
465         std::string valstr;
466
467         try {
468                 valstr = get(name);
469         } catch (SettingNotFoundException &e) {
470                 return false;
471         }
472
473         if (!deSerializeStringToStruct(valstr, format, out, olen))
474                 return false;
475
476         return true;
477 }
478
479
480 bool Settings::getNoiseParams(const std::string &name, NoiseParams &np) const
481 {
482         return getNoiseParamsFromGroup(name, np) || getNoiseParamsFromValue(name, np);
483 }
484
485
486 bool Settings::getNoiseParamsFromValue(const std::string &name,
487         NoiseParams &np) const
488 {
489         std::string value;
490
491         if (!getNoEx(name, value))
492                 return false;
493
494         Strfnd f(value);
495
496         np.offset   = stof(f.next(","));
497         np.scale    = stof(f.next(","));
498         f.next("(");
499         np.spread.X = stof(f.next(","));
500         np.spread.Y = stof(f.next(","));
501         np.spread.Z = stof(f.next(")"));
502         f.next(",");
503         np.seed     = stoi(f.next(","));
504         np.octaves  = stoi(f.next(","));
505         np.persist  = stof(f.next(","));
506
507         std::string optional_params = f.next("");
508         if (!optional_params.empty())
509                 np.lacunarity = stof(optional_params);
510
511         return true;
512 }
513
514
515 bool Settings::getNoiseParamsFromGroup(const std::string &name,
516         NoiseParams &np) const
517 {
518         Settings *group = NULL;
519
520         if (!getGroupNoEx(name, group))
521                 return false;
522
523         group->getFloatNoEx("offset",      np.offset);
524         group->getFloatNoEx("scale",       np.scale);
525         group->getV3FNoEx("spread",        np.spread);
526         group->getS32NoEx("seed",          np.seed);
527         group->getU16NoEx("octaves",       np.octaves);
528         group->getFloatNoEx("persistence", np.persist);
529         group->getFloatNoEx("lacunarity",  np.lacunarity);
530
531         np.flags = 0;
532         if (!group->getFlagStrNoEx("flags", np.flags, flagdesc_noiseparams))
533                 np.flags = NOISE_FLAG_DEFAULTS;
534
535         return true;
536 }
537
538
539 bool Settings::exists(const std::string &name) const
540 {
541         MutexAutoLock lock(m_mutex);
542
543         return (m_settings.find(name) != m_settings.end() ||
544                 m_defaults.find(name) != m_defaults.end());
545 }
546
547
548 std::vector<std::string> Settings::getNames() const
549 {
550         std::vector<std::string> names;
551         for (const auto &settings_it : m_settings) {
552                 names.push_back(settings_it.first);
553         }
554         return names;
555 }
556
557
558
559 /***************************************
560  * Getters that don't throw exceptions *
561  ***************************************/
562
563 bool Settings::getEntryNoEx(const std::string &name, SettingsEntry &val) const
564 {
565         try {
566                 val = getEntry(name);
567                 return true;
568         } catch (SettingNotFoundException &e) {
569                 return false;
570         }
571 }
572
573
574 bool Settings::getGroupNoEx(const std::string &name, Settings *&val) const
575 {
576         try {
577                 val = getGroup(name);
578                 return true;
579         } catch (SettingNotFoundException &e) {
580                 return false;
581         }
582 }
583
584
585 bool Settings::getNoEx(const std::string &name, std::string &val) const
586 {
587         try {
588                 val = get(name);
589                 return true;
590         } catch (SettingNotFoundException &e) {
591                 return false;
592         }
593 }
594
595
596 bool Settings::getFlag(const std::string &name) const
597 {
598         try {
599                 return getBool(name);
600         } catch(SettingNotFoundException &e) {
601                 return false;
602         }
603 }
604
605
606 bool Settings::getFloatNoEx(const std::string &name, float &val) const
607 {
608         try {
609                 val = getFloat(name);
610                 return true;
611         } catch (SettingNotFoundException &e) {
612                 return false;
613         }
614 }
615
616
617 bool Settings::getU16NoEx(const std::string &name, u16 &val) const
618 {
619         try {
620                 val = getU16(name);
621                 return true;
622         } catch (SettingNotFoundException &e) {
623                 return false;
624         }
625 }
626
627
628 bool Settings::getS16NoEx(const std::string &name, s16 &val) const
629 {
630         try {
631                 val = getS16(name);
632                 return true;
633         } catch (SettingNotFoundException &e) {
634                 return false;
635         }
636 }
637
638
639 bool Settings::getS32NoEx(const std::string &name, s32 &val) const
640 {
641         try {
642                 val = getS32(name);
643                 return true;
644         } catch (SettingNotFoundException &e) {
645                 return false;
646         }
647 }
648
649
650 bool Settings::getU64NoEx(const std::string &name, u64 &val) const
651 {
652         try {
653                 val = getU64(name);
654                 return true;
655         } catch (SettingNotFoundException &e) {
656                 return false;
657         }
658 }
659
660
661 bool Settings::getV2FNoEx(const std::string &name, v2f &val) const
662 {
663         try {
664                 val = getV2F(name);
665                 return true;
666         } catch (SettingNotFoundException &e) {
667                 return false;
668         }
669 }
670
671
672 bool Settings::getV3FNoEx(const std::string &name, v3f &val) const
673 {
674         try {
675                 val = getV3F(name);
676                 return true;
677         } catch (SettingNotFoundException &e) {
678                 return false;
679         }
680 }
681
682
683 // N.B. getFlagStrNoEx() does not set val, but merely modifies it.  Thus,
684 // val must be initialized before using getFlagStrNoEx().  The intention of
685 // this is to simplify modifying a flags field from a default value.
686 bool Settings::getFlagStrNoEx(const std::string &name, u32 &val,
687         FlagDesc *flagdesc) const
688 {
689         try {
690                 u32 flags, flagmask;
691
692                 flags = getFlagStr(name, flagdesc, &flagmask);
693
694                 val &= ~flagmask;
695                 val |=  flags;
696
697                 return true;
698         } catch (SettingNotFoundException &e) {
699                 return false;
700         }
701 }
702
703
704 /***********
705  * Setters *
706  ***********/
707
708 bool Settings::setEntry(const std::string &name, const void *data,
709         bool set_group, bool set_default)
710 {
711         Settings *old_group = NULL;
712
713         if (!checkNameValid(name))
714                 return false;
715         if (!set_group && !checkValueValid(*(const std::string *)data))
716                 return false;
717
718         {
719                 MutexAutoLock lock(m_mutex);
720
721                 SettingsEntry &entry = set_default ? m_defaults[name] : m_settings[name];
722                 old_group = entry.group;
723
724                 entry.value    = set_group ? "" : *(const std::string *)data;
725                 entry.group    = set_group ? *(Settings **)data : NULL;
726                 entry.is_group = set_group;
727         }
728
729         delete old_group;
730
731         return true;
732 }
733
734
735 bool Settings::set(const std::string &name, const std::string &value)
736 {
737         if (!setEntry(name, &value, false, false))
738                 return false;
739
740         doCallbacks(name);
741         return true;
742 }
743
744
745 bool Settings::setDefault(const std::string &name, const std::string &value)
746 {
747         return setEntry(name, &value, false, true);
748 }
749
750
751 bool Settings::setGroup(const std::string &name, Settings *group)
752 {
753         return setEntry(name, &group, true, false);
754 }
755
756
757 bool Settings::setGroupDefault(const std::string &name, Settings *group)
758 {
759         return setEntry(name, &group, true, true);
760 }
761
762
763 bool Settings::setBool(const std::string &name, bool value)
764 {
765         return set(name, value ? "true" : "false");
766 }
767
768
769 bool Settings::setS16(const std::string &name, s16 value)
770 {
771         return set(name, itos(value));
772 }
773
774
775 bool Settings::setU16(const std::string &name, u16 value)
776 {
777         return set(name, itos(value));
778 }
779
780
781 bool Settings::setS32(const std::string &name, s32 value)
782 {
783         return set(name, itos(value));
784 }
785
786
787 bool Settings::setU64(const std::string &name, u64 value)
788 {
789         std::ostringstream os;
790         os << value;
791         return set(name, os.str());
792 }
793
794
795 bool Settings::setFloat(const std::string &name, float value)
796 {
797         return set(name, ftos(value));
798 }
799
800
801 bool Settings::setV2F(const std::string &name, v2f value)
802 {
803         std::ostringstream os;
804         os << "(" << value.X << "," << value.Y << ")";
805         return set(name, os.str());
806 }
807
808
809 bool Settings::setV3F(const std::string &name, v3f value)
810 {
811         std::ostringstream os;
812         os << "(" << value.X << "," << value.Y << "," << value.Z << ")";
813         return set(name, os.str());
814 }
815
816
817 bool Settings::setFlagStr(const std::string &name, u32 flags,
818         const FlagDesc *flagdesc, u32 flagmask)
819 {
820         return set(name, writeFlagString(flags, flagdesc, flagmask));
821 }
822
823
824 bool Settings::setStruct(const std::string &name, const std::string &format,
825         void *value)
826 {
827         std::string structstr;
828         if (!serializeStructToString(&structstr, format, value))
829                 return false;
830
831         return set(name, structstr);
832 }
833
834
835 bool Settings::setNoiseParams(const std::string &name,
836         const NoiseParams &np, bool set_default)
837 {
838         Settings *group = new Settings;
839
840         group->setFloat("offset",      np.offset);
841         group->setFloat("scale",       np.scale);
842         group->setV3F("spread",        np.spread);
843         group->setS32("seed",          np.seed);
844         group->setU16("octaves",       np.octaves);
845         group->setFloat("persistence", np.persist);
846         group->setFloat("lacunarity",  np.lacunarity);
847         group->setFlagStr("flags",     np.flags, flagdesc_noiseparams, np.flags);
848
849         return setEntry(name, &group, true, set_default);
850 }
851
852
853 bool Settings::remove(const std::string &name)
854 {
855         MutexAutoLock lock(m_mutex);
856
857         SettingEntries::iterator it = m_settings.find(name);
858         if (it != m_settings.end()) {
859                 delete it->second.group;
860                 m_settings.erase(it);
861                 return true;
862         }
863
864         return false;
865 }
866
867
868 void Settings::clear()
869 {
870         MutexAutoLock lock(m_mutex);
871         clearNoLock();
872 }
873
874 void Settings::clearDefaults()
875 {
876         MutexAutoLock lock(m_mutex);
877         clearDefaultsNoLock();
878 }
879
880 void Settings::updateValue(const Settings &other, const std::string &name)
881 {
882         if (&other == this)
883                 return;
884
885         MutexAutoLock lock(m_mutex);
886
887         try {
888                 m_settings[name] = other.get(name);
889         } catch (SettingNotFoundException &e) {
890         }
891 }
892
893
894 void Settings::update(const Settings &other)
895 {
896         if (&other == this)
897                 return;
898
899         MutexAutoLock lock(m_mutex);
900         MutexAutoLock lock2(other.m_mutex);
901
902         updateNoLock(other);
903 }
904
905
906 SettingsParseEvent Settings::parseConfigObject(const std::string &line,
907         const std::string &end, std::string &name, std::string &value)
908 {
909         std::string trimmed_line = trim(line);
910
911         if (trimmed_line.empty())
912                 return SPE_NONE;
913         if (trimmed_line[0] == '#')
914                 return SPE_COMMENT;
915         if (trimmed_line == end)
916                 return SPE_END;
917
918         size_t pos = trimmed_line.find('=');
919         if (pos == std::string::npos)
920                 return SPE_INVALID;
921
922         name  = trim(trimmed_line.substr(0, pos));
923         value = trim(trimmed_line.substr(pos + 1));
924
925         if (value == "{")
926                 return SPE_GROUP;
927         if (value == "\"\"\"")
928                 return SPE_MULTILINE;
929
930         return SPE_KVPAIR;
931 }
932
933
934 void Settings::updateNoLock(const Settings &other)
935 {
936         m_settings.insert(other.m_settings.begin(), other.m_settings.end());
937         m_defaults.insert(other.m_defaults.begin(), other.m_defaults.end());
938 }
939
940
941 void Settings::clearNoLock()
942 {
943
944         for (SettingEntries::const_iterator it = m_settings.begin();
945                         it != m_settings.end(); ++it)
946                 delete it->second.group;
947         m_settings.clear();
948
949         clearDefaultsNoLock();
950 }
951
952 void Settings::clearDefaultsNoLock()
953 {
954         for (SettingEntries::const_iterator it = m_defaults.begin();
955                         it != m_defaults.end(); ++it)
956                 delete it->second.group;
957         m_defaults.clear();
958 }
959
960
961 void Settings::registerChangedCallback(const std::string &name,
962         SettingsChangedCallback cbf, void *userdata)
963 {
964         MutexAutoLock lock(m_callback_mutex);
965         m_callbacks[name].emplace_back(cbf, userdata);
966 }
967
968 void Settings::deregisterChangedCallback(const std::string &name,
969         SettingsChangedCallback cbf, void *userdata)
970 {
971         MutexAutoLock lock(m_callback_mutex);
972         SettingsCallbackMap::iterator it_cbks = m_callbacks.find(name);
973
974         if (it_cbks != m_callbacks.end()) {
975                 SettingsCallbackList &cbks = it_cbks->second;
976
977                 SettingsCallbackList::iterator position =
978                         std::find(cbks.begin(), cbks.end(), std::make_pair(cbf, userdata));
979
980                 if (position != cbks.end())
981                         cbks.erase(position);
982         }
983 }
984
985 void Settings::doCallbacks(const std::string &name) const
986 {
987         MutexAutoLock lock(m_callback_mutex);
988
989         SettingsCallbackMap::const_iterator it_cbks = m_callbacks.find(name);
990         if (it_cbks != m_callbacks.end()) {
991                 SettingsCallbackList::const_iterator it;
992                 for (it = it_cbks->second.begin(); it != it_cbks->second.end(); ++it)
993                         (it->first)(name, it->second);
994         }
995 }