3 Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
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.
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.
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.
21 #include "irrlichttypes_bloated.h"
22 #include "exceptions.h"
23 #include "jthread/jmutexautolock.h"
30 #include "util/serialize.h"
37 std::map<std::string, SettingsEntry>::const_iterator it;
38 for (it = m_settings.begin(); it != m_settings.end(); ++it)
39 delete it->second.group;
43 Settings & Settings::operator += (const Settings &other)
51 Settings & Settings::operator = (const Settings &other)
56 JMutexAutoLock lock(m_mutex);
57 JMutexAutoLock lock2(other.m_mutex);
66 std::string Settings::getMultiline(std::istream &is)
72 std::getline(is, line);
76 value.push_back('\n');
79 size_t len = value.size();
87 bool Settings::parseConfigLines(std::istream &is, const std::string &end)
89 JMutexAutoLock lock(m_mutex);
91 std::string line, name, value;
94 std::getline(is, line);
95 SettingsParseEvent event = parseConfigObject(line, end, name, value);
103 m_settings[name] = SettingsEntry(value);
108 Settings *branch = new Settings;
109 if (!branch->parseConfigLines(is, "}"))
112 m_settings[name] = SettingsEntry(branch);
116 m_settings[name] = SettingsEntry(getMultiline(is));
125 bool Settings::readConfigFile(const char *filename)
127 std::ifstream is(filename);
131 return parseConfigLines(is, "");
135 void Settings::writeLines(std::ostream &os, u32 tab_depth) const
137 JMutexAutoLock lock(m_mutex);
139 for (std::map<std::string, SettingsEntry>::const_iterator
140 it = m_settings.begin();
141 it != m_settings.end(); ++it) {
142 bool is_multiline = it->second.value.find('\n') != std::string::npos;
143 printValue(os, it->first, it->second, is_multiline, tab_depth);
148 void Settings::printValue(std::ostream &os, const std::string &name,
149 const SettingsEntry &entry, bool is_value_multiline, u32 tab_depth)
151 for (u32 i = 0; i != tab_depth; i++)
155 if (is_value_multiline)
156 os << "\"\"\"\n" << entry.value << "\n\"\"\"\n";
158 os << entry.value << "\n";
160 Settings *group = entry.group;
162 for (u32 i = 0; i != tab_depth; i++)
165 os << name << " = {\n";
166 group->writeLines(os, tab_depth + 1);
168 for (u32 i = 0; i != tab_depth; i++)
176 bool Settings::updateConfigObject(std::istream &is, std::ostream &os,
177 const std::string &end, u32 tab_depth)
179 std::map<std::string, SettingsEntry>::const_iterator it;
180 std::set<std::string> settings_in_config;
181 bool was_modified = false;
182 bool end_found = false;
183 std::string line, name, value;
185 // Add any settings that exist in the config file with the current value
186 // in the object if existing
187 while (is.good() && !end_found) {
188 std::getline(is, line);
189 SettingsParseEvent event = parseConfigObject(line, end, name, value);
197 it = m_settings.find(name);
198 if (it != m_settings.end()) {
199 if (event == SPE_MULTILINE)
200 value = getMultiline(is);
202 if (value != it->second.value) {
203 value = it->second.value;
208 settings_in_config.insert(name);
210 printValue(os, name, SettingsEntry(value),
211 event == SPE_MULTILINE, tab_depth);
215 Settings *group = NULL;
216 it = m_settings.find(name);
217 if (it != m_settings.end())
218 group = it->second.group;
220 settings_in_config.insert(name);
222 os << name << " = {\n";
225 was_modified |= group->updateConfigObject(is, os, "}", tab_depth + 1);
227 Settings dummy_settings;
228 dummy_settings.updateConfigObject(is, os, "}", tab_depth + 1);
231 for (u32 i = 0; i != tab_depth; i++)
237 os << line << (is.eof() ? "" : "\n");
242 // Add any settings in the object that don't exist in the config file yet
243 for (it = m_settings.begin(); it != m_settings.end(); ++it) {
244 if (settings_in_config.find(it->first) != settings_in_config.end())
249 bool is_multiline = it->second.value.find('\n') != std::string::npos;
250 printValue(os, it->first, it->second, is_multiline, tab_depth);
257 bool Settings::updateConfigFile(const char *filename)
259 JMutexAutoLock lock(m_mutex);
261 std::ifstream is(filename);
262 std::ostringstream os(std::ios_base::binary);
264 if (!updateConfigObject(is, os, ""))
267 if (!fs::safeWriteToFile(filename, os.str())) {
268 errorstream << "Error writing configuration file: \""
269 << filename << "\"" << std::endl;
277 bool Settings::parseCommandLine(int argc, char *argv[],
278 std::map<std::string, ValueSpec> &allowed_options)
280 int nonopt_index = 0;
281 for (int i = 1; i < argc; i++) {
282 std::string arg_name = argv[i];
283 if (arg_name.substr(0, 2) != "--") {
284 // If option doesn't start with -, read it in as nonoptX
285 if (arg_name[0] != '-'){
286 std::string name = "nonopt";
287 name += itos(nonopt_index);
292 errorstream << "Invalid command-line parameter \""
293 << arg_name << "\": --<option> expected." << std::endl;
297 std::string name = arg_name.substr(2);
299 std::map<std::string, ValueSpec>::iterator n;
300 n = allowed_options.find(name);
301 if (n == allowed_options.end()) {
302 errorstream << "Unknown command-line parameter \""
303 << arg_name << "\"" << std::endl;
307 ValueType type = n->second.type;
309 std::string value = "";
311 if (type == VALUETYPE_FLAG) {
314 if ((i + 1) >= argc) {
315 errorstream << "Invalid command-line parameter \""
316 << name << "\": missing value" << std::endl;
335 const SettingsEntry &Settings::getEntry(const std::string &name) const
337 JMutexAutoLock lock(m_mutex);
339 std::map<std::string, SettingsEntry>::const_iterator n;
340 if ((n = m_settings.find(name)) == m_settings.end()) {
341 if ((n = m_defaults.find(name)) == m_defaults.end())
342 throw SettingNotFoundException("Setting [" + name + "] not found.");
348 Settings *Settings::getGroup(const std::string &name) const
350 return getEntry(name).group;
354 std::string Settings::get(const std::string &name) const
356 return getEntry(name).value;
360 bool Settings::getBool(const std::string &name) const
362 return is_yes(get(name));
366 u16 Settings::getU16(const std::string &name) const
368 return stoi(get(name), 0, 65535);
372 s16 Settings::getS16(const std::string &name) const
374 return stoi(get(name), -32768, 32767);
378 s32 Settings::getS32(const std::string &name) const
380 return stoi(get(name));
384 float Settings::getFloat(const std::string &name) const
386 return stof(get(name));
390 u64 Settings::getU64(const std::string &name) const
393 std::string s = get(name);
394 std::istringstream ss(s);
400 v2f Settings::getV2F(const std::string &name) const
405 value.X = stof(f.next(","));
406 value.Y = stof(f.next(")"));
411 v3f Settings::getV3F(const std::string &name) const
416 value.X = stof(f.next(","));
417 value.Y = stof(f.next(","));
418 value.Z = stof(f.next(")"));
423 u32 Settings::getFlagStr(const std::string &name, const FlagDesc *flagdesc,
426 std::string val = get(name);
427 return std::isdigit(val[0])
429 : readFlagString(val, flagdesc, flagmask);
433 // N.B. if getStruct() is used to read a non-POD aggregate type,
434 // the behavior is undefined.
435 bool Settings::getStruct(const std::string &name, const std::string &format,
436 void *out, size_t olen) const
442 } catch (SettingNotFoundException &e) {
446 if (!deSerializeStringToStruct(valstr, format, out, olen))
453 bool Settings::exists(const std::string &name) const
455 JMutexAutoLock lock(m_mutex);
457 return (m_settings.find(name) != m_settings.end() ||
458 m_defaults.find(name) != m_defaults.end());
462 std::vector<std::string> Settings::getNames() const
464 std::vector<std::string> names;
465 for (std::map<std::string, SettingsEntry>::const_iterator
466 i = m_settings.begin();
467 i != m_settings.end(); ++i) {
468 names.push_back(i->first);
475 /***************************************
476 * Getters that don't throw exceptions *
477 ***************************************/
479 bool Settings::getEntryNoEx(const std::string &name, SettingsEntry &val) const
482 val = getEntry(name);
484 } catch (SettingNotFoundException &e) {
490 bool Settings::getGroupNoEx(const std::string &name, Settings *&val) const
493 val = getGroup(name);
495 } catch (SettingNotFoundException &e) {
501 bool Settings::getNoEx(const std::string &name, std::string &val) const
506 } catch (SettingNotFoundException &e) {
512 bool Settings::getFlag(const std::string &name) const
515 return getBool(name);
516 } catch(SettingNotFoundException &e) {
522 bool Settings::getFloatNoEx(const std::string &name, float &val) const
525 val = getFloat(name);
527 } catch (SettingNotFoundException &e) {
533 bool Settings::getU16NoEx(const std::string &name, u16 &val) const
538 } catch (SettingNotFoundException &e) {
544 bool Settings::getS16NoEx(const std::string &name, s16 &val) const
549 } catch (SettingNotFoundException &e) {
555 bool Settings::getS32NoEx(const std::string &name, s32 &val) const
560 } catch (SettingNotFoundException &e) {
566 bool Settings::getU64NoEx(const std::string &name, u64 &val) const
571 } catch (SettingNotFoundException &e) {
577 bool Settings::getV2FNoEx(const std::string &name, v2f &val) const
582 } catch (SettingNotFoundException &e) {
588 bool Settings::getV3FNoEx(const std::string &name, v3f &val) const
593 } catch (SettingNotFoundException &e) {
599 // N.B. getFlagStrNoEx() does not set val, but merely modifies it. Thus,
600 // val must be initialized before using getFlagStrNoEx(). The intention of
601 // this is to simplify modifying a flags field from a default value.
602 bool Settings::getFlagStrNoEx(const std::string &name, u32 &val,
603 FlagDesc *flagdesc) const
608 flags = getFlagStr(name, flagdesc, &flagmask);
614 } catch (SettingNotFoundException &e) {
625 void Settings::set(const std::string &name, const std::string &value)
627 JMutexAutoLock lock(m_mutex);
629 m_settings[name].value = value;
633 void Settings::setGroup(const std::string &name, Settings *group)
635 JMutexAutoLock lock(m_mutex);
637 delete m_settings[name].group;
638 m_settings[name].group = group;
642 void Settings::setDefault(const std::string &name, const std::string &value)
644 JMutexAutoLock lock(m_mutex);
646 m_defaults[name].value = value;
650 void Settings::setGroupDefault(const std::string &name, Settings *group)
652 JMutexAutoLock lock(m_mutex);
654 delete m_defaults[name].group;
655 m_defaults[name].group = group;
659 void Settings::setBool(const std::string &name, bool value)
661 set(name, value ? "true" : "false");
665 void Settings::setS16(const std::string &name, s16 value)
667 set(name, itos(value));
671 void Settings::setS32(const std::string &name, s32 value)
673 set(name, itos(value));
677 void Settings::setU64(const std::string &name, u64 value)
679 std::ostringstream os;
685 void Settings::setFloat(const std::string &name, float value)
687 set(name, ftos(value));
691 void Settings::setV2F(const std::string &name, v2f value)
693 std::ostringstream os;
694 os << "(" << value.X << "," << value.Y << ")";
699 void Settings::setV3F(const std::string &name, v3f value)
701 std::ostringstream os;
702 os << "(" << value.X << "," << value.Y << "," << value.Z << ")";
707 void Settings::setFlagStr(const std::string &name, u32 flags,
708 const FlagDesc *flagdesc, u32 flagmask)
710 set(name, writeFlagString(flags, flagdesc, flagmask));
714 bool Settings::setStruct(const std::string &name, const std::string &format,
717 std::string structstr;
718 if (!serializeStructToString(&structstr, format, value))
721 set(name, structstr);
726 bool Settings::remove(const std::string &name)
728 JMutexAutoLock lock(m_mutex);
729 return m_settings.erase(name);
733 void Settings::clear()
735 JMutexAutoLock lock(m_mutex);
740 void Settings::updateValue(const Settings &other, const std::string &name)
745 JMutexAutoLock lock(m_mutex);
748 std::string val = other.get(name);
750 m_settings[name] = val;
751 } catch (SettingNotFoundException &e) {
756 void Settings::update(const Settings &other)
761 JMutexAutoLock lock(m_mutex);
762 JMutexAutoLock lock2(other.m_mutex);
768 SettingsParseEvent Settings::parseConfigObject(const std::string &line,
769 const std::string &end, std::string &name, std::string &value)
771 std::string trimmed_line = trim(line);
773 if (trimmed_line.empty())
775 if (trimmed_line[0] == '#')
777 if (trimmed_line == end)
780 size_t pos = trimmed_line.find('=');
781 if (pos == std::string::npos)
784 name = trim(trimmed_line.substr(0, pos));
785 value = trim(trimmed_line.substr(pos + 1));
789 if (value == "\"\"\"")
790 return SPE_MULTILINE;
796 void Settings::updateNoLock(const Settings &other)
798 m_settings.insert(other.m_settings.begin(), other.m_settings.end());
799 m_defaults.insert(other.m_defaults.begin(), other.m_defaults.end());
803 void Settings::clearNoLock()
810 void Settings::registerChangedCallback(std::string name,
811 setting_changed_callback cbf)
813 m_callbacks[name].push_back(cbf);
817 void Settings::doCallbacks(const std::string name)
819 std::vector<setting_changed_callback> tempvector;
821 JMutexAutoLock lock(m_mutex);
822 if (m_callbacks.find(name) != m_callbacks.end())
824 tempvector = m_callbacks[name];
828 for (std::vector<setting_changed_callback>::iterator iter = tempvector.begin();
829 iter != tempvector.end(); iter ++)