Diana Software
QOptionHandler.cc
Go to the documentation of this file.
1 /*
2 * APOLLO: A complete DAQ and Online Data Analysis Framework for Diana
3 * $Id: QOptionHandler.cc 557 2006-12-14 17:00:51Z giacher $
4 * M.P. created 16/4/4
5 *
6 * Class QOptionHandler: inline option parser tool
7 *
8 */
9 
10 #include <stdlib.h>
11 #include <stdexcept>
12 #include <string>
13 #include "QOptionHandler.hh"
14 
15 
16 
18 
19 
20 //______________________________________________________________________________
22 {
23  //
24  // ctor
25  //
26 
27  return;
28 }
29 
30 
31 //______________________________________________________________________________
33 {
34  //
35  // dtor
36  //
37 
38  return;
39 }
40 
41 
42 //______________________________________________________________________________
44 {
45  //
46  // sigleton
47  //
48 
49  if ( !me )
50  me = new QOptionHandler();
51 
52  return me;
53 }
54 
55 //______________________________________________________________________________
56 void QOptionHandler::Parse(int n, char** par)
57 {
58  //
59  // parse strings and put option variables into a map
60  // input is of type argc,argv therefore first param is ignored
61  //
62 
63 
64  for(int i=1; i<n; i++)
65  {
66  if ( par[i] )
67  {
68  std::string s = par[i];
69 
70  std::string::size_type pos;
71  if ( (pos = s.find('=')) == std::string::npos )
72  throw std::runtime_error("Option Handler: bad option in "+std::string(par[0])+" :"+s);
73 
74  std::string key = s.substr(0,pos);
75  std::string value = s.substr(pos+1,std::string::npos);
76 
77  // try converting to an integer
78  char *p=0;
79  int val_i = ::strtol(value.c_str(),&p,0);
80  if ( *p=='\0' )
81  { // it is an integer
82  i_options[key] = val_i;
83  }
84  else
85  {
86  p=0;
87  double val_d = ::strtod(value.c_str(),&p);
88  if (val_d==0 || value.c_str()==p)
89  {
90  // it is not a double
91  s_options[key] = value;
92  }
93  else
94  {
95  // it is a double
96  d_options[key] = val_d;
97  }
98  }
99  }
100  }
101  return;
102 }
103 
104 //______________________________________________________________________________
105 const std::string& QOptionHandler::GetStringParam(const std::string& name)
106 {
107  //
108  // get a string parameter
109  //
110 
111  if (s_options.find(name) != s_options.end())
112  return s_options[name];
113  else
114  throw std::runtime_error("Unknown string parameter "+name);
115 }
116 
117 
118 //______________________________________________________________________________
119 int QOptionHandler::GetIntParam(const std::string& name)
120 {
121  //
122  // get an integer parameter
123  //
124  if (i_options.find(name) != i_options.end())
125  return i_options[name];
126  else
127  throw std::runtime_error("Unknown integer parameter "+name);
128 }
129 
130 
131 //______________________________________________________________________________
132 double QOptionHandler::GetRealParam(const std::string& name)
133 {
134  //
135  // get a double parameter
136  //
137 
138  if (d_options.find(name) != d_options.end())
139  return d_options[name];
140  else
141  throw std::runtime_error("Unknown double parameter "+name);
142 }
143 
144 
145 //______________________________________________________________________________
146 bool QOptionHandler::CheckParam(const std::string& name)
147 {
148  //
149  // check existance
150  //
151 
152  if (s_options.find(name) != s_options.end())
153  return true;
154  if (d_options.find(name) != d_options.end())
155  return true;
156  if (i_options.find(name) != i_options.end())
157  return true;
158 
159  return false;
160 }
161 
162 
163 //______________________________________________________________________________
164 void QOptionHandler::Dump(std::ostream& c)
165 {
166  //
167  // dump all parameters on stream
168  //
169 
170  std::map<std::string,int>::const_iterator it1 = i_options.begin();
171  c << "Integer parameters\n";
172  for( ; it1!=i_options.end(); it1++)
173  {
174  c << " "<<it1->first << " = " << it1->second << std::endl;
175  }
176 
177  std::map<std::string,double>::const_iterator it2 = d_options.begin();
178  c << "Double parameters\n";
179  for( ; it2!=d_options.end(); it2++)
180  {
181  c << " " << it2->first << " = " << it2->second << std::endl;
182  }
183 
184  std::map<std::string,std::string>::const_iterator it3 = s_options.begin();
185  c << "String parameters\n";
186  for( ; it3!=s_options.end(); it3++)
187  {
188  c << " "<<it3->first << " = " << it3->second << std::endl;
189  }
190 
191  return;
192 }
193 
bool CheckParam(const std::string &name)
void Dump(std::ostream &c=std::cout)
static QOptionHandler * me
void Parse(int, char **)
std::map< std::string, int > i_options
int GetIntParam(const std::string &name)
std::map< std::string, std::string > s_options
double GetRealParam(const std::string &name)
virtual ~QOptionHandler()
const std::string & GetStringParam(const std::string &name)
std::map< std::string, double > d_options
static QOptionHandler * Get()