From: Maxim Mamontov Date: Thu, 7 Apr 2011 13:06:07 +0000 (+0300) Subject: SETTINGS definition and implementation extracted from main.cpp to X-Git-Tag: 2.407-rc3~65 X-Git-Url: https://git.stg.codes/stg.git/commitdiff_plain/45f7304806cbe47a5235c43e25e54f2a043bc42b?ds=sidebyside SETTINGS definition and implementation extracted from main.cpp to separate files --- diff --git a/projects/sgauth/settings_impl.cpp b/projects/sgauth/settings_impl.cpp new file mode 100644 index 00000000..bcb56170 --- /dev/null +++ b/projects/sgauth/settings_impl.cpp @@ -0,0 +1,211 @@ +/* + * 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 "settings_impl.h" +#include "common.h" +#include "conffiles.h" + +SETTINGS_IMPL::SETTINGS_IMPL() + : 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) +{ +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); + +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; + } + +std::string temp; +cf.ReadString("ListenWebIP", &temp, "127.0.0.1"); +listenWebIP = inet_strington(temp); +if (listenWebIP == 0) + { + strError = "Parameter 'ListenWebIP' is not valid."; + 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; + } + +cf.ReadString("RefreshPeriod", &temp, "5"); +if (ParseIntInRange(temp, 1, 24*3600, &refreshPeriod)) + { + strError = "Parameter 'RefreshPeriod' is not valid."; + return -1; + } + +cf.ReadString("Reconnect", &temp, "yes"); +if (ParseYesNo(temp, &reconnect)) + { + strError = "Parameter 'Reconnect' is not valid."; + return -1; + } + +cf.ReadString("Daemon", &temp, "yes"); +if (ParseYesNo(temp, &daemon)) + { + strError = "Parameter 'Daemon' is not valid."; + return -1; + } + +cf.ReadString("ShowPid", &temp, "no"); +if (ParseYesNo(temp, &showPid)) + { + strError = "Parameter 'ShowPid' is not valid."; + return -1; + } + +cf.ReadString("DisableWeb", &temp, "no"); +if (ParseYesNo(temp, &noWeb)) + { + strError = "Parameter 'DisableWeb' is not valid."; + return -1; + } + +return 0; +} +//----------------------------------------------------------------------------- +void SETTINGS_IMPL::Print() const +{ +std::cout << "Login = " << login << "\n" + << "Password = " << password << "\n" + << "Ip = " << serverName << "\n" + << "Port = " << port << "\n" + << "LocalPort = " << localPort << "\n" + << "ListenWebIP = " << inet_ntostring(listenWebIP) << "\n" + << "RefreshPeriod = " << refreshPeriod << "\n" + << "Daemon = " << daemon << "\n" + << "DisableWeb = " << noWeb << "\n" + << "Reconnect = " << reconnect << "\n" + << "ShowPid = " << showPid << std::endl; +} diff --git a/projects/sgauth/settings_impl.h b/projects/sgauth/settings_impl.h new file mode 100644 index 00000000..74b70d39 --- /dev/null +++ b/projects/sgauth/settings_impl.h @@ -0,0 +1,78 @@ +/* + * 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_IMPL_H +#define SETTINGS_IMPL_H + +#include + +#include "os_int.h" + +class SETTINGS_IMPL { +public: + SETTINGS_IMPL(); + ~SETTINGS_IMPL() {} + 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; } + + bool GetDaemon() const { return daemon; } + bool GetShowPid() const { return showPid; } + bool GetNoWeb() const { return noWeb; } + bool GetReconnect() const { return reconnect; } + int GetRefreshPeriod() const { return refreshPeriod; } + uint32_t GetListenWebIP() const { return listenWebIP; } + + void Print() const; + +private: + std::string login; + std::string password; + std::string serverName; + int port; + int localPort; + uint32_t listenWebIP; + int refreshPeriod; + + bool daemon; + bool noWeb; + bool reconnect; + bool showPid; + + 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