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 // $Id: Defaults_Text.cc,v 1.1 2011/05/26 09:46:59 mseidel Exp $
3 //
4 // File: src/Defaults_Text.cc
5 // Purpose: A lightweight implementation of the Defaults interface
6 // that uses simple text files.
7 // Created: Jul, 2000, sss.
8 //
9 // CMSSW File : src/Defaults_Text.cc
10 // Original Author : Scott Stuart Snyder <snyder@bnl.gov> for D0
11 // Imported to CMSSW by Haryo Sumowidagdo <Suharyo.Sumowidagdo@cern.ch>
12 //
13 
38 #include <cassert>
39 #include <cstdlib>
40 #include <fstream>
41 #include <iostream>
42 #include <cctype>
43 #include <cstring>
44 #include <map>
45 
46 using std::cerr;
47 using std::string;
48 using std::ifstream;
49 using std::getline;
50 using std::isspace;
51 using std::tolower;
52 using std::atoi;
53 using std::atof;
54 using std::abort;
55 using std::strchr;
56 using std::map;
57 
58 namespace {
59 
67 string strip (string s)
68 //
69 // Purpose: Remove comments (text starting with `;' or `#') and leading
70 // and trailing spaces from S.
71 //
72 // Inputs:
73 // s - The string to strip.
74 //
75 // Returns:
76 // The stripped string.
77 //
78 {
79  string::size_type j = s.find_first_of (";#");
80  if (j == string::npos)
81  j = s.size();
82 
83  while (j > 0 && isspace (s[j-1]))
84  --j;
85 
86  string::size_type i = 0;
87  while (i < j && isspace (s[i]))
88  ++i;
89 
90  return string (s, i, j-i);
91 }
92 
93 
94 } // unnamed namespace
95 
96 
97 namespace hitfit {
98 
99 
100 //***************************************************************************
101 
102 
108 //
109 // Purpose: The internal representation for a Defaults_Text object.
110 //
111 {
112 public:
113  // Constructor.
114 
122  Defaults_Textrep (string file);
132  Defaults_Textrep (string file, int argc, char** argv);
133 
134  // The data. Maps from parameter names to values (which are stored
135  // as strings).
140  std::map<std::string,std::string> _map;
141 
142  // Look up parameter NAME and return its value.
149  string get_val (string name) const;
150 
151 
152 private:
153  // Read parameters from FILE and add them to our data.
159  void read_file (string file);
160 
161  // Look for additional parameter settings in the argument list
162  // ARGC, ARGV and add them to our data.
169  void process_args (int argc, char** argv);
170 
171  // Helper to process a line defining a single parameter.
176  void doline (string l);
177 };
178 
179 
181 //
182 // Purpose: Constructor.
183 //
184 // Inputs:
185 // file - The name of the defaults file to read.
186 // See the comments in the header for a description
187 // of the format for this and for the argument list.
188 // Pass an empty string to skip reading a file.
189 //
190 {
191  read_file (file);
192 }
193 
194 
196 //
197 // Purpose: Constructor.
198 //
199 // Inputs:
200 // file - The name of the defaults file to read.
201 // See the comments in the header for a description
202 // of the format for this and for the argument list.
203 // Pass an empty string to skip reading a file.
204 // argc - The arglist length.
205 // argv - The arglist.
206 //
207 {
208  read_file (file);
209  process_args (argc, argv);
210 }
211 
212 
214 //
215 // Purpose: Read parameters from FILE and add them to our data.
216 //
217 // Inputs:
218 // s - The name of the file to read.
219 //
220 {
221  // Just return if we weren't given a file.
222  if (file.size() == 0)
223  return;
224 
225  ifstream f (file.c_str());
226  if (!f.good()) {
227  cerr << "Can't open " << file << "\n";
228  abort ();
229  }
230 
231  string l;
232  while (getline (f, l)) {
233  doline (l);
234  }
235 
236  f.close ();
237 }
238 
239 
241 //
242 // Purpose: Process the argument list ARGC, ARGV and add additional
243 // parameters from it to our data (possibly overriding
244 // existing settings). See the header file for more details.
245 //
246 // Inputs:
247 // argc - The arglist length.
248 // argv - The arglist.
249 //
250 {
251  // Look for arguments starting with `--'.
252  for (int i=1; i < argc; i++) {
253  if (argv[i][0] == '-' && argv[i][1] == '-') {
254 
255  // Found one.
256  string l;
257  if (strchr (argv[i], '=') != 0)
258  // It was of the form `--NAME=VALUE'. Change to `NAME=VALUE'.
259  l = argv[i] + 2;
260  else if (argv[i][2] == 'n' && argv[i][3] == 'o') {
261  // It was of the form `--noNAME'. Change to `NAME=0'.
262  l = argv[i] + 4;
263  l += "=0";
264  }
265  else {
266  // It was of the form `--NAME'. Change to `NAME=1'.
267  l = argv[i] + 2;
268  l += "=1";
269  }
270 
271  // Process it like a line we read from a file.
272  doline (l);
273  }
274  }
275 }
276 
277 
278 string Defaults_Textrep::get_val (string name) const
279 //
280 // Purpose: Look up parameter NAME and return its value.
281 // The parameter must exist.
282 //
283 // Inputs:
284 // name - The name of the parameter.
285 //
286 // Returns:
287 // The value of the parameter.
288 //
289 {
290 
291  std::string val;
292 
293  if (_map.find(name) == _map.end()) {
294  cerr << "can't find default for " << name << "\n";
295  abort ();
296  } else {
297  std::map<string,string>::const_iterator it = _map.find(name);
298  val = it->second;
299  }
300 
301  return val;
302 }
303 
304 
306 //
307 // Purpose: Helper to process a line defining a single parameter.
308 //
309 // Inputs:
310 // l - The line to process.
311 //
312 {
313  // Strip spaces from the line and ignore it if it's blank.
314  l = strip (l);
315  if (l.size() == 0)
316  return;
317 
318  // It must contain a `=' character.
319  string::size_type pos = l.find ('=');
320  if (pos == string::npos) {
321  cerr << "bad defaults line " << l << "\n";
322  abort ();
323  }
324 
325  // Split off name and value parts.
326  std::string name = strip (l.substr (0, pos));
327  std::string val = strip (l.substr (pos+1));
328 
329  // Add it to the map.
330  _map[name] = val;
331 
332 }
333 
334 
335 //***************************************************************************
336 
337 
339 //
340 // Purpose: Constructor.
341 //
342 // Inputs:
343 // def_file - The name of the defaults file to read.
344 // See the comments in the header for a description
345 // of the format for this and for the argument list.
346 // Pass an empty string to skip reading a file.
347 //
348  : _rep (new Defaults_Textrep (def_file))
349 {
350 }
351 
352 
354 //
355 // Purpose: Constructor.
356 //
357 // Inputs:
358 // def_file - The name of the defaults file to read.
359 // See the comments in the header for a description
360 // of the format for this and for the argument list.
361 // Pass an empty string to skip reading a file.
362 // argc - The arglist length.
363 // argv - The arglist.
364 //
365  : _rep (new Defaults_Textrep (def_file, argc, argv))
366 {
367 }
368 
370 //
371 // Purpose: Destructor.
372 //
373 {
374  delete _rep;
375 }
376 
377 
379 //
380 // Purpose: Test to see if parameter NAME exists.
381 //
382 // Inputs:
383 // name - The name of the parameter to look up.
384 //
385 // Returns:
386 // True if NAME exists.
387 //
388 {
389  std::string val;
390  return (_rep->_map.find(name) != _rep->_map.end());
391 
392 }
393 
394 
396 //
397 // Purpose: Get the value of NAME as an integer.
398 //
399 // Inputs:
400 // name - The name of the parameter to look up.
401 //
402 // Returns:
403 // The parameter's value as an integer.
404 //
405 {
406  return atoi (_rep->get_val (name).c_str());
407 }
408 
409 
411 //
412 // Purpose: Get the value of NAME as a float.
413 //
414 // Inputs:
415 // name - The name of the parameter to look up.
416 //
417 // Returns:
418 // The parameter's value as a float.
419 //
420 {
421  return atof (_rep->get_val (name).c_str());
422 }
423 
424 
426 //
427 // Purpose: Get the value of NAME as a bool.
428 //
429 // Inputs:
430 // name - The name of the parameter to look up.
431 //
432 // Returns:
433 // The parameter's value as a bool.
434 //
435 {
436  string val = _rep->get_val (name);
437  if (tolower (val[0]) == 't' || tolower (val[0]) == 'y')
438  return true;
439  else if (tolower (val[0]) == 'f' || tolower (val[0]) == 'n')
440  return false;
441  return !!get_int (name);
442 }
443 
444 
446 //
447 // Purpose: Get the value of NAME as a string.
448 //
449 // Inputs:
450 // name - The name of the parameter to look up.
451 //
452 // Returns:
453 // The parameter's value as a string.
454 //
455 {
456  return _rep->get_val (name);
457 }
458 
459 
460 std::ostream& operator<< (std::ostream& s, const Defaults_Text& def)
461 //
462 // Purpose: Dump out all parameter settings.
463 //
464 // Inputs:
465 // s - The stream to which we're writing.
466 // def - The instance to dump.
467 //
468 // Returns:
469 // The stream S.
470 //
471 {
472 
473  for (std::map<std::string,std::string>::const_iterator it = def._rep->_map.begin() ;
474  it != def._rep->_map.end() ;
475  it++) {
476  s << "[" << it->first << "] = [" << it->second << "]\n";
477  }
478 
479  return s;
480 }
481 
482 
483 } // 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
dictionary map
Definition: Association.py:205
~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:41
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:12
virtual bool get_bool(std::string name) const