Configuration handler & parameter file parser.
The Config class parses parameter files and stores value-parameter pairs. These files may contain an arbitrary number of parameters in any order of the following form:
key value;
Additionally, the files may contain an arbitrary number of parameter blocks of the following form:
key { ... },
which can again contain an arbitrary number of parameters and parameter blocks in any order.
During the parameter extraction, the file parser checks for duplicate parameters within a single parameter block (which also includes the global block). Furthermore, the file parser is case insensitive with respect to the used keys. Therefore 'key', 'KEY' and 'Key' are handled equally and are recognized as duplicate parameters.
#include <Config.h>
Classes | |
class | Block |
Parameter block. More... | |
class | BlockHandle |
Handle for a Block object. More... | |
class | Parameter |
Public Types | |
enum | ErrorCode { noerror = 0, undefined = 1, badcast = 2 } |
using | Blocks = std::vector< BlockHandle > |
Container for block handles. More... | |
using | size_type = List::size_type |
Size type for a Block count. More... | |
using | iterator = Map::iterator |
Iterator over the contained parameters. More... | |
using | const_iterator = Map::const_iterator |
Constant iterator over the contained parameters. More... | |
Public Member Functions | |
Config () | |
Default constructor for the Config class. More... | |
~Config () | |
Destructor for the Config class. More... | |
template<typename Type > | |
Config::Parameter< Type > | getParameter (const std::string &key) const |
Operators | |
Config & | operator= (const Config &c) |
Copy assignment operator for the Config class. More... | |
operator bool () const | |
Conversion operator for the Config class. More... | |
Error functions | |
void | clear () |
Clearing the internal status of the file config object. More... | |
std::string | error () const |
Returns the error messages. More... | |
Get functions | |
bool | isDefined (std::string key) const |
Checks if a parameter was defined in the parameter file. More... | |
template<typename T > | |
Parameter< T > | getParameter (const std::string &key) const |
Returns an extracted parameter. More... | |
template<typename T > | |
Parameter< T > | getParameter (const std::string &key, const T &def) const |
Returns an extracted parameter. More... | |
iterator | begin () |
Returns an iterator to the first globally defined parameter. More... | |
const_iterator | begin () const |
iterator | end () |
Returns an iterator one past the last globally defined parameter. More... | |
const_iterator | end () const |
size_type | getNumBlocks () const |
size_type | getNumBlocks (const std::string &key) const |
BlockHandle | getBlock (const std::string &key) const |
BlockHandle | getOneBlock (const std::string &key) const |
void | getBlocks (const std::string &key, Blocks &blocks, size_t min=0, size_t max=std::numeric_limits< size_t >::max()) const |
Adds to the given blocks all extracted blocks with key key . More... | |
void | getBlocks (Blocks &blocks) const |
Adds to the given blocks all globally defined blocks. More... | |
BlockHandle | getGlobalBlock () const |
gives a handle to the global block. More... | |
Block & | getWritableGlobalBlock () |
returns the global block. More... | |
Private Types | |
using | Key = std::string |
Parameter key. More... | |
using | Value = std::string |
Parameter value. More... | |
using | Map = std::map< Key, Value, CaseInsensitiveCompare > |
Parameter map. More... | |
using | List = std::list< Block > |
List for parameter blocks. More... | |
using | ValueReplacementMap = std::map< Value, Value > |
Parameter value replacement map. More... | |
using | sstreamPos = std::stringstream::pos_type |
Stream position. More... | |
using | Pair = std::pair< sstreamPos, unsigned int > |
Pair consisting of a stream position and a line number. More... | |
using | LineVector = std::vector< Pair > |
Vector for Pair. More... | |
Private Attributes | |
Member variables | |
bool | stateFlag_ |
Internal status of the config object. More... | |
std::ostringstream | error_ |
Container for all error messages. More... | |
Block | block_ |
The global parameter block. More... | |
ValueReplacementMap | valueReplacements_ |
value replacements More... | |
Static Private Attributes | |
static const unsigned int | maxInclusionLevel = 30 |
Utility functions | |
void | readParameterFile (const char *const filename) |
Extraction of the parameter file filename . More... | |
void | listParameters () const |
Output function for the contained parameters. More... | |
void | addValueReplacement (const Value &original, const Value &replacement) |
static void | convertToLowerCase (std::string &s) |
Conversion of the character in s to lower case characters. More... | |
static std::string | getDirectory (const std::string &path) |
Returns the directory part of the given path. More... | |
static std::string | getFilename (const std::string &path) |
Returns the filename part of the given path. More... | |
void | removeTrailingWhiteSpaces (std::string &s) |
Removes the trailing white spaces from the given string. More... | |
unsigned int | getLineNumber (const LineVector &lineNumbers, sstreamPos pos) const |
Calculation of the line number of a command. More... | |
void | parseFromFile (const char *const filename, Block &block, unsigned int level) |
Parses the given file and extracts the parameters to the given block. More... | |
void | parseFromString (const std::string &str, Block &block, unsigned int level) |
Parses the given file and extracts the parameters to the given block. More... | |
void | extractBlock (const char *filename, std::stringstream &input, Block &block, const LineVector &lineNumbers, unsigned int lineNumber, unsigned int level) |
Extracting a parameter block from the preprocessed input stream. More... | |
using walberla::config::Config::Blocks = std::vector<BlockHandle> |
Container for block handles.
using walberla::config::Config::const_iterator = Map::const_iterator |
Constant iterator over the contained parameters.
using walberla::config::Config::iterator = Map::iterator |
Iterator over the contained parameters.
|
private |
Parameter key.
|
private |
Vector for Pair.
|
private |
List for parameter blocks.
|
private |
Parameter map.
|
private |
Pair consisting of a stream position and a line number.
using walberla::config::Config::size_type = List::size_type |
Size type for a Block count.
|
private |
Stream position.
|
private |
Parameter value.
|
private |
Parameter value replacement map.
walberla::config::Config::Config | ( | ) |
Default constructor for the Config class.
|
default |
Destructor for the Config class.
|
inline |
|
inline |
Returns an iterator to the first globally defined parameter.
|
inline |
|
inline |
Clearing the internal status of the file config object.
This function resets the internal status of the config object and clears all error messages.
|
inlinestatic |
Conversion of the character in s
to lower case characters.
s | Reference to the string that gets converted. |
|
inline |
Returns an iterator one past the last globally defined parameter.
|
inline |
|
inline |
Returns the error messages.
|
private |
Extracting a parameter block from the preprocessed input stream.
filename | Name of the parameter file |
input | The preprocessed input stream. |
block | The block whose parameters are extracted. |
lineNumbers | The line numbers belonging to the preprocessed input stream. |
lineNumber | Starting line of the block in the parameter file. |
level | The current inclusion level. |
|
inline |
|
inline |
Adds to the given blocks
all globally defined blocks.
[out] | blocks | Reference to the vector of blocks. |
|
inline |
Adds to the given blocks
all extracted blocks with key key
.
key | The key of the extracted blocks. | |
[out] | blocks | Reference to the vector of blocks. |
min | minimum number of blocks the caller expects | |
max | maximum number of blocks the caller can handle |
|
inlinestaticprivate |
Returns the directory part of the given path.
path | The path containing both directory and filename. |
|
inlinestaticprivate |
Returns the filename part of the given path.
path | The path containing both directory and filename. |
|
inline |
gives a handle to the global block.
|
private |
Calculation of the line number of a command.
lineNumbers | ? |
pos | Position in the preprocessed input stream. |
|
inline |
|
inline |
|
inline |
|
inline |
Returns an extracted parameter.
key | The key of the extracted parameter. |
|
inline |
|
inline |
Returns an extracted parameter.
key | The key of the extracted parameter. |
def | default value |
|
inline |
returns the global block.
|
inline |
Checks if a parameter was defined in the parameter file.
key | The parameter key to be checked. |
|
inline |
Output function for the contained parameters.
|
inline |
|
private |
Parses the given file and extracts the parameters to the given block.
filename | Name of the parameter file. |
block | Parameter block for the found parameters. |
level | The current inclusion level. |
|
private |
Parses the given file and extracts the parameters to the given block.
str | Config-generated input string. |
block | Parameter block for the found parameters. |
level | The current inclusion level. |
By internal convention, each block first contains all parameter, and then all sub-blocks
void walberla::config::Config::readParameterFile | ( | const char *const | filename | ) |
Extraction of the parameter file filename
.
filename | Name of the parameter file. |
|
inlineprivate |
Removes the trailing white spaces from the given string.
s | The string to be cleaned from trailing white spaces. |
|
private |
The global parameter block.
|
private |
Container for all error messages.
|
staticprivate |
|
private |
Internal status of the config object.
An error is indicated by false
.
|
private |
value replacements
"$(mapElements.first)" in input file is replaced by "mapElement.second"