AceSmartSoft API Documentation
|
Ini-parameter management. More...
#include <smartIniParameter.hh>
Public Member Functions | |
SmartIniParameter () | |
Constructor. More... | |
virtual | ~SmartIniParameter () |
Destructor. More... | |
void | addEntry (std::string prefix, std::string name, const std::string &value) |
Appends an entry to the ini-parameter list. More... | |
void | addCommandLineArgs (const int &argc, char *const *argv, const std::string &default_prefix="component", const int &skip_args=1) |
Appends command line arguments to the ini-parameter list. More... | |
void | addEnv (const std::string &prefix, const std::string &env) |
Appends environment variable to the ini-parameter list. More... | |
void | addFile (const std::string &filename) |
Appends the contents of a ini-parameter file to the ini-parameter list. More... | |
bool | searchFile (const std::string &filename, std::ifstream &filestream) |
This method searches for the file using different search strategies and if found returns the filestream. More... | |
bool | tryAddFileFromArgs (const int &argc, char *const *argv, const std::string &long_opt="filename", const char &short_opt=-1) |
This is a wrapper for addFile(argc,argv,argname,false) without throwing an exception if no argument given. More... | |
void | addFile (int argc, char *const *argv, const std::string &argname, bool allow_stdin=true) |
Append the contents of an ini-parameter file to the ini-parameter list. More... | |
void | addFile (std::istream &is) |
Append the contents of an ini-parameter file to the ini-parameter list. More... | |
void | print (void) |
print all currently known parameters with their values More... | |
bool | checkIfParameterExists (const std::string &prefix, const std::string ¶meter) |
Search for an optional parameter in ini-file. More... | |
std::map< std::string, std::string > | getAllParametersFromGroup (const std::string &prefix) |
Get all parameter pairs (key,value) for a given parameter group. More... | |
std::map< std::string, std::list< std::string > > | getAllParameterListsFromGroup (const std::string &prefix) |
double | getDouble (std::string prefix, std::string parameter) const |
Retrieve double casted value of ini-parameter name. More... | |
std::list< double > | getDoubleList (std::string prefix, std::string parameter) const |
template<class Float > | |
bool | getDouble (const std::string &prefix, const std::string ¶meter, Float &d) const |
Retrieve double casted value of ini-parameter name. More... | |
template<class Float > | |
bool | getDoubleList (const std::string &prefix, const std::string ¶meter, std::list< Float > &d) const |
int | getInteger (std::string prefix, std::string parameter) const |
Retrieve int casted value of ini-parameter name. More... | |
std::list< int > | getIntegerList (std::string prefix, std::string parameter) const |
template<class Integer > | |
bool | getInteger (const std::string &prefix, const std::string ¶meter, Integer &i) const |
Retrieve int casted value of ini-parameter name. More... | |
template<class Integer > | |
bool | getIntegerList (const std::string &prefix, const std::string ¶meter, std::list< Integer > &i) const |
bool | getBoolean (std::string prefix, std::string parameter) const |
Retrieve truth value of ini-parameter name. More... | |
std::list< bool > | getBooleanList (std::string prefix, std::string parameter) const |
bool | getBoolean (const std::string &prefix, const std::string ¶meter, bool &b) const |
Retrieve truth value of ini-parameter name. More... | |
bool | getBooleanList (const std::string &prefix, const std::string ¶meter, std::list< bool > &b) const |
std::string | getString (std::string prefix, std::string parameter) const |
Retrieve string value of ini-parameter name. More... | |
std::list< std::string > | getStringList (std::string prefix, std::string parameter) const |
bool | getString (const std::string &prefix, const std::string ¶meter, std::string &s) const |
Retrieve string value of ini-parameter name. More... | |
bool | getStringList (const std::string &prefix, const std::string ¶meter, std::list< std::string > &s) const |
std::map< std::pair< std::string, std::string >, std::list< std::string > >::const_iterator | begin () |
std::map< std::pair< std::string, std::string >, std::list< std::string > >::const_iterator | end () |
Ini-parameter management.
Supports storage of IniParameters in separate ini-parameter files. Ini-parameter files can be read by this class which then provides simple access to all kinds of ini-parameters. Ini-parameters can also be extracted from environment variables or be defined by command line arguments.
The prefix is used to group ini-parameters and can also be set to the empty string. An ini-parameter therefore is always referenced by its prefix and its name.
Prefix and ini-parameter name are case insensitive !
Demonstrated in seventh example
SmartACE::SmartIniParameter::SmartIniParameter | ( | ) |
Constructor.
|
virtual |
Destructor.
void SmartACE::SmartIniParameter::addCommandLineArgs | ( | const int & | argc, |
char *const * | argv, | ||
const std::string & | default_prefix = "component" , |
||
const int & | skip_args = 1 |
||
) |
Appends command line arguments to the ini-parameter list.
If same entry already exists overwrite it with the new value. Format of command line ini-parameter: -parameter=value
void SmartACE::SmartIniParameter::addEntry | ( | std::string | prefix, |
std::string | name, | ||
const std::string & | value | ||
) |
Appends an entry to the ini-parameter list.
If same entry already exists overwrite it with the new value.
void SmartACE::SmartIniParameter::addEnv | ( | const std::string & | prefix, |
const std::string & | env | ||
) |
Appends environment variable to the ini-parameter list.
prefix | prefix of ini-parameter name (prefix.name) |
env | is the name of the environment variable. |
If same entry already exists overwrite it with the new value. Throw exception if environment variable unknown.
void SmartACE::SmartIniParameter::addFile | ( | const std::string & | filename | ) |
Appends the contents of a ini-parameter file to the ini-parameter list.
Entries which already exist are replaced by the new values. Throw exception if something went wrong (file does not exist or contains syntax errors).
Looks first in the current directory and then in $SMART_ROOT/etc
The file extension normally used for ini-parameter files is "ini". There is no maximum length per line, newlines may be escaped to concatenate successive lines. The syntax is as follows:
ALL ENTRIES ARE CASE INSENSITIVE !
The first line always contains the identification which is "[smartsoft]". For compatibility reasons we also accept "[bParamFile]" and "[smartParamFile]".
The first string in the line is the parameter name. Everything else following is the value excluding any comment. Comments are invoked by a "#" and end with the line.
Square brackets invoke the next group. If ini-parameters are defined before any group is named, then their group name is simply "" (empty group name).
Truth values can be defined in many different formats:
void SmartACE::SmartIniParameter::addFile | ( | int | argc, |
char *const * | argv, | ||
const std::string & | argname, | ||
bool | allow_stdin = true |
||
) |
Append the contents of an ini-parameter file to the ini-parameter list.
The name of the ini-parameter file is not directly given, but extracted from the argument list. So you can start multiple instances of the same program each with a different ini-parameter set.
The command line arguments are given by argc and argv and the string argname gives the name of the argument which contains the actual filename.
If allow_stdin is true
(default) and the value of argument argname is "stdin", the method will read the configuration data from stdin.
void SmartACE::SmartIniParameter::addFile | ( | std::istream & | is | ) |
Append the contents of an ini-parameter file to the ini-parameter list.
The stream is assumed to be connected to a ini-parameter file.
std::map<std::pair<std::string,std::string>, std::list<std::string> >::const_iterator SmartACE::SmartIniParameter::begin | ( | ) |
return the begin Iterator from our map.
subject to change without notice.
bool SmartACE::SmartIniParameter::checkIfParameterExists | ( | const std::string & | prefix, |
const std::string & | parameter | ||
) |
Search for an optional parameter in ini-file.
If the parameter could be found, then "true", otherwise "false", is returned. If parameter is not found, no exception is thrown, which makes it easier to check for existence of an optional parameter in the ini-file.
std::map<std::pair<std::string,std::string>, std::list<std::string> >::const_iterator SmartACE::SmartIniParameter::end | ( | ) |
return the end Iterator from our map.
subject to change without notice.
std::map<std::string, std::list<std::string> > SmartACE::SmartIniParameter::getAllParameterListsFromGroup | ( | const std::string & | prefix | ) |
std::map<std::string,std::string> SmartACE::SmartIniParameter::getAllParametersFromGroup | ( | const std::string & | prefix | ) |
Get all parameter pairs (key,value) for a given parameter group.
Iterates through all parameters and returns all (key,value) pairs that match the prefix (parameter group).
bool SmartACE::SmartIniParameter::getBoolean | ( | std::string | prefix, |
std::string | parameter | ||
) | const |
Retrieve truth value of ini-parameter name.
throw "IniParameterError" if ini-parameter not known or conversion fails
bool SmartACE::SmartIniParameter::getBoolean | ( | const std::string & | prefix, |
const std::string & | parameter, | ||
bool & | b | ||
) | const |
Retrieve truth value of ini-parameter name.
leave b alone and return false, if ini-parameter not known or conversion fails
std::list<bool> SmartACE::SmartIniParameter::getBooleanList | ( | std::string | prefix, |
std::string | parameter | ||
) | const |
bool SmartACE::SmartIniParameter::getBooleanList | ( | const std::string & | prefix, |
const std::string & | parameter, | ||
std::list< bool > & | b | ||
) | const |
double SmartACE::SmartIniParameter::getDouble | ( | std::string | prefix, |
std::string | parameter | ||
) | const |
Retrieve double casted value of ini-parameter name.
throw "IniParameterError" if ini-parameter not known or conversion fails
|
inline |
Retrieve double casted value of ini-parameter name.
leave d alone and return false, if ini-parameter not known or conversion fails
std::list<double> SmartACE::SmartIniParameter::getDoubleList | ( | std::string | prefix, |
std::string | parameter | ||
) | const |
|
inline |
int SmartACE::SmartIniParameter::getInteger | ( | std::string | prefix, |
std::string | parameter | ||
) | const |
Retrieve int casted value of ini-parameter name.
throw "IniParameterError" if ini-parameter not known or conversion fails
|
inline |
Retrieve int casted value of ini-parameter name.
leave i alone and return false, if ini-parameter not known or conversion fails
std::list<int> SmartACE::SmartIniParameter::getIntegerList | ( | std::string | prefix, |
std::string | parameter | ||
) | const |
|
inline |
std::string SmartACE::SmartIniParameter::getString | ( | std::string | prefix, |
std::string | parameter | ||
) | const |
Retrieve string value of ini-parameter name.
throw "IniParameterError" if parameter not known
bool SmartACE::SmartIniParameter::getString | ( | const std::string & | prefix, |
const std::string & | parameter, | ||
std::string & | s | ||
) | const |
Retrieve string value of ini-parameter name.
leave s alone and return false if ini-parameter not known
std::list<std::string> SmartACE::SmartIniParameter::getStringList | ( | std::string | prefix, |
std::string | parameter | ||
) | const |
bool SmartACE::SmartIniParameter::getStringList | ( | const std::string & | prefix, |
const std::string & | parameter, | ||
std::list< std::string > & | s | ||
) | const |
void SmartACE::SmartIniParameter::print | ( | void | ) |
print all currently known parameters with their values
bool SmartACE::SmartIniParameter::searchFile | ( | const std::string & | filename, |
std::ifstream & | filestream | ||
) |
This method searches for the file using different search strategies and if found returns the filestream.
filename | The file name to search for |
filestream | The filestream pointing to the content of the found file |
bool SmartACE::SmartIniParameter::tryAddFileFromArgs | ( | const int & | argc, |
char *const * | argv, | ||
const std::string & | long_opt = "filename" , |
||
const char & | short_opt = -1 |
||
) |
This is a wrapper for addFile(argc,argv,argname,false) without throwing an exception if no argument given.
This method checks whether the argv vector contains the long_opt and/or short_opt parameter. If not found a 'false' is returned without throwing an exception. If found the method addFile(argc,argv,argument_name,false) is called and if this succeeds 'true' is returned.