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.
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.
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
18 * Author : Maxim Mamontov <faust@stargazer.dp.ua>
23 #include "stg/dotconfpp.h"
24 #include "stg/module_settings.h"
25 #include "stg/common.h"
27 #include "settings_impl.h"
29 SETTINGS_IMPL::SETTINGS_IMPL()
38 confFile("/etc/sgauth.conf")
41 //-----------------------------------------------------------------------------
42 int SETTINGS_IMPL::ParseYesNo(const string & value, bool * val)
44 if (0 == strcasecmp(value.c_str(), "yes"))
49 if (0 == strcasecmp(value.c_str(), "no"))
55 strError = "Incorrect value \'" + value + "\'.";
58 //-----------------------------------------------------------------------------
59 int SETTINGS_IMPL::ParseInt(const string & value, int * val)
61 if (str2x<int>(value, *val))
63 strError = "Cannot convert \'" + value + "\' to integer.";
68 //-----------------------------------------------------------------------------
69 int SETTINGS_IMPL::ParseUnsigned(const string & value, unsigned * val)
71 if (str2x<unsigned>(value, *val))
73 strError = "Cannot convert \'" + value + "\' to unsigned integer.";
78 //-----------------------------------------------------------------------------
79 int SETTINGS_IMPL::ParseIntInRange(const string & value, int min, int max, int * val)
81 if (ParseInt(value, val) != 0)
84 if (*val < min || *val > max)
86 strError = "Value \'" + value + "\' out of range.";
92 //-----------------------------------------------------------------------------
93 int SETTINGS_IMPL::ParseUnsignedInRange(const string & value, unsigned min, unsigned max, unsigned * val)
95 if (ParseUnsigned(value, val) != 0)
98 if (*val < min || *val > max)
100 strError = "Value \'" + value + "\' out of range.";
106 //-----------------------------------------------------------------------------
107 int SETTINGS_IMPL::ParseModuleSettings(const DOTCONFDocumentNode * node, std::vector<PARAM_VALUE> * params)
114 pv.param = node->getName();
116 if (node->getValue(1))
118 strError = "Unexpected value \'" + std::string(node->getValue(1)) + "\'.";
119 printfd(__FILE__, "SETTINGS_IMPL::ParseModuleSettings() - %s\n", strError.c_str());
123 const char * value = node->getValue(0);
127 strError = "Module name expected.";
128 printfd(__FILE__, "SETTINGS_IMPL::ParseModuleSettings() - %s\n", strError.c_str());
132 const DOTCONFDocumentNode * childNode = node->getChildNode();
135 pv.param = childNode->getName();
137 while ((value = childNode->getValue(i)) != NULL)
139 pv.value.push_back(value);
142 params->push_back(pv);
144 childNode = childNode->getNextNode();
149 //-----------------------------------------------------------------------------
150 int SETTINGS_IMPL::ReadSettings()
152 const char * requiredOptions[] = {
161 int storeModulesCount = 0;
163 DOTCONFDocument conf(DOTCONFDocument::CASEINSENSITIVE);
164 conf.setRequiredOptionNames(requiredOptions);
166 if(conf.setContent(confFile.c_str()) != 0)
168 strError = "Cannot read file " + confFile + ".";
169 printfd(__FILE__, "SETTINGS_IMPL::ReadSettings() - %s\n", strError.c_str());
173 const DOTCONFDocumentNode * node = conf.getFirstNode();
177 if (strcasecmp(node->getName(), "ModulesPath") == 0)
179 modulesPath = node->getValue(0);
182 if (strcasecmp(node->getName(), "StoreModule") == 0)
184 if (node->getValue(1))
186 strError = "Unexpected \'" + std::string(node->getValue(1)) + "\'.";
187 printfd(__FILE__, "SETTINGS_IMPL::ReadSettings() - %s\n", strError.c_str());
191 if (storeModulesCount)
193 strError = "Should be only one source StoreModule.";
194 printfd(__FILE__, "SETTINGS_IMPL::ReadSettings() - %s\n", strError.c_str());
199 storeModuleSettings.moduleName = node->getValue(0);
200 ParseModuleSettings(node, &storeModuleSettings.moduleParams);
203 node = node->getNextNode();
206 CONFIGFILE cf(confFile);
210 strError = "Cannot read file '" + confFile + "'";
214 cf.ReadString("Login", &login, "/?--?--?*");
215 if (login == "/?--?--?*")
217 strError = "Parameter 'Login' not found.";
221 cf.ReadString("Password", &password, "/?--?--?*");
222 if (login == "/?--?--?*")
224 strError = "Parameter 'Password' not found.";
228 cf.ReadString("ServerName", &serverName, "?*?*?");
229 if (serverName == "?*?*?")
231 strError = "Parameter 'ServerName' not found.";
235 cf.ReadString("ServerPort", &temp, "5555");
236 if (ParseIntInRange(temp, 1, 65535, &port))
238 strError = "Parameter 'ServerPort' is not valid.";
242 cf.ReadString("LocalPort", &temp, "0");
243 if (ParseIntInRange(temp, 0, 65535, &localPort))
245 strError = "Parameter 'LocalPort' is not valid.";