]> git.lizzy.rs Git - dragonfireclient.git/blob - src/settings.cpp
serverpackethandler: Minor log message fixes
[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         if (&other == this)
49                 return *this;
50
51         MutexAutoLock lock(m_mutex);
52         MutexAutoLock lock2(other.m_mutex);
53
54         updateNoLock(other);
55
56         return *this;
57 }
58
59
60 Settings & Settings::operator = (const Settings &other)
61 {
62         if (&other == this)
63                 return *this;
64
65         MutexAutoLock lock(m_mutex);
66         MutexAutoLock lock2(other.m_mutex);
67
68         clearNoLock();
69         updateNoLock(other);
70
71         return *this;
72 }
73
74
75 bool Settings::checkNameValid(const std::string &name)
76 {
77         bool valid = name.find_first_of("=\"{}#") == std::string::npos;
78         if (valid)
79                 valid = std::find_if(name.begin(), name.end(), ::isspace) == name.end();
80
81         if (!valid) {
82                 errorstream << "Invalid setting name \"" << name << "\""
83                         << std::endl;
84                 return false;
85         }
86         return true;
87 }
88
89
90 bool Settings::checkValueValid(const std::string &value)
91 {
92         if (value.substr(0, 3) == "\"\"\"" ||
93                 value.find("\n\"\"\"") != std::string::npos) {
94                 errorstream << "Invalid character sequence '\"\"\"' found in"
95                         " setting value!" << std::endl;
96                 return false;
97         }
98         return true;
99 }
100
101 std::string Settings::getMultiline(std::istream &is, size_t *num_lines)
102 {
103         size_t lines = 1;
104         std::string value;
105         std::string line;
106
107         while (is.good()) {
108                 lines++;
109                 std::getline(is, line);
110                 if (line == "\"\"\"")
111                         break;
112                 value += line;
113                 value.push_back('\n');
114         }
115
116         size_t len = value.size();
117         if (len)
118                 value.erase(len - 1);
119
120         if (num_lines)
121                 *num_lines = lines;
122
123         return value;
124 }
125
126
127 bool Settings::readConfigFile(const char *filename)
128 {
129         std::ifstream is(filename);
130         if (!is.good())
131                 return false;
132
133         return parseConfigLines(is, "");
134 }
135
136
137 bool Settings::parseConfigLines(std::istream &is, const std::string &end)
138 {
139         MutexAutoLock lock(m_mutex);
140
141         std::string line, name, value;
142
143         while (is.good()) {
144                 std::getline(is, line);
145                 SettingsParseEvent event = parseConfigObject(line, end, name, value);
146
147                 switch (event) {
148                 case SPE_NONE:
149                 case SPE_INVALID:
150                 case SPE_COMMENT:
151                         break;
152                 case SPE_KVPAIR:
153                         m_settings[name] = SettingsEntry(value);
154                         break;
155                 case SPE_END:
156                         return true;
157                 case SPE_GROUP: {
158                         Settings *group = new Settings;
159                         if (!group->parseConfigLines(is, "}")) {
160                                 delete group;
161                                 return false;
162                         }
163                         m_settings[name] = SettingsEntry(group);
164                         break;
165                 }
166                 case SPE_MULTILINE:
167                         m_settings[name] = SettingsEntry(getMultiline(is));
168                         break;
169                 }
170         }
171
172         return end.empty();
173 }
174
175
176 void Settings::writeLines(std::ostream &os, u32 tab_depth) const
177 {
178         MutexAutoLock lock(m_mutex);
179
180         for (const auto &setting_it : m_settings)
181                 printEntry(os, setting_it.first, setting_it.second, tab_depth);
182 }
183
184
185 void Settings::printEntry(std::ostream &os, const std::string &name,
186         const SettingsEntry &entry, u32 tab_depth)
187 {
188         for (u32 i = 0; i != tab_depth; i++)
189                 os << "\t";
190
191         if (entry.is_group) {
192                 os << name << " = {\n";
193
194                 entry.group->writeLines(os, tab_depth + 1);
195
196                 for (u32 i = 0; i != tab_depth; i++)
197                         os << "\t";
198                 os << "}\n";
199         } else {
200                 os << name << " = ";
201
202                 if (entry.value.find('\n') != std::string::npos)
203                         os << "\"\"\"\n" << entry.value << "\n\"\"\"\n";
204                 else
205                         os << entry.value << "\n";
206         }
207 }
208
209
210 bool Settings::updateConfigObject(std::istream &is, std::ostream &os,
211         const std::string &end, u32 tab_depth)
212 {
213         SettingEntries::const_iterator it;
214         std::set<std::string> present_entries;
215         std::string line, name, value;
216         bool was_modified = false;
217         bool end_found = false;
218
219         // Add any settings that exist in the config file with the current value
220         // in the object if existing
221         while (is.good() && !end_found) {
222                 std::getline(is, line);
223                 SettingsParseEvent event = parseConfigObject(line, end, name, value);
224
225                 switch (event) {
226                 case SPE_END:
227                         os << line << (is.eof() ? "" : "\n");
228                         end_found = true;
229                         break;
230                 case SPE_MULTILINE:
231                         value = getMultiline(is);
232                         /* FALLTHROUGH */
233                 case SPE_KVPAIR:
234                         it = m_settings.find(name);
235                         if (it != m_settings.end() &&
236                                         (it->second.is_group || it->second.value != value)) {
237                                 printEntry(os, name, it->second, tab_depth);
238                                 was_modified = true;
239                         } else if (it == m_settings.end()) {
240                                 // Remove by skipping
241                                 was_modified = true;
242                                 break;
243                         } else {
244                                 os << line << "\n";
245                                 if (event == SPE_MULTILINE)
246                                         os << value << "\n\"\"\"\n";
247                         }
248                         present_entries.insert(name);
249                         break;
250                 case SPE_GROUP:
251                         it = m_settings.find(name);
252                         if (it != m_settings.end() && it->second.is_group) {
253                                 os << line << "\n";
254                                 sanity_check(it->second.group != NULL);
255                                 was_modified |= it->second.group->updateConfigObject(is, os,
256                                         "}", tab_depth + 1);
257                         } else if (it == m_settings.end()) {
258                                 // Remove by skipping
259                                 was_modified = true;
260                                 Settings removed_group; // Move 'is' to group end
261                                 std::stringstream ss;
262                                 removed_group.updateConfigObject(is, ss, "}", tab_depth + 1);
263                                 break;
264                         } else {
265                                 printEntry(os, name, it->second, tab_depth);
266                                 was_modified = true;
267                         }
268                         present_entries.insert(name);
269                         break;
270                 default:
271                         os << line << (is.eof() ? "" : "\n");
272                         break;
273                 }
274         }
275
276         // Add any settings in the object that don't exist in the config file yet
277         for (it = m_settings.begin(); it != m_settings.end(); ++it) {
278                 if (present_entries.find(it->first) != present_entries.end())
279                         continue;
280
281                 printEntry(os, it->first, it->second, tab_depth);
282                 was_modified = true;
283         }
284
285         return was_modified;
286 }
287
288
289 bool Settings::updateConfigFile(const char *filename)
290 {
291         MutexAutoLock lock(m_mutex);
292
293         std::ifstream is(filename);
294         std::ostringstream os(std::ios_base::binary);
295
296         bool was_modified = updateConfigObject(is, os, "");
297         is.close();
298
299         if (!was_modified)
300                 return true;
301
302         if (!fs::safeWriteToFile(filename, os.str())) {
303                 errorstream << "Error writing configuration file: \""
304                         << filename << "\"" << std::endl;
305                 return false;
306         }
307
308         return true;
309 }
310
311
312 bool Settings::parseCommandLine(int argc, char *argv[],
313                 std::map<std::string, ValueSpec> &allowed_options)
314 {
315         int nonopt_index = 0;
316         for (int i = 1; i < argc; i++) {
317                 std::string arg_name = argv[i];
318                 if (arg_name.substr(0, 2) != "--") {
319                         // If option doesn't start with -, read it in as nonoptX
320                         if (arg_name[0] != '-'){
321                                 std::string name = "nonopt";
322                                 name += itos(nonopt_index);
323                                 set(name, arg_name);
324                                 nonopt_index++;
325                                 continue;
326                         }
327                         errorstream << "Invalid command-line parameter \""
328                                         << arg_name << "\": --<option> expected." << std::endl;
329                         return false;
330                 }
331
332                 std::string name = arg_name.substr(2);
333
334                 std::map<std::string, ValueSpec>::iterator n;
335                 n = allowed_options.find(name);
336                 if (n == allowed_options.end()) {
337                         errorstream << "Unknown command-line parameter \""
338                                         << arg_name << "\"" << std::endl;
339                         return false;
340                 }
341
342                 ValueType type = n->second.type;
343
344                 std::string value;
345
346                 if (type == VALUETYPE_FLAG) {
347                         value = "true";
348                 } else {
349                         if ((i + 1) >= argc) {
350                                 errorstream << "Invalid command-line parameter \""
351                                                 << name << "\": missing value" << std::endl;
352                                 return false;
353                         }
354                         value = argv[++i];
355                 }
356
357                 set(name, value);
358         }
359
360         return true;
361 }
362
363
364
365 /***********
366  * Getters *
367  ***********/
368
369
370 const SettingsEntry &Settings::getEntry(const std::string &name) const
371 {
372         MutexAutoLock lock(m_mutex);
373
374         SettingEntries::const_iterator n;
375         if ((n = m_settings.find(name)) == m_settings.end()) {
376                 if ((n = m_defaults.find(name)) == m_defaults.end())
377                         throw SettingNotFoundException("Setting [" + name + "] not found.");
378         }
379         return n->second;
380 }
381
382
383 const SettingsEntry &Settings::getEntryDefault(const std::string &name) const
384 {
385         MutexAutoLock lock(m_mutex);
386
387         SettingEntries::const_iterator n;
388         if ((n = m_defaults.find(name)) == m_defaults.end()) {
389                 throw SettingNotFoundException("Setting [" + name + "] not found.");
390         }
391         return n->second;
392 }
393
394
395 Settings *Settings::getGroup(const std::string &name) const
396 {
397         const SettingsEntry &entry = getEntry(name);
398         if (!entry.is_group)
399                 throw SettingNotFoundException("Setting [" + name + "] is not a group.");
400         return entry.group;
401 }
402
403
404 const std::string &Settings::get(const std::string &name) const
405 {
406         const SettingsEntry &entry = getEntry(name);
407         if (entry.is_group)
408                 throw SettingNotFoundException("Setting [" + name + "] is a group.");
409         return entry.value;
410 }
411
412
413 const std::string &Settings::getDefault(const std::string &name) const
414 {
415         const SettingsEntry &entry = getEntryDefault(name);
416         if (entry.is_group)
417                 throw SettingNotFoundException("Setting [" + name + "] is a group.");
418         return entry.value;
419 }
420
421
422 bool Settings::getBool(const std::string &name) const
423 {
424         return is_yes(get(name));
425 }
426
427
428 u16 Settings::getU16(const std::string &name) const
429 {
430         return stoi(get(name), 0, 65535);
431 }
432
433
434 s16 Settings::getS16(const std::string &name) const
435 {
436         return stoi(get(name), -32768, 32767);
437 }
438
439
440 u32 Settings::getU32(const std::string &name) const
441 {
442         return (u32) stoi(get(name));
443 }
444
445 s32 Settings::getS32(const std::string &name) const
446 {
447         return stoi(get(name));
448 }
449
450
451 float Settings::getFloat(const std::string &name) const
452 {
453         return stof(get(name));
454 }
455
456
457 u64 Settings::getU64(const std::string &name) const
458 {
459         u64 value = 0;
460         std::string s = get(name);
461         std::istringstream ss(s);
462         ss >> value;
463         return value;
464 }
465
466
467 v2f Settings::getV2F(const std::string &name) const
468 {
469         v2f value;
470         Strfnd f(get(name));
471         f.next("(");
472         value.X = stof(f.next(","));
473         value.Y = stof(f.next(")"));
474         return value;
475 }
476
477
478 v3f Settings::getV3F(const std::string &name) const
479 {
480         v3f value;
481         Strfnd f(get(name));
482         f.next("(");
483         value.X = stof(f.next(","));
484         value.Y = stof(f.next(","));
485         value.Z = stof(f.next(")"));
486         return value;
487 }
488
489
490 u32 Settings::getFlagStr(const std::string &name, const FlagDesc *flagdesc,
491         u32 *flagmask) const
492 {
493         u32 flags = 0;
494         u32 mask_default = 0;
495
496         std::string value;
497         // Read default value (if there is any)
498         if (getDefaultNoEx(name, value)) {
499                 flags = std::isdigit(value[0])
500                         ? stoi(value)
501                         : readFlagString(value, flagdesc, &mask_default);
502         }
503
504         // Apply custom flags "on top"
505         value = get(name);
506         u32 flags_user;
507         u32 mask_user = U32_MAX;
508         flags_user = std::isdigit(value[0])
509                 ? stoi(value) // Override default
510                 : readFlagString(value, flagdesc, &mask_user);
511
512         flags &= ~mask_user;
513         flags |=  flags_user;
514
515         if (flagmask)
516                 *flagmask = mask_default | mask_user;
517
518         return flags;
519 }
520
521
522 bool Settings::getNoiseParams(const std::string &name, NoiseParams &np) const
523 {
524         return getNoiseParamsFromGroup(name, np) || getNoiseParamsFromValue(name, np);
525 }
526
527
528 bool Settings::getNoiseParamsFromValue(const std::string &name,
529         NoiseParams &np) const
530 {
531         std::string value;
532
533         if (!getNoEx(name, value))
534                 return false;
535
536         // Format: f32,f32,(f32,f32,f32),s32,s32,f32[,f32]
537         Strfnd f(value);
538
539         np.offset   = stof(f.next(","));
540         np.scale    = stof(f.next(","));
541         f.next("(");
542         np.spread.X = stof(f.next(","));
543         np.spread.Y = stof(f.next(","));
544         np.spread.Z = stof(f.next(")"));
545         f.next(",");
546         np.seed     = stoi(f.next(","));
547         np.octaves  = stoi(f.next(","));
548         np.persist  = stof(f.next(","));
549
550         std::string optional_params = f.next("");
551         if (!optional_params.empty())
552                 np.lacunarity = stof(optional_params);
553
554         return true;
555 }
556
557
558 bool Settings::getNoiseParamsFromGroup(const std::string &name,
559         NoiseParams &np) const
560 {
561         Settings *group = NULL;
562
563         if (!getGroupNoEx(name, group))
564                 return false;
565
566         group->getFloatNoEx("offset",      np.offset);
567         group->getFloatNoEx("scale",       np.scale);
568         group->getV3FNoEx("spread",        np.spread);
569         group->getS32NoEx("seed",          np.seed);
570         group->getU16NoEx("octaves",       np.octaves);
571         group->getFloatNoEx("persistence", np.persist);
572         group->getFloatNoEx("lacunarity",  np.lacunarity);
573
574         np.flags = 0;
575         if (!group->getFlagStrNoEx("flags", np.flags, flagdesc_noiseparams))
576                 np.flags = NOISE_FLAG_DEFAULTS;
577
578         return true;
579 }
580
581
582 bool Settings::exists(const std::string &name) const
583 {
584         MutexAutoLock lock(m_mutex);
585
586         return (m_settings.find(name) != m_settings.end() ||
587                 m_defaults.find(name) != m_defaults.end());
588 }
589
590
591 std::vector<std::string> Settings::getNames() const
592 {
593         std::vector<std::string> names;
594         for (const auto &settings_it : m_settings) {
595                 names.push_back(settings_it.first);
596         }
597         return names;
598 }
599
600
601
602 /***************************************
603  * Getters that don't throw exceptions *
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 bool Settings::getFlagStrNoEx(const std::string &name, u32 &val,
727         const FlagDesc *flagdesc) const
728 {
729         if (!flagdesc) {
730                 if (!(flagdesc = getFlagDescFallback(name)))
731                         return false; // Not found
732         }
733
734         try {
735                 val = getFlagStr(name, flagdesc, nullptr);
736
737                 return true;
738         } catch (SettingNotFoundException &e) {
739                 return false;
740         }
741 }
742
743
744 /***********
745  * Setters *
746  ***********/
747
748 bool Settings::setEntry(const std::string &name, const void *data,
749         bool set_group, bool set_default)
750 {
751         Settings *old_group = NULL;
752
753         if (!checkNameValid(name))
754                 return false;
755         if (!set_group && !checkValueValid(*(const std::string *)data))
756                 return false;
757
758         {
759                 MutexAutoLock lock(m_mutex);
760
761                 SettingsEntry &entry = set_default ? m_defaults[name] : m_settings[name];
762                 old_group = entry.group;
763
764                 entry.value    = set_group ? "" : *(const std::string *)data;
765                 entry.group    = set_group ? *(Settings **)data : NULL;
766                 entry.is_group = set_group;
767         }
768
769         delete old_group;
770
771         return true;
772 }
773
774
775 bool Settings::set(const std::string &name, const std::string &value)
776 {
777         if (!setEntry(name, &value, false, false))
778                 return false;
779
780         doCallbacks(name);
781         return true;
782 }
783
784
785 bool Settings::setDefault(const std::string &name, const std::string &value)
786 {
787         return setEntry(name, &value, false, true);
788 }
789
790
791 bool Settings::setGroup(const std::string &name, const Settings &group)
792 {
793         // Settings must own the group pointer
794         // avoid double-free by copying the source
795         Settings *copy = new Settings();
796         *copy = group;
797         return setEntry(name, &copy, true, false);
798 }
799
800
801 bool Settings::setGroupDefault(const std::string &name, const Settings &group)
802 {
803         // Settings must own the group pointer
804         // avoid double-free by copying the source
805         Settings *copy = new Settings();
806         *copy = group;
807         return setEntry(name, &copy, true, true);
808 }
809
810
811 bool Settings::setBool(const std::string &name, bool value)
812 {
813         return set(name, value ? "true" : "false");
814 }
815
816
817 bool Settings::setS16(const std::string &name, s16 value)
818 {
819         return set(name, itos(value));
820 }
821
822
823 bool Settings::setU16(const std::string &name, u16 value)
824 {
825         return set(name, itos(value));
826 }
827
828
829 bool Settings::setS32(const std::string &name, s32 value)
830 {
831         return set(name, itos(value));
832 }
833
834
835 bool Settings::setU64(const std::string &name, u64 value)
836 {
837         std::ostringstream os;
838         os << value;
839         return set(name, os.str());
840 }
841
842
843 bool Settings::setFloat(const std::string &name, float value)
844 {
845         return set(name, ftos(value));
846 }
847
848
849 bool Settings::setV2F(const std::string &name, v2f value)
850 {
851         std::ostringstream os;
852         os << "(" << value.X << "," << value.Y << ")";
853         return set(name, os.str());
854 }
855
856
857 bool Settings::setV3F(const std::string &name, v3f value)
858 {
859         std::ostringstream os;
860         os << "(" << value.X << "," << value.Y << "," << value.Z << ")";
861         return set(name, os.str());
862 }
863
864
865 bool Settings::setFlagStr(const std::string &name, u32 flags,
866         const FlagDesc *flagdesc, u32 flagmask)
867 {
868         if (!flagdesc) {
869                 if (!(flagdesc = getFlagDescFallback(name)))
870                         return false; // Not found
871         }
872
873         return set(name, writeFlagString(flags, flagdesc, flagmask));
874 }
875
876
877 bool Settings::setNoiseParams(const std::string &name,
878         const NoiseParams &np, bool set_default)
879 {
880         Settings *group = new Settings;
881
882         group->setFloat("offset",      np.offset);
883         group->setFloat("scale",       np.scale);
884         group->setV3F("spread",        np.spread);
885         group->setS32("seed",          np.seed);
886         group->setU16("octaves",       np.octaves);
887         group->setFloat("persistence", np.persist);
888         group->setFloat("lacunarity",  np.lacunarity);
889         group->setFlagStr("flags",     np.flags, flagdesc_noiseparams, np.flags);
890
891         return setEntry(name, &group, true, set_default);
892 }
893
894
895 bool Settings::remove(const std::string &name)
896 {
897         // Lock as short as possible, unlock before doCallbacks()
898         m_mutex.lock();
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                 m_mutex.unlock();
905
906                 doCallbacks(name);
907                 return true;
908         }
909
910         m_mutex.unlock();
911         return false;
912 }
913
914
915 void Settings::clear()
916 {
917         MutexAutoLock lock(m_mutex);
918         clearNoLock();
919 }
920
921 void Settings::clearDefaults()
922 {
923         MutexAutoLock lock(m_mutex);
924         clearDefaultsNoLock();
925 }
926
927 SettingsParseEvent Settings::parseConfigObject(const std::string &line,
928         const std::string &end, std::string &name, std::string &value)
929 {
930         std::string trimmed_line = trim(line);
931
932         if (trimmed_line.empty())
933                 return SPE_NONE;
934         if (trimmed_line[0] == '#')
935                 return SPE_COMMENT;
936         if (trimmed_line == end)
937                 return SPE_END;
938
939         size_t pos = trimmed_line.find('=');
940         if (pos == std::string::npos)
941                 return SPE_INVALID;
942
943         name  = trim(trimmed_line.substr(0, pos));
944         value = trim(trimmed_line.substr(pos + 1));
945
946         if (value == "{")
947                 return SPE_GROUP;
948         if (value == "\"\"\"")
949                 return SPE_MULTILINE;
950
951         return SPE_KVPAIR;
952 }
953
954
955 void Settings::updateNoLock(const Settings &other)
956 {
957         m_settings.insert(other.m_settings.begin(), other.m_settings.end());
958         m_defaults.insert(other.m_defaults.begin(), other.m_defaults.end());
959 }
960
961
962 void Settings::clearNoLock()
963 {
964
965         for (SettingEntries::const_iterator it = m_settings.begin();
966                         it != m_settings.end(); ++it)
967                 delete it->second.group;
968         m_settings.clear();
969
970         clearDefaultsNoLock();
971 }
972
973 void Settings::clearDefaultsNoLock()
974 {
975         for (SettingEntries::const_iterator it = m_defaults.begin();
976                         it != m_defaults.end(); ++it)
977                 delete it->second.group;
978         m_defaults.clear();
979 }
980
981 void Settings::setDefault(const std::string &name, const FlagDesc *flagdesc,
982         u32 flags)
983 {
984         m_flags[name] = flagdesc;
985         setDefault(name, writeFlagString(flags, flagdesc, U32_MAX));
986 }
987
988 void Settings::overrideDefaults(Settings *other)
989 {
990         for (const auto &setting : other->m_settings) {
991                 if (setting.second.is_group) {
992                         setGroupDefault(setting.first, *setting.second.group);
993                         continue;
994                 }
995                 const FlagDesc *flagdesc = getFlagDescFallback(setting.first);
996                 if (flagdesc) {
997                         // Flags cannot be copied directly.
998                         // 1) Get the current set flags
999                         u32 flags = getFlagStr(setting.first, flagdesc, nullptr);
1000                         // 2) Set the flags as defaults
1001                         other->setDefault(setting.first, flagdesc, flags);
1002                         // 3) Get the newly set flags and override the default setting value
1003                         setDefault(setting.first, flagdesc,
1004                                 other->getFlagStr(setting.first, flagdesc, nullptr));
1005                         continue;
1006                 }
1007                 // Also covers FlagDesc settings
1008                 setDefault(setting.first, setting.second.value);
1009         }
1010 }
1011
1012 const FlagDesc *Settings::getFlagDescFallback(const std::string &name) const
1013 {
1014         auto it = m_flags.find(name);
1015         return it == m_flags.end() ? nullptr : it->second;
1016 }
1017
1018 void Settings::registerChangedCallback(const std::string &name,
1019         SettingsChangedCallback cbf, void *userdata)
1020 {
1021         MutexAutoLock lock(m_callback_mutex);
1022         m_callbacks[name].emplace_back(cbf, userdata);
1023 }
1024
1025 void Settings::deregisterChangedCallback(const std::string &name,
1026         SettingsChangedCallback cbf, void *userdata)
1027 {
1028         MutexAutoLock lock(m_callback_mutex);
1029         SettingsCallbackMap::iterator it_cbks = m_callbacks.find(name);
1030
1031         if (it_cbks != m_callbacks.end()) {
1032                 SettingsCallbackList &cbks = it_cbks->second;
1033
1034                 SettingsCallbackList::iterator position =
1035                         std::find(cbks.begin(), cbks.end(), std::make_pair(cbf, userdata));
1036
1037                 if (position != cbks.end())
1038                         cbks.erase(position);
1039         }
1040 }
1041
1042 void Settings::removeSecureSettings()
1043 {
1044         for (const auto &name : getNames()) {
1045                 if (name.compare(0, 7, "secure.") != 0)
1046                         continue;
1047
1048                 errorstream << "Secure setting " << name
1049                                 << " isn't allowed, so was ignored."
1050                                 << std::endl;
1051                 remove(name);
1052         }
1053 }
1054
1055 void Settings::doCallbacks(const std::string &name) const
1056 {
1057         MutexAutoLock lock(m_callback_mutex);
1058
1059         SettingsCallbackMap::const_iterator it_cbks = m_callbacks.find(name);
1060         if (it_cbks != m_callbacks.end()) {
1061                 SettingsCallbackList::const_iterator it;
1062                 for (it = it_cbks->second.begin(); it != it_cbks->second.end(); ++it)
1063                         (it->first)(name, it->second);
1064         }
1065 }