A utility class to parse L-Studio like parameter files. More...
#include <util/parms.h>
Public Member Functions | |
bool | all (const QString §ion, std::map< QString, QStringList > &value) |
Retrieve a all parameters with same [section]key. | |
bool | all (const QString §ion, std::map< QString, std::vector< QString > > &value) |
Retrieve a all parameters with same [section]key. | |
bool | all (const QString §ion, std::map< QString, std::vector< std::string > > &value) |
Retrieve a all parameters with same [section]key. | |
bool | all (const QString §ion, std::map< QString, std::vector< double > > &value) |
Retrieve a all parameters with same [section]. | |
bool | all (const QString §ion, std::map< QString, std::vector< float > > &value) |
Retrieve a all parameters with same [section]. | |
bool | all (const QString §ion, std::map< QString, std::vector< int > > &value) |
Retrieve a all parameters with same [section]. | |
bool | all (const QString §ion, std::map< QString, std::vector< bool > > &value) |
Retrieve a all parameters with same [section]. | |
template<typename T , typename Container > | |
bool | all (const QString §ion, std::map< QString, Container > &values) |
This operator retrieves all parameters with same [section]. | |
bool | all (const QString §ion, const QString &key, QStringList &value) |
Retrieve a all parameters with same [section]key. | |
bool | all (const QString §ion, const QString &key, std::vector< QString > &value) |
Retrieve a all parameters with same [section]key. | |
bool | all (const QString §ion, const QString &key, std::vector< std::string > &value) |
Retrieve a all parameters with same [section]key. | |
bool | all (const QString §ion, const QString &key, std::vector< double > &value) |
Retrieve a all parameters with same [section]key. | |
bool | all (const QString §ion, const QString &key, std::vector< float > &value) |
Retrieve a all parameters with same [section]key. | |
bool | all (const QString §ion, const QString &key, std::vector< int > &value) |
Retrieve a all parameters with same [section]key. | |
bool | all (const QString §ion, const QString &key, std::vector< bool > &value) |
Retrieve a all parameters with same [section]key. | |
template<typename Container > | |
bool | all (const QString §ion, const QString &key, Container &value) |
This operator retrieves all parameters with same [section]key. | |
bool | isLoaded () const |
Returns true if the parameter object has been correctly loaded. | |
template<typename T > | |
bool | operator() (const QString §ion, const QString &key, T &value, const T &def) |
Variation on the previous, but if the [section]key is not found, an information message is issued (instead of an error) and value is set up to def . | |
bool | operator() (const QString §ion, const QString &key, QString &value) |
Retrieve a single parameter. | |
bool | operator() (const QString §ion, const QString &key, std::string &value) |
Retrieve a single parameter. | |
bool | operator() (const QString §ion, const QString &key, double &value) |
Retrieve a single parameter. | |
bool | operator() (const QString §ion, const QString &key, float &value) |
Retrieve a single parameter. | |
bool | operator() (const QString §ion, const QString &key, int &value) |
Retrieve a single parameter. | |
bool | operator() (const QString §ion, const QString &key, bool &value) |
Retrieve a single parameter. | |
template<typename T > | |
bool | operator() (const QString §ion, const QString &key, T &value) |
This operator retrieve a single parameter. | |
Parms (const QString &parmFile, int verboseLevel=1) | |
Constructor of the parameter file. | |
void | verboseLevel (int vl) |
Change the verbosity level. |
A utility class to parse L-Studio like parameter files.
The basic information in the parameter file is a key associated to a value, possibly with a C++-like comment:
key: value // Comment
Keys can be organized in sections:
[Section1] key1: value1 key2: value2 ... [Section2] key1: value3 key2: value4 ...
Empty line or comment-only line are ignored, any other line will raise an error. The default section is named with an empty string "".
Here is an example:
int a,b; QString s1, s2; std::vector<double> all_d; util::Parms parms( "view.v", 2 ); // First read simple parameters parms( "", "a", a ); parms( "Main", "b", b, 0 ); parms( "Main", "string1", s1 ); parms( "Main", "string2", s2 ); // Then read all the keys "double" in section "Values" parms.all( "Values, "double", all_d);
To read user-defined typed as parameter, it is enough to overload the function
QTextStream& operator>>( QTextStream&, const T& ).
T
being the type of the parameter to read.
If the same key is used may times in the same section, either all the values can be retrieved using the Parms::all() method, or only the last can be retrieved using the normal Parms::operator()().
The class accepts 5 verbosity levels:
Debug information output the raw string before evaluation to set up a parameter.
Definition at line 105 of file parms.h.
util::Parms::Parms | ( | const QString & | parmFile, | |
int | verboseLevel = 1 | |||
) |
Constructor of the parameter file.
Definition at line 27 of file parms.cpp.
References verboseLevel().
00028 : ParmFileName( parmFile ) 00029 { 00030 verboseLevel( vl ); 00031 init(); 00032 }
bool util::Parms::all | ( | const QString & | section, | |
std::map< QString, QStringList > & | value | |||
) |
bool util::Parms::all | ( | const QString & | section, | |
std::map< QString, std::vector< std::string > > & | value | |||
) |
Retrieve a all parameters with same [section]key.
bool util::Parms::all | ( | const QString & | section, | |
std::map< QString, Container > & | values | |||
) | [inline] |
This operator retrieves all parameters with same [section].
section | Section in which the parameter is looked for | |
value | Variable to set up |
value
is filled with the different keys found in [section] and, for each key, the vector of all the values associated to it. If none, value
is simply empty. The only error that can arise is a reading error, if one parameter has invalid value. This parameter will simply be ignored, all other parameters being read.
Definition at line 561 of file parms.h.
References QString::clear(), forall, and QString::size().
00562 { 00563 bool valid = true; 00564 CheckExist = false; 00565 typedef std::map<QString, QStringList>::value_type value_type; 00566 result.clear(); 00567 int pos = section.size()+1; 00568 forall( const value_type& pair, Parameters) 00569 { 00570 QString sec(pair.first.mid(0, pos-1)); 00571 if(sec != section) 00572 continue; 00573 QString key(pair.first.mid(pos)); 00574 Container& value = result[key]; 00575 value.clear(); 00576 forall( const QString& val, pair.second ) 00577 { 00578 T single_value = T(); 00579 if( readValue( val, single_value ) ) 00580 { 00581 value.push_back( single_value ); 00582 } 00583 else 00584 { 00585 if( VerboseLevel > 2 ) 00586 { 00587 err << "Parms::all:Error reading key [" << section << "]" << key 00588 << " with value " << val << endl; 00589 } 00590 valid = false; 00591 } 00592 } 00593 } 00594 CheckExist = true; 00595 return valid; 00596 }
bool util::Parms::all | ( | const QString & | section, | |
const QString & | key, | |||
QStringList & | value | |||
) |
bool util::Parms::all | ( | const QString & | section, | |
const QString & | key, | |||
std::vector< std::string > & | value | |||
) |
Retrieve a all parameters with same [section]key.
bool util::Parms::all | ( | const QString & | section, | |
const QString & | key, | |||
Container & | value | |||
) | [inline] |
This operator retrieves all parameters with same [section]key.
section | Section in which the parameter is looked for | |
key | Key to look for | |
value | Variable to set up |
value
is filled with the different values found having same [section]key. If none, value
is simply empty. The only error that can arise is a reading error, if one parameter has invalid value. This parameter will simply be ignored, all other parameters being read.
Definition at line 529 of file parms.h.
References forall.
Referenced by util::GraphInset::readParms(), cell_system::CellSystem< TissueClass, RealModel >::readRules(), and cell_system::CellSystem< TissueClass, RealModel >::readSymbols().
00530 { 00531 bool valid = true; 00532 typedef typename Container::value_type T; 00533 CheckExist = false; 00534 QStringList values; 00535 if( !extractValues( section, key, values ) ) 00536 return false; 00537 value.clear(); 00538 std::insert_iterator<Container> it(value, value.end()); 00539 forall( const QString& val, values ) 00540 { 00541 T single_value = T(); 00542 if( readValue( val, single_value ) ) 00543 { 00544 *it++ = (const T&)single_value; 00545 } 00546 else 00547 { 00548 if( VerboseLevel > 2 ) 00549 { 00550 err << "Parms::all:Error reading key [" << section << "]" << key 00551 << " with value " << val << "\n" << flush; 00552 } 00553 valid = false; 00554 } 00555 } 00556 CheckExist = true; 00557 return valid; 00558 }
bool util::Parms::isLoaded | ( | ) | const [inline] |
bool util::Parms::operator() | ( | const QString & | section, | |
const QString & | key, | |||
T & | value, | |||
const T & | def | |||
) | [inline] |
Variation on the previous, but if the [section]key is not found, an information message is issued (instead of an error) and value
is set up to def
.
Definition at line 510 of file parms.h.
00511 { 00512 bool found = true; 00513 CheckExist = false; 00514 if( !( *this )( section, key, value ) ) 00515 { 00516 found = false; 00517 if( VerboseLevel > 2 ) 00518 { 00519 err << "Parms::operator()::Info key [" << section << "]" 00520 << key << " not found, using default value" << endl; 00521 } 00522 value = def; 00523 } 00524 CheckExist = true; 00525 return found; 00526 }
Retrieve a single parameter.
For string, the value is the whole line with whitespaces and comment stripped before and after the parameter.
Retrieve a single parameter.
Retrieve a single parameter.
Retrieve a single parameter.
Retrieve a single parameter.
Boolean value must read "true" or "false", case being meaningless.
bool util::Parms::operator() | ( | const QString & | section, | |
const QString & | key, | |||
T & | value | |||
) | [inline] |
This operator retrieve a single parameter.
section | Section in which the parameter is looked for | |
key | Key to look for | |
value | Variable to set up if possible |
If the [section]key exists, value
is set up to the parameter value. Any key placed before the first section is considered in the first secion. If the [section]key is multiply defined, only the last one is taken and a warning is issued (see Verbosity). If the [section]key does not exist, or its content cannot be interpreted as the requested type, then ar error is issued.
Definition at line 485 of file parms.h.
00486 { 00487 QStringList values; 00488 if( !extractValues( section, key, values ) ) 00489 return false; 00490 00491 if( ( values.size() > 1 ) && ( VerboseLevel > 1 ) ) 00492 { 00493 err << "Parms::operator():Warning multiple value for key [" << section << "]" 00494 << key << ", last one used.\n" << flush; 00495 } 00496 00497 if( !readValue( values.back(), value ) ) 00498 { 00499 if( VerboseLevel > 0 ) 00500 { 00501 err << "Parms::operator():Error getting value for key [" << section << "]" << key 00502 << " value " << values.back() << "\n" << flush; 00503 } 00504 return false; 00505 } 00506 return true; 00507 }
void util::Parms::verboseLevel | ( | int | vl | ) | [inline] |