EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
inputParser.cpp
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file 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 //
21 //
22 // File and Version Information:
23 // $Rev:: 228 $: revision of last commit
24 // $Author:: butter $: author of last commit
25 // $Date:: 2016-01-18 17:10:17 +#$: date of last commit
26 //
27 // Description:
28 //
29 //
30 //
32 
33 
34 #include "../include/inputParser.h"
35 #include <fstream>
36 #include <cstdlib>
37 #include <algorithm>
38 
40 {
41 }
42 
44 {
45 }
46 
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  val.erase(val.find_last_not_of(whitespaces)+1);
104  }
105 
106  if (name.length() > 0 && val.length() > 0)
107  {
108  intIt = _intParameters.find(name);
109  if (intIt != _intParameters.end())
110  {
111  intIt->second._found = true;
112  *(intIt->second._val) = atoi(val.c_str());
113  return true;
114  }
115  uIntIt = _uintParameters.find(name);
116  if (uIntIt != _uintParameters.end())
117  {
118  uIntIt->second._found = true;
119  *(uIntIt->second._val) = atoi(val.c_str());
120  return true;
121  }
122  floatIt = _floatParameters.find(name);
123  if (floatIt != _floatParameters.end())
124  {
125  floatIt->second._found = true;
126  *(floatIt->second._val) = atof(val.c_str());
127  return true;
128  }
129  doubleIt = _doubleParameters.find(name);
130  if (doubleIt != _doubleParameters.end())
131  {
132  doubleIt->second._found = true;
133  *(doubleIt->second._val) = atof(val.c_str());
134  return true;
135  }
136  boolIt = _boolParameters.find(name);
137  if (boolIt != _boolParameters.end())
138  {
139  boolIt->second._found = true;
140  *(boolIt->second._val) = atoi(val.c_str());
141  return true;
142  }
143  stringIt = _stringParameters.find(name);
144  if (stringIt != _stringParameters.end())
145  {
146  stringIt->second._found = true;
147  *(stringIt->second._val) = val;
148  return true;
149  }
150 
151  }
152  return false;
153 }
154 void inputParser::addIntParameter(std::string name, int *var, bool required)
155 {
156  _parameter<int> par(name, var, required);
157  _intParameters.insert(std::pair<std::string, _parameter<int> >(name, par));
158 }
159 
160 void inputParser::addUintParameter(std::string name, unsigned int *var, bool required)
161 {
162  _parameter<unsigned int> par(name, var, required);
163  _uintParameters.insert(std::pair<std::string, _parameter<unsigned int> >(name, par));
164 }
165 
166 void inputParser::addFloatParameter(std::string name, float *var, bool required)
167 {
168  _parameter<float> par(name, var, required);
169  _floatParameters.insert(std::pair<std::string, _parameter<float> >(name, par));
170 }
171 
172 void inputParser::addDoubleParameter(std::string name, double *var, bool required)
173 {
174  _parameter<double> par(name, var, required);
175  _doubleParameters.insert(std::pair<std::string, _parameter<double> >(name, par));
176 }
177 
178 void inputParser::addBoolParameter(std::string name, bool *var, bool required)
179 {
180  _parameter<bool> par(name, var, required);
181  _boolParameters.insert(std::pair<std::string, _parameter<bool> >(name, par));
182 }
183 
184 void inputParser::addStringParameter(std::string name, std::string *var, bool required)
185 {
186  _parameter<std::string> par(name, var, required);
187  _stringParameters.insert(std::pair<std::string, _parameter<std::string> >(name, par));
188 }
189 
190 void inputParser::printParameterInfo(std::ostream &out)
191 {
192 
193  std::map<std::string, _parameter<int> >::iterator intIt;
194  std::map<std::string, _parameter<unsigned int> >::iterator uIntIt;
195  std::map<std::string, _parameter<float> >::iterator floatIt;
196  std::map<std::string, _parameter<double> >::iterator doubleIt;
197  std::map<std::string, _parameter<bool> >::iterator boolIt;
198  std::map<std::string, _parameter<std::string> >::iterator stringIt;
199  out << "#########################################" << std::endl;
200  out << "PARAMETER:\t\tVALUE:" << std::endl;
201  out << "#########################################" << std::endl;
202  out << "-----------------------------------------" << std::endl;
203  for (intIt = _intParameters.begin(); intIt != _intParameters.end(); ++intIt)
204  {
205  intIt->second.printParameterInfo();
206  out << "-----------------------------------------" << std::endl;
207  }
208  for (uIntIt = _uintParameters.begin(); uIntIt != _uintParameters.end(); ++uIntIt)
209  {
210  uIntIt->second.printParameterInfo();
211  out << "-----------------------------------------" << std::endl;
212  }
213  for (floatIt = _floatParameters.begin(); floatIt != _floatParameters.end(); ++floatIt)
214  {
215  floatIt->second.printParameterInfo();
216  out << "-----------------------------------------" << std::endl;
217  }
218  for (doubleIt = _doubleParameters.begin(); doubleIt != _doubleParameters.end(); ++doubleIt)
219  {
220  doubleIt->second.printParameterInfo();
221  out << "-----------------------------------------" << std::endl;
222  }
223  for (boolIt = _boolParameters.begin(); boolIt != _boolParameters.end(); ++boolIt)
224  {
225  boolIt->second.printParameterInfo();
226  out << "-----------------------------------------" << std::endl;
227  }
228  for (stringIt = _stringParameters.begin(); stringIt != _stringParameters.end(); ++stringIt)
229  {
230  stringIt->second.printParameterInfo();
231  out << "-----------------------------------------" << std::endl;
232  }
233  out << "#########################################" << std::endl;
234 }
235 
236 bool inputParser::validateParameters(std::ostream& errOut)
237 {
238 
239  int nCriticalMissing = 0;
240 
241  std::map<std::string, _parameter<int> >::iterator intIt;
242  std::map<std::string, _parameter<float> >::iterator floatIt;
243  std::map<std::string, _parameter<double> >::iterator doubleIt;
244  std::map<std::string, _parameter<bool> >::iterator boolIt;
245 
246  for (intIt = _intParameters.begin(); intIt != _intParameters.end(); ++intIt)
247  {
248  if (!intIt->second._found)
249  {
250  if (intIt->second._required)
251  {
252  errOut << "Could not find parameter: " << intIt->second._name << " which is required. Please specify this parameter in the config file!" << std::endl;
253  nCriticalMissing++;
254  }
255  }
256  }
257  for (floatIt = _floatParameters.begin(); floatIt != _floatParameters.end(); ++floatIt)
258  {
259  if (!floatIt->second._found)
260  {
261  if (floatIt->second._required)
262  {
263  errOut << "Could not find parameter: " << floatIt->second._name << " which is required. Please specify this parameter in the config file!" << std::endl;
264  nCriticalMissing++;
265  }
266  }
267  }
268  for (doubleIt = _doubleParameters.begin(); doubleIt != _doubleParameters.end(); ++doubleIt)
269  {
270  if (!doubleIt->second._found)
271  {
272  if (doubleIt->second._required)
273  {
274  errOut << "Could not find parameter: " << doubleIt->second._name << " which is required. Please specify this parameter in the config file!" << std::endl;
275  nCriticalMissing++;
276  }
277  }
278  }
279  for (boolIt = _boolParameters.begin(); boolIt != _boolParameters.end(); ++boolIt)
280  {
281  if (!boolIt->second._found)
282  {
283  if (boolIt->second._required)
284  {
285  errOut << "Could not find parameter: " << boolIt->second._name << " which is required. Please specify this parameter in the config file!" << std::endl;
286  nCriticalMissing++;
287  }
288  }
289  }
290  if(nCriticalMissing > 0) return false;
291  return true;
292 }
293