CAPD::DynSys Library  6.0.0
capd::auxil::ConfigFileReader Class Reference

#include <capd/auxil/ConfigFileReader.h>

Classes

struct  CAPD_LOGGER
 

Public Member Functions

 ConfigFileReader ()
 
 ConfigFileReader (const std::string &fileName)
 
 ConfigFileReader (std::istream &file)
 
void setStopTag (const std::string &tag)
 
const std::string & getStopTag () const
 
bool isKey (const std::string &s) const
 
template<typename T >
get (const std::string &key, T defaultValue=T())
 
template<typename T >
bool getValue (const std::string &key, T &d)
 
bool getValue (const std::string &key, std::string &d)
 
template<typename T >
bool getValues (const std::string &key, T begin, T end)
 
template<typename T >
bool getTable (const std::string &key, T table[], size_t size)
 
void parse (std::istream &ins)
 

Detailed Description


The ConfigFileReader parses Windows .ini like file into map<string, string>

The file is list of pairs, the key and the value, separated by an equal sign '='. Pair is not permitted to span more than one line.

Start of the section is denoted by [SectionName]. Section name is added to the following keys name e.g. SectionName.KeyName Next section ends the previous one (use [] to set no section).

Commentary :

  • begins with a hash ('#') as the first non-space character on the line.
  • begins with semi-colon (;) anywhere and spans to the end of the line

By default it parses input file or stream to the end (EOF). It can be set up to stop on given section name. It is useful when file contains not only pairs "key = value" but also raw data that spans across several lines.

Example:

This is an example

command = copy

In the following we could also define section [source]

to shorten keys names

source.directory = C:\Documents and Settings\Jennifer\My Documents source.size = 4 ; number of files source.files = file1.cpp file2.cpp file3.cpp file4.cpp ; file names cannot have white-spaces [destination] directory = C:\Temp

Usage:

ConfigFileReader ini("iniFileName");

std::string command; ini.getValue("command", command);

std::string sourceDir, destinationDir; ini.getValue("source.directory", sourceDir); ini.getValue("destination.directory", destinationDir);

int numberOfFiles = 10; // or ini.getValue("source.size",numberOfFiles); // int numberOfFiles = ini.get<int>("source.size", 10);

ini.getValue("source.number of files", numberOfFiles); // it will not change the value because this key do not exist

std::vector<std::string> fileNames(numberOfFiles);
ini.getValues("source.files", fileNames.begin(), fileNames.end());

More examples in capdAux/examples/configFileReader

Constructor & Destructor Documentation

◆ ConfigFileReader() [1/3]

capd::auxil::ConfigFileReader::ConfigFileReader ( )
inline

◆ ConfigFileReader() [2/3]

capd::auxil::ConfigFileReader::ConfigFileReader ( const std::string &  fileName)
inline

◆ ConfigFileReader() [3/3]

capd::auxil::ConfigFileReader::ConfigFileReader ( std::istream &  file)
inline

Member Function Documentation

◆ get()

template<typename T >
T capd::auxil::ConfigFileReader::get ( const std::string &  key,
defaultValue = T() 
)
inline

◆ getStopTag()

const std::string& capd::auxil::ConfigFileReader::getStopTag ( ) const
inline

◆ getTable()

template<typename T >
bool capd::auxil::ConfigFileReader::getTable ( const std::string &  key,
table[],
size_t  size 
)
inline

◆ getValue() [1/2]

bool capd::auxil::ConfigFileReader::getValue ( const std::string &  key,
std::string &  d 
)
inline

◆ getValue() [2/2]

template<typename T >
bool capd::auxil::ConfigFileReader::getValue ( const std::string &  key,
T &  d 
)
inline

◆ getValues()

template<typename T >
bool capd::auxil::ConfigFileReader::getValues ( const std::string &  key,
begin,
end 
)
inline

◆ isKey()

bool capd::auxil::ConfigFileReader::isKey ( const std::string &  s) const
inline

◆ parse()

void capd::auxil::ConfigFileReader::parse ( std::istream &  ins)
inline

◆ setStopTag()

void capd::auxil::ConfigFileReader::setStopTag ( const std::string &  tag)
inline

The documentation for this class was generated from the following file: