diff options
Diffstat (limited to 'src/settings.cpp')
-rw-r--r-- | src/settings.cpp | 237 |
1 files changed, 153 insertions, 84 deletions
diff --git a/src/settings.cpp b/src/settings.cpp index 4f8b7b617..55404319e 100644 --- a/src/settings.cpp +++ b/src/settings.cpp @@ -42,14 +42,16 @@ Settings::~Settings() clear(); } -Settings &Settings::operator+=(const Settings &other) + +Settings & Settings::operator += (const Settings &other) { update(other); return *this; } -Settings &Settings::operator=(const Settings &other) + +Settings & Settings::operator = (const Settings &other) { if (&other == this) return *this; @@ -63,6 +65,7 @@ Settings &Settings::operator=(const Settings &other) return *this; } + bool Settings::checkNameValid(const std::string &name) { bool valid = name.find_first_of("=\"{}#") == std::string::npos; @@ -70,19 +73,20 @@ bool Settings::checkNameValid(const std::string &name) valid = std::find_if(name.begin(), name.end(), ::isspace) == name.end(); if (!valid) { - errorstream << "Invalid setting name \"" << name << "\"" << std::endl; + errorstream << "Invalid setting name \"" << name << "\"" + << std::endl; return false; } return true; } + bool Settings::checkValueValid(const std::string &value) { if (value.substr(0, 3) == "\"\"\"" || - value.find("\n\"\"\"") != std::string::npos) { + value.find("\n\"\"\"") != std::string::npos) { errorstream << "Invalid character sequence '\"\"\"' found in" - " setting value!" - << std::endl; + " setting value!" << std::endl; return false; } return true; @@ -113,6 +117,7 @@ std::string Settings::getMultiline(std::istream &is, size_t *num_lines) return value; } + bool Settings::readConfigFile(const char *filename) { std::ifstream is(filename); @@ -122,6 +127,7 @@ bool Settings::readConfigFile(const char *filename) return parseConfigLines(is, ""); } + bool Settings::parseConfigLines(std::istream &is, const std::string &end) { MutexAutoLock lock(m_mutex); @@ -160,6 +166,7 @@ bool Settings::parseConfigLines(std::istream &is, const std::string &end) return end.empty(); } + void Settings::writeLines(std::ostream &os, u32 tab_depth) const { MutexAutoLock lock(m_mutex); @@ -168,8 +175,9 @@ void Settings::writeLines(std::ostream &os, u32 tab_depth) const printEntry(os, setting_it.first, setting_it.second, tab_depth); } + void Settings::printEntry(std::ostream &os, const std::string &name, - const SettingsEntry &entry, u32 tab_depth) + const SettingsEntry &entry, u32 tab_depth) { for (u32 i = 0; i != tab_depth; i++) os << "\t"; @@ -192,8 +200,9 @@ void Settings::printEntry(std::ostream &os, const std::string &name, } } -bool Settings::updateConfigObject( - std::istream &is, std::ostream &os, const std::string &end, u32 tab_depth) + +bool Settings::updateConfigObject(std::istream &is, std::ostream &os, + const std::string &end, u32 tab_depth) { SettingEntries::const_iterator it; std::set<std::string> present_entries; @@ -218,8 +227,7 @@ bool Settings::updateConfigObject( case SPE_KVPAIR: it = m_settings.find(name); if (it != m_settings.end() && - (it->second.is_group || - it->second.value != value)) { + (it->second.is_group || it->second.value != value)) { printEntry(os, name, it->second, tab_depth); was_modified = true; } else if (it == m_settings.end()) { @@ -238,15 +246,14 @@ bool Settings::updateConfigObject( if (it != m_settings.end() && it->second.is_group) { os << line << "\n"; sanity_check(it->second.group != NULL); - was_modified |= it->second.group->updateConfigObject( - is, os, "}", tab_depth + 1); + was_modified |= it->second.group->updateConfigObject(is, os, + "}", tab_depth + 1); } else if (it == m_settings.end()) { // Remove by skipping was_modified = true; Settings removed_group; // Move 'is' to group end std::stringstream ss; - removed_group.updateConfigObject( - is, ss, "}", tab_depth + 1); + removed_group.updateConfigObject(is, ss, "}", tab_depth + 1); break; } else { printEntry(os, name, it->second, tab_depth); @@ -272,6 +279,7 @@ bool Settings::updateConfigObject( return was_modified; } + bool Settings::updateConfigFile(const char *filename) { MutexAutoLock lock(m_mutex); @@ -286,31 +294,32 @@ bool Settings::updateConfigFile(const char *filename) return true; if (!fs::safeWriteToFile(filename, os.str())) { - errorstream << "Error writing configuration file: \"" << filename << "\"" - << std::endl; + errorstream << "Error writing configuration file: \"" + << filename << "\"" << std::endl; return false; } return true; } -bool Settings::parseCommandLine( - int argc, char *argv[], std::map<std::string, ValueSpec> &allowed_options) + +bool Settings::parseCommandLine(int argc, char *argv[], + std::map<std::string, ValueSpec> &allowed_options) { int nonopt_index = 0; for (int i = 1; i < argc; i++) { std::string arg_name = argv[i]; if (arg_name.substr(0, 2) != "--") { // If option doesn't start with -, read it in as nonoptX - if (arg_name[0] != '-') { + if (arg_name[0] != '-'){ std::string name = "nonopt"; name += itos(nonopt_index); set(name, arg_name); nonopt_index++; continue; } - errorstream << "Invalid command-line parameter \"" << arg_name - << "\": --<option> expected." << std::endl; + errorstream << "Invalid command-line parameter \"" + << arg_name << "\": --<option> expected." << std::endl; return false; } @@ -319,8 +328,8 @@ bool Settings::parseCommandLine( std::map<std::string, ValueSpec>::iterator n; n = allowed_options.find(name); if (n == allowed_options.end()) { - errorstream << "Unknown command-line parameter \"" << arg_name - << "\"" << std::endl; + errorstream << "Unknown command-line parameter \"" + << arg_name << "\"" << std::endl; return false; } @@ -332,8 +341,8 @@ bool Settings::parseCommandLine( value = "true"; } else { if ((i + 1) >= argc) { - errorstream << "Invalid command-line parameter \"" << name - << "\": missing value" << std::endl; + errorstream << "Invalid command-line parameter \"" + << name << "\": missing value" << std::endl; return false; } value = argv[++i]; @@ -345,10 +354,13 @@ bool Settings::parseCommandLine( return true; } + + /*********** * Getters * ***********/ + const SettingsEntry &Settings::getEntry(const std::string &name) const { MutexAutoLock lock(m_mutex); @@ -356,12 +368,12 @@ const SettingsEntry &Settings::getEntry(const std::string &name) const SettingEntries::const_iterator n; if ((n = m_settings.find(name)) == m_settings.end()) { if ((n = m_defaults.find(name)) == m_defaults.end()) - throw SettingNotFoundException( - "Setting [" + name + "] not found."); + throw SettingNotFoundException("Setting [" + name + "] not found."); } return n->second; } + const SettingsEntry &Settings::getEntryDefault(const std::string &name) const { MutexAutoLock lock(m_mutex); @@ -373,6 +385,7 @@ const SettingsEntry &Settings::getEntryDefault(const std::string &name) const return n->second; } + Settings *Settings::getGroup(const std::string &name) const { const SettingsEntry &entry = getEntry(name); @@ -381,6 +394,7 @@ Settings *Settings::getGroup(const std::string &name) const return entry.group; } + const std::string &Settings::get(const std::string &name) const { const SettingsEntry &entry = getEntry(name); @@ -389,6 +403,7 @@ const std::string &Settings::get(const std::string &name) const return entry.value; } + const std::string &Settings::getDefault(const std::string &name) const { const SettingsEntry &entry = getEntryDefault(name); @@ -397,24 +412,28 @@ const std::string &Settings::getDefault(const std::string &name) const return entry.value; } + bool Settings::getBool(const std::string &name) const { return is_yes(get(name)); } + u16 Settings::getU16(const std::string &name) const { return stoi(get(name), 0, 65535); } + s16 Settings::getS16(const std::string &name) const { return stoi(get(name), -32768, 32767); } + u32 Settings::getU32(const std::string &name) const { - return (u32)stoi(get(name)); + return (u32) stoi(get(name)); } s32 Settings::getS32(const std::string &name) const @@ -422,11 +441,13 @@ s32 Settings::getS32(const std::string &name) const return stoi(get(name)); } + float Settings::getFloat(const std::string &name) const { return stof(get(name)); } + u64 Settings::getU64(const std::string &name) const { u64 value = 0; @@ -436,6 +457,7 @@ u64 Settings::getU64(const std::string &name) const return value; } + v2f Settings::getV2F(const std::string &name) const { v2f value; @@ -446,6 +468,7 @@ v2f Settings::getV2F(const std::string &name) const return value; } + v3f Settings::getV3F(const std::string &name) const { v3f value; @@ -457,8 +480,9 @@ v3f Settings::getV3F(const std::string &name) const return value; } -u32 Settings::getFlagStr( - const std::string &name, const FlagDesc *flagdesc, u32 *flagmask) const + +u32 Settings::getFlagStr(const std::string &name, const FlagDesc *flagdesc, + u32 *flagmask) const { u32 flags = 0; u32 mask_default = 0; @@ -467,19 +491,20 @@ u32 Settings::getFlagStr( // Read default value (if there is any) if (getDefaultNoEx(name, value)) { flags = std::isdigit(value[0]) - ? stoi(value) - : readFlagString(value, flagdesc, &mask_default); + ? stoi(value) + : readFlagString(value, flagdesc, &mask_default); } // Apply custom flags "on top" value = get(name); u32 flags_user; u32 mask_user = U32_MAX; - flags_user = std::isdigit(value[0]) ? stoi(value) // Override default - : readFlagString(value, flagdesc, &mask_user); + flags_user = std::isdigit(value[0]) + ? stoi(value) // Override default + : readFlagString(value, flagdesc, &mask_user); flags &= ~mask_user; - flags |= flags_user; + flags |= flags_user; if (flagmask) *flagmask = mask_default | mask_user; @@ -489,8 +514,8 @@ u32 Settings::getFlagStr( // N.B. if getStruct() is used to read a non-POD aggregate type, // the behavior is undefined. -bool Settings::getStruct(const std::string &name, const std::string &format, void *out, - size_t olen) const +bool Settings::getStruct(const std::string &name, const std::string &format, + void *out, size_t olen) const { std::string valstr; @@ -506,12 +531,15 @@ bool Settings::getStruct(const std::string &name, const std::string &format, voi return true; } + bool Settings::getNoiseParams(const std::string &name, NoiseParams &np) const { return getNoiseParamsFromGroup(name, np) || getNoiseParamsFromValue(name, np); } -bool Settings::getNoiseParamsFromValue(const std::string &name, NoiseParams &np) const + +bool Settings::getNoiseParamsFromValue(const std::string &name, + NoiseParams &np) const { std::string value; @@ -520,16 +548,16 @@ bool Settings::getNoiseParamsFromValue(const std::string &name, NoiseParams &np) Strfnd f(value); - np.offset = stof(f.next(",")); - np.scale = stof(f.next(",")); + np.offset = stof(f.next(",")); + np.scale = stof(f.next(",")); f.next("("); np.spread.X = stof(f.next(",")); np.spread.Y = stof(f.next(",")); np.spread.Z = stof(f.next(")")); f.next(","); - np.seed = stoi(f.next(",")); - np.octaves = stoi(f.next(",")); - np.persist = stof(f.next(",")); + np.seed = stoi(f.next(",")); + np.octaves = stoi(f.next(",")); + np.persist = stof(f.next(",")); std::string optional_params = f.next(""); if (!optional_params.empty()) @@ -538,20 +566,22 @@ bool Settings::getNoiseParamsFromValue(const std::string &name, NoiseParams &np) return true; } -bool Settings::getNoiseParamsFromGroup(const std::string &name, NoiseParams &np) const + +bool Settings::getNoiseParamsFromGroup(const std::string &name, + NoiseParams &np) const { Settings *group = NULL; if (!getGroupNoEx(name, group)) return false; - group->getFloatNoEx("offset", np.offset); - group->getFloatNoEx("scale", np.scale); - group->getV3FNoEx("spread", np.spread); - group->getS32NoEx("seed", np.seed); - group->getU16NoEx("octaves", np.octaves); + group->getFloatNoEx("offset", np.offset); + group->getFloatNoEx("scale", np.scale); + group->getV3FNoEx("spread", np.spread); + group->getS32NoEx("seed", np.seed); + group->getU16NoEx("octaves", np.octaves); group->getFloatNoEx("persistence", np.persist); - group->getFloatNoEx("lacunarity", np.lacunarity); + group->getFloatNoEx("lacunarity", np.lacunarity); np.flags = 0; if (!group->getFlagStrNoEx("flags", np.flags, flagdesc_noiseparams)) @@ -560,14 +590,16 @@ bool Settings::getNoiseParamsFromGroup(const std::string &name, NoiseParams &np) return true; } + bool Settings::exists(const std::string &name) const { MutexAutoLock lock(m_mutex); return (m_settings.find(name) != m_settings.end() || - m_defaults.find(name) != m_defaults.end()); + m_defaults.find(name) != m_defaults.end()); } + std::vector<std::string> Settings::getNames() const { std::vector<std::string> names; @@ -577,6 +609,8 @@ std::vector<std::string> Settings::getNames() const return names; } + + /*************************************** * Getters that don't throw exceptions * ***************************************/ @@ -591,6 +625,7 @@ bool Settings::getEntryNoEx(const std::string &name, SettingsEntry &val) const } } + bool Settings::getEntryDefaultNoEx(const std::string &name, SettingsEntry &val) const { try { @@ -601,6 +636,7 @@ bool Settings::getEntryDefaultNoEx(const std::string &name, SettingsEntry &val) } } + bool Settings::getGroupNoEx(const std::string &name, Settings *&val) const { try { @@ -611,6 +647,7 @@ bool Settings::getGroupNoEx(const std::string &name, Settings *&val) const } } + bool Settings::getNoEx(const std::string &name, std::string &val) const { try { @@ -621,6 +658,7 @@ bool Settings::getNoEx(const std::string &name, std::string &val) const } } + bool Settings::getDefaultNoEx(const std::string &name, std::string &val) const { try { @@ -631,15 +669,17 @@ bool Settings::getDefaultNoEx(const std::string &name, std::string &val) const } } + bool Settings::getFlag(const std::string &name) const { try { return getBool(name); - } catch (SettingNotFoundException &e) { + } catch(SettingNotFoundException &e) { return false; } } + bool Settings::getFloatNoEx(const std::string &name, float &val) const { try { @@ -650,6 +690,7 @@ bool Settings::getFloatNoEx(const std::string &name, float &val) const } } + bool Settings::getU16NoEx(const std::string &name, u16 &val) const { try { @@ -660,6 +701,7 @@ bool Settings::getU16NoEx(const std::string &name, u16 &val) const } } + bool Settings::getS16NoEx(const std::string &name, s16 &val) const { try { @@ -670,6 +712,7 @@ bool Settings::getS16NoEx(const std::string &name, s16 &val) const } } + bool Settings::getS32NoEx(const std::string &name, s32 &val) const { try { @@ -680,6 +723,7 @@ bool Settings::getS32NoEx(const std::string &name, s32 &val) const } } + bool Settings::getU64NoEx(const std::string &name, u64 &val) const { try { @@ -690,6 +734,7 @@ bool Settings::getU64NoEx(const std::string &name, u64 &val) const } } + bool Settings::getV2FNoEx(const std::string &name, v2f &val) const { try { @@ -700,6 +745,7 @@ bool Settings::getV2FNoEx(const std::string &name, v2f &val) const } } + bool Settings::getV3FNoEx(const std::string &name, v3f &val) const { try { @@ -710,8 +756,9 @@ bool Settings::getV3FNoEx(const std::string &name, v3f &val) const } } -bool Settings::getFlagStrNoEx( - const std::string &name, u32 &val, const FlagDesc *flagdesc) const + +bool Settings::getFlagStrNoEx(const std::string &name, u32 &val, + const FlagDesc *flagdesc) const { if (!flagdesc) { if (!(flagdesc = getFlagDescFallback(name))) @@ -727,12 +774,13 @@ bool Settings::getFlagStrNoEx( } } + /*********** * Setters * ***********/ -bool Settings::setEntry(const std::string &name, const void *data, bool set_group, - bool set_default) +bool Settings::setEntry(const std::string &name, const void *data, + bool set_group, bool set_default) { Settings *old_group = NULL; @@ -747,8 +795,8 @@ bool Settings::setEntry(const std::string &name, const void *data, bool set_grou SettingsEntry &entry = set_default ? m_defaults[name] : m_settings[name]; old_group = entry.group; - entry.value = set_group ? "" : *(const std::string *)data; - entry.group = set_group ? *(Settings **)data : NULL; + entry.value = set_group ? "" : *(const std::string *)data; + entry.group = set_group ? *(Settings **)data : NULL; entry.is_group = set_group; } @@ -757,6 +805,7 @@ bool Settings::setEntry(const std::string &name, const void *data, bool set_grou return true; } + bool Settings::set(const std::string &name, const std::string &value) { if (!setEntry(name, &value, false, false)) @@ -766,41 +815,49 @@ bool Settings::set(const std::string &name, const std::string &value) return true; } + bool Settings::setDefault(const std::string &name, const std::string &value) { return setEntry(name, &value, false, true); } + bool Settings::setGroup(const std::string &name, Settings *group) { return setEntry(name, &group, true, false); } + bool Settings::setGroupDefault(const std::string &name, Settings *group) { return setEntry(name, &group, true, true); } + bool Settings::setBool(const std::string &name, bool value) { return set(name, value ? "true" : "false"); } + bool Settings::setS16(const std::string &name, s16 value) { return set(name, itos(value)); } + bool Settings::setU16(const std::string &name, u16 value) { return set(name, itos(value)); } + bool Settings::setS32(const std::string &name, s32 value) { return set(name, itos(value)); } + bool Settings::setU64(const std::string &name, u64 value) { std::ostringstream os; @@ -808,11 +865,13 @@ bool Settings::setU64(const std::string &name, u64 value) return set(name, os.str()); } + bool Settings::setFloat(const std::string &name, float value) { return set(name, ftos(value)); } + bool Settings::setV2F(const std::string &name, v2f value) { std::ostringstream os; @@ -820,6 +879,7 @@ bool Settings::setV2F(const std::string &name, v2f value) return set(name, os.str()); } + bool Settings::setV3F(const std::string &name, v3f value) { std::ostringstream os; @@ -827,8 +887,9 @@ bool Settings::setV3F(const std::string &name, v3f value) return set(name, os.str()); } -bool Settings::setFlagStr(const std::string &name, u32 flags, const FlagDesc *flagdesc, - u32 flagmask) + +bool Settings::setFlagStr(const std::string &name, u32 flags, + const FlagDesc *flagdesc, u32 flagmask) { if (!flagdesc) { if (!(flagdesc = getFlagDescFallback(name))) @@ -838,7 +899,9 @@ bool Settings::setFlagStr(const std::string &name, u32 flags, const FlagDesc *fl return set(name, writeFlagString(flags, flagdesc, flagmask)); } -bool Settings::setStruct(const std::string &name, const std::string &format, void *value) + +bool Settings::setStruct(const std::string &name, const std::string &format, + void *value) { std::string structstr; if (!serializeStructToString(&structstr, format, value)) @@ -847,23 +910,25 @@ bool Settings::setStruct(const std::string &name, const std::string &format, voi return set(name, structstr); } -bool Settings::setNoiseParams( - const std::string &name, const NoiseParams &np, bool set_default) + +bool Settings::setNoiseParams(const std::string &name, + const NoiseParams &np, bool set_default) { Settings *group = new Settings; - group->setFloat("offset", np.offset); - group->setFloat("scale", np.scale); - group->setV3F("spread", np.spread); - group->setS32("seed", np.seed); - group->setU16("octaves", np.octaves); + group->setFloat("offset", np.offset); + group->setFloat("scale", np.scale); + group->setV3F("spread", np.spread); + group->setS32("seed", np.seed); + group->setU16("octaves", np.octaves); group->setFloat("persistence", np.persist); - group->setFloat("lacunarity", np.lacunarity); - group->setFlagStr("flags", np.flags, flagdesc_noiseparams, np.flags); + group->setFloat("lacunarity", np.lacunarity); + group->setFlagStr("flags", np.flags, flagdesc_noiseparams, np.flags); return setEntry(name, &group, true, set_default); } + bool Settings::remove(const std::string &name) { // Lock as short as possible, unlock before doCallbacks() @@ -883,6 +948,7 @@ bool Settings::remove(const std::string &name) return false; } + void Settings::clear() { MutexAutoLock lock(m_mutex); @@ -908,6 +974,7 @@ void Settings::updateValue(const Settings &other, const std::string &name) } } + void Settings::update(const Settings &other) { if (&other == this) @@ -919,8 +986,9 @@ void Settings::update(const Settings &other) updateNoLock(other); } + SettingsParseEvent Settings::parseConfigObject(const std::string &line, - const std::string &end, std::string &name, std::string &value) + const std::string &end, std::string &name, std::string &value) { std::string trimmed_line = trim(line); @@ -935,7 +1003,7 @@ SettingsParseEvent Settings::parseConfigObject(const std::string &line, if (pos == std::string::npos) return SPE_INVALID; - name = trim(trimmed_line.substr(0, pos)); + name = trim(trimmed_line.substr(0, pos)); value = trim(trimmed_line.substr(pos + 1)); if (value == "{") @@ -946,12 +1014,14 @@ SettingsParseEvent Settings::parseConfigObject(const std::string &line, return SPE_KVPAIR; } + void Settings::updateNoLock(const Settings &other) { m_settings.insert(other.m_settings.begin(), other.m_settings.end()); m_defaults.insert(other.m_defaults.begin(), other.m_defaults.end()); } + void Settings::clearNoLock() { @@ -971,7 +1041,8 @@ void Settings::clearDefaultsNoLock() m_defaults.clear(); } -void Settings::setDefault(const std::string &name, const FlagDesc *flagdesc, u32 flags) +void Settings::setDefault(const std::string &name, const FlagDesc *flagdesc, + u32 flags) { m_flags[name] = flagdesc; setDefault(name, writeFlagString(flags, flagdesc, U32_MAX)); @@ -991,11 +1062,9 @@ void Settings::overrideDefaults(Settings *other) u32 flags = getFlagStr(setting.first, flagdesc, nullptr); // 2) Set the flags as defaults other->setDefault(setting.first, flagdesc, flags); - // 3) Get the newly set flags and override the default setting - // value + // 3) Get the newly set flags and override the default setting value setDefault(setting.first, flagdesc, - other->getFlagStr(setting.first, flagdesc, - nullptr)); + other->getFlagStr(setting.first, flagdesc, nullptr)); continue; } // Also covers FlagDesc settings @@ -1009,15 +1078,15 @@ const FlagDesc *Settings::getFlagDescFallback(const std::string &name) const return it == m_flags.end() ? nullptr : it->second; } -void Settings::registerChangedCallback( - const std::string &name, SettingsChangedCallback cbf, void *userdata) +void Settings::registerChangedCallback(const std::string &name, + SettingsChangedCallback cbf, void *userdata) { MutexAutoLock lock(m_callback_mutex); m_callbacks[name].emplace_back(cbf, userdata); } -void Settings::deregisterChangedCallback( - const std::string &name, SettingsChangedCallback cbf, void *userdata) +void Settings::deregisterChangedCallback(const std::string &name, + SettingsChangedCallback cbf, void *userdata) { MutexAutoLock lock(m_callback_mutex); SettingsCallbackMap::iterator it_cbks = m_callbacks.find(name); @@ -1025,8 +1094,8 @@ void Settings::deregisterChangedCallback( if (it_cbks != m_callbacks.end()) { SettingsCallbackList &cbks = it_cbks->second; - SettingsCallbackList::iterator position = std::find( - cbks.begin(), cbks.end(), std::make_pair(cbf, userdata)); + SettingsCallbackList::iterator position = + std::find(cbks.begin(), cbks.end(), std::make_pair(cbf, userdata)); if (position != cbks.end()) cbks.erase(position); |