CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
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::cerr;
46 using std::string;
47 using std::ifstream;
48 using std::getline;
49 using std::isspace;
50 using std::tolower;
51 using std::atoi;
52 using std::atof;
53 using std::abort;
54 using std::strchr;
55 using std::map;
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 
93 } // unnamed namespace
94 
95 
96 namespace hitfit {
97 
98 
99 //***************************************************************************
100 
101 
107 //
108 // Purpose: The internal representation for a Defaults_Text object.
109 //
110 {
111 public:
112  // Constructor.
113 
121  Defaults_Textrep (string file);
131  Defaults_Textrep (string file, int argc, char** argv);
132 
133  // The data. Maps from parameter names to values (which are stored
134  // as strings).
139  std::map<std::string,std::string> _map;
140 
141  // Look up parameter NAME and return its value.
148  string get_val (string name) const;
149 
150 
151 private:
152  // Read parameters from FILE and add them to our data.
158  void read_file (string file);
159 
160  // Look for additional parameter settings in the argument list
161  // ARGC, ARGV and add them to our data.
168  void process_args (int argc, char** argv);
169 
170  // Helper to process a line defining a single parameter.
175  void doline (string l);
176 };
177 
178 
180 //
181 // Purpose: Constructor.
182 //
183 // Inputs:
184 // file - The name of the defaults file to read.
185 // See the comments in the header for a description
186 // of the format for this and for the argument list.
187 // Pass an empty string to skip reading a file.
188 //
189 {
190  read_file (file);
191 }
192 
193 
195 //
196 // Purpose: Constructor.
197 //
198 // Inputs:
199 // file - The name of the defaults file to read.
200 // See the comments in the header for a description
201 // of the format for this and for the argument list.
202 // Pass an empty string to skip reading a file.
203 // argc - The arglist length.
204 // argv - The arglist.
205 //
206 {
207  read_file (file);
208  process_args (argc, argv);
209 }
210 
211 
213 //
214 // Purpose: Read parameters from FILE and add them to our data.
215 //
216 // Inputs:
217 // s - The name of the file to read.
218 //
219 {
220  // Just return if we weren't given a file.
221  if (file.size() == 0)
222  return;
223 
224  ifstream f (file.c_str());
225  if (!f.good()) {
226  cerr << "Can't open " << file << "\n";
227  abort ();
228  }
229 
230  string l;
231  while (getline (f, l)) {
232  doline (l);
233  }
234 
235  f.close ();
236 }
237 
238 
240 //
241 // Purpose: Process the argument list ARGC, ARGV and add additional
242 // parameters from it to our data (possibly overriding
243 // existing settings). See the header file for more details.
244 //
245 // Inputs:
246 // argc - The arglist length.
247 // argv - The arglist.
248 //
249 {
250  // Look for arguments starting with `--'.
251  for (int i=1; i < argc; i++) {
252  if (argv[i][0] == '-' && argv[i][1] == '-') {
253 
254  // Found one.
255  string l;
256  if (strchr (argv[i], '=') != 0)
257  // It was of the form `--NAME=VALUE'. Change to `NAME=VALUE'.
258  l = argv[i] + 2;
259  else if (argv[i][2] == 'n' && argv[i][3] == 'o') {
260  // It was of the form `--noNAME'. Change to `NAME=0'.
261  l = argv[i] + 4;
262  l += "=0";
263  }
264  else {
265  // It was of the form `--NAME'. Change to `NAME=1'.
266  l = argv[i] + 2;
267  l += "=1";
268  }
269 
270  // Process it like a line we read from a file.
271  doline (l);
272  }
273  }
274 }
275 
276 
277 string Defaults_Textrep::get_val (string name) const
278 //
279 // Purpose: Look up parameter NAME and return its value.
280 // The parameter must exist.
281 //
282 // Inputs:
283 // name - The name of the parameter.
284 //
285 // Returns:
286 // The value of the parameter.
287 //
288 {
289 
290  std::string val;
291 
292  if (_map.find(name) == _map.end()) {
293  cerr << "can't find default for " << name << "\n";
294  abort ();
295  } else {
296  std::map<string,string>::const_iterator it = _map.find(name);
297  val = it->second;
298  }
299 
300  return val;
301 }
302 
303 
305 //
306 // Purpose: Helper to process a line defining a single parameter.
307 //
308 // Inputs:
309 // l - The line to process.
310 //
311 {
312  // Strip spaces from the line and ignore it if it's blank.
313  l = strip (l);
314  if (l.size() == 0)
315  return;
316 
317  // It must contain a `=' character.
318  string::size_type pos = l.find ('=');
319  if (pos == string::npos) {
320  cerr << "bad defaults line " << l << "\n";
321  abort ();
322  }
323 
324  // Split off name and value parts.
325  std::string name = strip (l.substr (0, pos));
326  std::string val = strip (l.substr (pos+1));
327 
328  // Add it to the map.
329  _map[name] = val;
330 
331 }
332 
333 
334 //***************************************************************************
335 
336 
338 //
339 // Purpose: Constructor.
340 //
341 // Inputs:
342 // def_file - The name of the defaults file to read.
343 // See the comments in the header for a description
344 // of the format for this and for the argument list.
345 // Pass an empty string to skip reading a file.
346 //
347  : _rep (new Defaults_Textrep (def_file))
348 {
349 }
350 
351 
353 //
354 // Purpose: Constructor.
355 //
356 // Inputs:
357 // def_file - The name of the defaults file to read.
358 // See the comments in the header for a description
359 // of the format for this and for the argument list.
360 // Pass an empty string to skip reading a file.
361 // argc - The arglist length.
362 // argv - The arglist.
363 //
364  : _rep (new Defaults_Textrep (def_file, argc, argv))
365 {
366 }
367 
369 //
370 // Purpose: Destructor.
371 //
372 {
373  delete _rep;
374 }
375 
376 
378 //
379 // Purpose: Test to see if parameter NAME exists.
380 //
381 // Inputs:
382 // name - The name of the parameter to look up.
383 //
384 // Returns:
385 // True if NAME exists.
386 //
387 {
388  std::string val;
389  return (_rep->_map.find(name) != _rep->_map.end());
390 
391 }
392 
393 
395 //
396 // Purpose: Get the value of NAME as an integer.
397 //
398 // Inputs:
399 // name - The name of the parameter to look up.
400 //
401 // Returns:
402 // The parameter's value as an integer.
403 //
404 {
405  return atoi (_rep->get_val (name).c_str());
406 }
407 
408 
410 //
411 // Purpose: Get the value of NAME as a float.
412 //
413 // Inputs:
414 // name - The name of the parameter to look up.
415 //
416 // Returns:
417 // The parameter's value as a float.
418 //
419 {
420  return atof (_rep->get_val (name).c_str());
421 }
422 
423 
425 //
426 // Purpose: Get the value of NAME as a bool.
427 //
428 // Inputs:
429 // name - The name of the parameter to look up.
430 //
431 // Returns:
432 // The parameter's value as a bool.
433 //
434 {
435  string val = _rep->get_val (name);
436  if (tolower (val[0]) == 't' || tolower (val[0]) == 'y')
437  return true;
438  else if (tolower (val[0]) == 'f' || tolower (val[0]) == 'n')
439  return false;
440  return !!get_int (name);
441 }
442 
443 
445 //
446 // Purpose: Get the value of NAME as a string.
447 //
448 // Inputs:
449 // name - The name of the parameter to look up.
450 //
451 // Returns:
452 // The parameter's value as a string.
453 //
454 {
455  return _rep->get_val (name);
456 }
457 
458 
459 std::ostream& operator<< (std::ostream& s, const Defaults_Text& def)
460 //
461 // Purpose: Dump out all parameter settings.
462 //
463 // Inputs:
464 // s - The stream to which we're writing.
465 // def - The instance to dump.
466 //
467 // Returns:
468 // The stream S.
469 //
470 {
471 
472  for (std::map<std::string,std::string>::const_iterator it = def._rep->_map.begin() ;
473  it != def._rep->_map.end() ;
474  it++) {
475  s << "[" << it->first << "] = [" << it->second << "]\n";
476  }
477 
478  return s;
479 }
480 
481 
482 } // namespace hitfit
Defaults_Textrep(string file)
Constructor, construct a Defaults_Textrep instance from an ASCII text-file and command line arguments...
int i
Definition: DBlmapReader.cc:9
A lightweight implementation of the Defaults interface that uses simple ASCII text files...
virtual bool exists(std::string name) const
The internal representation for a Defaults_Text object.
std::map< std::string, std::string > _map
uint16_t size_type
~Defaults_Text()
Destructor.
string get_val(string name) const
Defaults_Text(std::string def_file)
Constructor, create a Default_Text object from an ASCII text file. Pass an empty string to skip readi...
int j
Definition: DBlmapReader.cc:9
double f[11][100]
virtual double get_float(std::string name) const
virtual std::string get_string(std::string name) const
tuple argc
Definition: dir2webdir.py:38
Define a concrete interface for getting parameter settings from an ASCII text file.
virtual int get_int(std::string name) const
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)
JetCorrectorParameters::Definitions def
Definition: classes.h:6
virtual bool get_bool(std::string name) const