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>
25 #include "stg/dotconfpp.h"
26 #include "stg/module_settings.h"
27 #include "stg/common.h"
34 confFile("/etc/sgauth.conf")
37 //-----------------------------------------------------------------------------
38 int ParseYesNo(const std::string & value, bool * val)
40 if (0 == strcasecmp(value.c_str(), "yes"))
45 if (0 == strcasecmp(value.c_str(), "no"))
53 //-----------------------------------------------------------------------------
54 int ParseInt(const std::string & value, int * val)
56 if (str2x<int>(value, *val))
61 //-----------------------------------------------------------------------------
62 int ParseUnsigned(const std::string & value, unsigned * val)
64 if (str2x<unsigned>(value, *val))
69 //-----------------------------------------------------------------------------
70 int ParseIntInRange(const std::string & value, int min, int max, int * val)
72 if (ParseInt(value, val) != 0)
75 if (*val < min || *val > max)
80 //-----------------------------------------------------------------------------
81 int ParseUnsignedInRange(const std::string & value, unsigned min, unsigned max, unsigned * val)
83 if (ParseUnsigned(value, val) != 0)
86 if (*val < min || *val > max)
91 //-----------------------------------------------------------------------------
92 int ParseModuleSettings(const DOTCONFDocumentNode * node, std::vector<PARAM_VALUE> * params)
94 assert(node && "DOTCONFDocumentNode must not be NULL!");
96 const DOTCONFDocumentNode * childNode = node->getChildNode();
100 pv.param = childNode->getName();
103 while ((value = childNode->getValue(i++)) != NULL)
104 pv.value.push_back(value);
105 params->push_back(pv);
106 childNode = childNode->getNextNode();
111 //-----------------------------------------------------------------------------
112 int SETTINGS::ReadSettings()
114 const char * requiredOptions[] = {
124 DOTCONFDocument conf(DOTCONFDocument::CASEINSENSITIVE);
125 conf.setRequiredOptionNames(requiredOptions);
127 if(conf.setContent(confFile.c_str()) != 0)
129 strError = "Cannot read file " + confFile + ".";
130 printfd(__FILE__, "SETTINGS::ReadSettings() - %s\n", strError.c_str());
134 const DOTCONFDocumentNode * node = conf.getFirstNode();
136 int storeModulesCount = 0;
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)
149 if (ParseIntInRange(node->getValue(0), 1, 65535, &port))
151 strError = "Parameter 'ServerPort' is not valid.";
152 printfd(__FILE__, "SETTINGS::ReadSettings() - %s\n", strError.c_str());
156 else if (strcasecmp(node->getName(), "LocalPort") == 0)
158 if (ParseIntInRange(node->getValue(0), 0, 65535, &localPort))
160 strError = "Parameter 'LocalPort' is not valid.";
161 printfd(__FILE__, "SETTINGS::ReadSettings() - %s\n", strError.c_str());
165 else if (strcasecmp(node->getName(), "StoreModule") == 0)
167 if (node->getValue(1))
169 strError = "Unexpected \'" + std::string(node->getValue(1)) + "\'.";
170 printfd(__FILE__, "SETTINGS::ReadSettings() - %s\n", strError.c_str());
174 if (storeModulesCount)
176 strError = "Should be only one source StoreModule.";
177 printfd(__FILE__, "SETTINGS::ReadSettings() - %s\n", strError.c_str());
182 storeModuleSettings.moduleName = node->getValue(0);
183 if (ParseModuleSettings(node, &storeModuleSettings.moduleParams))
185 strError = "Failed to parse store module settings";
186 printfd(__FILE__, "SETTINGS::ReadSettings() - %s\n", strError.c_str());
191 node = node->getNextNode();