天天看点

用STL快速编写ini配置文件识别类

<a href="http://www.stlchina.org/twiki/bin/view.pl/Main/STLforIni#STL_ini">用STL快速编写ini配置文件识别类</a>

<a href="http://www.stlchina.org/twiki/bin/view.pl/Main/STLforIni#1">1 设计需求:</a>

<a href="http://www.stlchina.org/twiki/bin/view.pl/Main/STLforIni#2">2 设计实现:</a>

<a href="http://www.stlchina.org/twiki/bin/view.pl/Main/STLforIni#3">3 具体使用</a>

ini文件的格式一般如下:

用STL快速编写ini配置文件识别类

#ini for path

用STL快速编写ini配置文件识别类

[path]

用STL快速编写ini配置文件识别类

dictfile = /home/tmp/dict.dat

用STL快速编写ini配置文件识别类

inputfile= /home/tmp/input.txt

用STL快速编写ini配置文件识别类

outputfile= /home/tmp/output.txt

用STL快速编写ini配置文件识别类
用STL快速编写ini配置文件识别类

#ini for exe

用STL快速编写ini配置文件识别类

[exe]

用STL快速编写ini配置文件识别类

user= winter       //user name

用STL快速编写ini配置文件识别类

passwd= 1234567    #pass word

用STL快速编写ini配置文件识别类

database= mydatabase

用STL快速编写ini配置文件识别类
用STL快速编写ini配置文件识别类

class IniFile{

用STL快速编写ini配置文件识别类

public:

用STL快速编写ini配置文件识别类

    IniFile();

用STL快速编写ini配置文件识别类

    //打开ini文件

用STL快速编写ini配置文件识别类

    bool open(const char* pinipath);

用STL快速编写ini配置文件识别类

    //读取value值

用STL快速编写ini配置文件识别类

    const char* read(const char* psect, const char*pkey);

用STL快速编写ini配置文件识别类

  };

用STL快速编写ini配置文件识别类

用ifstream按行读入ini文件的内容

识别每一行的字符串,分析出sectiong,key,value,和注释。

用map &gt;来记录所有的sectiong-key和value。,&gt;

用STL快速编写ini配置文件识别类

typedef map&lt;string, string, less&lt;string&gt; &gt; strMap;

用STL快速编写ini配置文件识别类

typedef strMap::iterator strMapIt;

用STL快速编写ini配置文件识别类
用STL快速编写ini配置文件识别类

const char*const MIDDLESTRING = "_____***_______";

用STL快速编写ini配置文件识别类

class IniFile

用STL快速编写ini配置文件识别类

{

用STL快速编写ini配置文件识别类
用STL快速编写ini配置文件识别类

    IniFile( ){};

用STL快速编写ini配置文件识别类

    ~IniFile( ){};

用STL快速编写ini配置文件识别类

    bool open(const char* pinipath)

用STL快速编写ini配置文件识别类

    {

用STL快速编写ini配置文件识别类

        return do_open(pinipath);

用STL快速编写ini配置文件识别类

    }

用STL快速编写ini配置文件识别类

    string read(const char*psect, const char*pkey)

用STL快速编写ini配置文件识别类
用STL快速编写ini配置文件识别类

        string mapkey = psect;

用STL快速编写ini配置文件识别类

        mapkey += MIDDLESTRING;

用STL快速编写ini配置文件识别类

        mapkey += pkey;

用STL快速编写ini配置文件识别类

        strMapIt it = c_inimap.find(mapkey);

用STL快速编写ini配置文件识别类

        if(it == c_inimap.end())

用STL快速编写ini配置文件识别类

            return "";

用STL快速编写ini配置文件识别类

        else

用STL快速编写ini配置文件识别类

            return it-&gt;second;

用STL快速编写ini配置文件识别类
用STL快速编写ini配置文件识别类

protected:

用STL快速编写ini配置文件识别类

    bool do_open(const char* pinipath)

用STL快速编写ini配置文件识别类
用STL快速编写ini配置文件识别类

        ifstream fin(pinipath);

用STL快速编写ini配置文件识别类

        if(!fin.is_open())

用STL快速编写ini配置文件识别类

            return false;

用STL快速编写ini配置文件识别类

        vector&lt;string&gt; strvect;

用STL快速编写ini配置文件识别类

        while(!fin.eof())

用STL快速编写ini配置文件识别类

        {

用STL快速编写ini配置文件识别类

            string inbuf;

用STL快速编写ini配置文件识别类

            getline(fin, inbuf,'\n');

用STL快速编写ini配置文件识别类

            strvect.push_back(inbuf);

用STL快速编写ini配置文件识别类

        }

用STL快速编写ini配置文件识别类

        if(strvect.empty())

用STL快速编写ini配置文件识别类
用STL快速编写ini配置文件识别类

        for_each(strvect.begin(), strvect.end(), analyzeini(c_inimap));

用STL快速编写ini配置文件识别类

        return !c_inimap.empty();

用STL快速编写ini配置文件识别类
用STL快速编写ini配置文件识别类

    strMap c_inimap;

用STL快速编写ini配置文件识别类

};

用STL快速编写ini配置文件识别类

其中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函数来完成字符串的内容提取工作。声明一个结构,实现对操作符()的重载。代码如下:

用STL快速编写ini配置文件识别类

truct analyzeini{

用STL快速编写ini配置文件识别类

    string strsect;

用STL快速编写ini配置文件识别类

    strMap *pmap;

用STL快速编写ini配置文件识别类

    analyzeini(strMap &amp; strmap):pmap(&amp;strmap){}

用STL快速编写ini配置文件识别类

    void operator()( const string &amp; strini)

用STL快速编写ini配置文件识别类
用STL快速编写ini配置文件识别类

        int first =strini.find('[');

用STL快速编写ini配置文件识别类

        int last = strini.rfind(']');

用STL快速编写ini配置文件识别类

        if( first != string::npos &amp;&amp; last != string::npos &amp;&amp; first != last+1)

用STL快速编写ini配置文件识别类
用STL快速编写ini配置文件识别类

            strsect = strini.substr(first+1,last-first-1);

用STL快速编写ini配置文件识别类

            return ;

用STL快速编写ini配置文件识别类
用STL快速编写ini配置文件识别类

        if(strsect.empty())

用STL快速编写ini配置文件识别类
用STL快速编写ini配置文件识别类

        if((first=strini.find('='))== string::npos)

用STL快速编写ini配置文件识别类
用STL快速编写ini配置文件识别类

        string strtmp1= strini.substr(0,first);

用STL快速编写ini配置文件识别类

        string strtmp2=strini.substr(first+1, string::npos);

用STL快速编写ini配置文件识别类

        first= strtmp1.find_first_not_of(" \t");

用STL快速编写ini配置文件识别类

        last = strtmp1.find_last_not_of(" \t");

用STL快速编写ini配置文件识别类

        if(first == string::npos || last == string::npos)

用STL快速编写ini配置文件识别类
用STL快速编写ini配置文件识别类

        string strkey = strtmp1.substr(first, last-first+1);

用STL快速编写ini配置文件识别类

        first = strtmp2.find_first_not_of(" \t");

用STL快速编写ini配置文件识别类

        if(((last = strtmp2.find("\t#", first )) != string::npos) ||

用STL快速编写ini配置文件识别类

            ((last = strtmp2.find(" #", first )) != string::npos) ||

用STL快速编写ini配置文件识别类

            ((last = strtmp2.find("\t//", first )) != string::npos)||

用STL快速编写ini配置文件识别类

            ((last = strtmp2.find(" //", first )) != string::npos))

用STL快速编写ini配置文件识别类
用STL快速编写ini配置文件识别类

            strtmp2 = strtmp2.substr(0, last-first);

用STL快速编写ini配置文件识别类
用STL快速编写ini配置文件识别类

        last = strtmp2.find_last_not_of(" \t");

用STL快速编写ini配置文件识别类
用STL快速编写ini配置文件识别类
用STL快速编写ini配置文件识别类

        string value = strtmp2.substr(first, last-first+1);

用STL快速编写ini配置文件识别类

        string mapkey = strsect + MIDDLESTRING;

用STL快速编写ini配置文件识别类

        mapkey += strkey;

用STL快速编写ini配置文件识别类

        (*pmap)[mapkey]=value;

用STL快速编写ini配置文件识别类

        return ;

用STL快速编写ini配置文件识别类
用STL快速编写ini配置文件识别类
用STL快速编写ini配置文件识别类

这里大量使用了字符串的查找和字串功能。string的find_last_of系列和find系列,功能确实十分强大。所有在string中没有找到都会返回一个变量string::npos。

函数先找sectiong,然后分离key值和value值。符合要求的,把section和key值通过中间加上MIDDLESTRING组成一个新的string,插入map中。这里值得注意的是:

* for_each的使用,结构可以传递参数。 * string的查找函数及返回值 * string的链接和合并函数。 * map的下标操作符的使用。

把所有代码放在一个头文件中,以后别人使用的时候,只需要包含头文件就可以了,点击查看inifile.h文件。在使用的过程中,注意判断返回值。使用代码如下:

用STL快速编写ini配置文件识别类

#include &lt;iostream&gt;

用STL快速编写ini配置文件识别类

#include "inifile.h"

用STL快速编写ini配置文件识别类

using namespace std;

用STL快速编写ini配置文件识别类

int main()

用STL快速编写ini配置文件识别类
用STL快速编写ini配置文件识别类

    IniFile ini;

用STL快速编写ini配置文件识别类

    if(!ini.open("test.ini"))

用STL快速编写ini配置文件识别类

       return -1;

用STL快速编写ini配置文件识别类

    string strvalue = ini.read("sect1","key1");

用STL快速编写ini配置文件识别类

    if(strvalue.empty())

用STL快速编写ini配置文件识别类

        return -1;

用STL快速编写ini配置文件识别类

    else

用STL快速编写ini配置文件识别类

        cout&lt;&lt;"value="&lt;&lt;strvalue&lt;&lt;endl;

用STL快速编写ini配置文件识别类

    return 0;

用STL快速编写ini配置文件识别类

}    

Set MYTITLE = 用STL快速编写ini配置文件识别类

继续阅读