]> git.uio.no Git - u/mrichter/AliRoot.git/blob - STARLIGHT/starlight/src/inputParser.cpp~
698fc6a7add89d7eb9c97fa952e2b611a480dcb1
[u/mrichter/AliRoot.git] / STARLIGHT / starlight / src / inputParser.cpp~
1 ///////////////////////////////////////////////////////////////////////////
2 //
3 //    Copyright 2011
4 //
5 //    This file is part of starlight.
6 //
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.
11 //
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.
16 //
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/>.
19 //
20 ///////////////////////////////////////////////////////////////////////////
21 //
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
26 //
27 // Description:
28 //
29 //
30 //
31 ///////////////////////////////////////////////////////////////////////////
32
33
34 #include "../include/inputParser.h"
35 #include <fstream>
36 #include <cstdlib>
37 #include <algorithm>
38
39 inputParser::inputParser()
40 {
41 }
42
43 inputParser::~inputParser()
44 {
45 }
46
47 int inputParser::parseFile(std::string filename)
48 {
49
50     std::ifstream infile(filename.c_str());
51     if ((!infile) || (!infile.good())) 
52     {
53       return -1;
54     }
55     
56     int lineSize = 256;
57     char tmp[lineSize];
58     int nParameters = 0;
59     while (!infile.getline(tmp, lineSize).eof())
60     {
61
62         std::string line(tmp);
63         nParameters += parseString(line);
64     }
65
66     infile.close();
67     return nParameters;
68
69 }
70 int inputParser::parseString(std::string str)
71 {
72
73     std::string word;
74     std::string name;
75     std::string val;
76
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;
83     
84     // Check if there is commented out stuff...
85     size_t pos = str.find_first_of("#");
86
87     // Cut the comment out of the str
88     if (pos != str.npos) str.erase(pos, str.find_first_of('\n'));
89
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");
93
94     name = "";
95     val = "";
96
97     if (eqPos != str.npos)
98     {
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));
103     }
104
105     if (name.length() > 0 && val.length() > 0)
106     {
107         intIt = _intParameters.find(name);
108         if (intIt != _intParameters.end())
109         {
110             intIt->second._found = true;
111             *(intIt->second._val) = atoi(val.c_str());
112             return true;
113         }
114         uIntIt = _uintParameters.find(name);
115         if (uIntIt != _uintParameters.end())
116         {
117             uIntIt->second._found = true;
118             *(uIntIt->second._val) = atoi(val.c_str());
119             return true;
120         }
121         floatIt = _floatParameters.find(name);
122         if (floatIt != _floatParameters.end())
123         {
124             floatIt->second._found = true;
125             *(floatIt->second._val) = atof(val.c_str());
126             return true;
127         }
128         doubleIt = _doubleParameters.find(name);
129         if (doubleIt != _doubleParameters.end())
130         {
131             doubleIt->second._found = true;
132             *(doubleIt->second._val) = atof(val.c_str());
133             return true;
134         }
135         boolIt = _boolParameters.find(name);
136         if (boolIt != _boolParameters.end())
137         {
138             boolIt->second._found = true;
139             *(boolIt->second._val) = atoi(val.c_str());
140             return true;
141         }
142         stringIt = _stringParameters.find(name);
143         if (stringIt != _stringParameters.end())
144         {
145             stringIt->second._found = true;
146             *(stringIt->second._val) = val;
147             return true;
148         }
149
150     }
151     return false;
152 }
153 void inputParser::addIntParameter(std::string name, int *var, bool required)
154 {
155     _parameter<int> par(name, var, required);
156     _intParameters.insert(std::pair<std::string, _parameter<int> >(name, par));
157 }
158
159 void inputParser::addUintParameter(std::string name, unsigned int *var, bool required)
160 {
161     _parameter<unsigned int> par(name, var, required);
162     _uintParameters.insert(std::pair<std::string, _parameter<unsigned int> >(name, par));
163 }
164
165 void inputParser::addFloatParameter(std::string name, float *var, bool required)
166 {
167     _parameter<float> par(name, var, required);
168     _floatParameters.insert(std::pair<std::string, _parameter<float> >(name, par));
169 }
170
171 void inputParser::addDoubleParameter(std::string name, double *var, bool required)
172 {
173     _parameter<double> par(name, var, required);
174     _doubleParameters.insert(std::pair<std::string, _parameter<double> >(name, par));
175 }
176
177 void inputParser::addBoolParameter(std::string name, bool *var, bool required)
178 {
179     _parameter<bool> par(name, var, required);
180     _boolParameters.insert(std::pair<std::string, _parameter<bool> >(name, par));
181 }
182
183 void inputParser::addStringParameter(std::string name, std::string *var, bool required)
184 {
185     _parameter<std::string> par(name, var, required);
186     _stringParameters.insert(std::pair<std::string, _parameter<std::string> >(name, par));
187 }
188
189 void inputParser::printParameterInfo(std::ostream &out)
190 {
191
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)
202     {
203         intIt->second.printParameterInfo();
204         out << "-----------------------------------------" << std::endl;
205     }
206     for (uIntIt = _uintParameters.begin(); uIntIt != _uintParameters.end(); ++uIntIt)
207     {
208         uIntIt->second.printParameterInfo();
209         out << "-----------------------------------------" << std::endl;
210     }
211     for (floatIt = _floatParameters.begin(); floatIt != _floatParameters.end(); ++floatIt)
212     {
213         floatIt->second.printParameterInfo();
214         out << "-----------------------------------------" << std::endl;
215     }
216     for (doubleIt = _doubleParameters.begin(); doubleIt != _doubleParameters.end(); ++doubleIt)
217     {
218         doubleIt->second.printParameterInfo();
219         out << "-----------------------------------------" << std::endl;
220     }
221     for (boolIt = _boolParameters.begin(); boolIt != _boolParameters.end(); ++boolIt)
222     {
223         boolIt->second.printParameterInfo();
224         out << "-----------------------------------------" << std::endl;
225     }
226     out << "#########################################" << std::endl;
227 }
228
229 bool inputParser::validateParameters(std::ostream& warnOut, std::ostream& errOut)
230 {
231
232     int nNonCriticalMissing = 0;
233     int nCriticalMissing = 0;
234
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;
239
240     for (intIt = _intParameters.begin(); intIt != _intParameters.end(); ++intIt)
241     {
242         if (!intIt->second._found)
243         {
244             if (intIt->second._required)
245             {
246                 errOut << "Could not find parameter: " << intIt->second._name << " which is required. Please specify this parameter in the config file!" << std::endl;
247                 nCriticalMissing++;
248             }
249             else
250             {
251                 warnOut << "Could not find parameter: " << intIt->second._name << ", but it is not required, using default value: " << *intIt->second._val << std::endl;
252                 nNonCriticalMissing++;
253             }
254         }
255     }
256     for (floatIt = _floatParameters.begin(); floatIt != _floatParameters.end(); ++floatIt)
257     {
258         if (!floatIt->second._found)
259         {
260             if (floatIt->second._required)
261             {
262                 errOut << "Could not find parameter: " << floatIt->second._name << " which is required. Please specify this parameter in the config file!" << std::endl;
263                 nCriticalMissing++;
264             }
265             else
266             {
267                 warnOut << "Could not find parameter: " << floatIt->second._name << ", but it is not required, using default value: " << *floatIt->second._val << std::endl;
268                 nNonCriticalMissing++;
269             }
270         }
271     }
272     for (doubleIt = _doubleParameters.begin(); doubleIt != _doubleParameters.end(); ++doubleIt)
273     {
274         if (!doubleIt->second._found)
275         {
276             if (doubleIt->second._required)
277             {
278                 errOut << "Could not find parameter: " << doubleIt->second._name << " which is required. Please specify this parameter in the config file!" << std::endl;
279                 nCriticalMissing++;
280             }
281             else
282             {
283                 warnOut << "Could not find parameter: " << doubleIt->second._name << ", but it is not required, using default value: " << *doubleIt->second._val << std::endl;
284                 nNonCriticalMissing++;
285             }
286         }
287     }
288     for (boolIt = _boolParameters.begin(); boolIt != _boolParameters.end(); ++boolIt)
289     {
290         if (!boolIt->second._found)
291         {
292             if (boolIt->second._required)
293             {
294                 errOut << "Could not find parameter: " << boolIt->second._name << " which is required. Please specify this parameter in the config file!" << std::endl;
295                 nCriticalMissing++;
296             }
297             else
298             {
299                 warnOut << "Could not find parameter: " << boolIt->second._name << ", but it is not required, using default value: " << *boolIt->second._val << std::endl;
300                 nNonCriticalMissing++;
301             }
302         }
303     }
304     if(nCriticalMissing > 0) return false;
305     return true;
306 }
307
308
309 template<>
310 void inputParser::addParameter(const std::string& name, int * varPtr, bool required)
311 {
312   addIntParameter(name, varPtr, required);
313 }
314 template<>
315 void inputParser::addParameter(const std::string& name, unsigned int * varPtr, bool required)
316 {
317   addUintParameter(name, varPtr, required);
318 }
319 template<>
320 void inputParser::addParameter(const std::string& name, float * varPtr, bool required)
321 {
322   addFloatParameter(name, varPtr, required);
323 }
324
325 template<>
326 void inputParser::addParameter(const std::string& name, double * varPtr, bool required)
327 {
328   addDoubleParameter(name, varPtr, required);
329 }
330
331 template<>
332 void inputParser::addParameter(const std::string& name, bool * varPtr, bool required)
333 {
334   addBoolParameter(name, varPtr, required);
335 }
336
337 template<>
338 void inputParser::addParameter(const std::string& name, std::string * varPtr, bool required)
339 {
340   addStringParameter(name, varPtr, required);
341 }