]> git.lizzy.rs Git - dragonfireclient.git/blob - src/settings.cpp
guiFormspecMenu: Allow fraction values for container[] (#7497)
[dragonfireclient.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 const SettingsEntry &Settings::getEntryDefault(const std::string &name) const
365 {
366         MutexAutoLock lock(m_mutex);
367
368         SettingEntries::const_iterator n;
369         if ((n = m_defaults.find(name)) == m_defaults.end()) {
370                 throw SettingNotFoundException("Setting [" + name + "] not found.");
371         }
372         return n->second;
373 }
374
375
376 Settings *Settings::getGroup(const std::string &name) const
377 {
378         const SettingsEntry &entry = getEntry(name);
379         if (!entry.is_group)
380                 throw SettingNotFoundException("Setting [" + name + "] is not a group.");
381         return entry.group;
382 }
383
384
385 const std::string &Settings::get(const std::string &name) const
386 {
387         const SettingsEntry &entry = getEntry(name);
388         if (entry.is_group)
389                 throw SettingNotFoundException("Setting [" + name + "] is a group.");
390         return entry.value;
391 }
392
393
394 const std::string &Settings::getDefault(const std::string &name) const
395 {
396         const SettingsEntry &entry = getEntryDefault(name);
397         if (entry.is_group)
398                 throw SettingNotFoundException("Setting [" + name + "] is a group.");
399         return entry.value;
400 }
401
402
403 bool Settings::getBool(const std::string &name) const
404 {
405         return is_yes(get(name));
406 }
407
408
409 u16 Settings::getU16(const std::string &name) const
410 {
411         return stoi(get(name), 0, 65535);
412 }
413
414
415 s16 Settings::getS16(const std::string &name) const
416 {
417         return stoi(get(name), -32768, 32767);
418 }
419
420
421 u32 Settings::getU32(const std::string &name) const
422 {
423         return (u32) stoi(get(name));
424 }
425
426 s32 Settings::getS32(const std::string &name) const
427 {
428         return stoi(get(name));
429 }
430
431
432 float Settings::getFloat(const std::string &name) const
433 {
434         return stof(get(name));
435 }
436
437
438 u64 Settings::getU64(const std::string &name) const
439 {
440         u64 value = 0;
441         std::string s = get(name);
442         std::istringstream ss(s);
443         ss >> value;
444         return value;
445 }
446
447
448 v2f Settings::getV2F(const std::string &name) const
449 {
450         v2f value;
451         Strfnd f(get(name));
452         f.next("(");
453         value.X = stof(f.next(","));
454         value.Y = stof(f.next(")"));
455         return value;
456 }
457
458
459 v3f Settings::getV3F(const std::string &name) const
460 {
461         v3f value;
462         Strfnd f(get(name));
463         f.next("(");
464         value.X = stof(f.next(","));
465         value.Y = stof(f.next(","));
466         value.Z = stof(f.next(")"));
467         return value;
468 }
469
470
471 u32 Settings::getFlagStr(const std::string &name, const FlagDesc *flagdesc,
472         u32 *flagmask) const
473 {
474         std::string val = get(name);
475         return std::isdigit(val[0])
476                 ? stoi(val)
477                 : readFlagString(val, flagdesc, flagmask);
478 }
479
480
481 // N.B. if getStruct() is used to read a non-POD aggregate type,
482 // the behavior is undefined.
483 bool Settings::getStruct(const std::string &name, const std::string &format,
484         void *out, size_t olen) const
485 {
486         std::string valstr;
487
488         try {
489                 valstr = get(name);
490         } catch (SettingNotFoundException &e) {
491                 return false;
492         }
493
494         if (!deSerializeStringToStruct(valstr, format, out, olen))
495                 return false;
496
497         return true;
498 }
499
500
501 bool Settings::getNoiseParams(const std::string &name, NoiseParams &np) const
502 {
503         return getNoiseParamsFromGroup(name, np) || getNoiseParamsFromValue(name, np);
504 }
505
506
507 bool Settings::getNoiseParamsFromValue(const std::string &name,
508         NoiseParams &np) const
509 {
510         std::string value;
511
512         if (!getNoEx(name, value))
513                 return false;
514
515         Strfnd f(value);
516
517         np.offset   = stof(f.next(","));
518         np.scale    = stof(f.next(","));
519         f.next("(");
520         np.spread.X = stof(f.next(","));
521         np.spread.Y = stof(f.next(","));
522         np.spread.Z = stof(f.next(")"));
523         f.next(",");
524         np.seed     = stoi(f.next(","));
525         np.octaves  = stoi(f.next(","));
526         np.persist  = stof(f.next(","));
527
528         std::string optional_params = f.next("");
529         if (!optional_params.empty())
530                 np.lacunarity = stof(optional_params);
531
532         return true;
533 }
534
535
536 bool Settings::getNoiseParamsFromGroup(const std::string &name,
537         NoiseParams &np) const
538 {
539         Settings *group = NULL;
540
541         if (!getGroupNoEx(name, group))
542                 return false;
543
544         group->getFloatNoEx("offset",      np.offset);
545         group->getFloatNoEx("scale",       np.scale);
546         group->getV3FNoEx("spread",        np.spread);
547         group->getS32NoEx("seed",          np.seed);
548         group->getU16NoEx("octaves",       np.octaves);
549         group->getFloatNoEx("persistence", np.persist);
550         group->getFloatNoEx("lacunarity",  np.lacunarity);
551
552         np.flags = 0;
553         if (!group->getFlagStrNoEx("flags", np.flags, flagdesc_noiseparams))
554                 np.flags = NOISE_FLAG_DEFAULTS;
555
556         return true;
557 }
558
559
560 bool Settings::exists(const std::string &name) const
561 {
562         MutexAutoLock lock(m_mutex);
563
564         return (m_settings.find(name) != m_settings.end() ||
565                 m_defaults.find(name) != m_defaults.end());
566 }
567
568
569 std::vector<std::string> Settings::getNames() const
570 {
571         std::vector<std::string> names;
572         for (const auto &settings_it : m_settings) {
573                 names.push_back(settings_it.first);
574         }
575         return names;
576 }
577
578
579
580 /***************************************
581  * Getters that don't throw exceptions *
582  ***************************************/
583
584 bool Settings::getEntryNoEx(const std::string &name, SettingsEntry &val) const
585 {
586         try {
587                 val = getEntry(name);
588                 return true;
589         } catch (SettingNotFoundException &e) {
590                 return false;
591         }
592 }
593
594
595 bool Settings::getEntryDefaultNoEx(const std::string &name, SettingsEntry &val) const
596 {
597         try {
598                 val = getEntryDefault(name);
599                 return true;
600         } catch (SettingNotFoundException &e) {
601                 return false;
602         }
603 }
604
605
606 bool Settings::getGroupNoEx(const std::string &name, Settings *&val) const
607 {
608         try {
609                 val = getGroup(name);
610                 return true;
611         } catch (SettingNotFoundException &e) {
612                 return false;
613         }
614 }
615
616
617 bool Settings::getNoEx(const std::string &name, std::string &val) const
618 {
619         try {
620                 val = get(name);
621                 return true;
622         } catch (SettingNotFoundException &e) {
623                 return false;
624         }
625 }
626
627
628 bool Settings::getDefaultNoEx(const std::string &name, std::string &val) const
629 {
630         try {
631                 val = getDefault(name);
632                 return true;
633         } catch (SettingNotFoundException &e) {
634                 return false;
635         }
636 }
637
638
639 bool Settings::getFlag(const std::string &name) const
640 {
641         try {
642                 return getBool(name);
643         } catch(SettingNotFoundException &e) {
644                 return false;
645         }
646 }
647
648
649 bool Settings::getFloatNoEx(const std::string &name, float &val) const
650 {
651         try {
652                 val = getFloat(name);
653                 return true;
654         } catch (SettingNotFoundException &e) {
655                 return false;
656         }
657 }
658
659
660 bool Settings::getU16NoEx(const std::string &name, u16 &val) const
661 {
662         try {
663                 val = getU16(name);
664                 return true;
665         } catch (SettingNotFoundException &e) {
666                 return false;
667         }
668 }
669
670
671 bool Settings::getS16NoEx(const std::string &name, s16 &val) const
672 {
673         try {
674                 val = getS16(name);
675                 return true;
676         } catch (SettingNotFoundException &e) {
677                 return false;
678         }
679 }
680
681
682 bool Settings::getS32NoEx(const std::string &name, s32 &val) const
683 {
684         try {
685                 val = getS32(name);
686                 return true;
687         } catch (SettingNotFoundException &e) {
688                 return false;
689         }
690 }
691
692
693 bool Settings::getU64NoEx(const std::string &name, u64 &val) const
694 {
695         try {
696                 val = getU64(name);
697                 return true;
698         } catch (SettingNotFoundException &e) {
699                 return false;
700         }
701 }
702
703
704 bool Settings::getV2FNoEx(const std::string &name, v2f &val) const
705 {
706         try {
707                 val = getV2F(name);
708                 return true;
709         } catch (SettingNotFoundException &e) {
710                 return false;
711         }
712 }
713
714
715 bool Settings::getV3FNoEx(const std::string &name, v3f &val) const
716 {
717         try {
718                 val = getV3F(name);
719                 return true;
720         } catch (SettingNotFoundException &e) {
721                 return false;
722         }
723 }
724
725
726 // N.B. getFlagStrNoEx() does not set val, but merely modifies it.  Thus,
727 // val must be initialized before using getFlagStrNoEx().  The intention of
728 // this is to simplify modifying a flags field from a default value.
729 bool Settings::getFlagStrNoEx(const std::string &name, u32 &val,
730         FlagDesc *flagdesc) const
731 {
732         try {
733                 u32 flags, flagmask;
734
735                 flags = getFlagStr(name, flagdesc, &flagmask);
736
737                 val &= ~flagmask;
738                 val |=  flags;
739
740                 return true;
741         } catch (SettingNotFoundException &e) {
742                 return false;
743         }
744 }
745
746
747 /***********
748  * Setters *
749  ***********/
750
751 bool Settings::setEntry(const std::string &name, const void *data,
752         bool set_group, bool set_default)
753 {
754         Settings *old_group = NULL;
755
756         if (!checkNameValid(name))
757                 return false;
758         if (!set_group && !checkValueValid(*(const std::string *)data))
759                 return false;
760
761         {
762                 MutexAutoLock lock(m_mutex);
763
764                 SettingsEntry &entry = set_default ? m_defaults[name] : m_settings[name];
765                 old_group = entry.group;
766
767                 entry.value    = set_group ? "" : *(const std::string *)data;
768                 entry.group    = set_group ? *(Settings **)data : NULL;
769                 entry.is_group = set_group;
770         }
771
772         delete old_group;
773
774         return true;
775 }
776
777
778 bool Settings::set(const std::string &name, const std::string &value)
779 {
780         if (!setEntry(name, &value, false, false))
781                 return false;
782
783         doCallbacks(name);
784         return true;
785 }
786
787
788 bool Settings::setDefault(const std::string &name, const std::string &value)
789 {
790         return setEntry(name, &value, false, true);
791 }
792
793
794 bool Settings::setGroup(const std::string &name, Settings *group)
795 {
796         return setEntry(name, &group, true, false);
797 }
798
799
800 bool Settings::setGroupDefault(const std::string &name, Settings *group)
801 {
802         return setEntry(name, &group, true, true);
803 }
804
805
806 bool Settings::setBool(const std::string &name, bool value)
807 {
808         return set(name, value ? "true" : "false");
809 }
810
811
812 bool Settings::setS16(const std::string &name, s16 value)
813 {
814         return set(name, itos(value));
815 }
816
817
818 bool Settings::setU16(const std::string &name, u16 value)
819 {
820         return set(name, itos(value));
821 }
822
823
824 bool Settings::setS32(const std::string &name, s32 value)
825 {
826         return set(name, itos(value));
827 }
828
829
830 bool Settings::setU64(const std::string &name, u64 value)
831 {
832         std::ostringstream os;
833         os << value;
834         return set(name, os.str());
835 }
836
837
838 bool Settings::setFloat(const std::string &name, float value)
839 {
840         return set(name, ftos(value));
841 }
842
843
844 bool Settings::setV2F(const std::string &name, v2f value)
845 {
846         std::ostringstream os;
847         os << "(" << value.X << "," << value.Y << ")";
848         return set(name, os.str());
849 }
850
851
852 bool Settings::setV3F(const std::string &name, v3f value)
853 {
854         std::ostringstream os;
855         os << "(" << value.X << "," << value.Y << "," << value.Z << ")";
856         return set(name, os.str());
857 }
858
859
860 bool Settings::setFlagStr(const std::string &name, u32 flags,
861         const FlagDesc *flagdesc, u32 flagmask)
862 {
863         return set(name, writeFlagString(flags, flagdesc, flagmask));
864 }
865
866
867 bool Settings::setStruct(const std::string &name, const std::string &format,
868         void *value)
869 {
870         std::string structstr;
871         if (!serializeStructToString(&structstr, format, value))
872                 return false;
873
874         return set(name, structstr);
875 }
876
877
878 bool Settings::setNoiseParams(const std::string &name,
879         const NoiseParams &np, bool set_default)
880 {
881         Settings *group = new Settings;
882
883         group->setFloat("offset",      np.offset);
884         group->setFloat("scale",       np.scale);
885         group->setV3F("spread",        np.spread);
886         group->setS32("seed",          np.seed);
887         group->setU16("octaves",       np.octaves);
888         group->setFloat("persistence", np.persist);
889         group->setFloat("lacunarity",  np.lacunarity);
890         group->setFlagStr("flags",     np.flags, flagdesc_noiseparams, np.flags);
891
892         return setEntry(name, &group, true, set_default);
893 }
894
895
896 bool Settings::remove(const std::string &name)
897 {
898         MutexAutoLock lock(m_mutex);
899
900         SettingEntries::iterator it = m_settings.find(name);
901         if (it != m_settings.end()) {
902                 delete it->second.group;
903                 m_settings.erase(it);
904                 return true;
905         }
906
907         return false;
908 }
909
910
911 void Settings::clear()
912 {
913         MutexAutoLock lock(m_mutex);
914         clearNoLock();
915 }
916
917 void Settings::clearDefaults()
918 {
919         MutexAutoLock lock(m_mutex);
920         clearDefaultsNoLock();
921 }
922
923 void Settings::updateValue(const Settings &other, const std::string &name)
924 {
925         if (&other == this)
926                 return;
927
928         MutexAutoLock lock(m_mutex);
929
930         try {
931                 m_settings[name] = other.get(name);
932         } catch (SettingNotFoundException &e) {
933         }
934 }
935
936
937 void Settings::update(const Settings &other)
938 {
939         if (&other == this)
940                 return;
941
942         MutexAutoLock lock(m_mutex);
943         MutexAutoLock lock2(other.m_mutex);
944
945         updateNoLock(other);
946 }
947
948
949 SettingsParseEvent Settings::parseConfigObject(const std::string &line,
950         const std::string &end, std::string &name, std::string &value)
951 {
952         std::string trimmed_line = trim(line);
953
954         if (trimmed_line.empty())
955                 return SPE_NONE;
956         if (trimmed_line[0] == '#')
957                 return SPE_COMMENT;
958         if (trimmed_line == end)
959                 return SPE_END;
960
961         size_t pos = trimmed_line.find('=');
962         if (pos == std::string::npos)
963                 return SPE_INVALID;
964
965         name  = trim(trimmed_line.substr(0, pos));
966         value = trim(trimmed_line.substr(pos + 1));
967
968         if (value == "{")
969                 return SPE_GROUP;
970         if (value == "\"\"\"")
971                 return SPE_MULTILINE;
972
973         return SPE_KVPAIR;
974 }
975
976
977 void Settings::updateNoLock(const Settings &other)
978 {
979         m_settings.insert(other.m_settings.begin(), other.m_settings.end());
980         m_defaults.insert(other.m_defaults.begin(), other.m_defaults.end());
981 }
982
983
984 void Settings::clearNoLock()
985 {
986
987         for (SettingEntries::const_iterator it = m_settings.begin();
988                         it != m_settings.end(); ++it)
989                 delete it->second.group;
990         m_settings.clear();
991
992         clearDefaultsNoLock();
993 }
994
995 void Settings::clearDefaultsNoLock()
996 {
997         for (SettingEntries::const_iterator it = m_defaults.begin();
998                         it != m_defaults.end(); ++it)
999                 delete it->second.group;
1000         m_defaults.clear();
1001 }
1002
1003
1004 void Settings::registerChangedCallback(const std::string &name,
1005         SettingsChangedCallback cbf, void *userdata)
1006 {
1007         MutexAutoLock lock(m_callback_mutex);
1008         m_callbacks[name].emplace_back(cbf, userdata);
1009 }
1010
1011 void Settings::deregisterChangedCallback(const std::string &name,
1012         SettingsChangedCallback cbf, void *userdata)
1013 {
1014         MutexAutoLock lock(m_callback_mutex);
1015         SettingsCallbackMap::iterator it_cbks = m_callbacks.find(name);
1016
1017         if (it_cbks != m_callbacks.end()) {
1018                 SettingsCallbackList &cbks = it_cbks->second;
1019
1020                 SettingsCallbackList::iterator position =
1021                         std::find(cbks.begin(), cbks.end(), std::make_pair(cbf, userdata));
1022
1023                 if (position != cbks.end())
1024                         cbks.erase(position);
1025         }
1026 }
1027
1028 void Settings::doCallbacks(const std::string &name) const
1029 {
1030         MutexAutoLock lock(m_callback_mutex);
1031
1032         SettingsCallbackMap::const_iterator it_cbks = m_callbacks.find(name);
1033         if (it_cbks != m_callbacks.end()) {
1034                 SettingsCallbackList::const_iterator it;
1035                 for (it = it_cbks->second.begin(); it != it_cbks->second.end(); ++it)
1036                         (it->first)(name, it->second);
1037         }
1038 }