From: Maxim Mamontov Date: Mon, 25 Jul 2011 19:20:23 +0000 (+0300) Subject: Code deduplication X-Git-Tag: 2.408-alpha~78 X-Git-Url: https://git.stg.codes/stg.git/commitdiff_plain/3a45cd9275dc9279e133deb0932402ae5f4d0b5f?ds=sidebyside;hp=8f0e4a34b2dd1c54f80d953f7337ac89047a2654 Code deduplication --- diff --git a/projects/sgauth/settings_impl.cpp b/projects/sgauth/settings_impl.cpp index 44088d1f..f8536420 100644 --- a/projects/sgauth/settings_impl.cpp +++ b/projects/sgauth/settings_impl.cpp @@ -38,71 +38,6 @@ SETTINGS_IMPL::SETTINGS_IMPL() { } //----------------------------------------------------------------------------- -int SETTINGS_IMPL::ParseYesNo(const string & value, bool * val) -{ -if (0 == strcasecmp(value.c_str(), "yes")) - { - *val = true; - return 0; - } -if (0 == strcasecmp(value.c_str(), "no")) - { - *val = false; - return 0; - } - -strError = "Incorrect value \'" + value + "\'."; -return -1; -} -//----------------------------------------------------------------------------- -int SETTINGS_IMPL::ParseInt(const string & value, int * val) -{ -if (str2x(value, *val)) - { - strError = "Cannot convert \'" + value + "\' to integer."; - return -1; - } -return 0; -} -//----------------------------------------------------------------------------- -int SETTINGS_IMPL::ParseUnsigned(const string & value, unsigned * val) -{ -if (str2x(value, *val)) - { - strError = "Cannot convert \'" + value + "\' to unsigned integer."; - return -1; - } -return 0; -} -//----------------------------------------------------------------------------- -int SETTINGS_IMPL::ParseIntInRange(const string & value, int min, int max, int * val) -{ -if (ParseInt(value, val) != 0) - return -1; - -if (*val < min || *val > max) - { - strError = "Value \'" + value + "\' out of range."; - return -1; - } - -return 0; -} -//----------------------------------------------------------------------------- -int SETTINGS_IMPL::ParseUnsignedInRange(const string & value, unsigned min, unsigned max, unsigned * val) -{ -if (ParseUnsigned(value, val) != 0) - return -1; - -if (*val < min || *val > max) - { - strError = "Value \'" + value + "\' out of range."; - return -1; - } - -return 0; -} -//----------------------------------------------------------------------------- int SETTINGS_IMPL::ReadSettings() { CONFIGFILE cf(confFile); diff --git a/projects/sgauth/settings_impl.h b/projects/sgauth/settings_impl.h index b334dac9..2de52577 100644 --- a/projects/sgauth/settings_impl.h +++ b/projects/sgauth/settings_impl.h @@ -67,12 +67,6 @@ private: std::string confFile; std::string strError; - - int ParseInt(const std::string & value, int * val); - int ParseUnsigned(const std::string & value, unsigned * val); - int ParseIntInRange(const std::string & value, int min, int max, int * val); - int ParseUnsignedInRange(const std::string & value, unsigned min, unsigned max, unsigned * val); - int ParseYesNo(const std::string & value, bool * val); }; #endif diff --git a/projects/sgauthstress/settings.cpp b/projects/sgauthstress/settings.cpp index 1e1cf1a0..62adc143 100644 --- a/projects/sgauthstress/settings.cpp +++ b/projects/sgauthstress/settings.cpp @@ -35,60 +35,6 @@ SETTINGS::SETTINGS() { } //----------------------------------------------------------------------------- -int ParseYesNo(const std::string & value, bool * val) -{ -if (0 == strcasecmp(value.c_str(), "yes")) - { - *val = true; - return 0; - } -if (0 == strcasecmp(value.c_str(), "no")) - { - *val = false; - return 0; - } - -return -1; -} -//----------------------------------------------------------------------------- -int ParseInt(const std::string & value, int * val) -{ -if (str2x(value, *val)) - return -1; - -return 0; -} -//----------------------------------------------------------------------------- -int ParseUnsigned(const std::string & value, unsigned * val) -{ -if (str2x(value, *val)) - return -1; - -return 0; -} -//----------------------------------------------------------------------------- -int ParseIntInRange(const std::string & value, int min, int max, int * val) -{ -if (ParseInt(value, val) != 0) - return -1; - -if (*val < min || *val > max) - return -1; - -return 0; -} -//----------------------------------------------------------------------------- -int ParseUnsignedInRange(const std::string & value, unsigned min, unsigned max, unsigned * val) -{ -if (ParseUnsigned(value, val) != 0) - return -1; - -if (*val < min || *val > max) - return -1; - -return 0; -} -//----------------------------------------------------------------------------- int ParseModuleSettings(const DOTCONFDocumentNode * node, std::vector * params) { assert(node && "DOTCONFDocumentNode must not be NULL!"); diff --git a/projects/stargazer/plugins/authorization/inetaccess/inetaccess.cpp b/projects/stargazer/plugins/authorization/inetaccess/inetaccess.cpp index 439bee18..33c23bce 100644 --- a/projects/stargazer/plugins/authorization/inetaccess/inetaccess.cpp +++ b/projects/stargazer/plugins/authorization/inetaccess/inetaccess.cpp @@ -95,21 +95,6 @@ AUTH_IA_SETTINGS::AUTH_IA_SETTINGS() { } //----------------------------------------------------------------------------- -int AUTH_IA_SETTINGS::ParseIntInRange(const string & str, int min, int max, int * val) -{ -if (str2x(str.c_str(), *val)) - { - errorStr = "Incorrect value \'" + str + "\'."; - return -1; - } -if (*val < min || *val > max) - { - errorStr = "Value \'" + str + "\' out of range."; - return -1; - } -return 0; -} -//----------------------------------------------------------------------------- int AUTH_IA_SETTINGS::ParseSettings(const MODULE_SETTINGS & s) { int p; diff --git a/projects/stargazer/plugins/authorization/inetaccess/inetaccess.h b/projects/stargazer/plugins/authorization/inetaccess/inetaccess.h index e16c0257..fe556495 100644 --- a/projects/stargazer/plugins/authorization/inetaccess/inetaccess.h +++ b/projects/stargazer/plugins/authorization/inetaccess/inetaccess.h @@ -178,7 +178,6 @@ public: FREEMB GetFreeMbShowType() const { return freeMbShowType; }; private: - int ParseIntInRange(const std::string & str, int min, int max, int * val); int userDelay; int userTimeout; uint16_t port; diff --git a/projects/stargazer/plugins/authorization/stress/stress.cpp b/projects/stargazer/plugins/authorization/stress/stress.cpp index 756e6e1a..481444b5 100644 --- a/projects/stargazer/plugins/authorization/stress/stress.cpp +++ b/projects/stargazer/plugins/authorization/stress/stress.cpp @@ -91,21 +91,6 @@ AUTH_STRESS_SETTINGS::AUTH_STRESS_SETTINGS() { } //----------------------------------------------------------------------------- -int AUTH_STRESS_SETTINGS::ParseIntInRange(const string & str, int min, int max, int * val) -{ -if (str2x(str.c_str(), *val)) - { - errorStr = "Incorrect value \'" + str + "\'."; - return -1; - } -if (*val < min || *val > max) - { - errorStr = "Value \'" + str + "\' out of range."; - return -1; - } -return 0; -} -//----------------------------------------------------------------------------- int AUTH_STRESS_SETTINGS::ParseSettings(const MODULE_SETTINGS & s) { PARAM_VALUE pv; diff --git a/projects/stargazer/plugins/authorization/stress/stress.h b/projects/stargazer/plugins/authorization/stress/stress.h index 85eb75be..bfcf56d0 100644 --- a/projects/stargazer/plugins/authorization/stress/stress.h +++ b/projects/stargazer/plugins/authorization/stress/stress.h @@ -76,7 +76,6 @@ public: int ParseSettings(const MODULE_SETTINGS & s); int GetAverageOnlineTime() const; private: - int ParseIntInRange(const std::string & str, int min, int max, int * val); int averageOnlineTime; std::string errorStr; }; diff --git a/projects/stargazer/plugins/capture/divert_freebsd/divert_cap.cpp b/projects/stargazer/plugins/capture/divert_freebsd/divert_cap.cpp index f6e7731a..61090de7 100644 --- a/projects/stargazer/plugins/capture/divert_freebsd/divert_cap.cpp +++ b/projects/stargazer/plugins/capture/divert_freebsd/divert_cap.cpp @@ -300,18 +300,3 @@ port = p; return 0; } //----------------------------------------------------------------------------- -int DIVERT_CAP::ParseIntInRange(const std::string & str, int min, int max, int * val) -{ -if (str2x(str.c_str(), *val)) - { - errorStr = "Incorrect value \'" + str + "\'."; - return -1; - } -if (*val < min || *val > max) - { - errorStr = "Value \'" + str + "\' out of range."; - return -1; - } -return 0; -} -//----------------------------------------------------------------------------- diff --git a/projects/stargazer/plugins/capture/divert_freebsd/divert_cap.h b/projects/stargazer/plugins/capture/divert_freebsd/divert_cap.h index 645610eb..f5bb432b 100644 --- a/projects/stargazer/plugins/capture/divert_freebsd/divert_cap.h +++ b/projects/stargazer/plugins/capture/divert_freebsd/divert_cap.h @@ -76,8 +76,6 @@ private: int DivertCapRead(char * buffer, int blen, char ** iface, int n); int DivertCapClose(); - int ParseIntInRange(const std::string & str, int min, int max, int * val); - MODULE_SETTINGS settings; int port; diff --git a/projects/stargazer/plugins/configuration/rpcconfig/rpcconfig.cpp b/projects/stargazer/plugins/configuration/rpcconfig/rpcconfig.cpp index c2c544fd..8afbd9cd 100644 --- a/projects/stargazer/plugins/configuration/rpcconfig/rpcconfig.cpp +++ b/projects/stargazer/plugins/configuration/rpcconfig/rpcconfig.cpp @@ -47,24 +47,6 @@ RPC_CONFIG_SETTINGS::RPC_CONFIG_SETTINGS() { } -int RPC_CONFIG_SETTINGS::ParseIntInRange(const std::string & str, - int min, - int max, - int * val) -{ -if (str2x(str.c_str(), *val)) - { - errorStr = "Incorrect value \'" + str + "\'."; - return -1; - } -if (*val < min || *val > max) - { - errorStr = "Value \'" + str + "\' out of range."; - return -1; - } -return 0; -} - int RPC_CONFIG_SETTINGS::ParseSettings(const MODULE_SETTINGS & s) { int p; diff --git a/projects/stargazer/plugins/configuration/rpcconfig/rpcconfig.h b/projects/stargazer/plugins/configuration/rpcconfig/rpcconfig.h index da11d467..1213f7d3 100644 --- a/projects/stargazer/plugins/configuration/rpcconfig/rpcconfig.h +++ b/projects/stargazer/plugins/configuration/rpcconfig/rpcconfig.h @@ -36,10 +36,6 @@ public: uint16_t GetPort() const { return port; }; double GetCookieTimeout() const { return cookieTimeout; }; private: - int ParseIntInRange(const std::string & str, - int min, - int max, - int * val); std::string errorStr; int port; double cookieTimeout; diff --git a/projects/stargazer/plugins/configuration/sgconfig/stgconfig.cpp b/projects/stargazer/plugins/configuration/sgconfig/stgconfig.cpp index 9ffbc949..06e915c3 100644 --- a/projects/stargazer/plugins/configuration/sgconfig/stgconfig.cpp +++ b/projects/stargazer/plugins/configuration/sgconfig/stgconfig.cpp @@ -46,21 +46,6 @@ const std::string & STG_CONFIG_SETTINGS::GetStrError() const return errorStr; } //----------------------------------------------------------------------------- -int STG_CONFIG_SETTINGS::ParseIntInRange(const std::string & str, int min, int max, int * val) -{ -if (str2x(str.c_str(), *val)) - { - errorStr = "Incorrect value \'" + str + "\'."; - return -1; - } -if (*val < min || *val > max) - { - errorStr = "Value \'" + str + "\' out of range."; - return -1; - } -return 0; -} -//----------------------------------------------------------------------------- int STG_CONFIG_SETTINGS::ParseSettings(const MODULE_SETTINGS & s) { int p; diff --git a/projects/stargazer/plugins/configuration/sgconfig/stgconfig.h b/projects/stargazer/plugins/configuration/sgconfig/stgconfig.h index 7d490aef..efa57d45 100644 --- a/projects/stargazer/plugins/configuration/sgconfig/stgconfig.h +++ b/projects/stargazer/plugins/configuration/sgconfig/stgconfig.h @@ -21,7 +21,6 @@ public: int ParseSettings(const MODULE_SETTINGS & s); uint16_t GetPort() const; private: - int ParseIntInRange(const std::string & str, int min, int max, int * val); std::string errorStr; int port; }; diff --git a/projects/stargazer/plugins/configuration/sgconfig2/stgconfig.cpp b/projects/stargazer/plugins/configuration/sgconfig2/stgconfig.cpp index cec02d41..d83942dc 100644 --- a/projects/stargazer/plugins/configuration/sgconfig2/stgconfig.cpp +++ b/projects/stargazer/plugins/configuration/sgconfig2/stgconfig.cpp @@ -47,21 +47,6 @@ const string& STG_CONFIG_SETTINGS::GetStrError() const return errorStr; } -int STG_CONFIG_SETTINGS::ParseIntInRange(const string & str, int min, int max, int * val) -{ -if (str2x(str.c_str(), *val)) - { - errorStr = "Incorrect value \'" + str + "\'."; - return -1; - } -if (*val < min || *val > max) - { - errorStr = "Value \'" + str + "\' out of range."; - return -1; - } -return 0; -} - int STG_CONFIG_SETTINGS::ParseSettings(const MODULE_SETTINGS & s) { int p; diff --git a/projects/stargazer/plugins/configuration/sgconfig2/stgconfig.h b/projects/stargazer/plugins/configuration/sgconfig2/stgconfig.h index cf9d72b0..1c8743d1 100644 --- a/projects/stargazer/plugins/configuration/sgconfig2/stgconfig.h +++ b/projects/stargazer/plugins/configuration/sgconfig2/stgconfig.h @@ -22,7 +22,6 @@ public: int ParseSettings(const MODULE_SETTINGS & s); uint16_t GetPort(); private: - int ParseIntInRange(const string & str, int min, int max, int * val); string errorStr; int port; }; diff --git a/projects/stargazer/plugins/configuration/xrconfig/xrconfig.cpp b/projects/stargazer/plugins/configuration/xrconfig/xrconfig.cpp index cb156eed..856ae886 100644 --- a/projects/stargazer/plugins/configuration/xrconfig/xrconfig.cpp +++ b/projects/stargazer/plugins/configuration/xrconfig/xrconfig.cpp @@ -44,41 +44,8 @@ const string & XR_CONFIG_SETTINGS::GetStrError() const return errorStr; } //----------------------------------------------------------------------------- -int XR_CONFIG_SETTINGS::ParseIntInRange(const string & str, int min, int max, int * val) -{ -if (strtoi2(str.c_str(), *val)) - { - errorStr = "Incorrect value \'" + str + "\'."; - return -1; - } -if (*val < min || *val > max) - { - errorStr = "Value \'" + str + "\' out of range."; - return -1; - } -return 0; -} -//----------------------------------------------------------------------------- int XR_CONFIG_SETTINGS::ParseSettings(const MODULE_SETTINGS & s) { -/*int p; -PARAM_VALUE pv; -vector::const_iterator pvi; - -pv.param = "Port"; -pvi = find(s.moduleParams.begin(), s.moduleParams.end(), pv); -if (pvi == s.moduleParams.end()) - { - errorStr = "Parameter \'Port\' not found."; - return -1; - } - -if (ParseIntInRange(pvi->value[0], 2, 65535, &p)) - { - errorStr = "Cannot parse parameter \'Port\': " + errorStr; - return -1; - } -port = p;*/ return 0; } diff --git a/projects/stargazer/plugins/configuration/xrconfig/xrconfig.h b/projects/stargazer/plugins/configuration/xrconfig/xrconfig.h index 79e099f1..4630edb4 100644 --- a/projects/stargazer/plugins/configuration/xrconfig/xrconfig.h +++ b/projects/stargazer/plugins/configuration/xrconfig/xrconfig.h @@ -1,9 +1,7 @@ #include #include #include "base_plugin.h" -//#include "common_settings.h" #include "common.h" -//#include "configproto.h" using namespace std; @@ -22,7 +20,6 @@ public: uint16_t GetPort(); private: - int ParseIntInRange(const string & str, int min, int max, int * val); string errorStr; int port; }; diff --git a/projects/stargazer/plugins/other/ping/ping.cpp b/projects/stargazer/plugins/other/ping/ping.cpp index 0fb5d30b..ec137044 100644 --- a/projects/stargazer/plugins/other/ping/ping.cpp +++ b/projects/stargazer/plugins/other/ping/ping.cpp @@ -87,21 +87,6 @@ if (ParseIntInRange(pvi->value[0], 5, 3600, &pingDelay)) return 0; } //----------------------------------------------------------------------------- -int PING_SETTINGS::ParseIntInRange(const std::string & str, int min, int max, int * val) -{ -if (str2x(str.c_str(), *val)) - { - errorStr = "Incorrect value \'" + str + "\'."; - return -1; - } -if (*val < min || *val > max) - { - errorStr = "Value \'" + str + "\' out of range."; - return -1; - } -return 0; -} -//----------------------------------------------------------------------------- PING::PING() : users(NULL), nonstop(false), diff --git a/projects/stargazer/plugins/other/ping/ping.h b/projects/stargazer/plugins/other/ping/ping.h index 8dcbe202..aecbfb15 100644 --- a/projects/stargazer/plugins/other/ping/ping.h +++ b/projects/stargazer/plugins/other/ping/ping.h @@ -76,8 +76,6 @@ public: int ParseSettings(const MODULE_SETTINGS & s); int GetPingDelay() const { return pingDelay; } private: - int ParseIntInRange(const std::string & str, int min, int max, int * val); - int pingDelay; mutable std::string errorStr; }; diff --git a/projects/stargazer/plugins/other/radius/radius.cpp b/projects/stargazer/plugins/other/radius/radius.cpp index 937d853c..afcc46fd 100644 --- a/projects/stargazer/plugins/other/radius/radius.cpp +++ b/projects/stargazer/plugins/other/radius/radius.cpp @@ -78,21 +78,6 @@ return radc.GetPlugin(); //----------------------------------------------------------------------------- //----------------------------------------------------------------------------- //----------------------------------------------------------------------------- -int RAD_SETTINGS::ParseIntInRange(const std::string & str, int min, int max, int * val) -{ -if (str2x(str.c_str(), *val)) - { - errorStr = "Incorrect value \'" + str + "\'."; - return -1; - } -if (*val < min || *val > max) - { - errorStr = "Value \'" + str + "\' out of range."; - return -1; - } -return 0; -} -//----------------------------------------------------------------------------- int RAD_SETTINGS::ParseServices(const std::vector & str, std::list * lst) { std::copy(str.begin(), str.end(), std::back_inserter(*lst)); diff --git a/projects/stargazer/plugins/other/radius/radius.h b/projects/stargazer/plugins/other/radius/radius.h index 1c32bd0b..70c904ae 100644 --- a/projects/stargazer/plugins/other/radius/radius.h +++ b/projects/stargazer/plugins/other/radius/radius.h @@ -66,7 +66,6 @@ public: const std::list & GetAcctServices() const { return acctServices; } private: - int ParseIntInRange(const std::string & str, int min, int max, int * val); int ParseServices(const std::vector & str, std::list * lst); uint16_t port; diff --git a/projects/stargazer/plugins/other/rscript/rscript.cpp b/projects/stargazer/plugins/other/rscript/rscript.cpp index 410e7aa8..1c2e97bb 100644 --- a/projects/stargazer/plugins/other/rscript/rscript.cpp +++ b/projects/stargazer/plugins/other/rscript/rscript.cpp @@ -101,21 +101,6 @@ RS_SETTINGS::RS_SETTINGS() { } //----------------------------------------------------------------------------- -int RS_SETTINGS::ParseIntInRange(const string & str, int min, int max, int * val) -{ -if (str2x(str.c_str(), *val)) - { - errorStr = "Incorrect value \'" + str + "\'."; - return -1; - } -if (*val < min || *val > max) - { - errorStr = "Value \'" + str + "\' out of range."; - return -1; - } -return 0; -} -//----------------------------------------------------------------------------- int RS_SETTINGS::ParseSettings(const MODULE_SETTINGS & s) { int p; diff --git a/projects/stargazer/plugins/other/rscript/rscript.h b/projects/stargazer/plugins/other/rscript/rscript.h index 81cc1e17..24d44763 100644 --- a/projects/stargazer/plugins/other/rscript/rscript.h +++ b/projects/stargazer/plugins/other/rscript/rscript.h @@ -114,7 +114,6 @@ public: const std::string & GetMapFileName() const { return subnetFile; } private: - int ParseIntInRange(const std::string & str, int min, int max, int * val); int sendPeriod; uint16_t port; string errorStr; diff --git a/projects/stargazer/plugins/other/smux/smux.cpp b/projects/stargazer/plugins/other/smux/smux.cpp index 14f47da3..0cf56ff8 100644 --- a/projects/stargazer/plugins/other/smux/smux.cpp +++ b/projects/stargazer/plugins/other/smux/smux.cpp @@ -34,22 +34,6 @@ PLUGIN * GetPlugin() return sac.GetPlugin(); } -int ParseIntInRange(const std::string & str, - int min, - int max, - int * val) -{ -if (str2x(str.c_str(), *val)) - { - return -1; - } -if (*val < min || *val > max) - { - return -1; - } -return 0; -} - SMUX_SETTINGS::SMUX_SETTINGS() : ip(0), port(0) diff --git a/projects/stargazer/settings_impl.cpp b/projects/stargazer/settings_impl.cpp index d197efc4..2bf8d082 100644 --- a/projects/stargazer/settings_impl.cpp +++ b/projects/stargazer/settings_impl.cpp @@ -138,71 +138,6 @@ SETTINGS_IMPL::SETTINGS_IMPL(const SETTINGS_IMPL & rval) { } //----------------------------------------------------------------------------- -int SETTINGS_IMPL::ParseYesNo(const string & value, bool * val) -{ -if (0 == strcasecmp(value.c_str(), "yes")) - { - *val = true; - return 0; - } -if (0 == strcasecmp(value.c_str(), "no")) - { - *val = false; - return 0; - } - -strError = "Incorrect value \'" + value + "\'."; -return -1; -} -//----------------------------------------------------------------------------- -int SETTINGS_IMPL::ParseInt(const string & value, int * val) -{ -if (str2x(value, *val)) - { - strError = "Cannot convert \'" + value + "\' to integer."; - return -1; - } -return 0; -} -//----------------------------------------------------------------------------- -int SETTINGS_IMPL::ParseUnsigned(const string & value, unsigned * val) -{ -if (str2x(value, *val)) - { - strError = "Cannot convert \'" + value + "\' to unsigned integer."; - return -1; - } -return 0; -} -//----------------------------------------------------------------------------- -int SETTINGS_IMPL::ParseIntInRange(const string & value, int min, int max, int * val) -{ -if (ParseInt(value, val) != 0) - return -1; - -if (*val < min || *val > max) - { - strError = "Value \'" + value + "\' out of range."; - return -1; - } - -return 0; -} -//----------------------------------------------------------------------------- -int SETTINGS_IMPL::ParseUnsignedInRange(const string & value, unsigned min, unsigned max, unsigned * val) -{ -if (ParseUnsigned(value, val) != 0) - return -1; - -if (*val < min || *val > max) - { - strError = "Value \'" + value + "\' out of range."; - return -1; - } - -return 0; -} -//----------------------------------------------------------------------------- int SETTINGS_IMPL::ParseModuleSettings(const DOTCONFDocumentNode * node, vector * params) { const DOTCONFDocumentNode * childNode; diff --git a/projects/stargazer/settings_impl.h b/projects/stargazer/settings_impl.h index 43dcab0c..4ab5f31e 100644 --- a/projects/stargazer/settings_impl.h +++ b/projects/stargazer/settings_impl.h @@ -101,13 +101,7 @@ public: private: - int ParseInt(const std::string & value, int * val); - int ParseUnsigned(const std::string & value, unsigned * val); - int ParseIntInRange(const std::string & value, int min, int max, int * val); - int ParseUnsignedInRange(const std::string & value, unsigned min, unsigned max, unsigned * val); - int ParseYesNo(const std::string & value, bool * val); - int ParseDetailStatWritePeriod(const std::string & detailStatPeriodStr); - + int ParseDetailStatWritePeriod(const std::string & str); int ParseModuleSettings(const DOTCONFDocumentNode * dirNameNode, std::vector * params); static void ErrorCallback(void * data, const char * buf); diff --git a/stglibs/common.lib/common.cpp b/stglibs/common.lib/common.cpp index d0f1ff4d..606f057a 100644 --- a/stglibs/common.lib/common.cpp +++ b/stglibs/common.lib/common.cpp @@ -978,4 +978,59 @@ delete[] inBuf; return dst; } -//--------------------------------------------------------------------------- + +int ParseYesNo(const std::string & str, bool * val) +{ +if (0 == strncasecmp(str.c_str(), "yes", 3)) + { + *val = true; + return 0; + } + +if (0 == strncasecmp(str.c_str(), "no", 2)) + { + *val = false; + return 0; + } + +return -1; +} + +inline +int ParseInt(const std::string & str, int * val) +{ +if (str2x(str, *val)) + return -1; +return 0; +} + +inline +int ParseUnsigned(const string & str, unsigned * val) +{ +if (str2x(str, *val)) + return -1; +return 0; +} + +int ParseIntInRange(const string & str, int min, int max, int * val) +{ +if (ParseInt(str, val) != 0) + return -1; + +if (*val < min || *val > max) + return -1; + +return 0; +} + +int ParseUnsignedInRange(const string & str, unsigned min, + unsigned max, unsigned * val) +{ +if (ParseUnsigned(str, val) != 0) + return -1; + +if (*val < min || *val > max) + return -1; + +return 0; +} diff --git a/stglibs/common.lib/common.h b/stglibs/common.lib/common.h index 3752822a..19a8773e 100644 --- a/stglibs/common.lib/common.h +++ b/stglibs/common.lib/common.h @@ -98,6 +98,13 @@ std::string & Trim(std::string & val); std::string IconvString(const std::string & source, const std::string & from, const std::string & to); +int ParseInt(const std::string & str, int * val); +int ParseUnsigned(const std::string & str, unsigned * val); +int ParseIntInRange(const std::string & str, int min, int max, int * val); +int ParseUnsignedInRange(const std::string & str, unsigned min, + unsigned max, unsigned * val); +int ParseYesNo(const std::string & str, bool * val); + //----------------------------------------------------------------------------- template int str2x(const std::string & str, varT & x)