Click here to Skip to main content
15,914,488 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
AnswerRe: any way of dynamically adding and removing pages from a CPropertySheet??? Pin
Tomasz Sowinski7-Nov-01 1:06
Tomasz Sowinski7-Nov-01 1:06 
GeneralStartup Pin
Peter Liddle6-Nov-01 11:30
Peter Liddle6-Nov-01 11:30 
GeneralRe: Startup Pin
Ravi Bhavnani6-Nov-01 11:42
professionalRavi Bhavnani6-Nov-01 11:42 
GeneralRe: Startup Pin
Anders Molin6-Nov-01 11:46
professionalAnders Molin6-Nov-01 11:46 
GeneralRe: Startup Pin
Ravi Bhavnani6-Nov-01 12:02
professionalRavi Bhavnani6-Nov-01 12:02 
GeneralRe: Startup Pin
Christian Graus6-Nov-01 11:44
protectorChristian Graus6-Nov-01 11:44 
GeneralRe: Startup Pin
Nish Nishant6-Nov-01 21:33
sitebuilderNish Nishant6-Nov-01 21:33 
GeneralDisappearing Act Pin
Stephen Caldwell6-Nov-01 11:29
Stephen Caldwell6-Nov-01 11:29 
Here is my class declaration:
#ifndef _FSCONFIGOBJECT_H_
#define _FSCONFIGOBJECT_H_

//Error Codes
#define FS_CO_OK			100
#define FS_CO_CANTOPENFILE  101
#define BUFSIZE 1028


enum fsType {fsInteger, fsString, fsDouble};
//Holds Setting information for an Object
struct set_element
{
	char* name;		//Printable Name
	fsType type;	//Type (Integer, String, Double)
	char* doc;		//Description
	void* setting;	//Value
};

class fsConfigObject
{
protected:
	struct fsCOElem
	{
		set_element seData;
		fsCOElem* pNext;
	};

	fsCOElem* pLast;

public:
	fsConfigObject();
	virtual ~fsConfigObject();
	
	void Push(set_element Elem);
	void Push(char* name, char* doc, enum fsType type, void* value);
	const set_element* GetSetting(char* name);
	int SetSetting(char* name, void* value);
	int LoadConfig(char* filename);
	int SaveConfig(char* filename);

};

#endif


Here is my class definition(sorry if it's long):
#include <windows.h>
#include <stdio.h>
#include <string.h>
#include "fsConfigObject.h"

fsConfigObject::fsConfigObject()
{
	//Constructor
	pLast = NULL;
}

fsConfigObject::~fsConfigObject()
{
	//destructor
}

const set_element* fsConfigObject::GetSetting(char* name)
{
	fsCOElem* pCur = pLast;
	while(pCur)
	{
		if (strcmp(pCur->seData.name, name) == 0)
		{
			return (&pCur->seData);
		}
		pCur = pCur->pNext;
	}
	return ((set_element*)NULL);
}

int fsConfigObject::SetSetting(char* name, void* value)
{
	fsCOElem* pCur = pLast;
	while(pCur)
	{
		if (strcmp(pCur->seData.name, name) == 0 )
		{
			switch(pCur->seData.type)
			{
			case fsInteger:
				pCur->seData.setting = (int*)value;
				break;
			case fsString:
				pCur->seData.setting = (char*)value;
				break;
			case fsDouble:
				pCur->seData.setting = (double*)value;
				break;
			}
			break; //out of while loop
		}

		pCur = pCur->pNext;
	}

	if (!pCur)
		return NULL;
	
	return 1;
}

void fsConfigObject::Push(set_element Elem)
{
	fsCOElem newelem;
	newelem.pNext = pLast;
	newelem.seData = Elem;
	pLast = &newelem;
}

void fsConfigObject::Push(char* name, char* doc, enum fsType type, void* value)
{
	set_element elem;
	elem.name = name;
	elem.doc = doc;
	elem.type = type;
	elem.setting = value;
	Push(elem);
}


int fsConfigObject::LoadConfig(char* filename)
{
	FILE* pConfigFile;

	pConfigFile = fopen(filename,"r");
	if (!pConfigFile)
		return FS_CO_CANTOPENFILE;
	
	//Get the file size
	fseek(pConfigFile, 0, SEEK_END);
	int filelen = ftell(pConfigFile);
	fseek(pConfigFile, 0, SEEK_SET); //Back to the beginning

	//Parse the config file
	//setup our vars
	char pchLnBuffer[BUFSIZE];
	char pchBuffer;
	int nLnNum = 0;
	int nLnLen = 0;
	char pchVarName[BUFSIZE];
	char pchValue[BUFSIZE];
	int pos = 0;
	
	memset(pchVarName, '\0', sizeof(pchVarName));
	memset(pchValue, '\0', sizeof(pchVarName));

	while(!feof(pConfigFile))	//read a file
	{
		nLnNum++;
		while(1)//Read a line
		{
			if (!fread(&pchBuffer,1,1,pConfigFile))
			{
				pchLnBuffer[pos++] = '\0';
				break;
			}
			if (pchBuffer != '\n' && pchBuffer != '\t')
				pchLnBuffer[pos++] = pchBuffer;
			else
			{
				pchLnBuffer[pos++] = '\0';
				break;
			}

			pchBuffer = NULL;
		}
		
		nLnLen = strlen(pchLnBuffer);
		//Get the chars upto the equal sign
		char* pch = strchr(pchLnBuffer, '=');
		if (!pch)
		{
			printf("Syntax Error: Line %d", nLnNum); //= wasn't found, syntax bad!
			continue;
		}

		//Get the Var name
		strncpy(pchVarName,pchLnBuffer, pch-pchLnBuffer);

		//Get the value
		int tmp = strlen(pch);
		memmove(pch, pch+1, strlen(pch));
		strcpy(pchValue, pch);
		
		//Set the Value
		/*if (SetSetting(pchVarName, pchValue) == NULL)
		{
			printf("Undefined Identifier: %s Line: %d\n", pchVarName, nLnLen);
			continue;
		}*/

		//reset the counters
		pos = 0;
	}

	fclose(pConfigFile);
	return FS_CO_OK;
}


here is a sample program that uses the aforementioned class(Note: Some parts excluded):
int main(int argc, char** argv)
{
   fsConfigObject fsco;
   fsco.Push("TestVar", "Test Var", fsString, "Test Value"); //Set up a setting default
   fsco.LoadConfig("conf\\conf.file");  //Load a new value for the setting;
   printf("%s\n", fsco.GetSetting("TestVar"));
   return 0;
}


Whenever I run the above I recieve a Access Violation when GetSetting is called.
During debug I see that when LoadConfig is called the values set during push are no longer valid. They no longer exist. It only happens when LoadConfig is called. When I call GetSetting without calling LoadConfig it works fine. Please help. I don't understand what's happening Confused | :confused: . Thanx.

Stephen Caldwell
Blackfission, CEO
http://bf.steffc.yi.org:81
GeneralReappearing Act Pin
Alvaro Mendez6-Nov-01 12:17
Alvaro Mendez6-Nov-01 12:17 
QuestionCombobox list height? Pin
clintsinger6-Nov-01 11:03
clintsinger6-Nov-01 11:03 
AnswerRe: Combobox list height? Pin
Joaquín M López Muñoz6-Nov-01 12:35
Joaquín M López Muñoz6-Nov-01 12:35 
GeneralRe: Combobox list height? Pin
clintsinger6-Nov-01 18:40
clintsinger6-Nov-01 18:40 
GeneralRe: Combobox list height? Pin
Joaquín M López Muñoz6-Nov-01 19:57
Joaquín M López Muñoz6-Nov-01 19:57 
GeneralRe: Combobox list height? Pin
clintsinger7-Nov-01 6:03
clintsinger7-Nov-01 6:03 
GeneralFrom one ListBox to another Pin
Monica6-Nov-01 10:54
Monica6-Nov-01 10:54 
GeneralRe: From one ListBox to another Pin
Ravi Bhavnani6-Nov-01 11:03
professionalRavi Bhavnani6-Nov-01 11:03 
GeneralListBoxes and the magic of VKeyToItem() Pin
Andrew Stampor6-Nov-01 10:50
Andrew Stampor6-Nov-01 10:50 
GeneralRe: ListBoxes and the magic of VKeyToItem() Pin
Ravi Bhavnani6-Nov-01 11:15
professionalRavi Bhavnani6-Nov-01 11:15 
GeneralRe: ListBoxes and the magic of VKeyToItem() Pin
Andrew Stampor6-Nov-01 11:26
Andrew Stampor6-Nov-01 11:26 
GeneralSetCapture hell Pin
Christian Graus6-Nov-01 10:41
protectorChristian Graus6-Nov-01 10:41 
GeneralRe: SetCapture hell Pin
Andrew Peace6-Nov-01 13:27
Andrew Peace6-Nov-01 13:27 
GeneralRe: SetCapture hell Pin
Jon Hulatt6-Nov-01 22:16
Jon Hulatt6-Nov-01 22:16 
Generalbrowser Pin
6-Nov-01 9:45
suss6-Nov-01 9:45 
General#include "everything.h" Pin
Jamie Hale6-Nov-01 9:18
Jamie Hale6-Nov-01 9:18 
GeneralRe: #include "everything.h" Pin
PJ Arends6-Nov-01 9:31
professionalPJ Arends6-Nov-01 9:31 

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.