]> git.stg.codes - stg.git/blob - projects/sgauthstress/settings.cpp
Fix email.
[stg.git] / projects / sgauthstress / settings.cpp
1 /*
2  *    This program is free software; you can redistribute it and/or modify
3  *    it under the terms of the GNU General Public License as published by
4  *    the Free Software Foundation; either version 2 of the License, or
5  *    (at your option) any later version.
6  *
7  *    This program is distributed in the hope that it will be useful,
8  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
9  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  *    GNU General Public License for more details.
11  *
12  *    You should have received a copy of the GNU General Public License
13  *    along with this program; if not, write to the Free Software
14  *    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
15  */
16
17 /*
18  *    Author : Maksym Mamontov <stg@madf.info>
19  */
20
21 #include <cstring>
22 #include <cassert>
23 #include <vector>
24
25 #include "stg/dotconfpp.h"
26 #include "stg/module_settings.h"
27 #include "stg/common.h"
28
29 #include "settings.h"
30
31 SETTINGS::SETTINGS()
32     : port(0),
33       localPort(0),
34       confFile("sgauthstress.conf")
35 {
36 }
37 //-----------------------------------------------------------------------------
38 int ParseModuleSettings(const DOTCONFDocumentNode * node, std::vector<PARAM_VALUE> * params)
39 {
40 assert(node && "DOTCONFDocumentNode must not be NULL!");
41
42 const DOTCONFDocumentNode * childNode = node->getChildNode();
43 while (childNode)
44     {
45     PARAM_VALUE pv;
46     pv.param = childNode->getName();
47     int i = 0;
48     const char * value;
49     while ((value = childNode->getValue(i++)) != NULL)
50         pv.value.push_back(value);
51     params->push_back(pv);
52     childNode = childNode->getNextNode();
53     }
54
55 return 0;
56 }
57 //-----------------------------------------------------------------------------
58 int SETTINGS::ReadSettings()
59 {
60 const char * requiredOptions[] = {
61     "ModulesPath",
62     "StoreModule",
63     "Login",
64     "Password",
65     "ServerName",
66     "ServerPort",
67     NULL
68     };
69
70 DOTCONFDocument conf(DOTCONFDocument::CASEINSENSITIVE);
71 conf.setRequiredOptionNames(requiredOptions);
72
73 if(conf.setContent(confFile.c_str()) != 0)
74     {
75     strError = "Cannot read file " + confFile + ".";
76     printfd(__FILE__, "SETTINGS::ReadSettings() - %s\n", strError.c_str());
77     return -1;
78     }
79
80 const DOTCONFDocumentNode * node = conf.getFirstNode();
81
82 int storeModulesCount = 0;
83 while (node)
84     {
85     if (strcasecmp(node->getName(), "ModulesPath") == 0)
86         modulesPath = node->getValue(0);
87     else if (strcasecmp(node->getName(), "Login") == 0)
88         login = node->getValue(0);
89     else if (strcasecmp(node->getName(), "Password") == 0)
90         password = node->getValue(0);
91     else if (strcasecmp(node->getName(), "ServerName") == 0)
92         serverName = node->getValue(0);
93     else if (strcasecmp(node->getName(), "ServerPort") == 0)
94         {
95         if (ParseIntInRange(node->getValue(0), 1, 65535, &port))
96             {
97             strError = "Parameter 'ServerPort' is not valid.";
98             printfd(__FILE__, "SETTINGS::ReadSettings() - %s\n", strError.c_str());
99             return -1;
100             }
101         }
102     else if (strcasecmp(node->getName(), "LocalPort") == 0)
103         {
104         if (ParseIntInRange(node->getValue(0), 0, 65535, &localPort))
105             {
106             strError = "Parameter 'LocalPort' is not valid.";
107             printfd(__FILE__, "SETTINGS::ReadSettings() - %s\n", strError.c_str());
108             return -1;
109             }
110         }
111     else if (strcasecmp(node->getName(), "StoreModule") == 0)
112         {
113         if (node->getValue(1))
114             {
115             strError = "Unexpected \'" + std::string(node->getValue(1)) + "\'.";
116             printfd(__FILE__, "SETTINGS::ReadSettings() - %s\n", strError.c_str());
117             return -1;
118             }
119
120         if (storeModulesCount)
121             {
122             strError = "Should be only one source StoreModule.";
123             printfd(__FILE__, "SETTINGS::ReadSettings() - %s\n", strError.c_str());
124             return -1;
125             }
126         ++storeModulesCount;
127
128         storeModuleSettings.moduleName = node->getValue(0);
129         if (ParseModuleSettings(node, &storeModuleSettings.moduleParams))
130             {
131             strError = "Failed to parse store module settings";
132             printfd(__FILE__, "SETTINGS::ReadSettings() - %s\n", strError.c_str());
133             return -1;
134             }
135         }
136
137     node = node->getNextNode();
138     }
139
140 return 0;
141 }