linux下的ini文件操作类
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
Linux下读取INI文件
#include "CINI.h"
CIniFile::CIniFile()
{
char szFileName[MAX_PATH] = {0};
GetModuleFileName(szFileName,sizeof(szFileName));
strcpy(strrchr(szFileName, '/'), "/Config.ini");
SetFileName(szFileName);
cout<<"Ini File Path:"<<szFileName<<endl;
ParseIni();
}
CIniFile::~CIniFile(void)
{
mapSection.clear();
}
boolCIniFile::ParseIni()
{
FILE* pFile;
string str;
string strKey;
string strValue;
if(NULL == (pFile = fopen(m_szPath, "r")))
{
perror("Open File err");
return false;
}
while(0 == ReadLine(pFile,str))
{
if(isSection(str))
{
strSection = str;
//cout<<"+++"<<strSection<<"+++"<<endl;
}
else if(isKey(str,strKey,strValue))
{
cout<<"___"<<strSection<<"___"<<strKey<<"___"<<strValue<<"___"<<endl;
mapSection[strSection][strKey] = strValue;
}
}
fclose(pFile);
}
intCIniFile::ReadLine(FILE* pFile, string&strLine)
{
char szBuf[BUF_LEN] = {0};
if(fgets(szBuf, sizeof(szBuf), pFile) == NULL)
{
return -1;
}
if(strstr(szBuf,"\n"))
szBuf[strlen(szBuf)-2] = '\0'; // \n\r
strLine = szBuf;
return 0;
}
boolCIniFile::isSection(string&str)
{
size_tnPos, nEndPos;
if(str.length() == 0)
return false;
if (string::npos != (nPos = str.find_first_of("["))
&&string::npos != (nEndPos = str.find_first_of("]"))
&&nEndPos> nPos+1)
{
str = str.substr(nPos + 1, nEndPos - nPos - 1);
TrimString(str);
if(str.length() == 0)
return false;
//cout<<endl<<str<<endl;
}
return false;
}
boolCIniFile::isKey(string str,string&strKey,string&strValue) {
size_tnPos, nEndPos;
if(str.length() == 0)
return false;
if (string::npos != (nPos = str.find_first_of("=")))
{
strKey = str.substr(0,nPos);
strValue = str.substr(nPos + 1);
TrimString(strKey);
TrimString(strValue);
if(strKey.length() == 0 || strValue.length() == 0) return false;
//cout<<strKey<<"="<<strValue<<endl;
return true;
}
return false;
}
intCIniFile::TrimString(string&strToken)
{
if (strToken.empty())
{
return -1;
}
size_tnPos = strToken.find_first_not_of(" \t");
if(nPos == string::npos) //not found ,all are blank
{
strToken.clear();