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


转载于:EMeiMountainMonkey的专栏

https://blog.csdn.net/emeimountainmonkey/article/details/8536787

      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 */
{                                                         /* remain Sections to RemainSectionbuf*/
len = (int)(szEnd - szLineStart);
RemainSectionbuf =(char* )malloc(len + 1);
if (!RemainSectionbuf) 
{
fclose(fp);
free(ReadBuffer);
free(PreSectionbuf);
return ret;
}
strncpy(RemainSectionbuf,szLineStart,len);
RemainSectionbuf[len]='\0';
szSectionEnd = szLineStart-1;
break;
}
   char *szAppNameEnd;
if ((szAppNameEnd = (char*)memchr( szLineStart, ']', szLineEnd - szLineStart )))
{
szLineStart++;

char * SecStart = szLineStart;
char * SecEnd = szAppNameEnd -1;
while (SecStart < szLineEnd && isspace((unsigned char)*SecStart))  SecStart++;
while ((SecEnd > SecStart) && isspace((unsigned char)*SecEnd)) SecEnd--;


len =(int) (SecEnd - SecStart + 1);
strncpy(tempAppName,SecStart,len);
tempAppName[len] = '\0';
if (chAppName)
{
if (0 == _strnicmp(tempAppName,chAppName,AppNameLen) && tempAppName[AppNameLen] == '\0')                 /*  AppName is found, and store the previous section to PreSectionbuf*/
{
IsFindAppName = true;
len = (int) (szLineStart -2 - ReadBuffer + 1); 
PreSectionbuf = (char*)malloc (len+1); 
if (!PreSectionbuf) 
{
free(ReadBuffer);
fclose(fp);
return ret;
}
memset(PreSectionbuf,0,len+1);
strncpy(PreSectionbuf,ReadBuffer,len);
szSectionStart = szLineStart - 1;
}
}
continue;  
}
}


len =(int) (szLineEnd - szLineStart );
if ((szValueStart = (char*)memchr( szLineStart, '=', szLineEnd - szLineStart )) != NULL)
{
const char *szKeyNameEnd = szValueStart;
const char *EaqualSign =szValueStart;
while (szKeyNameEnd > szLineStart &&isspace((unsigned char)szKeyNameEnd[-1]))  szKeyNameEnd--;
len =(int) (szKeyNameEnd - szLineStart);
szValueStart++;
while (szValueStart < szLineEnd && isspace((unsigned char)*szValueStart)) szValueStart++;


if (len > 0)
{
strncpy(tempKeyName,szLineStart,len);
tempKeyName[len] = '\0';


if ( chKeyName && IsFindAppName)
{
if (0 == _strnicmp(tempKeyName,chKeyName,KeyNameLen) && tempKeyName[KeyNameLen] == '\0') /* AppName and kyeName are found, copy the remain Entries and Sections*/
{                                                                                       /* to RemainSectionbuf*/
IsFindKeyName = true;     
if (szValueStart)
{
len = (int)(szLineEnd - szValueStart );
TempValue =(char*) malloc(len + 1);
if (!TempValue) 
{
free(ReadBuffer);
free(PreSectionbuf);
fclose(fp);
return ret;
}
memcpy(TempValue, szValueStart, len * sizeof(char));
TempValue[len] = '\0';

}
else TempValue = NULL;


len =(int)( szEnd - szLineEnd);
RemainSectionbuf =(char* )malloc(len + 1);
if (!RemainSectionbuf) 
{
free(ReadBuffer);
free(PreSectionbuf);
if (TempValue) free(TempValue);
fclose(fp);
return ret;
}
strncpy(RemainSectionbuf,szLineEnd,len);
RemainSectionbuf[len]='\0';
szSectionEnd = szLineEnd;     


if (chValue)                                 /*If chValue is Not NULL, change the current entry, and store current */ 
{ /*Section to CurSectionbuf*/   
if (TempValue)
{
if (0 == strcmp(TempValue,chValue))
{
IsFileChanged =false;
free(TempValue);
break;
}
}

int len1 = (int) (szLineStart - szSectionStart);
int len2 = (int) (EaqualSign+1 - szLineStart);
len =len1 + len2+ ValueLen + 2;

CurSectionbuf =(char*) malloc(len +1);
if (!CurSectionbuf)
{
free(ReadBuffer);
free(PreSectionbuf);
free( RemainSectionbuf);
if (TempValue) free(RemainSectionbuf);
fclose(fp);
return ret;
}
memset(CurSectionbuf,0,len + 1);
strncpy(CurSectionbuf,szSectionStart,len1);
strncpy(CurSectionbuf +len1,szLineStart,len2);
strncpy(CurSectionbuf+ len1 +len2,chValue,ValueLen);
} /* If chValue is  NULL, delete the current entry, and store current */  
else                                        /* Section to CurSectionbuf */ 
{
len = (int) (szLineStart - 1 - szSectionStart); 
CurSectionbuf =(char*) malloc(len +1);
if (!CurSectionbuf) 
{
free(ReadBuffer);
free (PreSectionbuf);
free(RemainSectionbuf);
if (TempValue)  free(TempValue);
fclose(fp);
return ret;
}
memset(CurSectionbuf,0,len + 1);
strncpy(CurSectionbuf,szSectionStart,len);
}
if (TempValue) free(TempValue);
break;
}
}
}
}
}




if (IsFindKeyName && IsFileChanged)       /* KeyName is found.*/
{
len = (int)strlen(PreSectionbuf);
len +=(int)strlen(CurSectionbuf);
len +=(int)strlen(RemainSectionbuf);


WriteBuffer = (char *)malloc(len+1);
if (!WriteBuffer)
{
free(ReadBuffer);
free(PreSectionbuf);
free(CurSectionbuf);
free(RemainSectionbuf);
fclose(fp);
return ret;
}


*WriteBuffer ='\0';
strcat(WriteBuffer,PreSectionbuf);
strcat(WriteBuffer,CurSectionbuf);
strcat(WriteBuffer,RemainSectionbuf);
WriteBuffer[len]='\0';


free(PreSectionbuf);
free(CurSectionbuf);
free(RemainSectionbuf);
}


else if (IsFindAppName&&!IsFindKeyName &&IsFileChanged)       
{
if (!chKeyName)                             /*KeyName is NULL, delete all entries under the current Section.*/
{
len =(int) strlen(PreSectionbuf);
if(RemainSectionbuf) len +=(int)strlen(RemainSectionbuf);


WriteBuffer = (char *)malloc(len+1);
if (!WriteBuffer)
{
free(ReadBuffer);
free(PreSectionbuf);
if(RemainSectionbuf) free(RemainSectionbuf);
fclose(fp);
return ret;
}
*WriteBuffer ='\0';
strcat(WriteBuffer,PreSectionbuf);
if(RemainSectionbuf) 
strcat(WriteBuffer,RemainSectionbuf);
WriteBuffer[len]='\0';
if(RemainSectionbuf) free(RemainSectionbuf);
free(PreSectionbuf);


}
else                                        /*KeyName is not NULL*/
{                                         
if (chValue)                            /* chValue is not NULL, create new entry at the end of the current section*/
{
char  *buf;
if (szSectionEnd==NULL)
szSectionEnd = szEnd - 1;
buf =szSectionEnd;
int len1 = (int) (szSectionEnd - szSectionStart + 1);
while ( isspace((unsigned char)*szSectionEnd) ) szSectionEnd --;


int len2= (int)(buf - szSectionEnd);
int len3= (int) (szSectionEnd - szSectionStart+1);   


len =len1 +1 + KeyNameLen + ValueLen + 1 +len2;
if (len2 <= 0) len += 1;

CurSectionbuf =(char*) malloc(len+1);
if (!CurSectionbuf)
{
free(ReadBuffer);
free(PreSectionbuf);
if(RemainSectionbuf) free(RemainSectionbuf);
fclose(fp);
return ret;
}


memset(CurSectionbuf,0,len+1);
strncpy(CurSectionbuf,szSectionStart,len3);
strcat(CurSectionbuf,"\n");
strncpy(CurSectionbuf + len3 +1,chKeyName,KeyNameLen);
strcat(CurSectionbuf,"=");
strncpy(CurSectionbuf + len3 + 1 + KeyNameLen + 1 ,chValue,ValueLen);
strncpy(CurSectionbuf+len3+KeyNameLen + ValueLen+2,szSectionEnd+1,len2);
if (len2 <= 0) strcat(CurSectionbuf,"\n");


len = (int)strlen(PreSectionbuf);
len +=(int)strlen(CurSectionbuf);
if(RemainSectionbuf) len +=(int)strlen(RemainSectionbuf);


WriteBuffer = (char *)malloc(len+1);
if (!WriteBuffer)
{
free(ReadBuffer);
free(PreSectionbuf);
free(CurSectionbuf);
if(RemainSectionbuf) free(RemainSectionbuf);
fclose(fp);
return ret;
}
*WriteBuffer ='\0';
strcat(WriteBuffer,PreSectionbuf);
strcat(WriteBuffer,CurSectionbuf);
if(RemainSectionbuf) 
{
strcat(WriteBuffer,RemainSectionbuf);
free(RemainSectionbuf);
}
WriteBuffer[len]='\0';


free(PreSectionbuf);
free(CurSectionbuf);

}
else                                                  /*chValue is NULL, do nothing*/
{
IsFileChanged =false;
}
}
}
else if(!IsFindAppName&&!IsFindKeyName && IsFileChanged)     /*if keyName and chValue are not NULL, Create new Section.*/
{
if (!chKeyName || !chValue)
IsFileChanged =false;
else
{
len=(int)(strlen(ReadBuffer) +AppNameLen+KeyNameLen+ValueLen+ 3 + 2);
WriteBuffer=(char*)malloc(len + 2);
if (!WriteBuffer)
{
free(ReadBuffer);
fclose(fp);
return ret;
}
memset(WriteBuffer,0,len + 2);
strncpy(WriteBuffer,ReadBuffer,strlen(ReadBuffer));
if (ReadBuffer[strlen(ReadBuffer)-1] !='\n') 
strcat(WriteBuffer,"\n");
len =(int) strlen(WriteBuffer);
char *buf=WriteBuffer+len;
*buf++ ='[';
strncpy(WriteBuffer+ len +1,chAppName,AppNameLen);
buf +=strlen(buf);
*buf++ =']';
*buf++ ='\n';
strncpy(WriteBuffer +len +1+ AppNameLen +2,chKeyName,KeyNameLen);
buf +=strlen(buf);
*buf++ = '=';
strncpy(WriteBuffer+len +1+ AppNameLen +2 +KeyNameLen +1,chValue,ValueLen);
buf +=strlen(buf);
*buf++ ='\n';
}
}


fclose(fp);


if (!IsFileChanged)
{
if (WriteBuffer) free(WriteBuffer);
if (ReadBuffer) free(ReadBuffer);
if (CurSectionbuf) free(CurSectionbuf);
if (PreSectionbuf) free(PreSectionbuf);
if (RemainSectionbuf) free(RemainSectionbuf);


return ret=true;
}


if (WriteBuffer)
{
if (fp=fopen(chFileName,"w+"))
{
fseek(fp,0,SEEK_SET);
fprintf(fp,"%s",WriteBuffer,strlen(WriteBuffer));
fclose(fp);
}
free(WriteBuffer);
}


if(ReadBuffer) free(ReadBuffer);


ret =true;
return ret;
}


/******************************************************************************************/


unsigned int  get_profile_string  (char* chAppName,char* chKeyName,char* chDefault,char* chReturnedString,int nSize,char* chFileName)
{


FILE  *fp=NULL;
unsigned int ret=0;
char *tempDefValue=NULL;
int  AppNameLen=0,KeyNameLen=0;


if (!chReturnedString ||nSize<=1)
{
if (nSize == 1)
        chReturnedString='\0';
return 0;
}

memset(chReturnedString,0,nSize*sizeof(char));


if (chDefault)  
{
char *p = chDefault + strlen(chDefault) - 1;
while (p > chDefault && *p == ' ')
p--;
if (p >= chDefault)
{
   int len = (int)(p - chDefault) + 1;
tempDefValue = (char*)malloc(len + 1);
strncpy(tempDefValue,chDefault,len);
tempDefValue[len] = '\0';
chDefault = tempDefValue;
}
}
else
chDefault = "\0";


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 (!chFileName)
{
if(chAppName)
{
int len = (int)strlen(chDefault);
if(nSize<=len)
{
strncpy(chReturnedString,chDefault,nSize-1); 
chReturnedString[nSize-1]='\0';
ret = nSize-1 ;
}
else
{
strncpy(chReturnedString,chDefault,len);
ret = len;
}
}
else
{
ret=0;
*chReturnedString='\0';
}
if (tempDefValue) free(tempDefValue);
return ret;
}
if (!(fp = fopen(chFileName, "r+")))
{
if(chAppName)
{
   int len = (int)strlen(chDefault);
if(nSize<=len)
{
strncpy(chReturnedString,chDefault,nSize-1); 
chReturnedString[nSize-1]='\0';
ret = nSize-1 ;
}
else
{
strncpy(chReturnedString,chDefault,len);
ret = len;
}
}
else
{
ret=0;
*chReturnedString='\0';
}
if (tempDefValue) free(tempDefValue);
return ret;
}



bool bIsFindAppName =false,bIsFindKeyName=false;;
char *ReadBuffer = NULL;
char *szLineStart=NULL,*szLineEnd=NULL, *szEnd= NULL;
char *next_line=NULL,*szValueStart=NULL;

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


char * CopySectionName = (char*)malloc(nSize);
char * CopyKeyName = (char*)malloc(nSize);
if (!CopyKeyName || !CopySectionName)
{
fclose(fp);
if (tempDefValue) free(tempDefValue);
return 0;
}


char *p,*q;
int  len=0;
int plen=nSize - 1,qlen=nSize - 1;
bool bSectionCopyOver=false, bEntryCopyOver=false;


    memset(CopySectionName,0,nSize);
memset(CopyKeyName,0,nSize);


p = CopySectionName;
q = CopyKeyName;


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


if (nFileSize == INVALID_FILE_SIZE||nFileSize==0)   
{
int len = (int)strlen(chDefault);
if(nSize<=len)
{
strncpy(chReturnedString,chDefault,nSize-1); 
chReturnedString[nSize-1]='\0';
ret = nSize-1 ;
}
else
{
strncpy(chReturnedString,chDefault,len);
ret = len;
}


fclose(fp);
if (tempDefValue) free(tempDefValue);
   return ret;
}
else
{
ReadBuffer =(char*)malloc(nFileSize+1);
if (!ReadBuffer)
{
fclose(fp);
if (tempDefValue) free(tempDefValue);
assert(" malloc failed!");//
return ret;
}
memset(ReadBuffer,0,nFileSize+1);
if (!fread(ReadBuffer,sizeof(char),nFileSize,fp))
{
fclose(fp);
free(ReadBuffer);
if (tempDefValue) free(tempDefValue);
assert("file load failed!");//
return ret;
}
}


len = (int)strlen(ReadBuffer);
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;


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 (bIsFindAppName)
{
bEntryCopyOver = true;
}
const char *szAppNameEnd;
if ((szAppNameEnd = (char*)memchr( szLineStart, ']', szLineEnd - szLineStart )))
{
szLineStart++;
len =(int) (szAppNameEnd - szLineStart);
strncpy(tempAppName,szLineStart,len);
tempAppName[len] = '\0';
if (!bSectionCopyOver)                   /* Copy all SectionName to  CopySectionName buffer*/
{
int templen = (int)strlen(tempAppName) +1;
if (templen >= plen)
{
if (plen > 0)
{
strncpy(p, tempAppName,plen);
p+= plen-1;
*p++='\0';
}
*p ='\0';
ret = nSize -2 ;
bSectionCopyOver=true;
}
else
{
strncpy(p, tempAppName, templen-1);
strncpy(p+templen-1,"\t",1);
}

p += templen;
plen -= templen;
}
if (chAppName)
{
if (0 == _strnicmp(tempAppName,chAppName,AppNameLen)
     && tempAppName[AppNameLen] == '\0')                 /* AppName is found */ 
bIsFindAppName = true;
}
continue;  
}
}


len =(int) (szLineEnd - szLineStart) ;
if ((szValueStart = (char*)memchr( szLineStart, '=', szLineEnd - szLineStart)) != NULL)
{
const char *szKeyNameEnd = szValueStart;
while (szKeyNameEnd > szLineStart &&isspace((unsigned char)szKeyNameEnd[-1]))  szKeyNameEnd--;
len = (int)(szKeyNameEnd - szLineStart);
szValueStart++;
while (szValueStart < szLineEnd && isspace((unsigned char)*szValueStart)) szValueStart++;


if (len > 0  && bIsFindAppName)   
{
strncpy(tempKeyName,szLineStart,len);
tempKeyName[len] = '\0';
                if (!bEntryCopyOver)                  /* if AppName is found ,copy All KeyName to CopyKeyName buffer*/
                {
int templen =(int) strlen(tempKeyName) +1;
if (templen >= qlen)
{
if (qlen > 0)
{
strncpy(q, tempKeyName,qlen);
q+= qlen-1;
*q++='\0';
}
*q ='\0';
ret = nSize -2 ;
bEntryCopyOver =true;
}
else
{
strncpy(q, tempKeyName, templen-1);
strncpy(q+templen-1,"\t",1);
}
q += templen;
qlen -= templen;
                }
if (chKeyName)
{
if (0 == _strnicmp(tempKeyName,chKeyName,KeyNameLen) && tempKeyName[KeyNameLen] == '\0')    /* AppName and kyeName are found*/
{                                                
bIsFindKeyName = true; 
len = (int)(szLineEnd - szValueStart );
TempValue =(char*) malloc(len + 1);
memcpy(TempValue, szValueStart, len * sizeof(char));
TempValue[len] = '\0';
break;

}
}
}
}
}




    if(bIsFindKeyName)                     /* KeyName is found */
{
if (TempValue)
{
len =(int) strlen(TempValue);
if (nSize>len)
{
strncpy(chReturnedString,TempValue,len);
ret =len;
}
else
{
strncpy(chReturnedString,TempValue,nSize-1);
chReturnedString[nSize-1]='\0';
ret = nSize -1;
}
}
else
ret=0;
}
else if(bIsFindAppName)             /* Section is found,but Entry is not found */
{
if (!chKeyName)
{
len =(int) strlen(CopyKeyName);
strncpy(chReturnedString,CopyKeyName,len);
ret =len;
}
else
{
len = (int)strlen(chDefault);
if (nSize>len)
{
strncpy(chReturnedString,chDefault,len);
ret =len;
}
else
{
strncpy(chReturnedString,chDefault,nSize-1);
chReturnedString[nSize-1]='\0';
ret = nSize -1;                           /* UnSure return nSize or nSize-1*/
}
}
}
else                                           /* Section is not found*/
{
if (!chAppName)                            
{
len =(int) strlen(CopySectionName);
strncpy(chReturnedString,CopySectionName,len);
ret =len;
}
else
{
len = (int)strlen(chDefault);
if (nSize>len)
{
strncpy(chReturnedString,chDefault,len);
ret =len;
}
else
{
strncpy(chReturnedString,chDefault,nSize-1);
chReturnedString[nSize-1]='\0';
ret = nSize -1;                           /* UnSure return nSize or nSize-1*/
}
}
}


if(tempDefValue)    free(tempDefValue);
if(CopySectionName) free(CopySectionName);
if(CopyKeyName)     free(CopyKeyName);
if(ReadBuffer)      free(ReadBuffer);
if(TempValue)       free(TempValue);


fclose(fp);
return ret;
}



/******************************************************************************************************************************************************/

测试:

#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>

#define   Max_Size   100

int _tmain(int argc, _TCHAR* argv[])
{
char* strFileName="config.ini";

char* chAppName="DataSource_Local";
char* chKeyName="ipAdress";
char* chKeyValue="127.0.0.1";
write_profile_string(chAppName,chKeyName,chKeyValue,strFileName);

write_profile_string("DataSource_Local","Port","27017",strFileName);

write_profile_string("DataSource","Port","1012",strFileName);


        char *chValue=(char*)malloc(Max_Size*sizeof(char));

get_profile_string ("DataSource_Local","ipadress","127.0.0.1",chValue,Max_Size,strFileName);

free(chValue);

return 0;
}


.ini 文件

[DataSource_Local]

ipAdress=127.0.0.1

Port=27017

[DataSource]

Port=1012


 debug:

1>c:\users\administrator\desktop\readconfig\readconfig\readconfig.cpp(39) : warning C4996: 'strcpy': This function or variable may be unsafe. Consider using strcpy_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
1>        c:\program files (x86)\microsoft visual studio 8\vc\include\string.h(74) : see declaration of 'strcpy'
1>c:\users\administrator\desktop\readconfig\readconfig\readconfig.cpp(40) : warning C4996: 'strcat': This function or variable may be unsafe. Consider using strcat_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
1>        c:\program files (x86)\microsoft visual studio 8\vc\include\string.h(79) : see declaration of 'strcat'
1>c:\users\administrator\desktop\readconfig\readconfig\readconfig.cpp(44) : warning C4996: 'fopen': This function or variable may be unsafe. Consider using fopen_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
1>        c:\program files (x86)\microsoft visual studio 8\vc\include\stdio.h(234) : see declaration of 'fopen'

由于strcpy这类函数的不安全性,会产生一大堆的警告信息,如上。解决办法:在工程属性中的c/c++选项的Command Line 中添加上 D/"_CRT_SECURE_NO_WARNINGS" 就OK。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值