stl读取ini配置文件例子
2008-02-25 13:04
561 查看
导读:
#ifndef _INIFILE_H__
#define _INIFILE_H__
#include
#include
#include
#include
#include
#include
using namespace std;
typedef mapstrMap;
typedef strMap::iterator strMapIt;
const char*const MIDDLESTRING = "_____***_______";
struct analyzeini{
string strsect;
strMap *pmap;
analyzeini(strMap &strmap):pmap(&strmap){}
void operator()( const string &strini)
{
int first =strini.find('[');
int last = strini.rfind(']');
if( first != string::npos &&last != string::npos &&first != last+1)
{
strsect = strini.substr(first+1,last-first-1);
return ;
}
if(strsect.empty())
return ;
if((first=strini.find('='))== string::npos)
return ;
string strtmp1= strini.substr(0,first);
string strtmp2=strini.substr(first+1, string::npos);
first= strtmp1.find_first_not_of(" /t");
last = strtmp1.find_last_not_of(" /t");
if(first == string::npos || last == string::npos)
return ;
string strkey = strtmp1.substr(first, last-first+1);
first = strtmp2.find_first_not_of(" /t");
if(((last = strtmp2.find("/t#", first )) != -1) ||
((last = strtmp2.find(" #", first )) != -1) ||
((last = strtmp2.find("/t//", first )) != -1)||
((last = strtmp2.find(" //", first )) != -1))
{
strtmp2 = strtmp2.substr(0, last-first);
}
last = strtmp2.find_last_not_of(" /t");
if(first == string::npos || last == string::npos)
return ;
string value = strtmp2.substr(first, last-first+1);
string mapkey = strsect + MIDDLESTRING;
mapkey += strkey;
(*pmap)[mapkey]=value;
return ;
}
};
class IniFile
{
public:
IniFile( ){};
~IniFile( ){};
bool open(const char* pinipath)
{
return do_open(pinipath);
}
string read(const char*psect, const char*pkey)
{
string mapkey = psect;
mapkey += MIDDLESTRING;
mapkey += pkey;
strMapIt it = c_inimap.find(mapkey);
if(it == c_inimap.end())
return "";
else
return it->second;
}
protected:
bool do_open(const char* pinipath)
{
ifstream fin(pinipath);
if(!fin.is_open())
return false;
vector strvect;
while(!fin.eof())
{
string inbuf;
getline(fin, inbuf,'/n');
strvect.push_back(inbuf);
}
if(strvect.empty())
return false;
for_each(strvect.begin(), strvect.end(), analyzeini(c_inimap));
return !c_inimap.empty();
}
strMap c_inimap;
};
#endif
---------------------------------------------------------------------------------------------------
#include
#include "inifile.h"
using namespace std;
int main()
{
IniFile ini;
if(!ini.open("test.ini"))
return -1;
string strvalue = ini.read("sect1","key1");
if(strvalue.empty())
return -1;
else
cout<<"value="<
#ifndef _INIFILE_H__
#define _INIFILE_H__
#include
#include
#include
#include
#include
#include
using namespace std;
typedef mapstrMap;
typedef strMap::iterator strMapIt;
const char*const MIDDLESTRING = "_____***_______";
struct analyzeini{
string strsect;
strMap *pmap;
analyzeini(strMap &strmap):pmap(&strmap){}
void operator()( const string &strini)
{
int first =strini.find('[');
int last = strini.rfind(']');
if( first != string::npos &&last != string::npos &&first != last+1)
{
strsect = strini.substr(first+1,last-first-1);
return ;
}
if(strsect.empty())
return ;
if((first=strini.find('='))== string::npos)
return ;
string strtmp1= strini.substr(0,first);
string strtmp2=strini.substr(first+1, string::npos);
first= strtmp1.find_first_not_of(" /t");
last = strtmp1.find_last_not_of(" /t");
if(first == string::npos || last == string::npos)
return ;
string strkey = strtmp1.substr(first, last-first+1);
first = strtmp2.find_first_not_of(" /t");
if(((last = strtmp2.find("/t#", first )) != -1) ||
((last = strtmp2.find(" #", first )) != -1) ||
((last = strtmp2.find("/t//", first )) != -1)||
((last = strtmp2.find(" //", first )) != -1))
{
strtmp2 = strtmp2.substr(0, last-first);
}
last = strtmp2.find_last_not_of(" /t");
if(first == string::npos || last == string::npos)
return ;
string value = strtmp2.substr(first, last-first+1);
string mapkey = strsect + MIDDLESTRING;
mapkey += strkey;
(*pmap)[mapkey]=value;
return ;
}
};
class IniFile
{
public:
IniFile( ){};
~IniFile( ){};
bool open(const char* pinipath)
{
return do_open(pinipath);
}
string read(const char*psect, const char*pkey)
{
string mapkey = psect;
mapkey += MIDDLESTRING;
mapkey += pkey;
strMapIt it = c_inimap.find(mapkey);
if(it == c_inimap.end())
return "";
else
return it->second;
}
protected:
bool do_open(const char* pinipath)
{
ifstream fin(pinipath);
if(!fin.is_open())
return false;
vector strvect;
while(!fin.eof())
{
string inbuf;
getline(fin, inbuf,'/n');
strvect.push_back(inbuf);
}
if(strvect.empty())
return false;
for_each(strvect.begin(), strvect.end(), analyzeini(c_inimap));
return !c_inimap.empty();
}
strMap c_inimap;
};
#endif
---------------------------------------------------------------------------------------------------
#include
#include "inifile.h"
using namespace std;
int main()
{
IniFile ini;
if(!ini.open("test.ini"))
return -1;
string strvalue = ini.read("sect1","key1");
if(strvalue.empty())
return -1;
else
cout<<"value="<
相关文章推荐
- stl读取ini配置文件例子
- Java读取classpath中配置文件的小例子
- BAT 读取ini配置文件方法
- 用GetPrivateProfileString读取配置文件(.ini)
- 读取ini配置文件
- 用STL快速编写ini配置文件识别类
- PHP读取4种配置文件(php,ini,yaml,xml)
- 用vb.net读取INI配置文件的方法,使用API
- 利用GetPrivateProfileString读取配置文件(.ini)
- java读取和修改ini配置文件
- 保存和读取ini配置文件
- MFC中如何读取.ini配置文件
- 利用GetPrivateProfileString读取配置文件(.ini)
- Shell脚本读取ini配置文件的实现代码2例
- 读取配置文件的例子
- java读取和修改ini配置文件实例代码
- 利用GetPrivateProfileString读取配置文件(.ini)
- MySQL配置文件my.cnf(my.ini) 例子最详细翻译
- ini 配置文件读取程序(C语言)
- Android 读取Properties配置文件的小例子