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