C 语言实现读配置文件(.ini)函数,替代GetPrivateProfileString,WritePrivateProfileString

本文介绍了如何使用C语言重新实现Windows平台上的WritePrivateProfileString和GetPrivateProfileString函数,以达到跨平台读写.ini配置文件的目的。通过示例代码展示了函数的使用方法,并给出了测试用例。
摘要由CSDN通过智能技术生成


      WritePrivateProfileString和GetPrivateProfileString函数是windows系统平台提供给我们的读写配置文件(.ini)的函数。由于其具有不可移植性,在Linux平台下不可用。所以现在用C语言重新实现了上述两个函数的功能,其参数和返回结果保持一样。


C++中函数原型如下:(详见MSDN)

BOOL WINAPI WritePrivateProfileString(
  _In_  LPCTSTR lpAppName,
  _In_  LPCTSTR lpKeyName,
  _In_  LPCTSTR lpString,
  _In_  LPCTSTR lpFileName
);
DWORD WINAPI GetPrivateProfileString(
  _In_   LPCTSTR lpAppName,
  _In_   LPCTSTR lpKeyName,
  _In_   LPCTSTR lpDefault,
  _Out_  LPTSTR lpReturnedString,
  _In_   DWORD nSize,
  _In_   LPCTSTR lpFileName
);

c语言中新定义:

bool write_profile_string(

char* chAppName,

char* chKeyName,

char* chValue,

char* chFileName

);


int  get_profile_string (

char* chAppName,

char* chKeyName,

char* chDefault,

char* chReturnedString,

int      nSize,

char* chFileName

);


函数实现部分:

/******************************************************************************************/
#define INVALID_FILE_SIZE ((unsigned long)0xFFFFFFFF)




bool write_profile_string(char* chAppName,char* chKeyName,char* chValue,char* chFileName)
{
bool ret = true;
FILE *fp=NULL;
char* ReadBuffer=NULL,*WriteBuffer=NULL;
bool IsFindAppName=false,IsFindKeyName=false;
    bool IsFileChanged=true;


char *szLineStart=NULL,*szLineEnd=NULL,*szEnd=NULL;
char *next_line=NULL,*szValueStart=NULL,*szValueEnd=NULL;
char *szSectionStart=NULL,*szSectionEnd=NULL;
int  line=0;
int  AppNameLen=0,KeyNameLen=0,ValueLen=0;


if (!chAppName) return ret=false;
if (!chFileName)return ret=false; 


if (chAppName)
{
char *p;
while (isspace(((unsigned char)*chAppName))) chAppName++;
if (*chAppName)
p= chAppName +strlen(chAppName) -1 ;
else
p=chAppName;
while ((p > chAppName) && isspace((unsigned char)*p)) p--;
AppNameLen = (int)(p -chAppName) + 1;
}


if (chKeyName)
{
char *p;
while (isspace(((unsigned char)*chKeyName))) chKeyName++;
if (*chKeyName)
p= chKeyName +strlen(chKeyName) -1 ;
else
p=chKeyName;
while ((p > chKeyName) && isspace((unsigned char)*p)) p--;
KeyNameLen =(int)( p -chKeyName + 1);
}

if (chValue)
{
char *p;
while (isspace(((unsigned char)*chValue))) chValue++;
if (*chValue)
p= chValue +strlen(chValue) -1 ;
else
p=chValue;
ValueLen =(int)( p -chValue + 1);
}



char*    PreSectionbuf=NULL;
char*    CurSectionbuf=NULL;
char*    RemainSectionbuf=NULL;
char tempAppName[1024]="";
char tempKeyName[1024]="";
char *TempValue; 


if (-1==_access(chFileName,0))
fp=fopen(chFileName,"w+");
else
fp=fopen(chFileName,"r+");


if (!fp) 
return ret=false;


fseek(fp,0,SEEK_END);
long nFileSize = ftell(fp);
fseek(fp,0,SEEK_SET);


if (nFileSize == INVALID_FILE_SIZE||nFileSize==0)         
{
if (nFileSize==INVALID_FILE_SIZE)        /*file is too large*/
{
fclose(fp);
return ret;
}
if (!chKeyName||!chValue)                 /* file is NULL, and keyName or chValue is NuLL too. do nothing but return*/
{
fclose(fp);
return ret;
}
else                                      /* file is NULL, create a new Section */
{
int len=(int)(AppNameLen+KeyNameLen+ValueLen+ 3 + 2);
WriteBuffer=(char*)malloc(len + 1);
if (!WriteBuffer)
{
fclose(fp);
return ret;
}
memset(WriteBuffer,0,len + 1);
char *buf=WriteBuffer;
*buf++ ='[';
strncpy(WriteBuffer + 1,chAppName,AppNameLen);
buf +=strlen(buf);
*buf++ =']';
*buf++ ='\n';
strncpy(WriteBuffer +3 +AppNameLen,chKeyName,KeyNameLen);
buf +=strlen(buf);
*buf++ = '=';
strncpy(WriteBuffer +4 +AppNameLen + KeyNameLen,chValue,ValueLen);
buf +=strlen(buf);
*buf++ ='\n';


if (!fprintf(fp,"%s",WriteBuffer,len))
ret=false;


free(WriteBuffer);
fclose(fp);
return ret;
}
}
else                                           /* file is not NULL.   copy all data to ReadBuffer*/
{


ReadBuffer =(char*)malloc(nFileSize);
if (!ReadBuffer)
{
fclose(fp);
return ret;
}
memset(ReadBuffer,0,nFileSize);
if (!fread(ReadBuffer,sizeof(char),nFileSize,fp))
{
fclose(fp);
free(ReadBuffer);
return ret;
}
}


int len=nFileSize;
len =(int) strlen(ReadBuffer);
int cur=0,i=0;
next_line = ReadBuffer;
szEnd  = ReadBuffer + len;


while (next_line < szEnd)                                    /*  analysis ReadBuffer*/
{
szLineStart = next_line;
next_line = (char*)memchr(szLineStart,'\n',szEnd-szLineStart);
if (!next_line) 
next_line = (char*)memchr(szLineStart, '\r', szEnd - szLineStart);
if (!next_line) 
next_line = szEnd;
else 
next_line++;


szLineEnd = next_line;


line++;


while (szLineStart < szLineEnd && isspace((unsigned char)*szLineStart))  szLineStart++;
while ((szLineEnd > szLineStart) && isspace((unsigned char)szLineEnd[-1])) szLineEnd--;


if (szLineStart >= szLineEnd) continue;


if (*szLineStart == '[') /* section start*/
{
if (IsFindAppName)                                        /* AppName is found, and keyName is not found, copy the */
{              
纯cini配置文件 用c/c++ini配置文件有不少第三方的开源库,如iniparser、libini、rwini、UltraLightINIParser等,但都不理想,往往代码较大、功能较弱、 接口使用不方便。尤其在大小写处理、前后空格、各种注释、跨平台换行符支持、带引号字符串处理、无section操作、原格式保持等方面存在问题。 现将本人精心制作的ini写程序源码奉献给大家,纯c编写,简洁好用。支持windows和linux。 主要特点: 1、支持;和#注释符号,支持行尾注释。 2、支持带引号'或"成对匹配的字符串,提取时自动去引号。引号中可带其它引号或;#注释符。 3、支持无section或空section(名称为空)。 4、支持10、16、8进制数,0x开头为16进制数,0开头为8进制。 5、支持section、key或=号前后带空格。 6、支持\n、\r、\r\n或\n\r换行格式。 7、不区分section、key大小写,但写入时以新串为准,并保持其大小写。 8、新增数据时,若section存在则在该节最后一个有效数据后添加,否则在文件尾部添加。 9、支持指定key所在整行删除,即删除该键值,包括注释。 10、可自动跳过格式错误行,修改时仍然保留。 11、修改时保留原注释:包括整行注释、行尾注释(包括前面空格)。 12、修改时保留原空行。以上三点主要是尽量保留原格式。 不足之处: 1、不支持单key多value(逗号分割),只能一次性提取后自行处理。 2、不支持同名重复section和key。(重复section可视为错误,重复key则可能造成分歧) 3、不能提取所有section或key名称。 使用只需两个文件inirw.h、inirw.c,另有测试程序和工程文件,支持windows和linux
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值