Click here to Skip to main content
15,885,278 members
Articles / Desktop Programming / MFC
Article

SimpleConfig

Rate me:
Please Sign up or sign in to vote.
3.00/5 (9 votes)
7 Sep 20052 min read 34K   1.1K   23   3
Serialize and access your application's config values.

Sorry, it should have been SimpleConfig.jpg

Introduction

To increase an application's usability it is often a good idea to deal with some persistent information. If someone doesn't want to hide these parameters in his Registry (maybe he often modifies these parameters manually), he could use a plain text file instead. The Config class that is presented in this example, offers a complete solution to handle plain text files as config files.

Features

  • common parameter handling
    • <name> = <value>
  • scope names: different config values can have equal names.
    • <scopeA:name> = <value>
    • <scopeB:name> = <value>
  • value lists: indices will be added automatically, if <name> specifies a value array.
    • <name[0]> = <value>
    • <name[1]> = <value>
    • <name[2]> = <value>

The config file

This example deals with:

  • the window's last size and position
  • the filters of the CFileDialog
  • the index of the last used filter
  • the directory of the last opened file

Sorry, it should have been Result.jpg

Using the code

Tip

If you include the Config.h header in your StdAfx.h, you will be able to access all your config values from any file that itself includes the StdAfx.h.

Example

Calling all necessary functions to obtain the values from a value list. (Doing this in just one function is a bit hypothetical.)

// should always be the first Config method you call
// (a good place would be within your InitInstance() method)
Config::initialize();

// read your config file
Config::import("MyApp.cfg");

// the list of all values that are registered to "Foo"
vector<string> values;

// get first config value
const char* value = Config::getValue("Foo");

// get all config values
while(value)
{
    values.push_back(value);

    value = Config::getNextValue();
}

// write your config file (and use the path
// that was passed to Config::import())
Config::export();

// should always be the last Config method you call
// (a good place would be within your ExitInstance() method)
Config::depose();

Continuative stuff

The outright interface is a bit more extensive. Please have a look at the source code or the available documentation.

Other classes

  • ConfigValue

    This class implements just some conversion constructors. Each constructor converts its passed value to const char*. All implicit type conversion that is done for you is encoded in this class. Due to this class the Config class only needs to provide one kind of setValue() method.

  • Config::Implementation

    You don't have to worry about this class. This class holds all algorithms and data structures that enable the provided functionality. I separated this class from the Config class to be able to kick out all include directives from the Config.h file.

History

Version 1.0

Added:

  • Config handling
    • initialize()
    • depose()
    • import()
    • export()
    • enterScope()
    • leaveScope()
    • setValue()
    • addValue()
    • getValue()
    • getNextValue()
    • removeValue()
    • removeParameter()
  • Implicit type conversion
    • ConfigValue(int)
    • ConfigValue(long)
    • ConfigValue(unsigned long)
    • ConfigValue(double)
    • ConfigValue(bool)
    • ConfigValue(char)
    • ConfigValue(char*)

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
Germany Germany
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionWhy not use INI files? Pin
milkplus19-Nov-09 10:23
milkplus19-Nov-09 10:23 
A Small Class to Read INI File[^]
QuestionSome comments Pin
Johann Gerell12-Sep-05 13:44
Johann Gerell12-Sep-05 13:44 
AnswerRe: Some comments Pin
Achim Klein13-Sep-05 2:06
Achim Klein13-Sep-05 2:06 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.