]> git.lizzy.rs Git - dragonfireclient.git/blob - src/settings.cpp
Biomes: Make biome heat and humidity noise parameters user-configurable
[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         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         group->getFloatNoEx("lacunarity",  np.lacunarity);
595
596         np.flags = 0;
597         if (!group->getFlagStrNoEx("flags", np.flags, flagdesc_noiseparams))
598                 np.flags = NOISE_FLAG_DEFAULTS;
599
600         return true;
601 }
602
603
604 bool Settings::exists(const std::string &name) const
605 {
606         JMutexAutoLock lock(m_mutex);
607
608         return (m_settings.find(name) != m_settings.end() ||
609                 m_defaults.find(name) != m_defaults.end());
610 }
611
612
613 std::vector<std::string> Settings::getNames() const
614 {
615         std::vector<std::string> names;
616         for (std::map<std::string, SettingsEntry>::const_iterator
617                         i = m_settings.begin();
618                         i != m_settings.end(); ++i) {
619                 names.push_back(i->first);
620         }
621         return names;
622 }
623
624
625
626 /***************************************
627  * Getters that don't throw exceptions *
628  ***************************************/
629
630 bool Settings::getEntryNoEx(const std::string &name, SettingsEntry &val) const
631 {
632         try {
633                 val = getEntry(name);
634                 return true;
635         } catch (SettingNotFoundException &e) {
636                 return false;
637         }
638 }
639
640
641 bool Settings::getGroupNoEx(const std::string &name, Settings *&val) const
642 {
643         try {
644                 val = getGroup(name);
645                 return true;
646         } catch (SettingNotFoundException &e) {
647                 return false;
648         }
649 }
650
651
652 bool Settings::getNoEx(const std::string &name, std::string &val) const
653 {
654         try {
655                 val = get(name);
656                 return true;
657         } catch (SettingNotFoundException &e) {
658                 return false;
659         }
660 }
661
662
663 bool Settings::getFlag(const std::string &name) const
664 {
665         try {
666                 return getBool(name);
667         } catch(SettingNotFoundException &e) {
668                 return false;
669         }
670 }
671
672
673 bool Settings::getFloatNoEx(const std::string &name, float &val) const
674 {
675         try {
676                 val = getFloat(name);
677                 return true;
678         } catch (SettingNotFoundException &e) {
679                 return false;
680         }
681 }
682
683
684 bool Settings::getU16NoEx(const std::string &name, u16 &val) const
685 {
686         try {
687                 val = getU16(name);
688                 return true;
689         } catch (SettingNotFoundException &e) {
690                 return false;
691         }
692 }
693
694
695 bool Settings::getS16NoEx(const std::string &name, s16 &val) const
696 {
697         try {
698                 val = getS16(name);
699                 return true;
700         } catch (SettingNotFoundException &e) {
701                 return false;
702         }
703 }
704
705
706 bool Settings::getS32NoEx(const std::string &name, s32 &val) const
707 {
708         try {
709                 val = getS32(name);
710                 return true;
711         } catch (SettingNotFoundException &e) {
712                 return false;
713         }
714 }
715
716
717 bool Settings::getU64NoEx(const std::string &name, u64 &val) const
718 {
719         try {
720                 val = getU64(name);
721                 return true;
722         } catch (SettingNotFoundException &e) {
723                 return false;
724         }
725 }
726
727
728 bool Settings::getV2FNoEx(const std::string &name, v2f &val) const
729 {
730         try {
731                 val = getV2F(name);
732                 return true;
733         } catch (SettingNotFoundException &e) {
734                 return false;
735         }
736 }
737
738
739 bool Settings::getV3FNoEx(const std::string &name, v3f &val) const
740 {
741         try {
742                 val = getV3F(name);
743                 return true;
744         } catch (SettingNotFoundException &e) {
745                 return false;
746         }
747 }
748
749
750 // N.B. getFlagStrNoEx() does not set val, but merely modifies it.  Thus,
751 // val must be initialized before using getFlagStrNoEx().  The intention of
752 // this is to simplify modifying a flags field from a default value.
753 bool Settings::getFlagStrNoEx(const std::string &name, u32 &val,
754         FlagDesc *flagdesc) const
755 {
756         try {
757                 u32 flags, flagmask;
758
759                 flags = getFlagStr(name, flagdesc, &flagmask);
760
761                 val &= ~flagmask;
762                 val |=  flags;
763
764                 return true;
765         } catch (SettingNotFoundException &e) {
766                 return false;
767         }
768 }
769
770
771 /***********
772  * Setters *
773  ***********/
774
775
776 void Settings::set(const std::string &name, const std::string &value)
777 {
778         {
779                 JMutexAutoLock lock(m_mutex);
780
781                 m_settings[name].value = value;
782         }
783         doCallbacks(name);
784 }
785
786
787 void Settings::setGroup(const std::string &name, Settings *group)
788 {
789         Settings *old_group = NULL;
790         {
791                 JMutexAutoLock lock(m_mutex);
792
793                 old_group = m_settings[name].group;
794                 m_settings[name].group = group;
795         }
796         delete old_group;
797 }
798
799
800 void Settings::setDefault(const std::string &name, const std::string &value)
801 {
802         JMutexAutoLock lock(m_mutex);
803
804         m_defaults[name].value = value;
805 }
806
807
808 void Settings::setGroupDefault(const std::string &name, Settings *group)
809 {
810         Settings *old_group = NULL;
811         {
812                 JMutexAutoLock lock(m_mutex);
813
814                 old_group = m_defaults[name].group;
815                 m_defaults[name].group = group;
816         }
817         delete old_group;
818 }
819
820
821 void Settings::setBool(const std::string &name, bool value)
822 {
823         set(name, value ? "true" : "false");
824 }
825
826
827 void Settings::setS16(const std::string &name, s16 value)
828 {
829         set(name, itos(value));
830 }
831
832
833 void Settings::setU16(const std::string &name, u16 value)
834 {
835         set(name, itos(value));
836 }
837
838
839 void Settings::setS32(const std::string &name, s32 value)
840 {
841         set(name, itos(value));
842 }
843
844
845 void Settings::setU64(const std::string &name, u64 value)
846 {
847         std::ostringstream os;
848         os << value;
849         set(name, os.str());
850 }
851
852
853 void Settings::setFloat(const std::string &name, float value)
854 {
855         set(name, ftos(value));
856 }
857
858
859 void Settings::setV2F(const std::string &name, v2f value)
860 {
861         std::ostringstream os;
862         os << "(" << value.X << "," << value.Y << ")";
863         set(name, os.str());
864 }
865
866
867 void Settings::setV3F(const std::string &name, v3f value)
868 {
869         std::ostringstream os;
870         os << "(" << value.X << "," << value.Y << "," << value.Z << ")";
871         set(name, os.str());
872 }
873
874
875 void Settings::setFlagStr(const std::string &name, u32 flags,
876         const FlagDesc *flagdesc, u32 flagmask)
877 {
878         set(name, writeFlagString(flags, flagdesc, flagmask));
879 }
880
881
882 bool Settings::setStruct(const std::string &name, const std::string &format,
883         void *value)
884 {
885         std::string structstr;
886         if (!serializeStructToString(&structstr, format, value))
887                 return false;
888
889         set(name, structstr);
890         return true;
891 }
892
893
894 void Settings::setNoiseParams(const std::string &name, const NoiseParams &np)
895 {
896         Settings *group = new Settings;
897
898         group->setFloat("offset",      np.offset);
899         group->setFloat("scale",       np.scale);
900         group->setV3F("spread",        np.spread);
901         group->setS32("seed",          np.seed);
902         group->setU16("octaves",       np.octaves);
903         group->setFloat("persistence", np.persist);
904         group->setFloat("lacunarity",  np.lacunarity);
905         group->setFlagStr("flags",     np.flags, flagdesc_noiseparams, np.flags);
906
907         Settings *old_group;
908         {
909                 JMutexAutoLock lock(m_mutex);
910
911                 old_group = m_settings[name].group;
912                 m_settings[name].group = group;
913                 m_settings[name].value = "";
914         }
915         delete old_group;
916 }
917
918
919 bool Settings::remove(const std::string &name)
920 {
921         JMutexAutoLock lock(m_mutex);
922         return m_settings.erase(name);
923 }
924
925
926 void Settings::clear()
927 {
928         JMutexAutoLock lock(m_mutex);
929         clearNoLock();
930 }
931
932
933 void Settings::updateValue(const Settings &other, const std::string &name)
934 {
935         if (&other == this)
936                 return;
937
938         JMutexAutoLock lock(m_mutex);
939
940         try {
941                 std::string val = other.get(name);
942
943                 m_settings[name] = val;
944         } catch (SettingNotFoundException &e) {
945         }
946 }
947
948
949 void Settings::update(const Settings &other)
950 {
951         if (&other == this)
952                 return;
953
954         JMutexAutoLock lock(m_mutex);
955         JMutexAutoLock lock2(other.m_mutex);
956
957         updateNoLock(other);
958 }
959
960
961 SettingsParseEvent Settings::parseConfigObject(const std::string &line,
962         const std::string &end, std::string &name, std::string &value)
963 {
964         std::string trimmed_line = trim(line);
965
966         if (trimmed_line.empty())
967                 return SPE_NONE;
968         if (trimmed_line[0] == '#')
969                 return SPE_COMMENT;
970         if (trimmed_line == end)
971                 return SPE_END;
972
973         size_t pos = trimmed_line.find('=');
974         if (pos == std::string::npos)
975                 return SPE_INVALID;
976
977         name  = trim(trimmed_line.substr(0, pos));
978         value = trim(trimmed_line.substr(pos + 1));
979
980         if (value == "{")
981                 return SPE_GROUP;
982         if (value == "\"\"\"")
983                 return SPE_MULTILINE;
984
985         return SPE_KVPAIR;
986 }
987
988
989 void Settings::updateNoLock(const Settings &other)
990 {
991         m_settings.insert(other.m_settings.begin(), other.m_settings.end());
992         m_defaults.insert(other.m_defaults.begin(), other.m_defaults.end());
993 }
994
995
996 void Settings::clearNoLock()
997 {
998         m_settings.clear();
999         m_defaults.clear();
1000 }
1001
1002
1003 void Settings::registerChangedCallback(std::string name,
1004         setting_changed_callback cbf)
1005 {
1006         m_callbacks[name].push_back(cbf);
1007 }
1008
1009
1010 void Settings::doCallbacks(const std::string name)
1011 {
1012         std::vector<setting_changed_callback> tempvector;
1013         {
1014                 JMutexAutoLock lock(m_mutex);
1015                 if (m_callbacks.find(name) != m_callbacks.end())
1016                 {
1017                         tempvector = m_callbacks[name];
1018                 }
1019         }
1020
1021         for (std::vector<setting_changed_callback>::iterator iter = tempvector.begin();
1022                         iter != tempvector.end(); iter ++)
1023         {
1024                 (*iter)(name);
1025         }
1026 }