#include <simplecfg.h>
You know these
[operating system] windows=sux unix=rocks
Usage is quite simple. Just create a SimpleCfg object and retrieve the needed values in your code. All retrieval member need a default value. This ensures that first usage of the configuration file (Means file does not exist on harddisk) will create a file with hopefully sane (you have to specify them :) default values. Next time you'll find a complete configuration file on disk.
You could use the above example as follows:
SimpleCfg cfg( "filename" ); cfg.load(); // The file is _not_ loaded in the constructor! cerr << "Windows " << cfg.getString( "operating system", "windows", "is good" ) << endl; cerr << "Unix " << cfg.getString( "operating system", "unix", "is old crap" ) << endl; cfg.save(); // If file did not exist it is created now.
Windows sux Unix rocks
Windows is good Unix is old crap
QString SimpleCfg::filename () const [inline]
|
Retrieve filename.
void SimpleCfg::setFilename (const QString & fn) [inline]
|
Rename file.
Does not load the new file. Next save and load will use this filename.
fn | filename to be used from now on. |
bool SimpleCfg::load () |
Load configuration file from disk.
bool SimpleCfg::save () |
Store configuration file to disk.
void SimpleCfg::removeEmpty () |
Remove all empty keys.
Calling this member all keys that have an empty value are removed. This configuration file will look a bit nicer if you call this before saving.
void SimpleCfg::remove (const QString & group, const QString & key) |
Remove key key from group group.
group | Group to be used |
key | Key to be removed |
void SimpleCfg::remove (const QString & group) |
Remove group group.
group | Group to be removed |
bool SimpleCfg::contains (const QString & group) |
Check if configuration contains group named group.
group | Name of group to look for |
bool SimpleCfg::contains (const QString & group, const QString & key) |
Check if configuration contains key in group named group.
group | Name of group to look for |
key | Name of key to look for |
void SimpleCfg::add (const QString & group) |
Add empty group.
An empty group is added. This is needed for the load member. If the group already exists this request is ignored.
group | Group to be added |
void SimpleCfg::setInt (const QString & group, const QString & key, int val) |
Set integer value.
If the key does not exist it is created
group | Group to be used |
key | Key to be searched for |
val | Value to be assigned to key |
int SimpleCfg::getInt (const QString & group, const QString & key, int def) |
Retrieve integer value.
If the key does not exist it is created with the default value.
group | Group to be used |
key | Key to be searched for |
def | Default value to be returned if key not found |
void SimpleCfg::setDouble (const QString & group, const QString & key, double val) |
Set double value.
If the key does not exist it is created
group | Group to be used |
key | Key to be searched for |
val | Value to be assigned to key |
double SimpleCfg::getDouble (const QString & group, const QString & key, double def) |
Retrieve double value.
If the key does not exist it is created with the default value.
group | Group to be used |
key | Key to be searched for |
def | Default value to be returned if key not found |
void SimpleCfg::setString (const QString & group, const QString & key, const QString & val) |
Set string value.
If the key does not exist it is created
group | Group to be used |
key | Key to be searched for |
val | Value to be assigned to key |
QString SimpleCfg::getString (const QString & group, const QString & key, const QString & def) |
Retrieve string value.
If the key does not exist it is created with the default value.
group | Group to be used |
key | Key to be searched for |
def | Default value to be returned if key not found |
void SimpleCfg::setRGB (const QString & group, const QString & key, QRgb val) |
Set QRgb (unsigned long) value.
If the key does not exist it is created.
group | Group to be used |
key | Key to be searched for |
val | Value to be assigned to key |
QRgb SimpleCfg::getRGB (const QString & group, const QString & key, QRgb def) |
Retrieve QRgb (unsigned long) value.
If the key does not exist it is created with the default value.
group | Group to be used |
key | Key to be searched for |
def | Default value to be returned if key not found |
void SimpleCfg::setBool (const QString & group, const QString & key, bool val) |
Set bool value.
If the key does not exist it is created.
group | Group to be used |
key | Key to be searched for |
val | Value to be assigned to key |
bool SimpleCfg::getBool (const QString & group, const QString & key, bool def) |
Retrieve bool value.
If the key does not exist it is created with the default value.
group | Group to be used |
key | Key to be searched for |
def | Default value to be returned if key not found |
QMap<QString,SimpleCfgGroup *>::Iterator SimpleCfg::begin () [inline]
|
Access iterator over groups.
If there are no groups this member returns SimpleCfg::end().
If you want to iterate through the whole configuration you'll need code like the following:
SimpleCfg cfg( "filename" ); cfg.load(); QMap<QString,SimpleCfgGroup *>::Iterator group; for (group=cfg.begin(); group != cfg.end(); ++group) { // group.key() returns the group name as QString // group.data() returns a pointer to the group cerr << "[" << group.key().latin1() << "]" << endl; QMap<QString,QString>::Iterator key; for (key=group.data()->begin(); key != group.data()->end(); ++key) { // key.key() returns the key name // key.data() returns the value as QString cerr << key.key().latin1() << " = " << key.data().latin1() << endl; } }
QMap<QString,SimpleCfgGroup *>::Iterator SimpleCfg::end () [inline]
|