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 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 string & value, int * val)
 
  56 if (str2x<int>(value, *val))
 
  61 //-----------------------------------------------------------------------------
 
  62 int ParseUnsigned(const string & value, unsigned * val)
 
  64 if (str2x<unsigned>(value, *val))
 
  69 //-----------------------------------------------------------------------------
 
  70 int ParseIntInRange(const 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 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();
 
 102     while ((value = childNode->getValue(i++)) != NULL)
 
 103         pv.value.push_back(value);
 
 104     params->push_back(pv);
 
 105     childNode = childNode->getNextNode();
 
 110 //-----------------------------------------------------------------------------
 
 111 int SETTINGS::ReadSettings()
 
 113 const char * requiredOptions[] = {
 
 123 DOTCONFDocument conf(DOTCONFDocument::CASEINSENSITIVE);
 
 124 conf.setRequiredOptionNames(requiredOptions);
 
 126 if(conf.setContent(confFile.c_str()) != 0)
 
 128     strError = "Cannot read file " + confFile + ".";
 
 129     printfd(__FILE__, "SETTINGS::ReadSettings() - %s\n", strError.c_str());
 
 133 const DOTCONFDocumentNode * node = conf.getFirstNode();
 
 135 int storeModulesCount = 0;
 
 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))
 
 149             strError = "Parameter 'ServerPort' is not valid.";
 
 150             printfd(__FILE__, "SETTINGS::ReadSettings() - %s\n", strError.c_str());
 
 153     else if (strcasecmp(node->getName(), "LocalPort") == 0)
 
 154         if (ParseIntInRange(node->getValue(0), 0, 65535, &localPort))
 
 156             strError = "Parameter 'LocalPort' is not valid.";
 
 157             printfd(__FILE__, "SETTINGS::ReadSettings() - %s\n", strError.c_str());
 
 160     else if (strcasecmp(node->getName(), "StoreModule") == 0)
 
 162         if (node->getValue(1))
 
 164             strError = "Unexpected \'" + std::string(node->getValue(1)) + "\'.";
 
 165             printfd(__FILE__, "SETTINGS::ReadSettings() - %s\n", strError.c_str());
 
 169         if (storeModulesCount)
 
 171             strError = "Should be only one source StoreModule.";
 
 172             printfd(__FILE__, "SETTINGS::ReadSettings() - %s\n", strError.c_str());
 
 177         storeModuleSettings.moduleName = node->getValue(0);
 
 178         if (ParseModuleSettings(node, &storeModuleSettings.moduleParams))
 
 180             strError = "Failed to parse store module settings";
 
 181             printfd(__FILE__, "SETTINGS::ReadSettings() - %s\n", strError.c_str());
 
 186     node = node->getNextNode();