用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

原创粉丝点击