用STL快速编写ini配置文件识别类
来源:互联网 发布:女友养成游戏 知乎 编辑:程序博客网 时间:2024/05/16 03:17
IniFileAnalyse.h文件:
#include <vector>
#include <string>
#include <map>
#include <fstream>
#include <algorithm>
#include <iostream>
using namespace std;
typedef map<string, string, less<string> > strMap;
typedef strMap::iterator strMapIt;
const char * const MIDDLESTRING = "____***____";
struct analyzeini
{
string strsect;
strMap *pmap;
analyzeini(strMap & strmap):pmap(&strmap){};
void operator() (const string & strini)
{
// 处理section
size_t first = strini.find('[');
size_t last = strini.rfind(']');
if ((first != string::npos) &&
(last != string::npos) &&
(last != first + 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;
}
// 得到key
string strkey = strtmp1.substr(first, last - first + 1);
first = strtmp2.find_first_not_of(" /t");
// 检查是否存在注释字符
if ( ((last = strtmp2.find("/t#", first)) != string::npos) ||
((last = strtmp2.find(" #", first)) != string::npos) ||
((last = strtmp2.find("/t//", first)) != string::npos) ||
((last = strtmp2.find(" //", first)) != string::npos) )
{
strtmp2 = strtmp2.substr(0, last - first); // 去除注释部分
}
last = strtmp2.find_last_not_of(" /t");
if ((first == string::npos) || (last == string::npos))
{
return;
}
string keyvalue = strtmp2.substr(first, last - first + 1);
string mapkey = strsect + MIDDLESTRING;
mapkey += strkey;
(*pmap)[mapkey] = keyvalue;
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;
}
}
void print()
{
if (c_inimap.empty())
{
return;
}
for (strMapIt it = c_inimap.begin(); it != c_inimap.end(); it++)
{
cout << it->second << endl;
}
}
protected:
bool do_open(const char *pinipath)
{
ifstream fin(pinipath);
if (!fin.is_open())
{
return false;
}
vector<string> 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();
}
private:
strMap c_inimap;
};
使用:
#include "stdafx.h"
#include <string>
#include <iostream>
#include "IniFileAnalyse.h"
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
IniFile ini;
if (!ini.open("E://test.ini"))
{
return -1;
}
string strvalue = ini.read("exe", "passwd");
if (strvalue.empty())
{
return -1;
}
else
{
cout << "value = " << strvalue << endl;
}
ini.print();
return 0;
}
test.ini文件内容:
#ini for path
[path]
dictfile = /home/tmp/dict.dat
inputfile= /home/tmp/input.txt
outputfile= /home/tmp/output.txt
#ini for exe
[exe]
user= winter //user name
passwd= 1234567 #pass word
database= mydatabase
- 用STL快速编写ini配置文件识别类
- 用STL快速编写ini配置文件识别类--ZT
- 用STL快速编写ini配置文件识别类
- 用STL快速编写ini配置文件识别类
- 用STL快速编写ini配置文件识别类
- 用STL快速编写ini配置文件识别类
- 用STL快速编写ini配置文件识别类
- 用STL快速编写ini配置文件识别类
- 用STL快速编写ini配置文件识别类
- STL编写ini配置文件识别类
- stl读取ini配置文件例子
- stl读取ini配置文件例子
- C++ stl 读写ini配置文件
- INI配置文件类
- ini配置文件读取类
- 自用ini配置文件类
- 用java读写ini配置文件
- 用GetPrivateProfileString读取配置文件(.ini)
- 使用SQL查询表字段列的备注信息
- 如何快速建立数据字典,我如此设计小型数据库!
- 布线问题--队列式分支界限法
- XXX管理平台系统——概要
- 开博通知
- 用STL快速编写ini配置文件识别类
- XXX管理平台系统——架构
- Java中如何正确使用字体编码(推荐)转
- .net2.0 半角全角Bug解决
- 关于j2me的环境配置-----------Eclipse
- IE和firefox在JS用法方面的区别
- 五种开源协议的比较(BSD,Apache,GPL,LGPL,MIT)
- java算法--字符组合排序
- vim自动设置