openModeller  Version 1.5.0
Configuration.cpp
Go to the documentation of this file.
1 
29 
30 #include <iostream>
31 #include <functional>
32 #include <algorithm>
33 #include <sstream>
34 #include <iterator>
35 
36 #include <openmodeller/Sample.hh>
38 
39 using namespace std;
40 
41 /***********************************************************************************
42  *
43  * Helper code for this module
44  *
45  **********************************************************************************/
46 
47 // Functor to test attribute name
49 public:
50  AttributeNameTest( const string& name ) :
51  name( name )
52  {}
54  std::string cmp_name = a.first;
55  size_t prefix_pos = cmp_name.find(":");
56  if ( prefix_pos != string::npos ) {
57  // if there is a namespace prefix, remove it
58  cmp_name = cmp_name.substr( prefix_pos+1 );
59  }
60  return name == cmp_name;
61  }
62  string name;
63 };
64 
65 // Functor to test a configuration's name.
67 public:
68  ConfigurationNameTest( const string& name ) :
69  name( name )
70  {}
71  bool operator()( const ConfigurationPtr &config ) {
72  return name == config->getName();
73  }
74  string name;
75 };
76 
77 // Trim characters from front and back of a string.
78 static void trim( string& str, char const* delims = "\t\r\n ") {
79 
80  string::size_type index = str.find_last_not_of(delims);
81  if( index != string::npos )
82  str.erase(++index);
83 
84  index = str.find_first_not_of( delims );
85  if( index != string::npos )
86  str.erase(0,index);
87  else
88  str.erase();
89 }
90 
91 // Method to get integer from string
92 int ConfigurationImpl::getInt( const string& str, int defaultValue ) {
93  int returnValue = defaultValue;
94  sscanf( str.c_str(), "%d", &returnValue);
95  return returnValue;
96 }
97 
98 // Method to get double from string
99 double ConfigurationImpl::getDouble( const string& str, double defaultValue ) {
100  double returnValue = defaultValue;
101  sscanf( str.c_str(), "%lf", &returnValue );
102  return returnValue;
103 }
104 
105 // Method to get Sample from string
106 Sample ConfigurationImpl::getSample( const string& str ) {
107  stringstream ss( str, ios::in );
108  Sample s;
109  ss >> s;
110  return s;
111 }
112 
113 /***********************************************************************************
114  *
115  * ConfigurationImpl methods.
116  *
117  **********************************************************************************/
120  name(),
121  value(),
122  subsections(),
123  attributes()
124 {}
125 
128  name( name ),
129  value(),
130  subsections(),
131  attributes()
132 {}
133 
135 {}
136 
137 string
139 
140  size_t prefix_pos = name.find(":");
141 
142  if ( prefix_pos != string::npos ) {
143 
144  // if there is a namespace prefix, remove it
145  return name.substr( prefix_pos+1 );
146  }
147 
148  return name;
149 }
150 
151 void
152 ConfigurationImpl::setValue( const string& val )
153 {
154  value = val;
155  trim( value );
156 }
157 
158 string
160  return this->value;
161 }
162 
164 ConfigurationImpl::getSubsection( const string & name, bool throws ) const
165 {
166  ConfigurationNameTest tester(name);
167  Configuration::subsection_list::const_iterator c = find_if( subsections.begin(), subsections.end(), tester );
168 
169  // Subsection found.
170  if ( c != subsections.end() ) {
171 
172  return ConstConfigurationPtr( *c );
173  }
174 
175  // Subsection not found
176  if ( throws ) {
177 
178  throw SubsectionNotFound( name );
179  }
180 
181  return ConstConfigurationPtr();
182 }
183 
185 ConfigurationImpl::getSubsection( const string & name, bool throws )
186 {
187  ConfigurationNameTest tester(name);
188  Configuration::subsection_list::iterator c = find_if( subsections.begin(), subsections.end(), tester );
189 
190  // Subsection found.
191  if ( c != subsections.end() ) {
192 
193  return ConstConfigurationPtr( *c );
194  }
195 
196  // Subsection not found
197  if ( throws ) {
198  throw SubsectionNotFound( name );
199  }
200 
201  return ConstConfigurationPtr();
202 }
203 
204 void
206 {
207  subsections.push_back( config );
208 }
209 
210 string
211 ConfigurationImpl::getAttribute( const string & name ) const
212 {
213  AttributeNameTest tester( name );
214  Configuration::attribute_list::const_iterator nv = find_if( attributes.begin(), attributes.end(), tester );
215 
216  if ( nv == attributes.end() ) {
217 
218  throw AttributeNotFound( name );
219  }
220 
221  return nv->second;
222 }
223 
224 string
225 ConfigurationImpl::getAttribute( const string & name, const string& defaultValue ) const
226 {
227  AttributeNameTest tester( name );
228  Configuration::attribute_list::const_iterator nv = find_if( attributes.begin(), attributes.end(), tester );
229 
230  if ( nv == attributes.end() ) {
231 
232  return defaultValue;
233  }
234 
235  return nv->second;
236 }
237 
238 template<typename T>
239 vector<T>
240 ConfigurationImpl::getAttributeAsVec( const string & name ) const {
241 
242  string val = getAttribute( name );
243 
244  stringstream ss( val, ios::in );
245 
246  vector<T> v;
247 
248  istream_iterator<T> end;
249  for ( istream_iterator<T> is_it(ss); is_it != end; is_it ++ ) {
250 
251  v.push_back( *is_it );
252  }
253 
254  return v;
255 }
256 
257 int
258 ConfigurationImpl::getAttributeAsInt( const string & name, int defaultvalue ) const {
259 
260  string val = getAttribute( name, "" );
261 
262  int returnValue = defaultvalue;
263 
264  sscanf( val.c_str(), "%d", &returnValue );
265 
266  return returnValue;
267 }
268 
269 double
270 ConfigurationImpl::getAttributeAsDouble( const string & name, double defaultvalue ) const {
271  string val = getAttribute( name, "" );
272 
273  double returnValue = defaultvalue;
274 
275  sscanf( val.c_str(), "%lf", &returnValue );
276 
277  return returnValue;
278 }
279 
280 vector<double>
281 ConfigurationImpl::getAttributeAsVecDouble( const string & name ) const {
282 
283  string val = getAttribute( name );
284 
285  stringstream ss( val, ios::in );
286 
287  vector<double> v;
288 
289  istream_iterator<double> end;
290  for ( istream_iterator<double> is_it(ss);
291  is_it != end;
292  is_it ++ ) {
293 
294  v.push_back( *is_it );
295  }
296 
297  return v;
298 }
299 
300 void
301 ConfigurationImpl::getAttributeAsDoubleArray( const string & name, double **arry, int *dim ) const {
302 
303  vector<double> vec = getAttributeAsVecDouble( name );
304 
305  if ( dim ) {
306 
307  *dim = vec.size();
308  }
309 
310  if ( arry ) {
311 
312  int end = vec.size();
313 
314  *arry = new double[ end ];
315 
316  for (int i=0; i<end; i++ ) {
317  (*arry)[i] = vec[i];
318  }
319  }
320 }
321 
322 vector<int>
323 ConfigurationImpl::getAttributeAsVecInt( const string & name ) const {
324 
325  string val = getAttribute( name );
326 
327  stringstream ss( val, ios::in );
328 
329  vector<int> v;
330 
331  istream_iterator<int> end;
332  for ( istream_iterator<int> is_it(ss); is_it != end; is_it ++ ) {
333 
334  v.push_back( *is_it );
335  }
336 
337  return v;
338 }
339 
340 void
341 ConfigurationImpl::getAttributeAsIntArray( const string & name, int **arry, int *dim ) const {
342 
343  vector<int> vec = getAttributeAsVecInt( name );
344 
345  if ( dim ) {
346 
347  *dim = vec.size();
348  }
349 
350  if ( arry ) {
351 
352  int end = vec.size();
353 
354  *arry = new int[ end ];
355 
356  for ( int i=0; i<end; i++ ) {
357 
358  (*arry)[i] = vec[i];
359  }
360  }
361 }
362 
363 Sample
364 ConfigurationImpl::getAttributeAsSample( const string & name ) const {
365 
366  string v = getAttribute( name );
367 
368  stringstream is(v, ios::in );
369 
370  Sample sample;
371 
372  is >> sample;
373 
374  return sample;
375 }
376 
377 void
378 ConfigurationImpl::getAttributeAsByteArray( const string & name, unsigned char **arry, int *dim ) const {
379 
380  vector<int> vec = getAttributeAsVec<int>( name );
381 
382  if ( dim ) {
383 
384  *dim = vec.size();
385  }
386 
387  if ( arry ) {
388 
389  int end = vec.size();
390 
391  *arry = new unsigned char[ end ];
392 
393  for ( int i=0; i<end; i++ ) {
394 
395  (*arry)[i] = (unsigned char) (vec[i] & 0x00FF) ;
396  }
397  }
398 }
399 
400 void
401 ConfigurationImpl::addNameValue( const string & name, const string & value ) {
402 
403  string theval = value;
404  trim(theval);
405 
406  attributes.push_back( make_pair( name, theval ) );
407 }
408 
409 void
410 ConfigurationImpl::addNameValue( const string & name, char const *value ) {
411 
412  string sval( (value)? value: "");
413 
414  addNameValue( name, sval );
415 }
416 
417 void
418 ConfigurationImpl::addNameValue( const string & name, int value ) {
419 
420  stringstream ss(ios::out);
421 
422  ss << value;
423 
424  addNameValue( name, ss.str() );
425 }
426 
427 void
428 ConfigurationImpl::addNameValue( const string & name, double value, int precision ) {
429 
430  stringstream ss(ios::out);
431  ss.precision(precision);
432 
433  ss << value;
434 
435  addNameValue( name, ss.str() );
436 }
437 
438 void
439 ConfigurationImpl::addNameValue( const string & name, double const *values, int count, int precision ) {
440 
441  stringstream ss(ios::out);
442  ss.precision(precision);
443 
444  for ( int i=0; i<count; i++ ) {
445  ss << *values++ << " ";
446  }
447 
448  addNameValue( name, ss.str() );
449 }
450 
451 void
452 ConfigurationImpl::addNameValue( const string & name, int const *values, int count ) {
453 
454  stringstream ss(ios::out);
455 
456  for ( int i=0; i<count; i++ ) {
457  ss << *values++ << " ";
458  }
459 
460  addNameValue( name, ss.str() );
461 }
462 
463 void
464 ConfigurationImpl::addNameValue( const string & name, const Sample& value ) {
465 
466  stringstream ss(ios::out);
467 
468  ss << value;
469 
470  addNameValue( name, ss.str() );
471 }
472 
473 void
474 ConfigurationImpl::addNameValue( const string & name, unsigned char const *values, int count ) {
475 
476  stringstream ss(ios::out);
477 
478  for ( int i=0; i<count; i++ ) {
479 
480  ss << (int)*values++ << " ";
481  }
482 
483  addNameValue( name, ss.str() );
484 }
485 
static double getDouble(const std::string &str, double defaultValue)
double getAttributeAsDouble(const std::string &name, double defaultValue) const
void getAttributeAsIntArray(const std::string &name, int **, int *) const
ReferenceCountedPointer< const ConfigurationImpl > ConstConfigurationPtr
void getAttributeAsDoubleArray(const std::string &name, double **, int *) const
void getAttributeAsByteArray(const std::string &name, unsigned char **, int *) const
bool operator()(const Configuration::attribute &a)
std::string getValue() const
int getAttributeAsInt(const std::string &name, int defaultValue) const
std::vector< T > getAttributeAsVec(const std::string &name) const
static Sample getSample(const std::string &str)
AttributeNameTest(const string &name)
Configuration::subsection_list subsections
std::string getName() const
std::vector< int > getAttributeAsVecInt(const std::string &name) const
std::string getAttribute(const std::string &name) const
ConfigurationNameTest(const string &name)
std::vector< double > getAttributeAsVecDouble(const std::string &name) const
void setValue(const std::string &)
void addNameValue(const std::string &name, const std::string &value)
static int getInt(const std::string &str, int defaultValue)
std::pair< std::string, std::string > attribute
Sample getAttributeAsSample(const std::string &name) const
Configuration::attribute_list attributes
bool operator()(const ConfigurationPtr &config)
static void trim(string &str, char const *delims="\t\r\n ")
ConstConfigurationPtr getSubsection(const std::string &name, bool throws=true) const
Definition: Sample.hh:25
void addSubsection(const ConfigurationPtr &config)