Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2024-09-27 07:03:41

0001 ///////////////////////////////////////////////////////////////////////////
0002 //
0003 //    Copyright 2011
0004 //
0005 //    This file is part of starlight.
0006 //
0007 //    starlight is free software: you can redistribute it and/or modify
0008 //    it under the terms of the GNU General Public License as published by
0009 //    the Free Software Foundation, either version 3 of the License, or
0010 //    (at your option) any later version.
0011 //
0012 //    starlight is distributed in the hope that it will be useful,
0013 //    but WITHOUT ANY WARRANTY; without even the implied warranty of
0014 //    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
0015 //    GNU General Public License for more details.
0016 //
0017 //    You should have received a copy of the GNU General Public License
0018 //    along with starlight. If not, see <http://www.gnu.org/licenses/>.
0019 //
0020 ///////////////////////////////////////////////////////////////////////////
0021 //
0022 // File and Version Information:
0023 // $Rev:: 228                       $: revision of last commit
0024 // $Author:: butter             $: author of last commit
0025 // $Date:: 2016-01-18 17:10:17 +#$: date of last commit
0026 //
0027 // Description:
0028 //
0029 //
0030 //
0031 ///////////////////////////////////////////////////////////////////////////
0032 
0033 
0034 #include "../include/inputParser.h"
0035 #include <fstream>
0036 #include <cstdlib>
0037 #include <algorithm>
0038 
0039 inputParser::inputParser()
0040 {
0041 }
0042 
0043 inputParser::~inputParser()
0044 {
0045 }
0046 
0047 int inputParser::parseFile(std::string filename)
0048 {
0049 
0050     std::ifstream infile(filename.c_str());
0051     if ((!infile) || (!infile.good())) 
0052     {
0053       return -1;
0054     }
0055     
0056     int lineSize = 256;
0057     char tmp[lineSize];
0058     int nParameters = 0;
0059     while (!infile.getline(tmp, lineSize).eof())
0060     {
0061 
0062         std::string line(tmp);
0063     nParameters += parseString(line);
0064     }
0065 
0066     infile.close();
0067     return nParameters;
0068 
0069 }
0070 int inputParser::parseString(std::string str)
0071 {
0072 
0073     std::string word;
0074     std::string name;
0075     std::string val;
0076 
0077     std::map<std::string, _parameter<int> >::iterator intIt;
0078     std::map<std::string, _parameter<unsigned int> >::iterator uIntIt;
0079     std::map<std::string, _parameter<float> >::iterator floatIt;
0080     std::map<std::string, _parameter<double> >::iterator doubleIt;
0081     std::map<std::string, _parameter<bool> >::iterator boolIt;
0082     std::map<std::string, _parameter<std::string> >::iterator stringIt;
0083     
0084     // Check if there is commented out stuff...
0085     size_t pos = str.find_first_of("#");
0086 
0087     // Cut the comment out of the str
0088     if (pos != str.npos) str.erase(pos, str.find_first_of('\n'));
0089 
0090     // Find the required equal sign and split the string into name and value
0091     size_t eqPos = str.find("=");
0092     std::string whitespaces (" \t\f\v\n\r");
0093 
0094     name = "";
0095     val = "";
0096 
0097     if (eqPos != str.npos)
0098     {
0099         name = str.substr(0, eqPos);
0100         name.erase(name.find_last_not_of(whitespaces)+1);
0101         val = str.substr(eqPos+1);
0102         val.erase(0, val.find_first_not_of(whitespaces));
0103         val.erase(val.find_last_not_of(whitespaces)+1);
0104     }
0105 
0106     if (name.length() > 0 && val.length() > 0)
0107     {
0108         intIt = _intParameters.find(name);
0109         if (intIt != _intParameters.end())
0110         {
0111             intIt->second._found = true;
0112             *(intIt->second._val) = atoi(val.c_str());
0113         return true;
0114         }
0115         uIntIt = _uintParameters.find(name);
0116         if (uIntIt != _uintParameters.end())
0117         {
0118             uIntIt->second._found = true;
0119             *(uIntIt->second._val) = atoi(val.c_str());
0120         return true;
0121         }
0122         floatIt = _floatParameters.find(name);
0123         if (floatIt != _floatParameters.end())
0124         {
0125             floatIt->second._found = true;
0126             *(floatIt->second._val) = atof(val.c_str());
0127         return true;
0128         }
0129         doubleIt = _doubleParameters.find(name);
0130         if (doubleIt != _doubleParameters.end())
0131         {
0132             doubleIt->second._found = true;
0133             *(doubleIt->second._val) = atof(val.c_str());
0134         return true;
0135         }
0136         boolIt = _boolParameters.find(name);
0137         if (boolIt != _boolParameters.end())
0138         {
0139             boolIt->second._found = true;
0140             *(boolIt->second._val) = atoi(val.c_str());
0141         return true;
0142         }
0143         stringIt = _stringParameters.find(name);
0144         if (stringIt != _stringParameters.end())
0145         {
0146             stringIt->second._found = true;
0147             *(stringIt->second._val) = val;
0148         return true;
0149         }
0150 
0151     }
0152     return false;
0153 }
0154 void inputParser::addIntParameter(std::string name, int *var, bool required)
0155 {
0156     _parameter<int> par(name, var, required);
0157     _intParameters.insert(std::pair<std::string, _parameter<int> >(name, par));
0158 }
0159 
0160 void inputParser::addUintParameter(std::string name, unsigned int *var, bool required)
0161 {
0162     _parameter<unsigned int> par(name, var, required);
0163     _uintParameters.insert(std::pair<std::string, _parameter<unsigned int> >(name, par));
0164 }
0165 
0166 void inputParser::addFloatParameter(std::string name, float *var, bool required)
0167 {
0168     _parameter<float> par(name, var, required);
0169     _floatParameters.insert(std::pair<std::string, _parameter<float> >(name, par));
0170 }
0171 
0172 void inputParser::addDoubleParameter(std::string name, double *var, bool required)
0173 {
0174     _parameter<double> par(name, var, required);
0175     _doubleParameters.insert(std::pair<std::string, _parameter<double> >(name, par));
0176 }
0177 
0178 void inputParser::addBoolParameter(std::string name, bool *var, bool required)
0179 {
0180     _parameter<bool> par(name, var, required);
0181     _boolParameters.insert(std::pair<std::string, _parameter<bool> >(name, par));
0182 }
0183 
0184 void inputParser::addStringParameter(std::string name, std::string *var, bool required)
0185 {
0186     _parameter<std::string> par(name, var, required);
0187     _stringParameters.insert(std::pair<std::string, _parameter<std::string> >(name, par));
0188 }
0189 
0190 void inputParser::printParameterInfo(std::ostream &out)
0191 {
0192 
0193     std::map<std::string, _parameter<int> >::iterator intIt;
0194     std::map<std::string, _parameter<unsigned int> >::iterator uIntIt;
0195     std::map<std::string, _parameter<float> >::iterator floatIt;
0196     std::map<std::string, _parameter<double> >::iterator doubleIt;
0197     std::map<std::string, _parameter<bool> >::iterator boolIt;
0198     std::map<std::string, _parameter<std::string> >::iterator stringIt;
0199     out << "#########################################" << std::endl;
0200     out << "PARAMETER:\t\tVALUE:" << std::endl;
0201     out << "#########################################" << std::endl;
0202     out << "-----------------------------------------" << std::endl;
0203     for (intIt = _intParameters.begin(); intIt != _intParameters.end(); ++intIt)
0204     {
0205         intIt->second.printParameterInfo();
0206         out << "-----------------------------------------" << std::endl;
0207     }
0208     for (uIntIt = _uintParameters.begin(); uIntIt != _uintParameters.end(); ++uIntIt)
0209     {
0210         uIntIt->second.printParameterInfo();
0211         out << "-----------------------------------------" << std::endl;
0212     }
0213     for (floatIt = _floatParameters.begin(); floatIt != _floatParameters.end(); ++floatIt)
0214     {
0215         floatIt->second.printParameterInfo();
0216         out << "-----------------------------------------" << std::endl;
0217     }
0218     for (doubleIt = _doubleParameters.begin(); doubleIt != _doubleParameters.end(); ++doubleIt)
0219     {
0220         doubleIt->second.printParameterInfo();
0221         out << "-----------------------------------------" << std::endl;
0222     }
0223     for (boolIt = _boolParameters.begin(); boolIt != _boolParameters.end(); ++boolIt)
0224     {
0225         boolIt->second.printParameterInfo();
0226         out << "-----------------------------------------" << std::endl;
0227     }
0228     for (stringIt = _stringParameters.begin(); stringIt != _stringParameters.end(); ++stringIt)
0229     {
0230         stringIt->second.printParameterInfo();
0231         out << "-----------------------------------------" << std::endl;
0232     }
0233     out << "#########################################" << std::endl;
0234 }
0235 
0236 bool inputParser::validateParameters(std::ostream& errOut)
0237 {
0238 
0239     int nCriticalMissing = 0;
0240 
0241     std::map<std::string, _parameter<int> >::iterator intIt;
0242     std::map<std::string, _parameter<float> >::iterator floatIt;
0243     std::map<std::string, _parameter<double> >::iterator doubleIt;
0244     std::map<std::string, _parameter<bool> >::iterator boolIt;
0245 
0246     for (intIt = _intParameters.begin(); intIt != _intParameters.end(); ++intIt)
0247     {
0248         if (!intIt->second._found)
0249         {
0250             if (intIt->second._required)
0251             {
0252                 errOut << "Could not find parameter: " << intIt->second._name << " which is required. Please specify this parameter in the config file!" << std::endl;
0253                 nCriticalMissing++;
0254             }
0255         }
0256     }
0257     for (floatIt = _floatParameters.begin(); floatIt != _floatParameters.end(); ++floatIt)
0258     {
0259         if (!floatIt->second._found)
0260         {
0261             if (floatIt->second._required)
0262             {
0263                 errOut << "Could not find parameter: " << floatIt->second._name << " which is required. Please specify this parameter in the config file!" << std::endl;
0264                 nCriticalMissing++;
0265             }
0266         }
0267     }
0268     for (doubleIt = _doubleParameters.begin(); doubleIt != _doubleParameters.end(); ++doubleIt)
0269     {
0270         if (!doubleIt->second._found)
0271         {
0272             if (doubleIt->second._required)
0273             {
0274                 errOut << "Could not find parameter: " << doubleIt->second._name << " which is required. Please specify this parameter in the config file!" << std::endl;
0275                 nCriticalMissing++;
0276             }
0277         }
0278     }
0279     for (boolIt = _boolParameters.begin(); boolIt != _boolParameters.end(); ++boolIt)
0280     {
0281         if (!boolIt->second._found)
0282         {
0283             if (boolIt->second._required)
0284             {
0285                 errOut << "Could not find parameter: " << boolIt->second._name << " which is required. Please specify this parameter in the config file!" << std::endl;
0286                 nCriticalMissing++;
0287             }
0288         }
0289     }
0290     if(nCriticalMissing > 0) return false;
0291     return true;
0292 }
0293