]> git.stg.codes - stg.git/blobdiff - projects/sgauthstress/settings_impl.cpp
Settings code cleaned up
[stg.git] / projects / sgauthstress / settings_impl.cpp
index c203ca33ef25f3ab617ad0855aa24a042ab0e333..9055ceed24df59e9b11190f92a79ca2782653ed1 100644 (file)
  */
 
 #include <cstring>
+#include <cassert>
+#include <vector>
 
 #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<int>(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<unsigned>(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<PARAM_VALUE> * params)
+int ParseModuleSettings(const DOTCONFDocumentNode * node, std::vector<PARAM_VALUE> * 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;
 }