Add a ConfigTag::getString overload that calls a validation method.

This commit is contained in:
Peter Powell 2018-06-29 11:26:51 +01:00
parent 39b51a7c11
commit e22383c6f4
2 changed files with 17 additions and 0 deletions

View File

@ -42,6 +42,8 @@ class CoreExport ConfigTag : public refcountbase
const std::string src_name;
const int src_line;
/** Get the value of an option, using def if it does not exist */
std::string getString(const std::string& key, const std::string& def, const TR1NS::function<bool(const std::string&)>& validator);
/** Get the value of an option, using def if it does not exist */
std::string getString(const std::string& key, const std::string& def = "", size_t minlen = 0, size_t maxlen = UINT32_MAX);
/** Get the value of an option, using def if it does not exist */

View File

@ -402,6 +402,21 @@ bool ConfigTag::readString(const std::string& key, std::string& value, bool allo
return false;
}
std::string ConfigTag::getString(const std::string& key, const std::string& def, const TR1NS::function<bool(const std::string&)>& validator)
{
std::string res;
if (!readString(key, res))
return def;
if (!validator(res))
{
ServerInstance->Logs->Log("CONFIG", LOG_DEFAULT, "WARNING: The value of <%s:%s> is not valid; value set to %s.",
tag.c_str(), key.c_str(), def.c_str());
return def;
}
return res;
}
std::string ConfigTag::getString(const std::string& key, const std::string& def, size_t minlen, size_t maxlen)
{
std::string res;