edelib  2.1.0
Public Member Functions | List of all members
Config Class Reference

A config file reader. More...

#include <edelib/Config.h>

Inheritance diagram for Config:
DesktopFile

Public Member Functions

 Config ()
 
 ~Config ()
 
bool load (const char *fname)
 
bool save (const char *fname)
 
 operator bool (void) const
 
void clear (void)
 
bool get (const char *section, const char *key, char *ret, unsigned int size)
 
bool get_localized (const char *section, const char *key, char *ret, unsigned int size)
 
bool get_allocated (const char *section, const char *key, char **ret, unsigned int &retsize)
 
bool get (const char *section, const char *key, bool &ret, bool dfl=false)
 
bool get (const char *section, const char *key, int &ret, int dfl=0)
 
bool get (const char *section, const char *key, float &ret, float dfl=0)
 
bool get (const char *section, const char *key, long &ret, long dfl=0)
 
bool get (const char *section, const char *key, double &ret, double dfl=0)
 
bool get (const char *section, const char *key, char &ret, char dfl=0)
 
void set (const char *section, const char *key, char *val)
 
void set (const char *section, const char *key, const char *val)
 
void set_localized (const char *section, const char *key, char *val)
 
void set_localized (const char *section, const char *key, const char *val)
 
void set (const char *section, const char *key, bool val)
 
void set (const char *section, const char *key, int val)
 
void set (const char *section, const char *key, long val)
 
void set (const char *section, const char *key, float val)
 
void set (const char *section, const char *key, double val)
 
bool exist (const char *section)
 
bool key_exist (const char *section, const char *key)
 
unsigned int num_sections (void)
 
unsigned int line (void)
 
int error (void)
 
const char * strerror (void)
 
const char * strerror (int code)
 

Detailed Description

A config file reader.

This class is aimed for reading and storing classic INI files in form:

* # this is a comment
* [section1]
* item = value
*
* [section2]
* item = value
*

Section names can be words or multiple words separated by spaces. All leading and following spaces will be trimmed, like:

* [ some long section ] -> "some long section"
*

Duplicate sections will be merged in one (later when used as output), but in case of duplicate key lines, only last will be considered. Like in this sample:

* [my section]
* idea = bad <- ignored
* idea = good
*

In above case, using

* get("my section", "idea", &val, valsz)
*

will set val to "good".

During reading phase, file is checked is malformed and if that was true, CONF_ERR_BAD will be returned as error code. Target line can be inspected with line() function.

Althought there are various INI specifications, currently supported one is very basic, without subsections support, like:

* [parent]
* item = value
* [parent/child]
* item = value
*

Later option can be checked like:

* get("parent/child", "item", &val, valsz);
*
Todo:

don't allow [] sections

What Config as localized keys should read: LC_MESSAGES or LANG ??? I'm using for now LANG, but fd.o people said it should be LC_MESSAGES. Inspect this.

Constructor & Destructor Documentation

Config ( )

Constructor

~Config ( )
inline

Destructor

Member Function Documentation

void clear ( void  )

Clear all internal data.

int error ( void  )

Get error code.

This can be used to inspect why file can't be read.

Returns
one of ConfigErrors values
bool exist ( const char *  section)

Check if section exist.

Returns
true if exist, or false if not.
bool get ( const char *  section,
const char *  key,
char *  ret,
unsigned int  size 
)

Get char* value from named section.

Returns
true if value exist
Parameters
sectionname of target section
keyname of target key
retbuffer to copy returned value
sizesize of buffer
bool get ( const char *  section,
const char *  key,
bool &  ret,
bool  dfl = false 
)

Get bool value from named section.

Returns
true if value exist
Parameters
sectionname of target section
keyname of target key
retwhere to place returned value
dfldefault value, in case real value not found
bool get ( const char *  section,
const char *  key,
int &  ret,
int  dfl = 0 
)

Get int value from named section.

Returns
true if value exist
Parameters
sectionname of target section
keyname of target key
retwhere to place returned value
dfldefault value, in case real value not found
bool get ( const char *  section,
const char *  key,
float &  ret,
float  dfl = 0 
)

Get float value from named section.

Returns
true if value exist
Parameters
sectionname of target section
keyname of target key
retwhere to place returned value
dfldefault value, in case real value not found
bool get ( const char *  section,
const char *  key,
long &  ret,
long  dfl = 0 
)

Get long value from named section.

Returns
true if value exist
Parameters
sectionname of target section
keyname of target key
retwhere to place returned value
dfldefault value, in case real value not found
bool get ( const char *  section,
const char *  key,
double &  ret,
double  dfl = 0 
)

Get double value from named section.

Returns
true if value exist
Parameters
sectionname of target section
keyname of target key
retwhere to place returned value
dfldefault value, in case real value not found
bool get ( const char *  section,
const char *  key,
char &  ret,
char  dfl = 0 
)

Get char value from named section.

Returns
true if value exist
Parameters
sectionname of target section
keyname of target key
retwhere to place returned value
dfldefault value, in case real value not found
bool get_allocated ( const char *  section,
const char *  key,
char **  ret,
unsigned int &  retsize 
)

This function is the same as get() with char*, except returned data is allocated. Use must call delete[] on returned data. If this function returns false, returned data will not be allocated and retsize will be 0.

Returns
true if value exists
Parameters
sectionname of target section
keyname of target key
retis allocated by this function ad returned value will be copied in it
retsizesize of buffer
bool get_localized ( const char *  section,
const char *  key,
char *  ret,
unsigned int  size 
)

Get localized character data. This method behaves the same as get() with character data, except it will search first keys with locale tags, which coresponds system locale settings. This keys looks like:

* # localized value
* Key[de] = localized value in german
*
* # unlocalized value
* Key = some value
*

Function will try to find out current locale settings and if fails, of specific key with locale tag does not exist, it will try with ordinary unlocalized value.

Returns
true if value exist
Parameters
sectionname of target section
keyname of target key
retbuffer to copy returned value
sizesize of buffer
bool key_exist ( const char *  section,
const char *  key 
)

Check if key exists in given section.

Returns
true if exists, of false if not.
unsigned int line ( void  )

Get current line number.

This function can be used if file is malformed

Returns
problematic line
bool load ( const char *  fname)

Load file. Config's internal content will be cleared.

Returns
true if file reading was ok, otherwise false.
Parameters
fnamepath to config file.
unsigned int num_sections ( void  )

Return number of sections.

Returns
number of sections.
operator bool ( void  ) const
inline

Validate is previous operation was succesfull.

This is an shorthand for various error() checks, so it can be used:

* Config c;
* c.load("/no/file");
* if(!c) // do something smart
*
Returns
true if everything is fine, or false

References edelib::CONF_SUCCESS.

bool save ( const char *  fname)

Save content to the file. Note however that content will be first stored in temporary file, then will be renamed to final location; with this it is safe to use this function in places where folders are monitored for changes too.

Returns
true if file saving was ok, otherwise false.
Parameters
fnamepath to store config file.
void set ( const char *  section,
const char *  key,
char *  val 
)

Set char* value in named section. If section, or key does not exists, they will be created.

Parameters
sectionname of target section
keyname of target key
valvalue to store within key
void set ( const char *  section,
const char *  key,
const char *  val 
)

Set const char* value in named section. If section, or key does not exists, they will be created.

Parameters
sectionname of target section
keyname of target key
valvalue to store within key
void set ( const char *  section,
const char *  key,
bool  val 
)

Set bool value in named section. If section, or key does not exists, they will be created.

Parameters
sectionname of target section
keyname of target key
valvalue to store within key
void set ( const char *  section,
const char *  key,
int  val 
)

Set int value in named section. If section, or key does not exists, they will be created.

Parameters
sectionname of target section
keyname of target key
valvalue to store within key
void set ( const char *  section,
const char *  key,
long  val 
)

Set long value in named section. If section, or key does not exists, they will be created.

Parameters
sectionname of target section
keyname of target key
valvalue to store within key
void set ( const char *  section,
const char *  key,
float  val 
)

Set float value in named section. If section, or key does not exists, they will be created.

Parameters
sectionname of target section
keyname of target key
valvalue to store within key
void set ( const char *  section,
const char *  key,
double  val 
)

Set double value in named section. If section, or key does not exists, they will be created.

Parameters
sectionname of target section
keyname of target key
valvalue to store within key
void set_localized ( const char *  section,
const char *  key,
char *  val 
)

Set char* value for localized named section. Description for get_localized() applies here too.

Parameters
sectionname of target section
keyname of target key
valvalue to store within key
void set_localized ( const char *  section,
const char *  key,
const char *  val 
)

Set const char* value for localized named section. Description for get_localized() applies here too.

Parameters
sectionname of target section
keyname of target key
valvalue to store within key
const char* strerror ( void  )

Get error in string form.

Returns
current error in string form
const char* strerror ( int  code)

Interpret parameter and return associated error code.

Returns
string name for error code
Parameters
codeone of ConfigErrors values

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