/************************************************************************
Filename : IniFile.C
Version : 0.51
Author(s) : Carsten Breuer
--[ Description ]-------------------------------------------------------
This file contains a complete interface to read and write ini files
like windows do it. It's also avaiable as a C++ class.
--[ History ] ----------------------------------------------------------
0.10: Original file by Carsten Breuer. First beta version.
0.20: Some bugs resolved and some suggestions from
jim hall (freedos.org) implemented.
0.30: Some stuff for unix added. They dont know strupr.
Thanks to Dieter Engelbrecht (dieter@wintop.net).
0.40: Bug at WriteString fixed.
0.50: Problem with file pointer solved
0.51: We better do smaller steps now. I have reformated to tab4. Sorry
New function DeletepKey added. Thanks for the guy who post this.
--[ How to compile ]----------------------------------------------------
This file was developed under DJGPP and Rhide. If you are familiar with
Borland C++ 3.1, you will feel like at home ;-)).
Both tools are free software.
Downloads at: http://www.delorie.com/djgpp
--[ Where to get help/information ]-------------------------------------
The author : C.Breuer@OpenWin.de
--[ License ] ----------------------------------------------------------
LGPL (Free for private and comercial use)
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
------------------------------------------------------------------------
Copyright (c) 2000 Carsten Breuer
/************************************************************************/
/* defines for, or consts and inline functions for C++ */
/* global includes */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h> /* Only needed for the test function */
/* local includes */
#include "IniFile.h"
/* Global Variables */
struct ENTRY *Entry = NULL;
struct ENTRY *CurEntry = NULL;
char Result[255] =
{""};
FILE *IniFile;
/* Private functions declarations */
void AddpKey (struct ENTRY *Entry, cchr * pKey, cchr * Value);
void FreeMem (void *Ptr);
void FreeAllMem (void);
bool FindpKey (cchr * Section, cchr * pKey, EFIND * List);
bool AddSectionAndpKey (cchr * Section, cchr * pKey, cchr * Value);
struct ENTRY *MakeNewEntry (void);
/*=========================================================================
strupr -de-
-------------------------------------------------------------------------
Job : String to Uppercase 22.03.2001 Dieter Engelbrecht dieter@wintop.net
/*========================================================================*/
#ifdef DONT_HAVE_STRUPR
/* DONT_HAVE_STRUPR is set when INI_REMOVE_CR is defined */
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++;
}
}
#endif
/*=========================================================================
OpenIniFile
-------------------------------------------------------------------------
Job : Opens an ini file or creates a new one if the requested file
doesnt exists.
Att : Be sure to call CloseIniFile to free all mem allocated during
operation!
/*========================================================================*/
bool
OpenIniFile (cchr * FileName)
{
char Str[255];
char *pStr;
struct ENTRY *pEntry;
FreeAllMem ();
if (FileName == NULL)
{
return FALSE;
}
if ((IniFile = fopen (FileName, "r")) == NULL)
{
return FALSE;
}
while (fgets (Str, 255, IniFile) != 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->Text = (char *) malloc (strlen (Str) + 1);
if (pEntry->Text == NULL)
{
FreeAllMem ();
return FALSE;
}
strcpy (pEntry->Text, 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;
}
}
CurEntry = pEntry;
}
fclose (IniFile);
IniFile = NULL;
return TRUE;
}
/*=========================================================================
CloseIniFile
-------------------------------------------------------------------------
Job : Frees the memory and closes the ini file without any
modifications. If you want to write the file use
WriteIniFile instead.
/*========================================================================*/
void
CloseIniFile (void)
{
FreeAllMem ();
if (IniFile != NULL)
{
fclose (IniFile);
IniFile = NULL;
}
}
/*=========================================================================
WriteIniFile
-------------------------------------------------------------------------
Job : Writes the iniFile to the disk and close it. Frees all memory
allocated by WriteIniFile;
/*========================================================================*/
bool
WriteIniFile (const char *FileName)
{
struct ENTRY *pEntry = Entry;
IniFile = NULL;
if (IniFile != NULL)
{
fclose (IniFile);
}
if ((IniFile = fopen (FileName, "wb")) == NULL)
{
FreeAllMem ();
return FALSE;
}
while (pEntry != NULL)
{
if (pEntry->Type != tpNULL)
{
#ifdef INI_REMOVE_CR
fprintf (IniFile, "%s\n", pEntry->Text);
#else
fprintf (IniFile, "%s\r\n", pEntry->Text);
#endif
pEntry = pEntry->pNext;
}
}
fclose (IniFile);
IniFile = NULL;
return TRUE;
}
/*=========================================================================
WriteString : Writes a string to the ini file
*========================================================================*/
void
WriteString (cchr * Section, cchr * pKey, cchr * Value)
{
EFIND List;
char Str[255];
if (ArePtrValid (Section, pKey, Value) == FALSE)
{
return;
}
if (FindpKey (Section, pKey, &List) == TRUE)
{
sprintf (Str, "%s=%s%s", List.KeyText, Value, List.Comment);
FreeMem (List.pKey->Text);
List.pKey->Text = (char *) malloc (strlen (Str) + 1);
strcpy (List.pKey->Text, Str);
}
else
{
if ((List.pSec != NULL) && (List.pKey == NULL)) /* section exist, pKey not */
{
AddpKey (List.pSec, pKey, Value);
}
else
{
AddSectionAndpKey (Section, pKey, Value);
}
}
}
/*=========================================================================
WriteBool : Writes a boolean to the ini file
*========================================================================*/
void
WriteBool (cchr * Section, cchr * pKey, bool Value)
{
char Val[2]
- 1
- 2
- 3
- 4
- 5
- 6
前往页