用STL快速编写ini配置文件识别类
2007-07-09 22:25
435 查看
ini文件是技术人员经常用到的一种系统配置方法,如何读取和快速识别ini文件中的内容实现起来比较繁琐。STL强大的功能在于能快速的实现排序、查找、 识别等功能。本文通过STL中的map,string,vector,ifstream等,来快速实现ini文件的识别类class IniFile。IniFile可以实现常见查找功能,并提供完整的源码。
设计需求:
ini文件的格式一般如下:
[section1]
key1=value1
key2=value2
......
[section2]
key1=value1
key2=value2 #注释
......
实际的例子是:
#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
其中有五种元素:section 名,Key名,value值,注释 #或者//开头,标志字符"[" "]" "="。查找项的对应关系为sectiong-key和value对应。需要得到是value。class IniFile要实现的是两个函数:读入ini文件,读取sect-key对应的value值。即实现下面的接口:
class IniFile{
public:
IniFile();
//打开ini文件
bool open(const char* pinipath);
//读取value值
const char* read(const char* psect, const char*pkey);
};
设计实现:
用ifstream按行读入ini文件的内容
识别每一行的字符串,分析出sectiong,key,value,和注释。
用map<string, string, less<string> >来记录所有的sectiong-key和value。
重新定义class IniFile
typedef map<string, string, less<string> > strMap;
typedef strMap::iterator strMapIt;
const char*const MIDDLESTRING = "_____***_______";
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<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();
}
strMap c_inimap;
};
其中do_open是用来真正实现初始化ini内容的函数。先用ifstream fin打开一个文件,然后用is_open判断文件是否正常打开。顺序读取文件的时候用eof()判断是否到文件尾。getline是一个字符处理函数:直接从fin中读取一行。然后用while循环过滤一行末尾的空格等字符。最后保存到一个vector中,完成读入文本工作。其中比较值得关注的是以下为体,你知道为什么这么做么?
用ifstream和getline来读入而不是用fopen和fread。
用is_open判断是否打开,而不是直接读取。
用vector的push_pack而不是insert。
用empty判断是否为空,而不是用size()==0。
下一步用for_each函数来完成字符串的内容提取工作。声明一个结构,实现对操作符()的重载。代码如下:
truct 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 )) != 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 value = strtmp2.substr(first, last-first+1);
string mapkey = strsect + MIDDLESTRING;
mapkey += strkey;
(*pmap)[mapkey]=value;
return ;
}
};
这里大量使用了字符串的查找和字串功能。string的find_last_of系列和find系列,功能确实十分强大。所有在string中没有找到都会返回一个变量string::npos。
函数先找sectiong,然后分离key值和value值。符合要求的,把section和key值通过中间加上MIDDLESTRING组成一个新的string,插入map中。这里值得注意的是:
for_each的使用,结构可以传递参数。
string的查找函数及返回值
string的链接和合并函数。
map的下标操作符的使用。
具体使用
把所有代码放在一个头文件中,以后别人使用的时候,只需要包含头文件就可以了,点击查看inifile.h文件。在使用的过程中,注意判断返回值。使用代码如下:
#include <iostream>
#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="<<strvalue<<endl;
return 0;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
设计需求:
ini文件的格式一般如下:
[section1]
key1=value1
key2=value2
......
[section2]
key1=value1
key2=value2 #注释
......
实际的例子是:
#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
其中有五种元素:section 名,Key名,value值,注释 #或者//开头,标志字符"[" "]" "="。查找项的对应关系为sectiong-key和value对应。需要得到是value。class IniFile要实现的是两个函数:读入ini文件,读取sect-key对应的value值。即实现下面的接口:
class IniFile{
public:
IniFile();
//打开ini文件
bool open(const char* pinipath);
//读取value值
const char* read(const char* psect, const char*pkey);
};
设计实现:
用ifstream按行读入ini文件的内容
识别每一行的字符串,分析出sectiong,key,value,和注释。
用map<string, string, less<string> >来记录所有的sectiong-key和value。
重新定义class IniFile
typedef map<string, string, less<string> > strMap;
typedef strMap::iterator strMapIt;
const char*const MIDDLESTRING = "_____***_______";
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<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();
}
strMap c_inimap;
};
其中do_open是用来真正实现初始化ini内容的函数。先用ifstream fin打开一个文件,然后用is_open判断文件是否正常打开。顺序读取文件的时候用eof()判断是否到文件尾。getline是一个字符处理函数:直接从fin中读取一行。然后用while循环过滤一行末尾的空格等字符。最后保存到一个vector中,完成读入文本工作。其中比较值得关注的是以下为体,你知道为什么这么做么?
用ifstream和getline来读入而不是用fopen和fread。
用is_open判断是否打开,而不是直接读取。
用vector的push_pack而不是insert。
用empty判断是否为空,而不是用size()==0。
下一步用for_each函数来完成字符串的内容提取工作。声明一个结构,实现对操作符()的重载。代码如下:
truct 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 )) != 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 value = strtmp2.substr(first, last-first+1);
string mapkey = strsect + MIDDLESTRING;
mapkey += strkey;
(*pmap)[mapkey]=value;
return ;
}
};
这里大量使用了字符串的查找和字串功能。string的find_last_of系列和find系列,功能确实十分强大。所有在string中没有找到都会返回一个变量string::npos。
函数先找sectiong,然后分离key值和value值。符合要求的,把section和key值通过中间加上MIDDLESTRING组成一个新的string,插入map中。这里值得注意的是:
for_each的使用,结构可以传递参数。
string的查找函数及返回值
string的链接和合并函数。
map的下标操作符的使用。
具体使用
把所有代码放在一个头文件中,以后别人使用的时候,只需要包含头文件就可以了,点击查看inifile.h文件。在使用的过程中,注意判断返回值。使用代码如下:
#include <iostream>
#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="<<strvalue<<endl;
return 0;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#ifndef _INIFILE_H__ #define _INIFILE_H__ #include <map> #include <string> #include <vector> #include <algorithm> #include <functional> #include <fstream> 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) { 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<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(); } strMap c_inimap; }; #endif
相关文章推荐
- 用STL快速编写ini配置文件识别类
- 用STL快速编写ini配置文件识别类
- 用STL快速编写ini配置文件识别类
- 用STL快速编写ini配置文件识别类
- 用STL快速编写ini配置文件识别类
- 用STL快速编写ini配置文件识别类
- 用STL快速编写ini配置文件识别类
- 无言以对:chinablog 用STL快速编写ini配置文件识别类
- 用STL快速编写ini配置文件识别类--ZT
- 用STL快速编写ini配置文件识别类
- STL编写ini配置文件识别类
- stl读取ini配置文件例子
- C#编程如何编写的自动读写配置文件.ini格式
- stl读取ini配置文件例子
- C++ stl 读写ini配置文件
- INI配置文件在linux下的读写
- MFC之静态调用DLL实现编辑ini配置文件
- [转载]windows7中没用boot.ini文件而是改用BCDEDIT来配置了
- 识别和匹配idc配置文件
- Windows下MySQL5.6查找my.ini配置文件