#ifndef __CINI_FILE__
#define __CINI_FILE__
#include <string>
#include <vector>
using namespace std;
struct CIniEntry
{
CIniEntry(){}
CIniEntry(char* szName,char* szValue):m_strName(szName),m_strValue(szValue){}
string m_strName;
string m_strValue;
};
struct CIniComment
{
CIniComment(){}
CIniComment(char* strCmt):m_strComment(strCmt){}
string m_strComment;
};
struct CIniSection
{
string m_isName;
vector<CIniEntry> m_ieEntry;
vector<CIniComment> m_icComment;
};
class CIniFile
{
public:
int ReadIniFile(char* szIniFile);
int WriteIniFile(char* szIniFile);
int Trim(char* &szString);
int RemoveComment(char* szLine);
string GetKeyValue(string section,string keyname);
void SetKeyValue(string section,string keyname,string keyvalue);
private:
vector<CIniSection> m
_isSection;
};
#endif
#include "IniFile.h"
#include <fstream>
using namespace std;
#define MAX_COLS 260
#define E_OK 0x01L
#define E_OPEN_FILE_FAILED 0x00L
int CIniFile::ReadIniFile(char* szIniFile)
{
ifstream fIniFile(szIniFile);
if(fIniFile == NULL)
{
return E_OPEN_FILE_FAILED;
}
char szLine[MAX_COLS] = {0};
while (fIniFile.getline(szLine,MAX_COLS))
{
char* p = szLine;
if( *p == '[')
{
RemoveComment( p );
char* pEnd = strchr( p ,']');
if( pEnd == NULL)
continue;
*pEnd = 0;
CIniSection is;
is.m_isName = string( p + 1 );
m_isSection.push_back(is);
continue;
}
Trim( p );
if( *p == ';')
{
m_isSection[m_isSection.size() - 1].m_icComment.push_back(p + 1);
continue;
}
char* pTemp = strchr( p,'=');
if(pTemp == NULL)
{
continue;
}
*pTemp = 0;
CIniEntry ie;
ie.m_strName = p ;
ie.m_strValue = pTemp + 1;
m_isSection[m_isSection.size() - 1 ].m_ieEntry.push_back(ie);
memset(szLine,0,MAX_COLS);
}
fIniFile.close();
return E_OK;
}
int CIniFile::WriteIniFile(char* szIniFile)
{
ofstream fIniFile(szIniFile);
if(fIniFile == NULL)
return E_OPEN_FILE_FAILED;
for (size_t i = 0; i < m_isSection.size();++i)
{
fIniFile.write("[",1);
fIniFile.write(m_isSection[i].m_isName.c_str(),m_isSection[i].m_isName.length());
fIniFile.write("]",1);
fIniFile << endl;
for (size_t j = 0; j < m_isSection[i].m_ieEntry.size();++ j)
{
fIniFile.write(m_isSection[i].m_ieEntry[j].m_strName.c_str(),m_isSection[i].m_ieEntry[j].m_strName.length());
fIniFile.write("=",1);
fIniFile.write(m_isSection[i].m_ieEntry[j].m_strValue.c_str(),m_isSection[i].m_ieEntry[j].m_strValue.length());
fIniFile << endl;
}
}
fIniFile.close();
return E_OK;
}
int CIniFile::Trim(char* &szString)
{
char* p = szString;
while (*p == ' ' || *p == '/t')
{
p ++;
}
szString = p;
p = szString + strlen(szString) - 1;
while ( *p == ' ' || *p == '/t' || *p == '/r' || *p == '/n')
{
-- p;
}
*( p + 1 ) = 0;
return E_OK;
}
int CIniFile::RemoveComment(char* szLine)
{
char* p = strchr(szLine,';');
if( p == NULL)
return 0;
*p = 0;
return 0;
}
string CIniFile::GetKeyValue(string section,string keyname)
{
for (size_t i = 0; i < m_isSection.size();++i)
{
if(!section.compare(m_isSection[i].m_isName))
{
for (size_t j = 0; j < m_isSection[i].m_ieEntry.size();++ j)
{
if(!m_isSection[i].m_ieEntry[j].m_strName.compare(keyname))
return m_isSection[i].m_ieEntry[j].m_strValue;
}
}
}
return "";
}
void CIniFile::SetKeyValue(string section,string keyname,string keyvalue)
{
for (size_t i = 0; i < m_isSection.size();++i)
{
if(!section.compare(m_isSection[i].m_isName))
{
for (size_t j = 0; j < m_isSection[i].m_ieEntry.size();++ j)
{
if(!m_isSection[i].m_ieEntry[j].m_strName.compare(keyname))
{
m_isSection[i].m_ieEntry[j].m_strValue=keyvalue;
return ;
}
}
CIniEntry ie;
ie.m_strName=keyname;
ie.m_strValue=keyvalue;
m_isSection[i].m_ieEntry.push_back(ie);
return ;
}
}
CIniSection is;
is.m_isName = section;
CIniEntry ie;
ie.m_strName=keyname;
ie.m_strValue=keyvalue;
is.m_ieEntry.push_back(ie);
m_isSection.push_back(is);
}