1 ///////////////////////////////////////////////////////////////////////////
5 // This file is part of starlight.
7 // starlight is free software: you can redistribute it and/or modify
8 // it under the terms of the GNU General Public License as published by
9 // the Free Software Foundation, either version 3 of the License, or
10 // (at your option) any later version.
12 // starlight is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
17 // You should have received a copy of the GNU General Public License
18 // along with starlight. If not, see <http://www.gnu.org/licenses/>.
20 ///////////////////////////////////////////////////////////////////////////
22 // File and Version Information:
23 // $Rev:: 155 $: revision of last commit
24 // $Author:: odjuvsla $: author of last commit
25 // $Date:: 2013-10-06 16:17:50 +#$: date of last commit
31 ///////////////////////////////////////////////////////////////////////////
34 #include "../include/inputParser.h"
39 inputParser::inputParser()
43 inputParser::~inputParser()
47 int inputParser::parseFile(std::string filename)
50 std::ifstream infile(filename.c_str());
51 if ((!infile) || (!infile.good()))
59 while (!infile.getline(tmp, lineSize).eof())
62 std::string line(tmp);
63 nParameters += parseString(line);
70 int inputParser::parseString(std::string str)
77 std::map<std::string, _parameter<int> >::iterator intIt;
78 std::map<std::string, _parameter<unsigned int> >::iterator uIntIt;
79 std::map<std::string, _parameter<float> >::iterator floatIt;
80 std::map<std::string, _parameter<double> >::iterator doubleIt;
81 std::map<std::string, _parameter<bool> >::iterator boolIt;
82 std::map<std::string, _parameter<std::string> >::iterator stringIt;
84 // Check if there is commented out stuff...
85 size_t pos = str.find_first_of("#");
87 // Cut the comment out of the str
88 if (pos != str.npos) str.erase(pos, str.find_first_of('\n'));
90 // Find the required equal sign and split the string into name and value
91 size_t eqPos = str.find("=");
92 std::string whitespaces (" \t\f\v\n\r");
97 if (eqPos != str.npos)
99 name = str.substr(0, eqPos);
100 name.erase(name.find_last_not_of(whitespaces)+1);
101 val = str.substr(eqPos+1);
102 val.erase(0, val.find_first_not_of(whitespaces));
105 if (name.length() > 0 && val.length() > 0)
107 intIt = _intParameters.find(name);
108 if (intIt != _intParameters.end())
110 intIt->second._found = true;
111 *(intIt->second._val) = atoi(val.c_str());
114 uIntIt = _uintParameters.find(name);
115 if (uIntIt != _uintParameters.end())
117 uIntIt->second._found = true;
118 *(uIntIt->second._val) = atoi(val.c_str());
121 floatIt = _floatParameters.find(name);
122 if (floatIt != _floatParameters.end())
124 floatIt->second._found = true;
125 *(floatIt->second._val) = atof(val.c_str());
128 doubleIt = _doubleParameters.find(name);
129 if (doubleIt != _doubleParameters.end())
131 doubleIt->second._found = true;
132 *(doubleIt->second._val) = atof(val.c_str());
135 boolIt = _boolParameters.find(name);
136 if (boolIt != _boolParameters.end())
138 boolIt->second._found = true;
139 *(boolIt->second._val) = atoi(val.c_str());
142 stringIt = _stringParameters.find(name);
143 if (stringIt != _stringParameters.end())
145 stringIt->second._found = true;
146 *(stringIt->second._val) = val;
153 void inputParser::addIntParameter(std::string name, int *var, bool required)
155 _parameter<int> par(name, var, required);
156 _intParameters.insert(std::pair<std::string, _parameter<int> >(name, par));
159 void inputParser::addUintParameter(std::string name, unsigned int *var, bool required)
161 _parameter<unsigned int> par(name, var, required);
162 _uintParameters.insert(std::pair<std::string, _parameter<unsigned int> >(name, par));
165 void inputParser::addFloatParameter(std::string name, float *var, bool required)
167 _parameter<float> par(name, var, required);
168 _floatParameters.insert(std::pair<std::string, _parameter<float> >(name, par));
171 void inputParser::addDoubleParameter(std::string name, double *var, bool required)
173 _parameter<double> par(name, var, required);
174 _doubleParameters.insert(std::pair<std::string, _parameter<double> >(name, par));
177 void inputParser::addBoolParameter(std::string name, bool *var, bool required)
179 _parameter<bool> par(name, var, required);
180 _boolParameters.insert(std::pair<std::string, _parameter<bool> >(name, par));
183 void inputParser::addStringParameter(std::string name, std::string *var, bool required)
185 _parameter<std::string> par(name, var, required);
186 _stringParameters.insert(std::pair<std::string, _parameter<std::string> >(name, par));
189 void inputParser::printParameterInfo(std::ostream &out)
192 std::map<std::string, _parameter<int> >::iterator intIt;
193 std::map<std::string, _parameter<unsigned int> >::iterator uIntIt;
194 std::map<std::string, _parameter<float> >::iterator floatIt;
195 std::map<std::string, _parameter<double> >::iterator doubleIt;
196 std::map<std::string, _parameter<bool> >::iterator boolIt;
197 out << "#########################################" << std::endl;
198 out << "PARAMETER:\t\tVALUE:" << std::endl;
199 out << "#########################################" << std::endl;
200 out << "-----------------------------------------" << std::endl;
201 for (intIt = _intParameters.begin(); intIt != _intParameters.end(); ++intIt)
203 intIt->second.printParameterInfo();
204 out << "-----------------------------------------" << std::endl;
206 for (uIntIt = _uintParameters.begin(); uIntIt != _uintParameters.end(); ++uIntIt)
208 uIntIt->second.printParameterInfo();
209 out << "-----------------------------------------" << std::endl;
211 for (floatIt = _floatParameters.begin(); floatIt != _floatParameters.end(); ++floatIt)
213 floatIt->second.printParameterInfo();
214 out << "-----------------------------------------" << std::endl;
216 for (doubleIt = _doubleParameters.begin(); doubleIt != _doubleParameters.end(); ++doubleIt)
218 doubleIt->second.printParameterInfo();
219 out << "-----------------------------------------" << std::endl;
221 for (boolIt = _boolParameters.begin(); boolIt != _boolParameters.end(); ++boolIt)
223 boolIt->second.printParameterInfo();
224 out << "-----------------------------------------" << std::endl;
226 out << "#########################################" << std::endl;
229 bool inputParser::validateParameters(std::ostream& warnOut, std::ostream& errOut)
232 int nNonCriticalMissing = 0;
233 int nCriticalMissing = 0;
235 std::map<std::string, _parameter<int> >::iterator intIt;
236 std::map<std::string, _parameter<float> >::iterator floatIt;
237 std::map<std::string, _parameter<double> >::iterator doubleIt;
238 std::map<std::string, _parameter<bool> >::iterator boolIt;
240 for (intIt = _intParameters.begin(); intIt != _intParameters.end(); ++intIt)
242 if (!intIt->second._found)
244 if (intIt->second._required)
246 errOut << "Could not find parameter: " << intIt->second._name << " which is required. Please specify this parameter in the config file!" << std::endl;
251 warnOut << "Could not find parameter: " << intIt->second._name << ", but it is not required, using default value: " << *intIt->second._val << std::endl;
252 nNonCriticalMissing++;
256 for (floatIt = _floatParameters.begin(); floatIt != _floatParameters.end(); ++floatIt)
258 if (!floatIt->second._found)
260 if (floatIt->second._required)
262 errOut << "Could not find parameter: " << floatIt->second._name << " which is required. Please specify this parameter in the config file!" << std::endl;
267 warnOut << "Could not find parameter: " << floatIt->second._name << ", but it is not required, using default value: " << *floatIt->second._val << std::endl;
268 nNonCriticalMissing++;
272 for (doubleIt = _doubleParameters.begin(); doubleIt != _doubleParameters.end(); ++doubleIt)
274 if (!doubleIt->second._found)
276 if (doubleIt->second._required)
278 errOut << "Could not find parameter: " << doubleIt->second._name << " which is required. Please specify this parameter in the config file!" << std::endl;
283 warnOut << "Could not find parameter: " << doubleIt->second._name << ", but it is not required, using default value: " << *doubleIt->second._val << std::endl;
284 nNonCriticalMissing++;
288 for (boolIt = _boolParameters.begin(); boolIt != _boolParameters.end(); ++boolIt)
290 if (!boolIt->second._found)
292 if (boolIt->second._required)
294 errOut << "Could not find parameter: " << boolIt->second._name << " which is required. Please specify this parameter in the config file!" << std::endl;
299 warnOut << "Could not find parameter: " << boolIt->second._name << ", but it is not required, using default value: " << *boolIt->second._val << std::endl;
300 nNonCriticalMissing++;
304 if(nCriticalMissing > 0) return false;
310 void inputParser::addParameter(const std::string& name, int * varPtr, bool required)
312 addIntParameter(name, varPtr, required);
315 void inputParser::addParameter(const std::string& name, unsigned int * varPtr, bool required)
317 addUintParameter(name, varPtr, required);
320 void inputParser::addParameter(const std::string& name, float * varPtr, bool required)
322 addFloatParameter(name, varPtr, required);
326 void inputParser::addParameter(const std::string& name, double * varPtr, bool required)
328 addDoubleParameter(name, varPtr, required);
332 void inputParser::addParameter(const std::string& name, bool * varPtr, bool required)
334 addBoolParameter(name, varPtr, required);
338 void inputParser::addParameter(const std::string& name, std::string * varPtr, bool required)
340 addStringParameter(name, varPtr, required);