]> git.lizzy.rs Git - dragonfireclient.git/blob - src/settings.cpp
a8b15b6c2b47f87e2349efbb50993a1ddd403dbb
[dragonfireclient.git] / src / settings.cpp
1 /*
2 Minetest
3 Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #include "settings.h"
21 #include "irrlichttypes_bloated.h"
22 #include "exceptions.h"
23 #include "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                                 assert(it->second.group == NULL);
258                                 os << line << "\n";
259                                 if (event == SPE_MULTILINE)
260                                         os << value << "\n\"\"\"\n";
261                         }
262                         present_entries.insert(name);
263                         break;
264                 case SPE_GROUP:
265                         it = m_settings.find(name);
266                         if (it != m_settings.end() && it->second.is_group) {
267                                 os << line << "\n";
268                                 assert(it->second.group != NULL);
269                                 was_modified |= it->second.group->updateConfigObject(is, os,
270                                         "}", tab_depth + 1);
271                         } else {
272                                 printEntry(os, name, it->second, tab_depth);
273                                 was_modified = true;
274                         }
275                         present_entries.insert(name);
276                         break;
277                 default:
278                         os << line << (is.eof() ? "" : "\n");
279                         break;
280                 }
281         }
282
283         // Add any settings in the object that don't exist in the config file yet
284         for (it = m_settings.begin(); it != m_settings.end(); ++it) {
285                 if (present_entries.find(it->first) != present_entries.end())
286                         continue;
287
288                 printEntry(os, it->first, it->second, tab_depth);
289                 was_modified = true;
290         }
291
292         return was_modified;
293 }
294
295
296 bool Settings::updateConfigFile(const char *filename)
297 {
298         JMutexAutoLock lock(m_mutex);
299
300         std::ifstream is(filename);
301         std::ostringstream os(std::ios_base::binary);
302
303         bool was_modified = updateConfigObject(is, os, "");
304         is.close();
305
306         if (!was_modified)
307                 return true;
308
309         if (!fs::safeWriteToFile(filename, os.str())) {
310                 errorstream << "Error writing configuration file: \""
311                         << filename << "\"" << std::endl;
312                 return false;
313         }
314
315         return true;
316 }
317
318
319 bool Settings::parseCommandLine(int argc, char *argv[],
320                 std::map<std::string, ValueSpec> &allowed_options)
321 {
322         int nonopt_index = 0;
323         for (int i = 1; i < argc; i++) {
324                 std::string arg_name = argv[i];
325                 if (arg_name.substr(0, 2) != "--") {
326                         // If option doesn't start with -, read it in as nonoptX
327                         if (arg_name[0] != '-'){
328                                 std::string name = "nonopt";
329                                 name += itos(nonopt_index);
330                                 set(name, arg_name);
331                                 nonopt_index++;
332                                 continue;
333                         }
334                         errorstream << "Invalid command-line parameter \""
335                                         << arg_name << "\": --<option> expected." << std::endl;
336                         return false;
337                 }
338
339                 std::string name = arg_name.substr(2);
340
341                 std::map<std::string, ValueSpec>::iterator n;
342                 n = allowed_options.find(name);
343                 if (n == allowed_options.end()) {
344                         errorstream << "Unknown command-line parameter \""
345                                         << arg_name << "\"" << std::endl;
346                         return false;
347                 }
348
349                 ValueType type = n->second.type;
350
351                 std::string value = "";
352
353                 if (type == VALUETYPE_FLAG) {
354                         value = "true";
355                 } else {
356                         if ((i + 1) >= argc) {
357                                 errorstream << "Invalid command-line parameter \""
358                                                 << name << "\": missing value" << std::endl;
359                                 return false;
360                         }
361                         value = argv[++i];
362                 }
363
364                 set(name, value);
365         }
366
367         return true;
368 }
369
370
371
372 /***********
373  * Getters *
374  ***********/
375
376
377 const SettingsEntry &Settings::getEntry(const std::string &name) const
378 {
379         JMutexAutoLock lock(m_mutex);
380
381         std::map<std::string, SettingsEntry>::const_iterator n;
382         if ((n = m_settings.find(name)) == m_settings.end()) {
383                 if ((n = m_defaults.find(name)) == m_defaults.end())
384                         throw SettingNotFoundException("Setting [" + name + "] not found.");
385         }
386         return n->second;
387 }
388
389
390 Settings *Settings::getGroup(const std::string &name) const
391 {
392         const SettingsEntry &entry = getEntry(name);
393         if (!entry.is_group)
394                 throw SettingNotFoundException("Setting [" + name + "] is not a group.");
395         return entry.group;
396 }
397
398
399 std::string Settings::get(const std::string &name) const
400 {
401         const SettingsEntry &entry = getEntry(name);
402         if (entry.is_group)
403                 throw SettingNotFoundException("Setting [" + name + "] is a group.");
404         return entry.value;
405 }
406
407
408 bool Settings::getBool(const std::string &name) const
409 {
410         return is_yes(get(name));
411 }
412
413
414 u16 Settings::getU16(const std::string &name) const
415 {
416         return stoi(get(name), 0, 65535);
417 }
418
419
420 s16 Settings::getS16(const std::string &name) const
421 {
422         return stoi(get(name), -32768, 32767);
423 }
424
425
426 s32 Settings::getS32(const std::string &name) const
427 {
428         return stoi(get(name));
429 }
430
431
432 float Settings::getFloat(const std::string &name) const
433 {
434         return stof(get(name));
435 }
436
437
438 u64 Settings::getU64(const std::string &name) const
439 {
440         u64 value = 0;
441         std::string s = get(name);
442         std::istringstream ss(s);
443         ss >> value;
444         return value;
445 }
446
447
448 v2f Settings::getV2F(const std::string &name) const
449 {
450         v2f value;
451         Strfnd f(get(name));
452         f.next("(");
453         value.X = stof(f.next(","));
454         value.Y = stof(f.next(")"));
455         return value;
456 }
457
458
459 v3f Settings::getV3F(const std::string &name) const
460 {
461         v3f value;
462         Strfnd f(get(name));
463         f.next("(");
464         value.X = stof(f.next(","));
465         value.Y = stof(f.next(","));
466         value.Z = stof(f.next(")"));
467         return value;
468 }
469
470
471 u32 Settings::getFlagStr(const std::string &name, const FlagDesc *flagdesc,
472         u32 *flagmask) const
473 {
474         std::string val = get(name);
475         return std::isdigit(val[0])
476                 ? stoi(val)
477                 : readFlagString(val, flagdesc, flagmask);
478 }
479
480
481 // N.B. if getStruct() is used to read a non-POD aggregate type,
482 // the behavior is undefined.
483 bool Settings::getStruct(const std::string &name, const std::string &format,
484         void *out, size_t olen) const
485 {
486         std::string valstr;
487
488         try {
489                 valstr = get(name);
490         } catch (SettingNotFoundException &e) {
491                 return false;
492         }
493
494         if (!deSerializeStringToStruct(valstr, format, out, olen))
495                 return false;
496
497         return true;
498 }
499
500
501 bool Settings::getNoiseParams(const std::string &name, NoiseParams &np) const
502 {
503         return getNoiseParamsFromGroup(name, np) || getNoiseParamsFromValue(name, np);
504 }
505
506
507 bool Settings::getNoiseParamsFromValue(const std::string &name,
508         NoiseParams &np) const
509 {
510         std::string value;
511
512         if (!getNoEx(name, value))
513                 return false;
514
515         Strfnd f(value);
516
517         np.offset   = stof(f.next(","));
518         np.scale    = stof(f.next(","));
519         f.next("(");
520         np.spread.X = stof(f.next(","));
521         np.spread.Y = stof(f.next(","));
522         np.spread.Z = stof(f.next(")"));
523         f.next(",");
524         np.seed     = stoi(f.next(","));
525         np.octaves  = stoi(f.next(","));
526         np.persist  = stof(f.next(","));
527
528         std::string optional_params = f.next("");
529         if (optional_params != "")
530                 np.lacunarity = stof(optional_params);
531
532         return true;
533 }
534
535
536 bool Settings::getNoiseParamsFromGroup(const std::string &name,
537         NoiseParams &np) const
538 {
539         Settings *group = NULL;
540
541         if (!getGroupNoEx(name, group))
542                 return false;
543
544         group->getFloatNoEx("offset",      np.offset);
545         group->getFloatNoEx("scale",       np.scale);
546         group->getV3FNoEx("spread",        np.spread);
547         group->getS32NoEx("seed",          np.seed);
548         group->getU16NoEx("octaves",       np.octaves);
549         group->getFloatNoEx("persistence", np.persist);
550         group->getFloatNoEx("lacunarity",  np.lacunarity);
551
552         np.flags = 0;
553         if (!group->getFlagStrNoEx("flags", np.flags, flagdesc_noiseparams))
554                 np.flags = NOISE_FLAG_DEFAULTS;
555
556         return true;
557 }
558
559
560 bool Settings::exists(const std::string &name) const
561 {
562         JMutexAutoLock lock(m_mutex);
563
564         return (m_settings.find(name) != m_settings.end() ||
565                 m_defaults.find(name) != m_defaults.end());
566 }
567
568
569 std::vector<std::string> Settings::getNames() const
570 {
571         std::vector<std::string> names;
572         for (std::map<std::string, SettingsEntry>::const_iterator
573                         i = m_settings.begin();
574                         i != m_settings.end(); ++i) {
575                 names.push_back(i->first);
576         }
577         return names;
578 }
579
580
581
582 /***************************************
583  * Getters that don't throw exceptions *
584  ***************************************/
585
586 bool Settings::getEntryNoEx(const std::string &name, SettingsEntry &val) const
587 {
588         try {
589                 val = getEntry(name);
590                 return true;
591         } catch (SettingNotFoundException &e) {
592                 return false;
593         }
594 }
595
596
597 bool Settings::getGroupNoEx(const std::string &name, Settings *&val) const
598 {
599         try {
600                 val = getGroup(name);
601                 return true;
602         } catch (SettingNotFoundException &e) {
603                 return false;
604         }
605 }
606
607
608 bool Settings::getNoEx(const std::string &name, std::string &val) const
609 {
610         try {
611                 val = get(name);
612                 return true;
613         } catch (SettingNotFoundException &e) {
614                 return false;
615         }
616 }
617
618
619 bool Settings::getFlag(const std::string &name) const
620 {
621         try {
622                 return getBool(name);
623         } catch(SettingNotFoundException &e) {
624                 return false;
625         }
626 }
627
628
629 bool Settings::getFloatNoEx(const std::string &name, float &val) const
630 {
631         try {
632                 val = getFloat(name);
633                 return true;
634         } catch (SettingNotFoundException &e) {
635                 return false;
636         }
637 }
638
639
640 bool Settings::getU16NoEx(const std::string &name, u16 &val) const
641 {
642         try {
643                 val = getU16(name);
644                 return true;
645         } catch (SettingNotFoundException &e) {
646                 return false;
647         }
648 }
649
650
651 bool Settings::getS16NoEx(const std::string &name, s16 &val) const
652 {
653         try {
654                 val = getS16(name);
655                 return true;
656         } catch (SettingNotFoundException &e) {
657                 return false;
658         }
659 }
660
661
662 bool Settings::getS32NoEx(const std::string &name, s32 &val) const
663 {
664         try {
665                 val = getS32(name);
666                 return true;
667         } catch (SettingNotFoundException &e) {
668                 return false;
669         }
670 }
671
672
673 bool Settings::getU64NoEx(const std::string &name, u64 &val) const
674 {
675         try {
676                 val = getU64(name);
677                 return true;
678         } catch (SettingNotFoundException &e) {
679                 return false;
680         }
681 }
682
683
684 bool Settings::getV2FNoEx(const std::string &name, v2f &val) const
685 {
686         try {
687                 val = getV2F(name);
688                 return true;
689         } catch (SettingNotFoundException &e) {
690                 return false;
691         }
692 }
693
694
695 bool Settings::getV3FNoEx(const std::string &name, v3f &val) const
696 {
697         try {
698                 val = getV3F(name);
699                 return true;
700         } catch (SettingNotFoundException &e) {
701                 return false;
702         }
703 }
704
705
706 // N.B. getFlagStrNoEx() does not set val, but merely modifies it.  Thus,
707 // val must be initialized before using getFlagStrNoEx().  The intention of
708 // this is to simplify modifying a flags field from a default value.
709 bool Settings::getFlagStrNoEx(const std::string &name, u32 &val,
710         FlagDesc *flagdesc) const
711 {
712         try {
713                 u32 flags, flagmask;
714
715                 flags = getFlagStr(name, flagdesc, &flagmask);
716
717                 val &= ~flagmask;
718                 val |=  flags;
719
720                 return true;
721         } catch (SettingNotFoundException &e) {
722                 return false;
723         }
724 }
725
726
727 /***********
728  * Setters *
729  ***********/
730
731 bool Settings::setEntry(const std::string &name, const void *data,
732         bool set_group, bool set_default)
733 {
734         Settings *old_group = NULL;
735
736         if (!checkNameValid(name))
737                 return false;
738         if (!set_group && !checkValueValid(*(const std::string *)data))
739                 return false;
740
741         {
742                 JMutexAutoLock lock(m_mutex);
743
744                 SettingsEntry &entry = set_default ? m_defaults[name] : m_settings[name];
745                 old_group = entry.group;
746
747                 entry.value    = set_group ? "" : *(const std::string *)data;
748                 entry.group    = set_group ? *(Settings **)data : NULL;
749                 entry.is_group = set_group;
750         }
751
752         delete old_group;
753
754         return true;
755 }
756
757
758 bool Settings::set(const std::string &name, const std::string &value)
759 {
760         if (!setEntry(name, &value, false, false))
761                 return false;
762
763         doCallbacks(name);
764         return true;
765 }
766
767
768 bool Settings::setDefault(const std::string &name, const std::string &value)
769 {
770         return setEntry(name, &value, false, true);
771 }
772
773
774 bool Settings::setGroup(const std::string &name, Settings *group)
775 {
776         return setEntry(name, &group, true, false);
777 }
778
779
780 bool Settings::setGroupDefault(const std::string &name, Settings *group)
781 {
782         return setEntry(name, &group, true, true);
783 }
784
785
786 bool Settings::setBool(const std::string &name, bool value)
787 {
788         return set(name, value ? "true" : "false");
789 }
790
791
792 bool Settings::setS16(const std::string &name, s16 value)
793 {
794         return set(name, itos(value));
795 }
796
797
798 bool Settings::setU16(const std::string &name, u16 value)
799 {
800         return set(name, itos(value));
801 }
802
803
804 bool Settings::setS32(const std::string &name, s32 value)
805 {
806         return set(name, itos(value));
807 }
808
809
810 bool Settings::setU64(const std::string &name, u64 value)
811 {
812         std::ostringstream os;
813         os << value;
814         return set(name, os.str());
815 }
816
817
818 bool Settings::setFloat(const std::string &name, float value)
819 {
820         return set(name, ftos(value));
821 }
822
823
824 bool Settings::setV2F(const std::string &name, v2f value)
825 {
826         std::ostringstream os;
827         os << "(" << value.X << "," << value.Y << ")";
828         return set(name, os.str());
829 }
830
831
832 bool Settings::setV3F(const std::string &name, v3f value)
833 {
834         std::ostringstream os;
835         os << "(" << value.X << "," << value.Y << "," << value.Z << ")";
836         return set(name, os.str());
837 }
838
839
840 bool Settings::setFlagStr(const std::string &name, u32 flags,
841         const FlagDesc *flagdesc, u32 flagmask)
842 {
843         return set(name, writeFlagString(flags, flagdesc, flagmask));
844 }
845
846
847 bool Settings::setStruct(const std::string &name, const std::string &format,
848         void *value)
849 {
850         std::string structstr;
851         if (!serializeStructToString(&structstr, format, value))
852                 return false;
853
854         return set(name, structstr);
855 }
856
857
858 bool Settings::setNoiseParams(const std::string &name,
859         const NoiseParams &np, bool set_default)
860 {
861         Settings *group = new Settings;
862
863         group->setFloat("offset",      np.offset);
864         group->setFloat("scale",       np.scale);
865         group->setV3F("spread",        np.spread);
866         group->setS32("seed",          np.seed);
867         group->setU16("octaves",       np.octaves);
868         group->setFloat("persistence", np.persist);
869         group->setFloat("lacunarity",  np.lacunarity);
870         group->setFlagStr("flags",     np.flags, flagdesc_noiseparams, np.flags);
871
872         return setEntry(name, &group, true, set_default);
873 }
874
875
876 bool Settings::remove(const std::string &name)
877 {
878         JMutexAutoLock lock(m_mutex);
879
880         delete m_settings[name].group;
881         return m_settings.erase(name);
882 }
883
884
885 void Settings::clear()
886 {
887         JMutexAutoLock lock(m_mutex);
888         clearNoLock();
889 }
890
891
892 void Settings::updateValue(const Settings &other, const std::string &name)
893 {
894         if (&other == this)
895                 return;
896
897         JMutexAutoLock lock(m_mutex);
898
899         try {
900                 std::string val = other.get(name);
901
902                 m_settings[name] = val;
903         } catch (SettingNotFoundException &e) {
904         }
905 }
906
907
908 void Settings::update(const Settings &other)
909 {
910         if (&other == this)
911                 return;
912
913         JMutexAutoLock lock(m_mutex);
914         JMutexAutoLock lock2(other.m_mutex);
915
916         updateNoLock(other);
917 }
918
919
920 SettingsParseEvent Settings::parseConfigObject(const std::string &line,
921         const std::string &end, std::string &name, std::string &value)
922 {
923         std::string trimmed_line = trim(line);
924
925         if (trimmed_line.empty())
926                 return SPE_NONE;
927         if (trimmed_line[0] == '#')
928                 return SPE_COMMENT;
929         if (trimmed_line == end)
930                 return SPE_END;
931
932         size_t pos = trimmed_line.find('=');
933         if (pos == std::string::npos)
934                 return SPE_INVALID;
935
936         name  = trim(trimmed_line.substr(0, pos));
937         value = trim(trimmed_line.substr(pos + 1));
938
939         if (value == "{")
940                 return SPE_GROUP;
941         if (value == "\"\"\"")
942                 return SPE_MULTILINE;
943
944         return SPE_KVPAIR;
945 }
946
947
948 void Settings::updateNoLock(const Settings &other)
949 {
950         m_settings.insert(other.m_settings.begin(), other.m_settings.end());
951         m_defaults.insert(other.m_defaults.begin(), other.m_defaults.end());
952 }
953
954
955 void Settings::clearNoLock()
956 {
957         std::map<std::string, SettingsEntry>::const_iterator it;
958         for (it = m_settings.begin(); it != m_settings.end(); ++it)
959                 delete it->second.group;
960         m_settings.clear();
961
962         for (it = m_defaults.begin(); it != m_defaults.end(); ++it)
963                 delete it->second.group;
964         m_defaults.clear();
965 }
966
967
968 void Settings::registerChangedCallback(std::string name,
969         setting_changed_callback cbf)
970 {
971         m_callbacks[name].push_back(cbf);
972 }
973
974
975 void Settings::doCallbacks(const std::string name)
976 {
977         std::vector<setting_changed_callback> tempvector;
978         {
979                 JMutexAutoLock lock(m_mutex);
980                 if (m_callbacks.find(name) != m_callbacks.end())
981                 {
982                         tempvector = m_callbacks[name];
983                 }
984         }
985
986         std::vector<setting_changed_callback>::iterator iter;
987         for (iter = tempvector.begin(); iter != tempvector.end(); iter++)
988         {
989                 (*iter)(name);
990         }
991 }