rambrain
rambrain::configReader Class Reference

Reader class to read in and properly parse config files. More...

#include <configreader.h>

Public Member Functions

 configReader ()
 Init a new reader. More...
 
bool readConfig ()
 Read in and parse config. More...
 
void setCustomConfigPath (const string &path)
 Simple setter. More...
 
configurationgetConfig ()
 Simple getter. More...
 
bool readSuccessfully () const
 Simple getter. More...
 
void setReread ()
 Simple setter. More...
 

Private Member Functions

bool reopenStreams ()
 (Re)Open streams to config files More...
 
bool parseConfigFile (istream &stream, vector< configLineBase * > &readLines)
 Parse config file. More...
 
bool parseConfigBlock (istream &stream, vector< configLineBase * > &readLines)
 Parse an identified configuration block and extract all matching variables. More...
 
string getApplicationName () const
 Extract the current binary's name out of the /proc file system. More...
 
string getHomeDir () const
 Look up the home directory of the current user. More...
 
void stripLeadingTrailingWhitespace (string &str) const
 Strips any leading and trailing whitespace from a given string. More...
 

Private Attributes

const string globalConfigPath = "/etc/rambrain.conf"
 
string localConfigPath = ""
 
string customConfigPath = ""
 
const regexMatcher regex
 
ifstream streams [3]
 
configuration config
 
bool readSuccess = false
 

Friends

class ::configReader_Unit_ParseProgramName_Test
 

Detailed Description

Reader class to read in and properly parse config files.

A sample config file looks like following:
[default]
key1 = value1
key2 = value2
[binname1]
key = value
...
While keys are exactly the names of members of the configuration struct There are three different locations to search for a config file, in descending priority: custom location > user home dir > global Not all options need to be supplied. Missing options are taken from the default block or (if not present there either) from the defaults Also all different files are taken into account to supply missing options

Note
Comments can be inserted with leading hash

Definition at line 163 of file configreader.h.

Constructor & Destructor Documentation

rambrain::configReader::configReader ( )

Init a new reader.

Note
Does not trigger a config read in since this is done lazily

Definition at line 152 of file configreader.cpp.

Member Function Documentation

string rambrain::configReader::getApplicationName ( ) const
private

Extract the current binary's name out of the /proc file system.

Returns
The name

Definition at line 295 of file configreader.cpp.

configuration& rambrain::configReader::getConfig ( )
inline

Simple getter.

Definition at line 192 of file configreader.h.

string rambrain::configReader::getHomeDir ( ) const
private

Look up the home directory of the current user.

Returns
The home dir

Definition at line 312 of file configreader.cpp.

bool rambrain::configReader::parseConfigBlock ( istream &  stream,
vector< configLineBase * > &  readLines 
)
private

Parse an identified configuration block and extract all matching variables.

Parameters
streamThe input stream
readLinesVector to track which config options are already read
Returns
Success
Warning
Does not check for unknown variables or typos, every unknown thing is simply ignored
See also
parseConfigLine

Definition at line 247 of file configreader.cpp.

bool rambrain::configReader::parseConfigFile ( istream &  stream,
vector< configLineBase * > &  readLines 
)
private

Parse config file.

Look for default block as well as block matching the current binary name (which has priority of course)

Parameters
streamThe input stream
readLinesVector to track which config options are already read
Returns
Success
See also
parseConfigBlock()

Definition at line 209 of file configreader.cpp.

bool rambrain::configReader::readConfig ( )

Read in and parse config.

Returns
Success
See also
readSuccessfully
openStream
parseConfigFile

Definition at line 157 of file configreader.cpp.

bool rambrain::configReader::readSuccessfully ( ) const
inline

Simple getter.

Definition at line 202 of file configreader.h.

bool rambrain::configReader::reopenStreams ( )
private

(Re)Open streams to config files

Order is Custom location > Local user space > Global config

Returns
Success

Definition at line 177 of file configreader.cpp.

void rambrain::configReader::setCustomConfigPath ( const string &  path)
inline

Simple setter.

Definition at line 185 of file configreader.h.

void rambrain::configReader::setReread ( )
inline

Simple setter.

Definition at line 208 of file configreader.h.

void rambrain::configReader::stripLeadingTrailingWhitespace ( string &  str) const
private

Strips any leading and trailing whitespace from a given string.

Parameters
strThe string to be stripped

Definition at line 318 of file configreader.cpp.

Friends And Related Function Documentation

friend class ::configReader_Unit_ParseProgramName_Test
friend

Definition at line 273 of file configreader.h.

Member Data Documentation

configuration rambrain::configReader::config
private

Definition at line 267 of file configreader.h.

string rambrain::configReader::customConfigPath = ""
private

Definition at line 261 of file configreader.h.

const string rambrain::configReader::globalConfigPath = "/etc/rambrain.conf"
private

Definition at line 259 of file configreader.h.

string rambrain::configReader::localConfigPath = ""
private

Definition at line 260 of file configreader.h.

bool rambrain::configReader::readSuccess = false
private

Definition at line 269 of file configreader.h.

const regexMatcher rambrain::configReader::regex
private

Definition at line 263 of file configreader.h.

ifstream rambrain::configReader::streams[3]
private

Definition at line 265 of file configreader.h.


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