From e22383c6f4f4c5f16e40e04db0f14ad4b357a142 Mon Sep 17 00:00:00 2001 From: Peter Powell Date: Fri, 29 Jun 2018 11:26:51 +0100 Subject: Add a ConfigTag::getString overload that calls a validation method. --- include/configreader.h | 2 ++ src/configparser.cpp | 15 +++++++++++++++ 2 files changed, 17 insertions(+) diff --git a/include/configreader.h b/include/configreader.h index a82420b4e..b603f2e10 100644 --- a/include/configreader.h +++ b/include/configreader.h @@ -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& 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 */ diff --git a/src/configparser.cpp b/src/configparser.cpp index 8a3042eba..627cd78e1 100644 --- a/src/configparser.cpp +++ b/src/configparser.cpp @@ -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& 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; -- cgit v1.2.3