From: Maxim Mamontov Date: Thu, 5 May 2011 09:34:10 +0000 (+0300) Subject: Settings code cleaned up X-Git-Tag: 2.407-p1~37 X-Git-Url: https://git.stg.codes/stg.git/commitdiff_plain/8edf5615209b806b4e1aff6bae0db06efc23644b?ds=sidebyside Settings code cleaned up --- diff --git a/projects/sgauthstress/settings_impl.cpp b/projects/sgauthstress/settings_impl.cpp index c203ca33..9055ceed 100644 --- a/projects/sgauthstress/settings_impl.cpp +++ b/projects/sgauthstress/settings_impl.cpp @@ -19,27 +19,23 @@ */ #include +#include +#include #include "stg/dotconfpp.h" #include "stg/module_settings.h" #include "stg/common.h" -#include "settings_impl.h" +#include "settings.h" -SETTINGS_IMPL::SETTINGS_IMPL() +SETTINGS::SETTINGS() : port(0), localPort(0), - listenWebIP(0), - refreshPeriod(0), - daemon(false), - noWeb(false), - reconnect(false), - showPid(false), confFile("/etc/sgauth.conf") { } //----------------------------------------------------------------------------- -int SETTINGS_IMPL::ParseYesNo(const string & value, bool * val) +int ParseYesNo(const string & value, bool * val) { if (0 == strcasecmp(value.c_str(), "yes")) { @@ -52,102 +48,67 @@ if (0 == strcasecmp(value.c_str(), "no")) return 0; } -strError = "Incorrect value \'" + value + "\'."; return -1; } //----------------------------------------------------------------------------- -int SETTINGS_IMPL::ParseInt(const string & value, int * val) +int 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) +int 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) +int 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) +int 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, std::vector * params) +int ParseModuleSettings(const DOTCONFDocumentNode * node, std::vector * params) { -if (!node) - return 0; - -PARAM_VALUE pv; - -pv.param = node->getName(); - -if (node->getValue(1)) - { - strError = "Unexpected value \'" + std::string(node->getValue(1)) + "\'."; - printfd(__FILE__, "SETTINGS_IMPL::ParseModuleSettings() - %s\n", strError.c_str()); - return -1; - } - -const char * value = node->getValue(0); - -if (!value) - { - strError = "Module name expected."; - printfd(__FILE__, "SETTINGS_IMPL::ParseModuleSettings() - %s\n", strError.c_str()); - return -1; - } +assert(node && "DOTCONFDocumentNode must not be NULL!"); const DOTCONFDocumentNode * childNode = node->getChildNode(); while (childNode) { + PARAM_VALUE pv; pv.param = childNode->getName(); int i = 0; - while ((value = childNode->getValue(i)) != NULL) - { + while ((value = childNode->getValue(i++)) != NULL) pv.value.push_back(value); - ++i; - } params->push_back(pv); - pv.value.clear(); childNode = childNode->getNextNode(); } return 0; } //----------------------------------------------------------------------------- -int SETTINGS_IMPL::ReadSettings() +int SETTINGS::ReadSettings() { const char * requiredOptions[] = { "ModulesPath", @@ -158,7 +119,6 @@ const char * requiredOptions[] = { "ServerPort" NULL }; -int storeModulesCount = 0; DOTCONFDocument conf(DOTCONFDocument::CASEINSENSITIVE); conf.setRequiredOptionNames(requiredOptions); @@ -166,85 +126,65 @@ conf.setRequiredOptionNames(requiredOptions); if(conf.setContent(confFile.c_str()) != 0) { strError = "Cannot read file " + confFile + "."; - printfd(__FILE__, "SETTINGS_IMPL::ReadSettings() - %s\n", strError.c_str()); + printfd(__FILE__, "SETTINGS::ReadSettings() - %s\n", strError.c_str()); return -1; } const DOTCONFDocumentNode * node = conf.getFirstNode(); +int storeModulesCount = 0; while (node) { if (strcasecmp(node->getName(), "ModulesPath") == 0) - { modulesPath = node->getValue(0); - } - - if (strcasecmp(node->getName(), "StoreModule") == 0) + else if (strcasecmp(node->getName(), "Login") == 0) + login = node->getValue(0); + else if (strcasecmp(node->getName(), "Password") == 0) + password = node->getValue(0); + else if (strcasecmp(node->getName(), "ServerName") == 0) + serverName = node->getValue(0); + else if (strcasecmp(node->getName(), "ServerPort") == 0) + if (ParseIntInRange(node->getValue(0), 1, 65535, &port)) + { + strError = "Parameter 'ServerPort' is not valid."; + printfd(__FILE__, "SETTINGS::ReadSettings() - %s\n", strError.c_str()); + return -1; + } + else if (strcasecmp(node->getName(), "LocalPort") == 0) + if (ParseIntInRange(node->getValue(0), 0, 65535, &localPort)) + { + strError = "Parameter 'LocalPort' is not valid."; + printfd(__FILE__, "SETTINGS::ReadSettings() - %s\n", strError.c_str()); + return -1; + } + else if (strcasecmp(node->getName(), "StoreModule") == 0) { if (node->getValue(1)) { strError = "Unexpected \'" + std::string(node->getValue(1)) + "\'."; - printfd(__FILE__, "SETTINGS_IMPL::ReadSettings() - %s\n", strError.c_str()); + printfd(__FILE__, "SETTINGS::ReadSettings() - %s\n", strError.c_str()); return -1; } if (storeModulesCount) { strError = "Should be only one source StoreModule."; - printfd(__FILE__, "SETTINGS_IMPL::ReadSettings() - %s\n", strError.c_str()); + printfd(__FILE__, "SETTINGS::ReadSettings() - %s\n", strError.c_str()); return -1; } ++storeModulesCount; storeModuleSettings.moduleName = node->getValue(0); - ParseModuleSettings(node, &storeModuleSettings.moduleParams); + if (ParseModuleSettings(node, &storeModuleSettings.moduleParams)) + { + strError = "Failed to parse store module settings"; + printfd(__FILE__, "SETTINGS::ReadSettings() - %s\n", strError.c_str()); + return -1; + } } node = node->getNextNode(); } -CONFIGFILE cf(confFile); - -if (cf.Error()) - { - strError = "Cannot read file '" + confFile + "'"; - return -1; - } - -cf.ReadString("Login", &login, "/?--?--?*"); -if (login == "/?--?--?*") - { - strError = "Parameter 'Login' not found."; - return -1; - } - -cf.ReadString("Password", &password, "/?--?--?*"); -if (login == "/?--?--?*") - { - strError = "Parameter 'Password' not found."; - return -1; - } - -cf.ReadString("ServerName", &serverName, "?*?*?"); -if (serverName == "?*?*?") - { - strError = "Parameter 'ServerName' not found."; - return -1; - } - -cf.ReadString("ServerPort", &temp, "5555"); -if (ParseIntInRange(temp, 1, 65535, &port)) - { - strError = "Parameter 'ServerPort' is not valid."; - return -1; - } - -cf.ReadString("LocalPort", &temp, "0"); -if (ParseIntInRange(temp, 0, 65535, &localPort)) - { - strError = "Parameter 'LocalPort' is not valid."; - return -1; - } - return 0; } diff --git a/projects/sgauthstress/settings_impl.h b/projects/sgauthstress/settings_impl.h index 8035aba4..b933fa74 100644 --- a/projects/sgauthstress/settings_impl.h +++ b/projects/sgauthstress/settings_impl.h @@ -18,21 +18,20 @@ * Author : Maxim Mamontov */ -#ifndef SETTINGS_IMPL_H -#define SETTINGS_IMPL_H +#ifndef SETTINGS_H +#define SETTINGS_H #include -#include #include "stg/os_int.h" struct MODULE_SETTINGS; class DOTCONFDocumentNode; -class SETTINGS_IMPL { +class SETTINGS { public: - SETTINGS_IMPL(); - ~SETTINGS_IMPL() {} + SETTINGS(); + ~SETTINGS() {} int Reload() { return 0; } void SetConfFile(const std::string cf) { confFile = cf; } int ReadSettings(); @@ -51,13 +50,6 @@ public: const MODULE_SETTINGS & GetStoreModuleSettings() const { return storeModuleSettings; } 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 ParseModuleSettings(const DOTCONFDocumentNode * dirNameNode, std::vector * params); - std::string login; std::string password; std::string serverName;