#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cctype>
#include "iniFile.h"
using namespace std;
void strupr( char *str )
{
// We dont check the ptr because the original also dont do it.
while (*str != 0)
{
if ( islower( *str ) )
{
*str = toupper( *str );
}
str++;
}
}
/*=========================================================================
strupr -de-
-------------------------------------------------------------------------
Job : String to Uppercase 22.03.2001 Dieter Engelbrecht dieter@wintop.net
/*========================================================================*/
/* DONT_HAVE_STRUPR is set when INI_REMOVE_CR is defined */
/*=========================================================================
CIniFile : The constructor
*========================================================================*/
CIniFile::CIniFile ()
{
m_pEntry = NULL;
m_pCurEntry = NULL;
m_result [0] = 0;
m_pIniFile = NULL;
}
/*=========================================================================
CIniFile : The destructor
*========================================================================*/
CIniFile::~CIniFile ()
{
FreeAllMem ();
if (m_pIniFile != NULL)
{
fclose (m_pIniFile);
m_pIniFile = NULL;
}
}
/*=========================================================================
CIniFile : GetVersion
Info : The version is BCD coded. It maintain the major version in
the upper 8 bits and the minor in the lower.
0x0120 means version 1.20
*========================================================================*/
UINT CIniFile::GetVersion (void)
{
return 0x0030;
}
/*=========================================================================
OpenIniFile
-------------------------------------------------------------------------
Job : Opens an ini file or creates a new one if the requested file
doesnt exists.
/*========================================================================*/
BOOL CIniFile::OpenIniFile (CCHR * FileName)
{
char Str [255];
char *pStr;
struct ENTRY *pEntry;
FreeAllMem ();
int Len;
if (FileName == NULL) { return FALSE; }
if ((m_pIniFile = fopen (FileName, "r")) == NULL) { return FALSE; }
while (fgets (Str, 255, m_pIniFile) != NULL)
{
pStr = strchr (Str, '\n');
if (pStr != NULL) { *pStr = 0; }
pEntry = MakeNewEntry ();
if (pEntry == NULL) { return FALSE; }
#ifdef INI_REMOVE_CR
Len = strlen(Str);
if ( Len > 0 )
{
if ( Str[Len-1] == '\r' )
{
Str[Len-1] = '\0';
}
}
#endif
pEntry->pText = (char *)malloc (strlen (Str)+1);
if (pEntry->pText == NULL)
{
FreeAllMem ();
return FALSE;
}
strcpy (pEntry->pText, Str);
pStr = strchr (Str,';');
if (pStr != NULL) { *pStr = 0; } /* Cut all comments */
if ( (strstr (Str, "[") > 0) && (strstr (Str, "]") > 0) ) /* Is Section */
{
pEntry->Type = tpSECTION;
}
else
{
if (strstr (Str, "=") > 0)
{
pEntry->Type = tpKEYVALUE;
}
else
{
pEntry->Type = tpCOMMENT;
}
}
m_pCurEntry = pEntry;
}
fclose (m_pIniFile);
m_pIniFile = NULL;
return TRUE;
}
/*=========================================================================
CloseIniFile
-------------------------------------------------------------------------
Job : Closes the ini file without any modifications. If you want to
write the file use WriteIniFile instead.
/*========================================================================*/
void CIniFile::CloseIniFile (void)
{
FreeAllMem ();
if (m_pIniFile != NULL)
{
fclose (m_pIniFile);
m_pIniFile = NULL;
}
}
/*=========================================================================
WriteIniFile
-------------------------------------------------------------------------
Job : Writes the iniFile to the disk and close it. Frees all memory
allocated by WriteIniFile;
/*========================================================================*/
bool CIniFile::WriteIniFile (const char *pFileName)
{
struct ENTRY *pEntry = m_pEntry;
if (m_pIniFile != NULL)
{
fclose (m_pIniFile);
}
if ((m_pIniFile = fopen (pFileName, "wb")) == NULL)
{
FreeAllMem ();
return FALSE;
}
while (pEntry != NULL)
{
if (pEntry->Type != tpNULL)
{
#ifdef INI_REMOVE_CR
fprintf (m_pIniFile, "%s\n", pEntry->pText);
#else
fprintf (m_pIniFile, "%s\r\n", pEntry->pText);
#endif
}
pEntry = pEntry->pNext;
}
fclose (m_pIniFile);
m_pIniFile = NULL;
return TRUE;
}
/*=========================================================================
WriteString : Writes a string to the ini file
*========================================================================*/
void CIniFile::WriteString (CCHR *pSection, CCHR *pKey, CCHR *pValue)
{
EFIND List;
char Str [255];
if (ArePtrValid (pSection, pKey, pValue) == FALSE) { return; }
if (FindKey (pSection, pKey, &List) == TRUE)
{
sprintf (Str, "%s=%s%s", List.KeyText, pValue, List.Comment);
FreeMem (List.pKey->pText);
List.pKey->pText = (char *)malloc (strlen (Str)+1);
strcpy (List.pKey->pText, Str);
}
else
{
if ((List.pSec != NULL) && (List.pKey == NULL)) /* section exist, Key not */
{
AddKey (List.pSec, pKey, pValue);
}
else
{
AddSectionAndKey (pSection, pKey, pValue);
}
}
}
/*=========================================================================
WriteBool : Writes a boolean to the ini file
*========================================================================*/
void CIniFile::WriteBool (CCHR *pSection, CCHR *pKey, bool Value)
{
char Val [2] = {'0',0};
if (Value != 0) { Val [0] = '1'; }
WriteString (pSection, pKey, Val);
}
/*=========================================================================
WriteInt : Writes an integer to the ini file
*========================================================================*/
void CIniFile::WriteInt (CCHR *pSection, CCHR *pKey, int Value)
{
char Val [12]; /* 32bit maximum + sign + \0 */
sprintf (Val, "%d", Value);
WriteString (pSection, pKey, Val);
}
/*=========================================================================
WriteDouble : Writes a double to the ini file
*========================================================================*/
void CIniFile::WriteDouble (CCHR *pSection, CCHR *pKey, double Value)
{
char Val [32]; /* DDDDDDDDDDDDDDD+E308\0 */
sprintf (Val, "%1.10lE", Value);
WriteString (pSection, pKey, Val);
}
/*=========================================================================
ReadString : Reads a string from the ini file
*========================================================================*/
CCHR *CIniFile::