CMS 3D CMS Logo

Defaults_Text.cc
Go to the documentation of this file.
1 //
2 //
3 // File: src/Defaults_Text.cc
4 // Purpose: A lightweight implementation of the Defaults interface
5 // that uses simple text files.
6 // Created: Jul, 2000, sss.
7 //
8 // CMSSW File : src/Defaults_Text.cc
9 // Original Author : Scott Stuart Snyder <snyder@bnl.gov> for D0
10 // Imported to CMSSW by Haryo Sumowidagdo <Suharyo.Sumowidagdo@cern.ch>
11 //
12 
37 #include <cassert>
38 #include <cstdlib>
39 #include <fstream>
40 #include <iostream>
41 #include <cctype>
42 #include <cstring>
43 #include <map>
44 
45 using std::abort;
46 using std::atof;
47 using std::atoi;
48 using std::cerr;
49 using std::getline;
50 using std::ifstream;
51 using std::isspace;
52 using std::map;
53 using std::strchr;
54 using std::string;
55 using std::tolower;
56 
57 namespace {
58 
66  string strip(string s)
67  //
68  // Purpose: Remove comments (text starting with `;' or `#') and leading
69  // and trailing spaces from S.
70  //
71  // Inputs:
72  // s - The string to strip.
73  //
74  // Returns:
75  // The stripped string.
76  //
77  {
78  string::size_type j = s.find_first_of(";#");
79  if (j == string::npos)
80  j = s.size();
81 
82  while (j > 0 && isspace(s[j - 1]))
83  --j;
84 
85  string::size_type i = 0;
86  while (i < j && isspace(s[i]))
87  ++i;
88 
89  return string(s, i, j - i);
90  }
91 
92 } // unnamed namespace
93 
94 namespace hitfit {
95 
96  //***************************************************************************
97 
103  //
104  // Purpose: The internal representation for a Defaults_Text object.
105  //
106  {
107  public:
108  // Constructor.
109 
117  Defaults_Textrep(string file);
127  Defaults_Textrep(string file, int argc, char** argv);
128 
129  // The data. Maps from parameter names to values (which are stored
130  // as strings).
135  std::map<std::string, std::string> _map;
136 
137  // Look up parameter NAME and return its value.
144  string get_val(string name) const;
145 
146  private:
147  // Read parameters from FILE and add them to our data.
153  void read_file(string file);
154 
155  // Look for additional parameter settings in the argument list
156  // ARGC, ARGV and add them to our data.
163  void process_args(int argc, char** argv);
164 
165  // Helper to process a line defining a single parameter.
170  void doline(string l);
171  };
172 
174  //
175  // Purpose: Constructor.
176  //
177  // Inputs:
178  // file - The name of the defaults file to read.
179  // See the comments in the header for a description
180  // of the format for this and for the argument list.
181  // Pass an empty string to skip reading a file.
182  //
183  {
184  read_file(file);
185  }
186 
188  //
189  // Purpose: Constructor.
190  //
191  // Inputs:
192  // file - The name of the defaults file to read.
193  // See the comments in the header for a description
194  // of the format for this and for the argument list.
195  // Pass an empty string to skip reading a file.
196  // argc - The arglist length.
197  // argv - The arglist.
198  //
199  {
200  read_file(file);
202  }
203 
205  //
206  // Purpose: Read parameters from FILE and add them to our data.
207  //
208  // Inputs:
209  // s - The name of the file to read.
210  //
211  {
212  // Just return if we weren't given a file.
213  if (file.empty())
214  return;
215 
216  ifstream f(file.c_str());
217  if (!f.good()) {
218  cerr << "Can't open " << file << "\n";
219  abort();
220  }
221 
222  string l;
223  while (getline(f, l)) {
224  doline(l);
225  }
226 
227  f.close();
228  }
229 
231  //
232  // Purpose: Process the argument list ARGC, ARGV and add additional
233  // parameters from it to our data (possibly overriding
234  // existing settings). See the header file for more details.
235  //
236  // Inputs:
237  // argc - The arglist length.
238  // argv - The arglist.
239  //
240  {
241  // Look for arguments starting with `--'.
242  for (int i = 1; i < argc; i++) {
243  if (argv[i][0] == '-' && argv[i][1] == '-') {
244  // Found one.
245  string l;
246  if (strchr(argv[i], '=') != nullptr)
247  // It was of the form `--NAME=VALUE'. Change to `NAME=VALUE'.
248  l = argv[i] + 2;
249  else if (argv[i][2] == 'n' && argv[i][3] == 'o') {
250  // It was of the form `--noNAME'. Change to `NAME=0'.
251  l = argv[i] + 4;
252  l += "=0";
253  } else {
254  // It was of the form `--NAME'. Change to `NAME=1'.
255  l = argv[i] + 2;
256  l += "=1";
257  }
258 
259  // Process it like a line we read from a file.
260  doline(l);
261  }
262  }
263  }
264 
265  string Defaults_Textrep::get_val(string name) const
266  //
267  // Purpose: Look up parameter NAME and return its value.
268  // The parameter must exist.
269  //
270  // Inputs:
271  // name - The name of the parameter.
272  //
273  // Returns:
274  // The value of the parameter.
275  //
276  {
278 
279  if (_map.find(name) == _map.end()) {
280  cerr << "can't find default for " << name << "\n";
281  abort();
282  } else {
283  std::map<string, string>::const_iterator it = _map.find(name);
284  val = it->second;
285  }
286 
287  return val;
288  }
289 
291  //
292  // Purpose: Helper to process a line defining a single parameter.
293  //
294  // Inputs:
295  // l - The line to process.
296  //
297  {
298  // Strip spaces from the line and ignore it if it's blank.
299  l = strip(l);
300  if (l.empty())
301  return;
302 
303  // It must contain a `=' character.
304  string::size_type pos = l.find('=');
305  if (pos == string::npos) {
306  cerr << "bad defaults line " << l << "\n";
307  abort();
308  }
309 
310  // Split off name and value parts.
311  std::string name = strip(l.substr(0, pos));
312  std::string val = strip(l.substr(pos + 1));
313 
314  // Add it to the map.
315  _map[name] = val;
316  }
317 
318  //***************************************************************************
319 
321  //
322  // Purpose: Constructor.
323  //
324  // Inputs:
325  // def_file - The name of the defaults file to read.
326  // See the comments in the header for a description
327  // of the format for this and for the argument list.
328  // Pass an empty string to skip reading a file.
329  //
330  : _rep(new Defaults_Textrep(def_file)) {}
331 
333  //
334  // Purpose: Constructor.
335  //
336  // Inputs:
337  // def_file - The name of the defaults file to read.
338  // See the comments in the header for a description
339  // of the format for this and for the argument list.
340  // Pass an empty string to skip reading a file.
341  // argc - The arglist length.
342  // argv - The arglist.
343  //
344  : _rep(new Defaults_Textrep(def_file, argc, argv)) {}
345 
347  //
348  // Purpose: Destructor.
349  //
350  {
351  delete _rep;
352  }
353 
355  //
356  // Purpose: Test to see if parameter NAME exists.
357  //
358  // Inputs:
359  // name - The name of the parameter to look up.
360  //
361  // Returns:
362  // True if NAME exists.
363  //
364  {
366  return (_rep->_map.find(name) != _rep->_map.end());
367  }
368 
370  //
371  // Purpose: Get the value of NAME as an integer.
372  //
373  // Inputs:
374  // name - The name of the parameter to look up.
375  //
376  // Returns:
377  // The parameter's value as an integer.
378  //
379  {
380  return atoi(_rep->get_val(name).c_str());
381  }
382 
384  //
385  // Purpose: Get the value of NAME as a float.
386  //
387  // Inputs:
388  // name - The name of the parameter to look up.
389  //
390  // Returns:
391  // The parameter's value as a float.
392  //
393  {
394  return atof(_rep->get_val(name).c_str());
395  }
396 
398  //
399  // Purpose: Get the value of NAME as a bool.
400  //
401  // Inputs:
402  // name - The name of the parameter to look up.
403  //
404  // Returns:
405  // The parameter's value as a bool.
406  //
407  {
408  string val = _rep->get_val(name);
409  if (tolower(val[0]) == 't' || tolower(val[0]) == 'y')
410  return true;
411  else if (tolower(val[0]) == 'f' || tolower(val[0]) == 'n')
412  return false;
413  return !!get_int(name);
414  }
415 
417  //
418  // Purpose: Get the value of NAME as a string.
419  //
420  // Inputs:
421  // name - The name of the parameter to look up.
422  //
423  // Returns:
424  // The parameter's value as a string.
425  //
426  {
427  return _rep->get_val(name);
428  }
429 
430  std::ostream& operator<<(std::ostream& s, const Defaults_Text& def)
431  //
432  // Purpose: Dump out all parameter settings.
433  //
434  // Inputs:
435  // s - The stream to which we're writing.
436  // def - The instance to dump.
437  //
438  // Returns:
439  // The stream S.
440  //
441  {
442  for (std::map<std::string, std::string>::const_iterator it = def._rep->_map.begin(); it != def._rep->_map.end();
443  it++) {
444  s << "[" << it->first << "] = [" << it->second << "]\n";
445  }
446 
447  return s;
448  }
449 
450 } // namespace hitfit
Defaults_Textrep(string file)
Constructor, construct a Defaults_Textrep instance from an ASCII text-file and command line arguments...
int def(FILE *, FILE *, int)
bool exists(std::string name) const override
std::string get_string(std::string name) const override
A lightweight implementation of the Defaults interface that uses simple ASCII text files...
The internal representation for a Defaults_Text object.
int get_int(std::string name) const override
uint16_t size_type
double get_float(std::string name) const override
Defaults_Text(std::string def_file)
Constructor, create a Default_Text object from an ASCII text file. Pass an empty string to skip readi...
double f[11][100]
std::map< std::string, std::string > _map
string get_val(string name) const
bool get_bool(std::string name) const override
~Defaults_Text() override
Destructor.
Define a concrete interface for getting parameter settings from an ASCII text file.
Defaults_Textrep * _rep
void process_args(int argc, char **argv)
std::ostream & operator<<(std::ostream &s, const Constraint_Intermed &ci)
Output stream operator, print the content of this Constraint_Intermed to an output stream...
void read_file(string file)