/************************************************************************
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 */
//#include <stdbool.h>
/* local includes */
#include "IniFile.h"
#ifdef LINUX
#include <termios.h>
#include <unistd.h>
#include <fcntl.h>
#endif /*LINUX*/
/* 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
int 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
*=====================
C语言解析ini文件
需积分: 50 185 浏览量
2018-08-16
16:04:38
上传
评论
收藏 7KB RAR 举报
Homekit2015
- 粉丝: 64
- 资源: 32
最新资源
- 探索tecreate:软件开发的未来之星.zip
- 打标机项目C#源码连接扫码
- 基于SSM的房屋租赁系统的设计与实现
- xyctf:从入门到精通的实用指南.zip
- mmqrcode1714153659780.png
- Screenshot_2024-04-27-06-08-58-486_com.baidu.xin.aiqicha.jpg
- 基于Javaweb+Tomcat+MySQL的大学生公寓管理系统+sql文件.zip
- 实训作业基于javaweb的订单管理系统源码+数据库+实训报告.zip
- 多机调度问题贪心算法基于最小堆和贪心算法求解多机调度问题.zip
- 基于同态加密技术的匿名电子投票系统源码.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈