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