edelib
2.1.0
|
A config file reader. More...
#include <edelib/Config.h>
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) |
A config file reader.
This class is aimed for reading and storing classic INI files in form:
Section names can be words or multiple words separated by spaces. All leading and following spaces will be trimmed, like:
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:
In above case, using
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:
Later option can be checked like:
Config | ( | ) |
Constructor
|
inline |
Destructor
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.
bool exist | ( | const char * | section | ) |
Check if section exist.
bool get | ( | const char * | section, |
const char * | key, | ||
char * | ret, | ||
unsigned int | size | ||
) |
Get char* value from named section.
section | name of target section |
key | name of target key |
ret | buffer to copy returned value |
size | size of buffer |
bool get | ( | const char * | section, |
const char * | key, | ||
bool & | ret, | ||
bool | dfl = false |
||
) |
Get bool value from named section.
section | name of target section |
key | name of target key |
ret | where to place returned value |
dfl | default 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.
section | name of target section |
key | name of target key |
ret | where to place returned value |
dfl | default 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.
section | name of target section |
key | name of target key |
ret | where to place returned value |
dfl | default 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.
section | name of target section |
key | name of target key |
ret | where to place returned value |
dfl | default 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.
section | name of target section |
key | name of target key |
ret | where to place returned value |
dfl | default 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.
section | name of target section |
key | name of target key |
ret | where to place returned value |
dfl | default 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.
section | name of target section |
key | name of target key |
ret | is allocated by this function ad returned value will be copied in it |
retsize | size 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:
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.
section | name of target section |
key | name of target key |
ret | buffer to copy returned value |
size | size of buffer |
bool key_exist | ( | const char * | section, |
const char * | key | ||
) |
Check if key exists in given section.
unsigned int line | ( | void | ) |
Get current line number.
This function can be used if file is malformed
bool load | ( | const char * | fname | ) |
Load file. Config's internal content will be cleared.
fname | path to config file. |
unsigned int num_sections | ( | void | ) |
Return number of sections.
|
inline |
Validate is previous operation was succesfull.
This is an shorthand for various error() checks, so it can be used:
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.
fname | path 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.
section | name of target section |
key | name of target key |
val | value 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.
section | name of target section |
key | name of target key |
val | value 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.
section | name of target section |
key | name of target key |
val | value 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.
section | name of target section |
key | name of target key |
val | value 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.
section | name of target section |
key | name of target key |
val | value 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.
section | name of target section |
key | name of target key |
val | value 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.
section | name of target section |
key | name of target key |
val | value 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.
section | name of target section |
key | name of target key |
val | value 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.
section | name of target section |
key | name of target key |
val | value to store within key |
const char* strerror | ( | void | ) |
Get error in string form.
const char* strerror | ( | int | code | ) |
Interpret parameter and return associated error code.
code | one of ConfigErrors values |