]> git.stg.codes - stg.git/blob - projects/sgauthstress/settings.cpp
1e1cf1a0ef6cc6325bba7bc4cc34fce1e7b35b5a
[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 : Maxim Mamontov <faust@stargazer.dp.ua>
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 ParseYesNo(const std::string & value, bool * val)
39 {
40 if (0 == strcasecmp(value.c_str(), "yes"))
41     {
42     *val = true;
43     return 0;
44     }
45 if (0 == strcasecmp(value.c_str(), "no"))
46     {
47     *val = false;
48     return 0;
49     }
50
51 return -1;
52 }
53 //-----------------------------------------------------------------------------
54 int ParseInt(const std::string & value, int * val)
55 {
56 if (str2x<int>(value, *val))
57     return -1;
58
59 return 0;
60 }
61 //-----------------------------------------------------------------------------
62 int ParseUnsigned(const std::string & value, unsigned * val)
63 {
64 if (str2x<unsigned>(value, *val))
65     return -1;
66
67 return 0;
68 }
69 //-----------------------------------------------------------------------------
70 int ParseIntInRange(const std::string & value, int min, int max, int * val)
71 {
72 if (ParseInt(value, val) != 0)
73     return -1;
74
75 if (*val < min || *val > max)
76     return -1;
77
78 return 0;
79 }
80 //-----------------------------------------------------------------------------
81 int ParseUnsignedInRange(const std::string & value, unsigned min, unsigned max, unsigned * val)
82 {
83 if (ParseUnsigned(value, val) != 0)
84     return -1;
85
86 if (*val < min || *val > max)
87     return -1;
88
89 return 0;
90 }
91 //-----------------------------------------------------------------------------
92 int ParseModuleSettings(const DOTCONFDocumentNode * node, std::vector<PARAM_VALUE> * params)
93 {
94 assert(node && "DOTCONFDocumentNode must not be NULL!");
95
96 const DOTCONFDocumentNode * childNode = node->getChildNode();
97 while (childNode)
98     {
99     PARAM_VALUE pv;
100     pv.param = childNode->getName();
101     int i = 0;
102     const char * value;
103     while ((value = childNode->getValue(i++)) != NULL)
104         pv.value.push_back(value);
105     params->push_back(pv);
106     childNode = childNode->getNextNode();
107     }
108
109 return 0;
110 }
111 //-----------------------------------------------------------------------------
112 int SETTINGS::ReadSettings()
113 {
114 const char * requiredOptions[] = {
115     "ModulesPath",
116     "StoreModule",
117     "Login",
118     "Password",
119     "ServerName",
120     "ServerPort",
121     NULL
122     };
123
124 DOTCONFDocument conf(DOTCONFDocument::CASEINSENSITIVE);
125 conf.setRequiredOptionNames(requiredOptions);
126
127 if(conf.setContent(confFile.c_str()) != 0)
128     {
129     strError = "Cannot read file " + confFile + ".";
130     printfd(__FILE__, "SETTINGS::ReadSettings() - %s\n", strError.c_str());
131     return -1;
132     }
133
134 const DOTCONFDocumentNode * node = conf.getFirstNode();
135
136 int storeModulesCount = 0;
137 while (node)
138     {
139     if (strcasecmp(node->getName(), "ModulesPath") == 0)
140         modulesPath = node->getValue(0);
141     else if (strcasecmp(node->getName(), "Login") == 0)
142         login = node->getValue(0);
143     else if (strcasecmp(node->getName(), "Password") == 0)
144         password = node->getValue(0);
145     else if (strcasecmp(node->getName(), "ServerName") == 0)
146         serverName = node->getValue(0);
147     else if (strcasecmp(node->getName(), "ServerPort") == 0)
148         {
149         if (ParseIntInRange(node->getValue(0), 1, 65535, &port))
150             {
151             strError = "Parameter 'ServerPort' is not valid.";
152             printfd(__FILE__, "SETTINGS::ReadSettings() - %s\n", strError.c_str());
153             return -1;
154             }
155         }
156     else if (strcasecmp(node->getName(), "LocalPort") == 0)
157         {
158         if (ParseIntInRange(node->getValue(0), 0, 65535, &localPort))
159             {
160             strError = "Parameter 'LocalPort' is not valid.";
161             printfd(__FILE__, "SETTINGS::ReadSettings() - %s\n", strError.c_str());
162             return -1;
163             }
164         }
165     else if (strcasecmp(node->getName(), "StoreModule") == 0)
166         {
167         if (node->getValue(1))
168             {
169             strError = "Unexpected \'" + std::string(node->getValue(1)) + "\'.";
170             printfd(__FILE__, "SETTINGS::ReadSettings() - %s\n", strError.c_str());
171             return -1;
172             }
173
174         if (storeModulesCount)
175             {
176             strError = "Should be only one source StoreModule.";
177             printfd(__FILE__, "SETTINGS::ReadSettings() - %s\n", strError.c_str());
178             return -1;
179             }
180         ++storeModulesCount;
181
182         storeModuleSettings.moduleName = node->getValue(0);
183         if (ParseModuleSettings(node, &storeModuleSettings.moduleParams))
184             {
185             strError = "Failed to parse store module settings";
186             printfd(__FILE__, "SETTINGS::ReadSettings() - %s\n", strError.c_str());
187             return -1;
188             }
189         }
190
191     node = node->getNextNode();
192     }
193
194 return 0;
195 }