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 */
{