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