]> git.stg.codes - stg.git/blob - projects/sgauthstress/settings.cpp
Settings code and header files renamed
[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("/etc/sgauth.conf")
35 {
36 }
37 //-----------------------------------------------------------------------------
38 int ParseYesNo(const 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 string & value, int * val)
55 {
56 if (str2x<int>(value, *val))
57     return -1;
58
59 return 0;
60 }
61 //-----------------------------------------------------------------------------
62 int ParseUnsigned(const string & value, unsigned * val)
63 {
64 if (str2x<unsigned>(value, *val))
65     return -1;
66
67 return 0;
68 }
69 //-----------------------------------------------------------------------------
70 int ParseIntInRange(const 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 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     while ((value = childNode->getValue(i++)) != NULL)
103         pv.value.push_back(value);
104     params->push_back(pv);
105     childNode = childNode->getNextNode();
106     }
107
108 return 0;
109 }
110 //-----------------------------------------------------------------------------
111 int SETTINGS::ReadSettings()
112 {
113 const char * requiredOptions[] = {
114     "ModulesPath",
115     "StoreModule",
116     "Login",
117     "Password",
118     "ServerName",
119     "ServerPort"
120     NULL
121     };
122
123 DOTCONFDocument conf(DOTCONFDocument::CASEINSENSITIVE);
124 conf.setRequiredOptionNames(requiredOptions);
125
126 if(conf.setContent(confFile.c_str()) != 0)
127     {
128     strError = "Cannot read file " + confFile + ".";
129     printfd(__FILE__, "SETTINGS::ReadSettings() - %s\n", strError.c_str());
130     return -1;
131     }
132
133 const DOTCONFDocumentNode * node = conf.getFirstNode();
134
135 int storeModulesCount = 0;
136 while (node)
137     {
138     if (strcasecmp(node->getName(), "ModulesPath") == 0)
139         modulesPath = node->getValue(0);
140     else if (strcasecmp(node->getName(), "Login") == 0)
141         login = node->getValue(0);
142     else if (strcasecmp(node->getName(), "Password") == 0)
143         password = node->getValue(0);
144     else if (strcasecmp(node->getName(), "ServerName") == 0)
145         serverName = node->getValue(0);
146     else if (strcasecmp(node->getName(), "ServerPort") == 0)
147         if (ParseIntInRange(node->getValue(0), 1, 65535, &port))
148             {
149             strError = "Parameter 'ServerPort' is not valid.";
150             printfd(__FILE__, "SETTINGS::ReadSettings() - %s\n", strError.c_str());
151             return -1;
152             }
153     else if (strcasecmp(node->getName(), "LocalPort") == 0)
154         if (ParseIntInRange(node->getValue(0), 0, 65535, &localPort))
155             {
156             strError = "Parameter 'LocalPort' is not valid.";
157             printfd(__FILE__, "SETTINGS::ReadSettings() - %s\n", strError.c_str());
158             return -1;
159             }
160     else if (strcasecmp(node->getName(), "StoreModule") == 0)
161         {
162         if (node->getValue(1))
163             {
164             strError = "Unexpected \'" + std::string(node->getValue(1)) + "\'.";
165             printfd(__FILE__, "SETTINGS::ReadSettings() - %s\n", strError.c_str());
166             return -1;
167             }
168
169         if (storeModulesCount)
170             {
171             strError = "Should be only one source StoreModule.";
172             printfd(__FILE__, "SETTINGS::ReadSettings() - %s\n", strError.c_str());
173             return -1;
174             }
175         ++storeModulesCount;
176
177         storeModuleSettings.moduleName = node->getValue(0);
178         if (ParseModuleSettings(node, &storeModuleSettings.moduleParams))
179             {
180             strError = "Failed to parse store module settings";
181             printfd(__FILE__, "SETTINGS::ReadSettings() - %s\n", strError.c_str());
182             return -1;
183             }
184         }
185
186     node = node->getNextNode();
187     }
188
189 return 0;
190 }