Introduction
Storing and loading configuration data for application is a very common task for programmers, and there are many ways to do it: store information in a file, in the registry, in the Windows common initialization files (system.ini, win.ini, etc...). But information saving and sharing has a new standard called XML.
Windows APIs and MFC have a set of functions which simplify these procedures a lot, storing and retrieving information from win.ini, the registry or any file based on the Windows-INI structure. Currently, handling XML files under Visual C++ is a bit complex, due to the COM nature of the Microsoft XML services.
CXMLProfile
is a class which simplifies this work, encapsulating the standard configuration input/output operations in a really simple set of functions.
Using CXMLProfile
When you instantiate a CXMLProfile
class and call the loadProfile
function, it tries to open the XML file containing the profile specified by the first param of the constructor; if it is not found, a blank profile is created. The data is stored in the file every time you call the saveProfile
function.
Now, let's have a look on the class public declarations:
CXMLProfile(LPCTSTR lpszProfileName);
bool writeProfileInt(LPCTSTR lpszSection, LPCTSTR lpszEntry, int nValue);
bool writeProfileString(LPCTSTR lpszSection,
LPCTSTR lpszEntry, LPCTSTR lpszData);
int getProfileInt(LPCTSTR lpszSection, LPCTSTR lpszEntry, int nDefault);
LPSTR getProfileString(LPCTSTR lpszSection, LPCTSTR lpszEntry,
LPCSTR lpszDefault, LPSTR lpBuffer, UINT nBufferSize);
bool saveProfile();
bool loadProfile();
~CXMLProfile();
The function prototypes are self-explaining, but I will give a short description:
CXMLProfile(LPCTSTR lpszProfileName)
The only and one constructor, lpszProfileName
contains the profile name (your application's name, for example).
virtual ~CXMLProfile();
The only and one destructor; performs a class cleanup.
bool writeProfileInt(LPCTSTR lpszSection, LPCTSTR lpszEntry, int nValue);
Writes an integer value into the specified section and entry.
bool writeProfileString(LPCTSTR lpszSection, LPCTSTR lpszEntry, LPCTSTR lpszData);
Writes a string value into the specified section and entry.
int getProfileInt(LPCTSTR lpszSection, LPCTSTR lpszEntry, int nDefault);
Retrieves an integer value from the specified section and entry.
LPSTR getProfileString(LPCTSTR lpszSection, LPCTSTR lpszEntry, LPCSTR lpszDefault, LPSTR lpBuffer, UINT nBufferSize);
Retrieves a string value from the specified section and entry.
bool saveProfile();
Saves the current profile to disk.
bool loadProfile();
Loads the current profile from disk. It must be called before reading or writing from the profile.
Comments
To use this code, just include the xmlprofile.h and xmlprofile.cpp into your project and add comsupp.lib to the linker dependencies.
Example code:
#include "xmlprofile.h"
int main()
{
CXMLProfile xmlProfile("MyApplication");
xmlProfile.loadProfile();
xmlProfile.writeProfileString("Owner", "Name", "John");
xmlProfile.writeProfileInt("Owner", "Age", 20);
CHAR szName[255];
xmlProfile.getProfileString("Owner", "Name", "None", szName, 255);
int nAge = xmlProfile.getProfileInt("Owner", "Age", 0);
xmlProfile.saveProfile();
}
Updates
- 21/August/2002 - Now uses the "Documents and Settings\Username" directory to store the profile if the current OS allows it (NT/2000/XP).
- 18/February/2004 - Now this class is not MFC-dependent and some functions have been added/improved.