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