From: Maxim Mamontov Date: Thu, 5 May 2011 09:34:36 +0000 (+0300) Subject: Settings code and header files renamed X-Git-Tag: 2.407-p1~36 X-Git-Url: https://git.stg.codes/stg.git/commitdiff_plain/7043a9a70ea75f584bc65d53ca97b9eb69a6fafc Settings code and header files renamed --- diff --git a/projects/sgauthstress/settings.cpp b/projects/sgauthstress/settings.cpp new file mode 100644 index 00000000..9055ceed --- /dev/null +++ b/projects/sgauthstress/settings.cpp @@ -0,0 +1,190 @@ +/* + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +/* + * Author : Maxim Mamontov + */ + +#include +#include +#include + +#include "stg/dotconfpp.h" +#include "stg/module_settings.h" +#include "stg/common.h" + +#include "settings.h" + +SETTINGS::SETTINGS() + : port(0), + localPort(0), + confFile("/etc/sgauth.conf") +{ +} +//----------------------------------------------------------------------------- +int 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; + } + +return -1; +} +//----------------------------------------------------------------------------- +int ParseInt(const string & value, int * val) +{ +if (str2x(value, *val)) + return -1; + +return 0; +} +//----------------------------------------------------------------------------- +int ParseUnsigned(const string & value, unsigned * val) +{ +if (str2x(value, *val)) + return -1; + +return 0; +} +//----------------------------------------------------------------------------- +int ParseIntInRange(const 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 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!"); + +const DOTCONFDocumentNode * childNode = node->getChildNode(); +while (childNode) + { + PARAM_VALUE pv; + pv.param = childNode->getName(); + int i = 0; + while ((value = childNode->getValue(i++)) != NULL) + pv.value.push_back(value); + params->push_back(pv); + childNode = childNode->getNextNode(); + } + +return 0; +} +//----------------------------------------------------------------------------- +int SETTINGS::ReadSettings() +{ +const char * requiredOptions[] = { + "ModulesPath", + "StoreModule", + "Login", + "Password", + "ServerName", + "ServerPort" + NULL + }; + +DOTCONFDocument conf(DOTCONFDocument::CASEINSENSITIVE); +conf.setRequiredOptionNames(requiredOptions); + +if(conf.setContent(confFile.c_str()) != 0) + { + strError = "Cannot read file " + confFile + "."; + 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); + 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::ReadSettings() - %s\n", strError.c_str()); + return -1; + } + + if (storeModulesCount) + { + strError = "Should be only one source StoreModule."; + printfd(__FILE__, "SETTINGS::ReadSettings() - %s\n", strError.c_str()); + return -1; + } + ++storeModulesCount; + + storeModuleSettings.moduleName = node->getValue(0); + 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(); + } + +return 0; +} diff --git a/projects/sgauthstress/settings.h b/projects/sgauthstress/settings.h new file mode 100644 index 00000000..b933fa74 --- /dev/null +++ b/projects/sgauthstress/settings.h @@ -0,0 +1,66 @@ +/* + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +/* + * Author : Maxim Mamontov + */ + +#ifndef SETTINGS_H +#define SETTINGS_H + +#include + +#include "stg/os_int.h" + +struct MODULE_SETTINGS; +class DOTCONFDocumentNode; + +class SETTINGS { +public: + SETTINGS(); + ~SETTINGS() {} + int Reload() { return 0; } + void SetConfFile(const std::string cf) { confFile = cf; } + int ReadSettings(); + + const std::string & GetStrError() const { return strError; } + + const std::string & GetServerName() const { return serverName; } + uint16_t GetServerPort() const { return port; } + uint16_t GetLocalPort() const { return localPort; } + + const std::string & GetLogin() const { return login; } + const std::string & GetPassword() const { return password; } + + const std::string & GetConfDir() const; + const std::string & GetModulesPath() const { return modulesPath; } + const MODULE_SETTINGS & GetStoreModuleSettings() const { return storeModuleSettings; } + +private: + std::string login; + std::string password; + std::string serverName; + int port; + int localPort; + + std::string confFile; + std::string strError; + std::string modulesPath; + + MODULE_SETTINGS storeModuleSettings; +}; + +#endif diff --git a/projects/sgauthstress/settings_impl.cpp b/projects/sgauthstress/settings_impl.cpp deleted file mode 100644 index 9055ceed..00000000 --- a/projects/sgauthstress/settings_impl.cpp +++ /dev/null @@ -1,190 +0,0 @@ -/* - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - */ - -/* - * Author : Maxim Mamontov - */ - -#include -#include -#include - -#include "stg/dotconfpp.h" -#include "stg/module_settings.h" -#include "stg/common.h" - -#include "settings.h" - -SETTINGS::SETTINGS() - : port(0), - localPort(0), - confFile("/etc/sgauth.conf") -{ -} -//----------------------------------------------------------------------------- -int 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; - } - -return -1; -} -//----------------------------------------------------------------------------- -int ParseInt(const string & value, int * val) -{ -if (str2x(value, *val)) - return -1; - -return 0; -} -//----------------------------------------------------------------------------- -int ParseUnsigned(const string & value, unsigned * val) -{ -if (str2x(value, *val)) - return -1; - -return 0; -} -//----------------------------------------------------------------------------- -int ParseIntInRange(const 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 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!"); - -const DOTCONFDocumentNode * childNode = node->getChildNode(); -while (childNode) - { - PARAM_VALUE pv; - pv.param = childNode->getName(); - int i = 0; - while ((value = childNode->getValue(i++)) != NULL) - pv.value.push_back(value); - params->push_back(pv); - childNode = childNode->getNextNode(); - } - -return 0; -} -//----------------------------------------------------------------------------- -int SETTINGS::ReadSettings() -{ -const char * requiredOptions[] = { - "ModulesPath", - "StoreModule", - "Login", - "Password", - "ServerName", - "ServerPort" - NULL - }; - -DOTCONFDocument conf(DOTCONFDocument::CASEINSENSITIVE); -conf.setRequiredOptionNames(requiredOptions); - -if(conf.setContent(confFile.c_str()) != 0) - { - strError = "Cannot read file " + confFile + "."; - 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); - 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::ReadSettings() - %s\n", strError.c_str()); - return -1; - } - - if (storeModulesCount) - { - strError = "Should be only one source StoreModule."; - printfd(__FILE__, "SETTINGS::ReadSettings() - %s\n", strError.c_str()); - return -1; - } - ++storeModulesCount; - - storeModuleSettings.moduleName = node->getValue(0); - 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(); - } - -return 0; -} diff --git a/projects/sgauthstress/settings_impl.h b/projects/sgauthstress/settings_impl.h deleted file mode 100644 index b933fa74..00000000 --- a/projects/sgauthstress/settings_impl.h +++ /dev/null @@ -1,66 +0,0 @@ -/* - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - */ - -/* - * Author : Maxim Mamontov - */ - -#ifndef SETTINGS_H -#define SETTINGS_H - -#include - -#include "stg/os_int.h" - -struct MODULE_SETTINGS; -class DOTCONFDocumentNode; - -class SETTINGS { -public: - SETTINGS(); - ~SETTINGS() {} - int Reload() { return 0; } - void SetConfFile(const std::string cf) { confFile = cf; } - int ReadSettings(); - - const std::string & GetStrError() const { return strError; } - - const std::string & GetServerName() const { return serverName; } - uint16_t GetServerPort() const { return port; } - uint16_t GetLocalPort() const { return localPort; } - - const std::string & GetLogin() const { return login; } - const std::string & GetPassword() const { return password; } - - const std::string & GetConfDir() const; - const std::string & GetModulesPath() const { return modulesPath; } - const MODULE_SETTINGS & GetStoreModuleSettings() const { return storeModuleSettings; } - -private: - std::string login; - std::string password; - std::string serverName; - int port; - int localPort; - - std::string confFile; - std::string strError; - std::string modulesPath; - - MODULE_SETTINGS storeModuleSettings; -}; - -#endif