• Advertisement

Archived

This topic is now archived and is closed to further replies.

INI Parsing class

This topic is 5067 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

This is the INI file parsing class I made.
class DfgIniParser
{
public:
	char m_strFilename[MAX_PATH];

	void GetFilename(char* pIniFile);
	void GetString(char* pSection, char* pKeyword, char* pDefault, char* pDest);
	void GetString(char* pTitle, char* pSection, char* pKeyword, char* pDefault, char* pDest);
	void GetInt(char* pSection, char* pKeyword, int iDefault, int& pDest);
	void GetInt(char* pTitle, char* pSection, char* pKeyword, int iDefault, int& pDest);
	void GetFloat(char* pSection , char* pKeyword, float fDefault, float& pDest);
	void GetFloat(char* pTitle, char* pSection , char* pKeyword, float fDefault, float& pDest);
	void GetBool(char* pSection, char* pKeyword, bool bDefault, bool& bDest);
	void GetBool(char* pTitle, char* pSection, char* pKeyword, bool bDefault, bool& bDest);
};

// Definitions

void DfgIniParser::GetFilename(char* pIniFile)
{
	char Drive[64], Directory[64];
	GetModuleFileName(NULL, m_strFilename, MAX_PATH);
	_splitpath(m_strFilename, Drive, Directory, NULL, NULL);
	_makepath(m_strFilename, Drive, Directory, pIniFile, ".ini");
}

void DfgIniParser::GetString(char* pSection, char* pKeyword, char* pDefault, char* pDest)
{
	GetPrivateProfileString(pSection, pKeyword, pDefault, pDest, strlen(pDest) + 1, m_strFilename);
}

void DfgIniParser::GetString(char* pTitle, char* pSection, char* pKeyword, char* pDefault, char* pDest)
{
	GetFilename(pTitle);
	GetPrivateProfileString(pSection, pKeyword, pDefault, pDest, strlen(pDest) + 1, m_strFilename);
}

void DfgIniParser::GetInt(char* pSection, char* pKeyword, int iDefault, int& pDest)
{
	pDest = GetPrivateProfileInt(pSection, pKeyword, iDefault, m_strFilename);
}

void DfgIniParser::GetInt(char* pTitle, char* pSection, char* pKeyword, int iDefault, int& pDest)
{
	GetFilename(pTitle);
	pDest = GetPrivateProfileInt(pSection, pKeyword, iDefault, m_strFilename);
}

void DfgIniParser::GetFloat(char* pSection , char* pKeyword, float fDefault, float& pDest)
{
	char Dest[64];
	
	GetString(pSection, pKeyword, NULL, Dest);
	
	if(stricmp(Dest, "") == 0)
		pDest = fDefault;
	else
		pDest = (float)atof(Dest);
}

void DfgIniParser::GetFloat(char* pTitle, char* pSection , char* pKeyword, float fDefault, float& pDest)
{
	char Dest[64];
	GetFilename(pTitle);
	GetString(pSection, pKeyword, NULL, Dest);
	
	if(stricmp(Dest, "") == 0)
		pDest = fDefault;
	else
		pDest = (float)atof(Dest);
}

void DfgIniParser::GetBool(char* pSection, char* pKeyword, bool bDefault, bool& bDest)
{
	int iDest;
	GetInt(pSection, pKeyword, (int)bDefault, iDest);

	if(iDest == 0)
	{
		bDest = false;
		return;
	}
	
	bDest = true;
}

void DfgIniParser::GetBool(char* pTitle, char* pSection, char* pKeyword, bool bDefault, bool& bDest)
{
	int iDest;
	GetFilename(pTitle);
	GetInt(pSection, pKeyword, (int)bDefault, iDest);

	if(iDest == 0)
	{
		bDest = false;
		return;
	}
	
	bDest = true;
}
// For those who need help on the problem I had: Sorry, I didn't catch that the function requires the full pathname. I've figured it out and have got it working. If you're having where it is just using the default value, then check out this post: http://www.gamedev.net/community/forums/topic.asp?topic_id=131365 [edited by - Programmer16 on April 10, 2004 10:10:23 PM]

Share this post


Link to post
Share on other sites
Advertisement
Okay, I had it fixed, but now I can't get it to load a string from a file. This is my code:

// 2 functions from my parser

void GetFilename(char* pIniFile)
{
char Drive[64], Directory[64];
GetModuleFileName(NULL, m_strFilename, MAX_PATH);
_splitpath(m_strFilename, Drive, Directory, NULL, NULL);
_makepath(m_strFilename, Drive, Directory, pIniFile, ".ini");
}

void GetString(char* pTitle, char* pSection, char* pKeyword, char* pDefault, char* pDest)
{
GetFilename(pTitle);
GetPrivateProfileString(pSection, pKeyword, pDefault, pDest, strlen(pDest) + 1, m_strFilename);
}

// My code

DfgIniParser ipParser;
char strTitle[64];
int iWidth, iHeight;

ipParser.GetString(pIniFile, pSection, pTitleKeyword, "App", strTitle);
ipParser.GetInt(pSection, pResKeyword, 800, iWidth);
iHeight = (int)(iWidth / WND_RES_MOD);
//WND_RES_MOD = 1.333333333333333333333333333333


// And my ini file

[GAME_INFO]
Title=Valkyria Legends
Resolution=800


It has to be something I'm doing wrong because my GetInt() function works right. Thanks in advance!

[EDIT] I thought maybe that it might help if I tell you that it keeps giving me the default value no matter what keyword I use.

/*
I use DirectX 8.1 and C++ (Microsoft Visual C++ 6.0 Professional edition)
*/

[edited by - Programmer16 on April 10, 2004 11:49:26 AM]

[edited by - Programmer16 on April 10, 2004 11:52:23 AM]

Share this post


Link to post
Share on other sites
Ok, sorry! I feel like an ass right now. I have a HUGE comment that I wrote myself stating DON''T USE THE EXTENSION ON THE END OF THE FILENAME but I did anyway and that was my problem. Sorry again!

/*
I use DirectX 9 and C++ (Microsoft Visual C++ 6.0 Professional edition)
*/

Share this post


Link to post
Share on other sites

  • Advertisement