Main Page   Compound List   Header Files   Compound Members  

SimpleCfg Class Reference

Encapsulation of an "Windows-ini-style" ASCII configuration file. More...

#include <simplecfg.h>

List of all members.

Public Members


Detailed Description

Encapsulation of an "Windows-ini-style" ASCII configuration file.

You know these

    [operating system]
    windows=sux
    unix=rocks
configuration files.

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.
If the file could be read you would see the following output:
    Windows sux
    Unix rocks
If the file could not be read:
    Windows is good
    Unix is old crap
So always ensure your configuration files can be read :)
Warning:
You can not have two or more groups with the same name. Same applies to keys within a group.

SimpleCfg is licensed under GPL

Author(s):
Matthias Toussaint, © 2000 Matthias Toussaint

Member Function Documentation

QString SimpleCfg::filename () const [inline]

Retrieve filename.

Returns:
filename

void SimpleCfg::setFilename (const QString & fn) [inline]

Rename file.

Does not load the new file. Next save and load will use this filename.

Parameters:
fn   filename to be used from now on.

bool SimpleCfg::load ()

Load configuration file from disk.

Returns:
true on success

bool SimpleCfg::save ()

Store configuration file to disk.

Returns:
true on success

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.

Warning:
It might fuck up your configuration if a value was empty by intention and the default value for reading is not empty. If you don't know what you are doing don't call this member!

void SimpleCfg::remove (const QString & group, const QString & key)

Remove key key from group group.

Parameters:
group   Group to be used
key   Key to be removed

void SimpleCfg::remove (const QString & group)

Remove group group.

Parameters:
group   Group to be removed

bool SimpleCfg::contains (const QString & group)

Check if configuration contains group named group.

Parameters:
group   Name of group to look for
Returns:
true if found

bool SimpleCfg::contains (const QString & group, const QString & key)

Check if configuration contains key in group named group.

Parameters:
group   Name of group to look for
key   Name of key to look for
Returns:
true if found

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.

Parameters:
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

Parameters:
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.

Parameters:
group   Group to be used
key   Key to be searched for
def   Default value to be returned if key not found
Returns:
Value of key or default value

void SimpleCfg::setDouble (const QString & group, const QString & key, double val)

Set double value.

If the key does not exist it is created

Parameters:
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.

Parameters:
group   Group to be used
key   Key to be searched for
def   Default value to be returned if key not found
Returns:
Value of key or default value

void SimpleCfg::setString (const QString & group, const QString & key, const QString & val)

Set string value.

If the key does not exist it is created

Parameters:
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.

Parameters:
group   Group to be used
key   Key to be searched for
def   Default value to be returned if key not found
Returns:
Value of key or default value

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.

Parameters:
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.

Parameters:
group   Group to be used
key   Key to be searched for
def   Default value to be returned if key not found
Returns:
Value of key or default value

void SimpleCfg::setBool (const QString & group, const QString & key, bool val)

Set bool value.

If the key does not exist it is created.

Parameters:
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.

Parameters:
group   Group to be used
key   Key to be searched for
def   Default value to be returned if key not found
Returns:
Value of key or default value

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;
        }
      }
The above code will print the content of your configuration file (without comments and alphabetically sorted) to stderr.
See also:
SimpleCfg::end()

QMap<QString,SimpleCfgGroup *>::Iterator SimpleCfg::end () [inline]

Access end value for iterator.

See also:
SimpleCfg::begin()

The documentation for this class was generated from the following files:
Generated at Sat Nov 11 18:45:04 2000 for SimpleCfg by doxygen 1.0.0 written by Dimitri van Heesch, © 1997-1999