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