Marine Library  1.0
C++ library for Linux Networking Development
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
Public Member Functions | Static Public Attributes | List of all members
CConfiguration Class Reference

Parse config file and get attributes. More...

#include <configuration.hh>

Public Member Functions

std::string getConfName () const
 Get config file name. More...
 
void clear ()
 Clear all attributes loaded. More...
 
bool load (const std::string &file_name, int format=kFormatSpace)
 Parse and load content of a config file. More...
 
std::string getString (const std::string &key, const std::string &strdefault="") const
 Get string value of an attribute. More...
 
std::string getFilepath (const std::string &key, const std::string &strdefault="") const
 Get file name value of an attribute. More...
 
std::string toString () const
 Get a readable description of this object. More...
 
getInt

These functions try to translate the value to an integer.
A number of suffixes (case insensitive) could appear at the end of the value:

Suffix Explanation
k or kb $N\times1024$
m or mb $N\times1024^2(=1,048,576)$
g or gb $N\times1024^3(=1,073,741,824)$
t or tb $N\times1024^4(=1,099,511,627,776)$
p or pb $N\times1024^5(=1,125,899,906,842,624)$
e or eb $N\times1024^6(=1,152,921,504,606,846,976)$

For example, 2KB is equal to 2048.
If key is not found, ndefault will be used as an candidate;
If the returned integer would be smaller than min, min will be returned;
If the returned integer would be larger than max, max will be returned;

Parameters
keyAttribute name
ndefaultDefault value if key is not found, before range checking
minLower boundary of the returned value
maxUpper boundary of the returned value
Returns
Value of the attribute
int getInt (const std::string &key, int ndefault=0, int min=std::numeric_limits< int >::min(), int max=std::numeric_limits< int >::max()) const
 
template<typename Int >
Int getInt (const std::string &key, Int ndefault=0, Int min=std::numeric_limits< Int >::min(), Int max=std::numeric_limits< Int >::max()) const
 

Static Public Attributes

static const int kFormatEqual = 0
 
static const int kFormatSpace = 1
 
static const int kFormatColon = 2
 

Detailed Description

Parse config file and get attributes.

A config file is a text file consisting of attributes. An attribute consists of a name and its value.
There is only one attribute per line. Empty lines are allowed. Any content after # will be treated as comments, until the end of the line.
Attribute name and value consist of any readable characters. Leading and trailing spaces will be trimmed. But spaces inside of a name or value are considered part of it.
CConfiguration supports 3 formats of config files:

kFormatEqual
An attribute has the format of NAME = VALUE, which means = cannot be part of an attribute name or value.
Example:
# this is an example of kFormatEqual conf file.
first attribute name = first attribute value # first attribute description
second:attribute:name = second:attribute:value
third_attribute_name # with an empty value
kFormatSpace
An attribute has the format of NAME VALUE, which means SPACE cannot be part of an attribute name or value.
Example:
# this is an example of kFormatSpace conf file.
first=attribute=name first=attribute=value # first attribute description
second:attribute:name second:attribute:value
third_attribute_name # with an empty value
kFormatColon
An attribute has the format of NAME : VALUE, which means : cannot be part of an attribute name or value.
Example:
# this is an example of kFormatColon conf file.
first=attribute=name : first attribute value # first attribute description
second attribute name : second attribute value
third_attribute_name # with an empty value

Member Function Documentation

void CConfiguration::clear ( )
inline

Clear all attributes loaded.

This function won't change the content of config file.

std::string CConfiguration::getConfName ( ) const
inline

Get config file name.

Returns
Config file name
std::string CConfiguration::getFilepath ( const std::string &  key,
const std::string &  strdefault = "" 
) const
inline

Get file name value of an attribute.

This function considers the value to be a file name relative to the current working directory, and tries to expand it to a full pathname.
If the value is empty or a full pathname already, no expansion will take place.

Parameters
keyAttribute name
strdefaultDefault value if key is not found, before expansion
Returns
Value of the attribute
std::string CConfiguration::getString ( const std::string &  key,
const std::string &  strdefault = "" 
) const
inline

Get string value of an attribute.

Parameters
keyAttribute name
strdefaultDefault value if key is not found
Returns
Value of the attribute
bool CConfiguration::load ( const std::string &  file_name,
int  format = kFormatSpace 
)
inline

Parse and load content of a config file.

Parameters
file_nameConfig file name
formatConfig file format, could be kFormatEqual, kFormatSpace or kFormatColon
Returns
true if succeeded; otherwise false
std::string CConfiguration::toString ( ) const
inline

Get a readable description of this object.

Returns
A readable description of this object

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