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::sanitizeString(const std::string &value)
68 std::string str = value;
69 for (const char *s = "\t\n\v\f\r\b =\""; *s; s++)
70 str.erase(std::remove(str.begin(), str.end(), *s), str.end());
76 std::string Settings::getMultiline(std::istream &is)
82 std::getline(is, line);
86 value.push_back('\n');
89 size_t len = value.size();
97 bool Settings::parseConfigLines(std::istream &is, const std::string &end)
99 JMutexAutoLock lock(m_mutex);
101 std::string line, name, value;
104 std::getline(is, line);
105 SettingsParseEvent event = parseConfigObject(line, end, name, value);
113 m_settings[name] = SettingsEntry(value);
118 Settings *branch = new Settings;
119 if (!branch->parseConfigLines(is, "}"))
122 m_settings[name] = SettingsEntry(branch);
126 m_settings[name] = SettingsEntry(getMultiline(is));
135 bool Settings::readConfigFile(const char *filename)
137 std::ifstream is(filename);
141 return parseConfigLines(is, "");
145 void Settings::writeLines(std::ostream &os, u32 tab_depth) const
147 JMutexAutoLock lock(m_mutex);
149 for (std::map<std::string, SettingsEntry>::const_iterator
150 it = m_settings.begin();
151 it != m_settings.end(); ++it) {
152 bool is_multiline = it->second.value.find('\n') != std::string::npos;
153 printValue(os, it->first, it->second, is_multiline, tab_depth);
158 void Settings::printValue(std::ostream &os, const std::string &name,
159 const SettingsEntry &entry, bool is_value_multiline, u32 tab_depth)
161 for (u32 i = 0; i != tab_depth; i++)
165 if (is_value_multiline)
166 os << "\"\"\"\n" << entry.value << "\n\"\"\"\n";
168 os << entry.value << "\n";
170 Settings *group = entry.group;
172 for (u32 i = 0; i != tab_depth; i++)
175 os << name << " = {\n";
176 group->writeLines(os, tab_depth + 1);
178 for (u32 i = 0; i != tab_depth; i++)
186 bool Settings::updateConfigObject(std::istream &is, std::ostream &os,
187 const std::string &end, u32 tab_depth)
189 std::map<std::string, SettingsEntry>::const_iterator it;
190 std::set<std::string> settings_in_config;
191 bool was_modified = false;
192 bool end_found = false;
193 std::string line, name, value;
195 // Add any settings that exist in the config file with the current value
196 // in the object if existing
197 while (is.good() && !end_found) {
198 std::getline(is, line);
199 SettingsParseEvent event = parseConfigObject(line, end, name, value);
207 it = m_settings.find(name);
208 if (it != m_settings.end()) {
209 if (event == SPE_MULTILINE)
210 value = getMultiline(is);
212 if (value != it->second.value) {
213 value = it->second.value;
218 settings_in_config.insert(name);
220 printValue(os, name, SettingsEntry(value),
221 event == SPE_MULTILINE, tab_depth);
225 Settings *group = NULL;
226 it = m_settings.find(name);
227 if (it != m_settings.end())
228 group = it->second.group;
230 settings_in_config.insert(name);
232 os << name << " = {\n";
235 was_modified |= group->updateConfigObject(is, os, "}", tab_depth + 1);
237 Settings dummy_settings;
238 dummy_settings.updateConfigObject(is, os, "}", tab_depth + 1);
241 for (u32 i = 0; i != tab_depth; i++)
247 os << line << (is.eof() ? "" : "\n");
252 // Add any settings in the object that don't exist in the config file yet
253 for (it = m_settings.begin(); it != m_settings.end(); ++it) {
254 if (settings_in_config.find(it->first) != settings_in_config.end())
259 bool is_multiline = it->second.value.find('\n') != std::string::npos;
260 printValue(os, it->first, it->second, is_multiline, tab_depth);
267 bool Settings::updateConfigFile(const char *filename)
269 JMutexAutoLock lock(m_mutex);
271 std::ifstream is(filename);
272 std::ostringstream os(std::ios_base::binary);
274 if (!updateConfigObject(is, os, ""))
277 if (!fs::safeWriteToFile(filename, os.str())) {
278 errorstream << "Error writing configuration file: \""
279 << filename << "\"" << std::endl;
287 bool Settings::parseCommandLine(int argc, char *argv[],
288 std::map<std::string, ValueSpec> &allowed_options)
290 int nonopt_index = 0;
291 for (int i = 1; i < argc; i++) {
292 std::string arg_name = argv[i];
293 if (arg_name.substr(0, 2) != "--") {
294 // If option doesn't start with -, read it in as nonoptX
295 if (arg_name[0] != '-'){
296 std::string name = "nonopt";
297 name += itos(nonopt_index);
302 errorstream << "Invalid command-line parameter \""
303 << arg_name << "\": --<option> expected." << std::endl;
307 std::string name = arg_name.substr(2);
309 std::map<std::string, ValueSpec>::iterator n;
310 n = allowed_options.find(name);
311 if (n == allowed_options.end()) {
312 errorstream << "Unknown command-line parameter \""
313 << arg_name << "\"" << std::endl;
317 ValueType type = n->second.type;
319 std::string value = "";
321 if (type == VALUETYPE_FLAG) {
324 if ((i + 1) >= argc) {
325 errorstream << "Invalid command-line parameter \""
326 << name << "\": missing value" << std::endl;
345 const SettingsEntry &Settings::getEntry(const std::string &name) const
347 JMutexAutoLock lock(m_mutex);
349 std::map<std::string, SettingsEntry>::const_iterator n;
350 if ((n = m_settings.find(name)) == m_settings.end()) {
351 if ((n = m_defaults.find(name)) == m_defaults.end())
352 throw SettingNotFoundException("Setting [" + name + "] not found.");
358 Settings *Settings::getGroup(const std::string &name) const
360 return getEntry(name).group;
364 std::string Settings::get(const std::string &name) const
366 return getEntry(name).value;
370 bool Settings::getBool(const std::string &name) const
372 return is_yes(get(name));
376 u16 Settings::getU16(const std::string &name) const
378 return stoi(get(name), 0, 65535);
382 s16 Settings::getS16(const std::string &name) const
384 return stoi(get(name), -32768, 32767);
388 s32 Settings::getS32(const std::string &name) const
390 return stoi(get(name));
394 float Settings::getFloat(const std::string &name) const
396 return stof(get(name));
400 u64 Settings::getU64(const std::string &name) const
403 std::string s = get(name);
404 std::istringstream ss(s);
410 v2f Settings::getV2F(const std::string &name) const
415 value.X = stof(f.next(","));
416 value.Y = stof(f.next(")"));
421 v3f Settings::getV3F(const std::string &name) const
426 value.X = stof(f.next(","));
427 value.Y = stof(f.next(","));
428 value.Z = stof(f.next(")"));
433 u32 Settings::getFlagStr(const std::string &name, const FlagDesc *flagdesc,
436 std::string val = get(name);
437 return std::isdigit(val[0])
439 : readFlagString(val, flagdesc, flagmask);
443 // N.B. if getStruct() is used to read a non-POD aggregate type,
444 // the behavior is undefined.
445 bool Settings::getStruct(const std::string &name, const std::string &format,
446 void *out, size_t olen) const
452 } catch (SettingNotFoundException &e) {
456 if (!deSerializeStringToStruct(valstr, format, out, olen))
463 bool Settings::exists(const std::string &name) const
465 JMutexAutoLock lock(m_mutex);
467 return (m_settings.find(name) != m_settings.end() ||
468 m_defaults.find(name) != m_defaults.end());
472 std::vector<std::string> Settings::getNames() const
474 std::vector<std::string> names;
475 for (std::map<std::string, SettingsEntry>::const_iterator
476 i = m_settings.begin();
477 i != m_settings.end(); ++i) {
478 names.push_back(i->first);
485 /***************************************
486 * Getters that don't throw exceptions *
487 ***************************************/
489 bool Settings::getEntryNoEx(const std::string &name, SettingsEntry &val) const
492 val = getEntry(name);
494 } catch (SettingNotFoundException &e) {
500 bool Settings::getGroupNoEx(const std::string &name, Settings *&val) const
503 val = getGroup(name);
505 } catch (SettingNotFoundException &e) {
511 bool Settings::getNoEx(const std::string &name, std::string &val) const
516 } catch (SettingNotFoundException &e) {
522 bool Settings::getFlag(const std::string &name) const
525 return getBool(name);
526 } catch(SettingNotFoundException &e) {
532 bool Settings::getFloatNoEx(const std::string &name, float &val) const
535 val = getFloat(name);
537 } catch (SettingNotFoundException &e) {
543 bool Settings::getU16NoEx(const std::string &name, u16 &val) const
548 } catch (SettingNotFoundException &e) {
554 bool Settings::getS16NoEx(const std::string &name, s16 &val) const
559 } catch (SettingNotFoundException &e) {
565 bool Settings::getS32NoEx(const std::string &name, s32 &val) const
570 } catch (SettingNotFoundException &e) {
576 bool Settings::getU64NoEx(const std::string &name, u64 &val) const
581 } catch (SettingNotFoundException &e) {
587 bool Settings::getV2FNoEx(const std::string &name, v2f &val) const
592 } catch (SettingNotFoundException &e) {
598 bool Settings::getV3FNoEx(const std::string &name, v3f &val) const
603 } catch (SettingNotFoundException &e) {
609 // N.B. getFlagStrNoEx() does not set val, but merely modifies it. Thus,
610 // val must be initialized before using getFlagStrNoEx(). The intention of
611 // this is to simplify modifying a flags field from a default value.
612 bool Settings::getFlagStrNoEx(const std::string &name, u32 &val,
613 FlagDesc *flagdesc) const
618 flags = getFlagStr(name, flagdesc, &flagmask);
624 } catch (SettingNotFoundException &e) {
635 void Settings::set(const std::string &name, const std::string &value)
637 JMutexAutoLock lock(m_mutex);
639 m_settings[name].value = value;
643 void Settings::setGroup(const std::string &name, Settings *group)
645 JMutexAutoLock lock(m_mutex);
647 delete m_settings[name].group;
648 m_settings[name].group = group;
652 void Settings::setDefault(const std::string &name, const std::string &value)
654 JMutexAutoLock lock(m_mutex);
656 m_defaults[name].value = value;
660 void Settings::setGroupDefault(const std::string &name, Settings *group)
662 JMutexAutoLock lock(m_mutex);
664 delete m_defaults[name].group;
665 m_defaults[name].group = group;
669 void Settings::setBool(const std::string &name, bool value)
671 set(name, value ? "true" : "false");
675 void Settings::setS16(const std::string &name, s16 value)
677 set(name, itos(value));
681 void Settings::setS32(const std::string &name, s32 value)
683 set(name, itos(value));
687 void Settings::setU64(const std::string &name, u64 value)
689 std::ostringstream os;
695 void Settings::setFloat(const std::string &name, float value)
697 set(name, ftos(value));
701 void Settings::setV2F(const std::string &name, v2f value)
703 std::ostringstream os;
704 os << "(" << value.X << "," << value.Y << ")";
709 void Settings::setV3F(const std::string &name, v3f value)
711 std::ostringstream os;
712 os << "(" << value.X << "," << value.Y << "," << value.Z << ")";
717 void Settings::setFlagStr(const std::string &name, u32 flags,
718 const FlagDesc *flagdesc, u32 flagmask)
720 set(name, writeFlagString(flags, flagdesc, flagmask));
724 bool Settings::setStruct(const std::string &name, const std::string &format,
727 std::string structstr;
728 if (!serializeStructToString(&structstr, format, value))
731 set(name, structstr);
736 bool Settings::remove(const std::string &name)
738 JMutexAutoLock lock(m_mutex);
739 return m_settings.erase(name);
743 void Settings::clear()
745 JMutexAutoLock lock(m_mutex);
750 void Settings::updateValue(const Settings &other, const std::string &name)
755 JMutexAutoLock lock(m_mutex);
758 std::string val = other.get(name);
760 m_settings[name] = val;
761 } catch (SettingNotFoundException &e) {
766 void Settings::update(const Settings &other)
771 JMutexAutoLock lock(m_mutex);
772 JMutexAutoLock lock2(other.m_mutex);
778 SettingsParseEvent Settings::parseConfigObject(const std::string &line,
779 const std::string &end, std::string &name, std::string &value)
781 std::string trimmed_line = trim(line);
783 if (trimmed_line.empty())
785 if (trimmed_line[0] == '#')
787 if (trimmed_line == end)
790 size_t pos = trimmed_line.find('=');
791 if (pos == std::string::npos)
794 name = trim(trimmed_line.substr(0, pos));
795 value = trim(trimmed_line.substr(pos + 1));
799 if (value == "\"\"\"")
800 return SPE_MULTILINE;
806 void Settings::updateNoLock(const Settings &other)
808 m_settings.insert(other.m_settings.begin(), other.m_settings.end());
809 m_defaults.insert(other.m_defaults.begin(), other.m_defaults.end());
813 void Settings::clearNoLock()
820 void Settings::registerChangedCallback(std::string name,
821 setting_changed_callback cbf)
823 m_callbacks[name].push_back(cbf);
827 void Settings::doCallbacks(const std::string name)
829 std::vector<setting_changed_callback> tempvector;
831 JMutexAutoLock lock(m_mutex);
832 if (m_callbacks.find(name) != m_callbacks.end())
834 tempvector = m_callbacks[name];
838 for (std::vector<setting_changed_callback>::iterator iter = tempvector.begin();
839 iter != tempvector.end(); iter ++)