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