Code
// ConfigFileMng.cpp: implementation of the CConfigFileMng class.
//
//////////////////////////////////////////////////////////////////////
#include "ConfigFileMng.h"
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CConfigFileMng::CConfigFileMng()
{
}
CConfigFileMng::~CConfigFileMng()
{
}
bool CConfigFileMng::Open(const char *strFileName)
{
char szFileName[50]="";
int nFileNameLen=0;
char szFilePox[5]="";
FILE *pFile;
/*1.对传入的指针进行非空判断*/
if(0==strFileName)
{
return false;
}
//
strcpy(szFileName,strFileName);
if(strcmp(szFileName,"")==0)
{
return false;
}
nFileNameLen=strlen(szFileName);
if(0!=nFileNameLen)
{
for(int i=0;i<4,i++)
{
szFilePox[i]=szFileName[nFileLen-1-i];
}
szFilePox[i]='\0';
if(0!=strcmp(strFilePox,"ini."))
{
strcat(szFileName,".ini");
}
if(strlen(szFileName)>MAX_FILENAME_LEN)
{
return false;
}
pFile=fopen(szFileName,"r");
if(pFile==0)
{
return false;
}
m_bOpenFlag=true;
return true;
}
m_bOpenFlag=false;
return false;
}
bool CConfigFileMng::isOpen()
{
return m_bOpenFlag;
}
char* CConfigFileMng::getSession(const char *pStr,const char *strSesName)
{
char szTmpSes[1024]="";
int nPosBeg=0;
int nPosEnd=0;
if(0!=strstr(strSesName,"[")||0!=strstr(strSesName,"]"))
{
return false;
}
else
{
nPosBeg=strstr(strSesName,"[");
nPosEnd=strstr(strSes,"]");
}
for(int i=nPosBeg+1,i<nPosEnd,i++)
{
szTmpSes[i-nPosBeg-1]=strSesName[i];
}
if(0==strcmp(szTmpSes,szFileName))
{
return true;
}
else
{
return false;
}
}
char* CConfigFileMng::getContent(const char *pStr,const char *valueName,char *retStr)
{
char tmpStr[100];
int i=0;
int j=0;
retStr[0]=0;
while(pStr[i]==''||pStr[i]=='\t')
{
i++;
}
if(pStr[i]=="#")
{
return "this is a Comment!";
}
if(pStr[i]=="[")
{
return "this is a Session!";
}
if(pStr[i]==0)
{
return "noThing!";
}
while(pStr[i]!=''&&pStr[i]!='='&&pStr!='\t'&&pStr=0)
{
tmpStr[i]=pStr[i];
i++;
j++;
}
TmpStr[j]=0;
if(strcmp(tmpStr,valueName)!=0)
{
return "not Found!";
}
/*获取变量的值*/
while(pStr[i]==''||pStr[i]=='='||pStr[i]=='\t')
{
i++;
}
if(pStr[i]!="")
{
i++;
j=0;
while( pStr[i] !='"' && i< MAX_LINE_LENGTH)
{
RetStr[j] = pStr[i];
i++;
j++;
}
RetStr[j] = 0;
}
else
{
j=0;
while( pStr[i] >' ' && pStr[i] != '#' && pStr[i] != 127)//
{
RetStr[j] = pStr[i];
i++;
j++;
}
RetStr[j] = 0;
}
return *RetStr;
}
short CConfigFileMng::getValue(const char *Session,const char *ValueName,char *RetStr)
{
}
// ConfigFileMng.cpp: implementation of the CConfigFileMng class.
//
//////////////////////////////////////////////////////////////////////
#include "ConfigFileMng.h"
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CConfigFileMng::CConfigFileMng()
{
}
CConfigFileMng::~CConfigFileMng()
{
}
bool CConfigFileMng::Open(const char *strFileName)
{
char szFileName[50]="";
int nFileNameLen=0;
char szFilePox[5]="";
FILE *pFile;
/*1.对传入的指针进行非空判断*/
if(0==strFileName)
{
return false;
}
//
strcpy(szFileName,strFileName);
if(strcmp(szFileName,"")==0)
{
return false;
}
nFileNameLen=strlen(szFileName);
if(0!=nFileNameLen)
{
for(int i=0;i<4,i++)
{
szFilePox[i]=szFileName[nFileLen-1-i];
}
szFilePox[i]='\0';
if(0!=strcmp(strFilePox,"ini."))
{
strcat(szFileName,".ini");
}
if(strlen(szFileName)>MAX_FILENAME_LEN)
{
return false;
}
pFile=fopen(szFileName,"r");
if(pFile==0)
{
return false;
}
m_bOpenFlag=true;
return true;
}
m_bOpenFlag=false;
return false;
}
bool CConfigFileMng::isOpen()
{
return m_bOpenFlag;
}
char* CConfigFileMng::getSession(const char *pStr,const char *strSesName)
{
char szTmpSes[1024]="";
int nPosBeg=0;
int nPosEnd=0;
if(0!=strstr(strSesName,"[")||0!=strstr(strSesName,"]"))
{
return false;
}
else
{
nPosBeg=strstr(strSesName,"[");
nPosEnd=strstr(strSes,"]");
}
for(int i=nPosBeg+1,i<nPosEnd,i++)
{
szTmpSes[i-nPosBeg-1]=strSesName[i];
}
if(0==strcmp(szTmpSes,szFileName))
{
return true;
}
else
{
return false;
}
}
char* CConfigFileMng::getContent(const char *pStr,const char *valueName,char *retStr)
{
char tmpStr[100];
int i=0;
int j=0;
retStr[0]=0;
while(pStr[i]==''||pStr[i]=='\t')
{
i++;
}
if(pStr[i]=="#")
{
return "this is a Comment!";
}
if(pStr[i]=="[")
{
return "this is a Session!";
}
if(pStr[i]==0)
{
return "noThing!";
}
while(pStr[i]!=''&&pStr[i]!='='&&pStr!='\t'&&pStr=0)
{
tmpStr[i]=pStr[i];
i++;
j++;
}
TmpStr[j]=0;
if(strcmp(tmpStr,valueName)!=0)
{
return "not Found!";
}
/*获取变量的值*/
while(pStr[i]==''||pStr[i]=='='||pStr[i]=='\t')
{
i++;
}
if(pStr[i]!="")
{
i++;
j=0;
while( pStr[i] !='"' && i< MAX_LINE_LENGTH)
{
RetStr[j] = pStr[i];
i++;
j++;
}
RetStr[j] = 0;
}
else
{
j=0;
while( pStr[i] >' ' && pStr[i] != '#' && pStr[i] != 127)//
{
RetStr[j] = pStr[i];
i++;
j++;
}
RetStr[j] = 0;
}
return *RetStr;
}
short CConfigFileMng::getValue(const char *Session,const char *ValueName,char *RetStr)
{
}