IniFileParser.h
/*************************************************************************/
/* FileName:IniFileParser.cpp */
/* Describe:IniFile@read、write */
/* Author :Kagayaku */
/* Date :3.22.2010 */
/************************************************************************/
#ifndef _INIFILEPARSER_H_
#define _INIFILEPARSER_H_
#include <string>
#include <vector>
using namespace std;
struct CIniEntry
{
CIniEntry(){}
CIniEntry(char *szName,char *szValue):m_strIEName(szName),m_strIEValue(szValue){}
string m_strIEName;
string m_strIEValue;
};
struct CIniComment
{
CIniComment(){}
CIniComment(char *szIC):m_strIC(szIC){}
string m_strIC;
};
struct CIniSection
{
vector<CIniEntry> m_vecIE;
vector<CIniComment> m_vecIC;
string m_strISName;
};
class CIniFile
{
public:
CIniFile(const char *szIniFileFullPath);
~CIniFile();
bool ReadIniFile(const char *szinifile);
bool WriteIniFile(const char *szinifile);
void TrimIniFile(char* &szinifile) const;
void RemoveComment(char *szinifile) const;
bool SearchMatchingIniFileSectionGetEntryValue(const char *szinifile,const char *szsectionname,const char *szentryname);
bool SearchMatchingIniFileSectionSetEntryValue(const char *szinifile,const char *szsectionname,const char *szentryname,const char *szentryvalue);
private:
vector<CIniSection> m_vecIS;
string m_strBufIEValue;
string m_strIniFileName;
};
#endif
IniFileParser.cpp
#include "IniFileParser.h"
#include <fstream>
CIniFile::CIniFile(const char *szIniFileFullPath):m_strIniFileName(szIniFileFullPath)
{
ReadIniFile(szIniFileFullPath);
}
CIniFile::~CIniFile()
{
}
bool CIniFile::ReadIniFile(const char *szinifile)
{
if (NULL==szinifile)
{
return false;
}
ifstream inifile(szinifile);
if (NULL==inifile)
{
return false;
}
const int MAX_ROW_LENGTH=200;
char chLineBufArray[MAX_ROW_LENGTH]={0};
while(inifile.getline(chLineBufArray,MAX_ROW_LENGTH))
{
char *p=chLineBufArray;
TrimIniFile(p);
if (*p=='[')
{
RemoveComment(p);
char *pEnd=strchr(p,']');
if (NULL==pEnd || pEnd==p+1)
{
continue;
}
*pEnd = 0;
CIniSection is;
is.m_strISName=string(p+1);
m_vecIS.push_back(is);
continue;
}
if (m_vecIS.size()<1)
{
continue;
}
if (*p==';')
{
if (NULL==*(p+1))
{
continue;
}
else
{
CIniComment ic(p+1);
m_vecIS[m_vecIS.size()-1].m_vecIC.push_back(ic);
continue;
}
}
char *pFlag=strchr(p,'=');
if (NULL==pFlag || pFlag==p || NULL==*(pFlag+1))
{
continue;
}
else
{
*pFlag=0;
CIniEntry ie;
ie.m_strIEName=string(p);
ie.m_strIEValue=string(pFlag+1);
m_vecIS[m_vecIS.size()-1].m_vecIE.push_back(ie);
continue;
}
}
inifile.close();
return true;
}
bool CIniFile::WriteIniFile(const char *szinifile)
{
if (NULL==szinifile || m_strIniFileName!=szinifile)
{
return false;
}
ofstream inifile(szinifile);
if (NULL==inifile)
{
return false;
}
for (int i=0;i!=m_vecIS.size();++i)
{
inifile.write("[",1);
inifile.write(m_vecIS[i].m_strISName.c_str(),m_vecIS[i].m_strISName.size());
inifile.write("]",1);
inifile << endl;
for (int j=0;j!=m_vecIS[i].m_vecIE.size();++j)
{
inifile.write(m_vecIS[i].m_vecIE[j].m_strIEName.c_str(),m_vecIS[i].m_vecIE[j].m_strIEName.size());
inifile.write("=",1);
inifile.write(m_vecIS[i].m_vecIE[j].m_strIEValue.c_str(),m_vecIS[i].m_vecIE[j].m_strIEValue.size());
inifile << endl;
}
}
inifile.close();
return true;
}
void CIniFile::TrimIniFile(char* &szinifile) const
{
if (NULL==szinifile)
{
return;
}
char *p=szinifile;
while(*p==' ' || *p=='\t')
{
++p;
}
szinifile=p;
p=szinifile+strlen(szinifile)-1;
while(*p==' ' || *p=='\t'|| *p=='\r'|| *p=='\n')
{
*p=0;
--p;
}
}
void CIniFile::RemoveComment(char *szinifile) const
{
if (NULL==szinifile)
{
return;
}
char *p=strchr(szinifile,';');
*p = 0;
}
bool CIniFile::SearchMatchingIniFileSectionGetEntryValue(const char *szinifile,const char *szsectionname,const char *szentryname)
{
if (NULL==szinifile || NULL==szsectionname || NULL==szentryname)
{
return false;
}
if (m_strIniFileName!=szinifile)
{
return false;
}
bool temp=false;
for (vector<CIniSection>::iterator iterIS=m_vecIS.begin();iterIS!=m_vecIS.end();++iterIS)
{
if ((*iterIS).m_strISName==szsectionname)
{
for (vector<CIniEntry>::iterator iterIE=(*iterIS).m_vecIE.begin();iterIE!=(*iterIS).m_vecIE.end();++iterIE)
{
if ((*iterIE).m_strIEName==szentryname)
{
m_strBufIEValue=(*iterIE).m_strIEValue;
temp=true;
}
}
}
}
return temp;
}
bool CIniFile::SearchMatchingIniFileSectionSetEntryValue(const char *szinifile,const char *szsectionname,const char *szentryname,const char *szentryvalue)
{
if (NULL==szinifile || NULL==szsectionname || NULL==szentryname|| NULL==szentryvalue)
{
return false;
}
if (m_strIniFileName!=szinifile)
{
return false;
}
bool temp=false;
for (vector<CIniSection>::iterator iterIS=m_vecIS.begin();iterIS!=m_vecIS.end();++iterIS)
{
if ((*iterIS).m_strISName==szsectionname)
{
for (vector<CIniEntry>::iterator iterIE=(*iterIS).m_vecIE.begin();iterIE!=(*iterIS).m_vecIE.end();++iterIE)
{
if ((*iterIE).m_strIEName==szentryname)
{
(*iterIE).m_strIEValue=szentryvalue;
temp=true;
}
}
}
}
return temp?WriteIniFile(szinifile):false;
}