Click here to Skip to main content
15,886,258 members
Articles / Desktop Programming / MFC

Application Configuration File Variables

Rate me:
Please Sign up or sign in to vote.
3.18/5 (8 votes)
12 Apr 20048 min read 44.5K   795   12  
Type-safe access to read-only or read/write configuration file variables using "one-line-of-code".
#include "StdAfx.h"
#include "WinAppGlobalVars.h"

/////////////////////////////////////////////////////////////////

IMPLEMENT_SERIAL(var_info, CObject, APPVAR_VER);
IMPLEMENT_SERIAL(CAppVar, CObject, APPVAR_VER);
CObList CAppVar::m_variableList;

CAppVar::~CAppVar()
{
	POSITION pos = m_variableList.GetHeadPosition();

	while(pos)
	{
		var_info* info = static_cast<var_info*>(m_variableList.GetNext(pos));
		ASSERT_VALID(info);
		ASSERT(info->IsKindOf(RUNTIME_CLASS(var_info)));
		delete info;
	}
}

var_info* CAppVar::Fetch(const char* name, bool assert)
{
	POSITION pos = m_variableList.GetHeadPosition();

	while(pos)
	{
		var_info* info = static_cast<var_info*>(m_variableList.GetNext(pos));
		ASSERT_VALID(info);
		ASSERT(info->IsKindOf(RUNTIME_CLASS(var_info)));

		if(info->name == name)
			return info;
	}

	if(!assert)
		return NULL;

	// must return a valid value.
	// assert is here so debugger doesnt point to macro
	ASSERT(false);
	return NULL;
}


void CAppVar::Serialize(CArchive& ar)
{
	if(ar.IsStoring())
	{
		POSITION pos = m_variableList.GetHeadPosition();
		while(pos)
		{
			var_info* var = (var_info*)m_variableList.GetNext(pos);
			ASSERT_VALID(var);
			ASSERT(var->IsKindOf(RUNTIME_CLASS(var_info)));
			var->Serialize(ar);
		}
	}
	else
	{
		int len = ar.GetFile()->GetLength();
		for(int i = 0; i < m_variableList.GetCount(); i++)
		{
			CString name;	ar >> name;
			var_info* var = Fetch(name, false);
			if(var)	var->Serialize(ar);

			if(ar.IsBufferEmpty()) break;
		}
	}
}

CString var_info::ToString()
{
	if(type.Find(_T("char*")) != -1)
		return CString(_string(char*, data));

	if(type == _T("BOOL"))
		return CString((_simple(BOOL, data) == TRUE) ? _T("TRUE"):_T("FALSE"));

	if(type == _T("int"))
	{
		CString str;
		str = _simple(int, data);
		return str;
	}

	if(type == _T("UINT"))
	{
		CString str;
		str = _simple(int, data);
		return str;
	}
		
	CString error("** Need to add a conversion for the type (");
	error += type;
	error += "). See var_info::ToString() **";
	return error;
}

void var_info::ToValue(const char* val)
{
	ASSERT(val);
	if(type.Find(_T("char*")) != -1)
	{
		operator=(val);
		return;
	}

	if((type.Find(_T("int")) != -1) || (type.Find(_T("INT")) != -1))
	{
		int temp = atoi(val);
		operator=(temp);
		return;
	}

	if(type == "BOOL")
	{
		BOOL temp = FALSE;
		if(stricmp(val, "TRUE") == 0)
			temp = TRUE;
		operator=(temp);
		return;
	}

	CString error("** Need to add a conversion for the type (");
	error += type;
	error += "). See var_info::ToValue() **";
	
}

/////////////////////////////////////////////////////////////////

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

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
Australia Australia
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions