[section1]
key1=XXXX
key2=YYYY
[section2]
key1=XXX#adada#oooo
key2=yyy
这是两个配置段,第一个配置段名为section,里面有两个配置项目即:key1,key2,值分别为xxx和yyyy要求能够读取这样的文件,并提供提取所有section名字列表的接口,提取访问某个section下所有key的列表,提取访问某个section下某个key的值的信息(存为string类型)#为注释部分的开头可以出现先任何有效信息之后,以及单列的一行,处理中应略去。
#ifndef _INI_FILE_
#define _INI_FILE_
#include <string>
#include <map>
#include <iostream>
using namespace std;
#define MAX_LINE_BUF_SIZE 80
#define MAX_SECTION_CONTEXT_BUF_SIZE 40
#define MAX_KEY_SIZE 40
#define MAX_VALUE_SIZE 40
class IniFile
{
typedef std::map< string, string > MapKeyValue;
typedef map< string, MapKeyValue > MapSection;
public:
IniFile();
~IniFile();
void ShowFileContext();
bool Init(char* szFileName);
string GetSection();
string GetKey(const string &strSection);
string GetValueFromSection(const string& strSection,const string& strKey);
private:
void DelInvalidSign(char* szOldLine, char* szNewLine);
bool IsEmptyLine(char* szLine);
bool IsNewSection(char* szLine);
bool IsKeyValueLine(char* szLine);
bool GetNewSectionContext(char* szLine, string& strNewSectionContext);
bool GetKeyValue(char* szLine, string& strKey, string& strValue);
private:
string m_strFileName;
MapSection m_mapSection;
};
#endif
#include"inifile.h"
IniFile::IniFile( )
{
}
IniFile::~IniFile()
{
}
bool IniFile::Init( char* szFileName )
{
if (NULL == szFileName || strlen(szFileName) == 0)
{
return false;
}
m_strFileName = szFileName;
FILE* pFile = fopen( szFileName, "rb" );
if (NULL == pFile)
{
return false;
}
char szReadLineBuf[MAX_LINE_BUF_SIZE];
char szLineBuf[MAX_LINE_BUF_SIZE];
string strCurSection;
string strKey;
string strValue;
while(NULL != fgets(szReadLineBuf, MAX_LINE_BUF_SIZE, pFile))
{
DelInvalidSign(szReadLineBuf, szLineBuf);
if (IsEmptyLine(szLineBuf))
{
continue;
}
else if (IsNewSection(szLineBuf))
{
GetNewSectionContext(szLineBuf, strCurSection);
}
else if (IsKeyValueLine(szLineBuf))
{
GetKeyValue(szLineBuf, strKey, strValue);
m_mapSection[strCurSection][strKey] = strValue;
}
else
{
continue;
}
}
return true;
}
bool IniFile::IsEmptyLine( char* szLine )
{
int nLineSize = strlen(szLine);
if (nLineSize == 0)
{
return true;
}
else
{
return false;
}
}
bool IniFile::IsNewSection( char* szLine )
{
return (strchr(szLine, '[') && strchr(szLine, ']'));
}
bool IniFile::IsKeyValueLine( char* szLine )
{
return (NULL != strchr(szLine, '='));
}
bool IniFile::GetNewSectionContext( char* szLine, string& strNewSectionContext )
{
char szSectionContextBuf[MAX_SECTION_CONTEXT_BUF_SIZE] = {0};
strNewSectionContext.clear();
char* pSectionContextBegin = strchr(szLine, '[');
char* pSectionContextEnd = strchr(szLine, ']');
int nSectionContextLen = pSectionContextEnd - pSectionContextBegin - 1;
memcpy_s(szSectionContextBuf, MAX_SECTION_CONTEXT_BUF_SIZE, pSectionContextBegin + 1, nSectionContextLen);
strNewSectionContext = szSectionContextBuf;
return true;
}
bool IniFile::GetKeyValue( char* szLine, string& strKey, string& strValue )
{
strKey.clear();
strValue.clear();
char* pEqualPos = strchr(szLine, '=');
char szKeyBuf[MAX_KEY_SIZE] = {0};
char szValueBuf[MAX_VALUE_SIZE] = {0};
int nKeyLen = pEqualPos - szLine;
int nValueLen = strlen(szLine) - nKeyLen - 1;
memcpy_s(szKeyBuf, MAX_KEY_SIZE, szLine, nKeyLen);
strKey = szKeyBuf;
memcpy_s(szValueBuf, MAX_VALUE_SIZE, pEqualPos + 1, nValueLen);
strValue = szValueBuf;
return true;
}
//void IniFile::ShowFileContext()
//{
// MapSection::iterator itSection = m_mapSection.begin();
// for (; itSection != m_mapSection.end(); itSection++)
// {
// MapKeyValue& refKeyValueMap = itSection->second;
// cout << "==============================" << endl;
// cout << "Section:" << itSection->first << endl;
//
// MapKeyValue::iterator itKV = refKeyValueMap.begin();
// for (; itKV != refKeyValueMap.end(); itKV++)
// {
// cout << itKV->first << " = " << itKV->second << endl;
// }
// cout << "==============================" << endl;
// cout << endl;
// }
//}
void IniFile::DelInvalidSign( char* szOldLine, char* szNewLine )
{
int iOldLineLen;
if (NULL == szOldLine || (iOldLineLen = strlen(szOldLine)) == 0)
{
return;
}
char tmpChar;
int nNewLineIndex = 0;
for (int i = 0; i < iOldLineLen; i++)
{
tmpChar = szOldLine[i];
if (tmpChar == ' '
|| tmpChar == '\t'
|| tmpChar == '\r'
|| tmpChar == '\n')
{
continue;
}
else
if(tmpChar=='#')
//++count;
tmpChar =szOldLine[++i];
szNewLine[nNewLineIndex++] = tmpChar;
}
szNewLine[nNewLineIndex] = 0;
}
string IniFile::GetSection()
{
MapSection::iterator itSection = m_mapSection.begin();
for (; itSection != m_mapSection.end(); itSection++)
{
cout << "==============================" << endl;
cout << "Section:" << itSection->first << endl;
}
return " ";
}
string IniFile::GetKey(const string& strSection)
{
MapSection::iterator iter =m_mapSection.find(strSection);
if(iter!=m_mapSection.end())
{
MapKeyValue& refKeyValueMap = iter->second;
for(MapKeyValue::iterator it1=refKeyValueMap.begin();
it1!=refKeyValueMap.end();++it1)
{
cout << "==============================" << endl;
cout<<it1->first<<endl;
}
}
return " ";
}
string IniFile::GetValueFromSection( const string& strSection, const string& strKey )
{
MapSection::iterator itSection = m_mapSection.find(strSection);
if (itSection == m_mapSection.end())
{
return " ";
}
MapKeyValue& refKeyValueMap = itSection->second;
MapKeyValue::iterator itKV = refKeyValueMap.find(strKey);
if (itKV != refKeyValueMap.end())
{
return itKV->second;
}
return " ";
}
void IniFile::ShowFileContext()
{
MapSection::iterator itSection = m_mapSection.begin();
for (; itSection != m_mapSection.end(); itSection++)
{
MapKeyValue& refKeyValueMap = itSection->second;
cout << "==============================" << endl;
cout << "Section:" << itSection->first << endl;
MapKeyValue::iterator itKV = refKeyValueMap.begin();
for (; itKV != refKeyValueMap.end(); itKV++)
{
cout << itKV->first << " = " << itKV->second << endl;
}
cout << "==============================" << endl;
cout << endl;
}
}
#include "inifile.h"
#include <iostream>
#include<vector>
using namespace std;
int main(int argc, char* argv[])
{
IniFile file;
if (!file.Init("test2.ini"))
{
return -1;
}
file.ShowFileContext();
cout<<file.GetSection()<<endl;
cout<<file.GetKey("section2")<<endl;
cout << file.GetValueFromSection("section4","key1")<< endl;
while(1);
system("pause");
return 0;
}