1、适用于C++项目,适用于Windows和Linux平台,依赖STL标准库。 2、用于C++读取INI文件,写入INI文件,以及查找相应键值对的快速操作。 3、在项目中需要此功能时,只需要引入两个文件,即:INIParser.h和INIParser.cpp。
INIParser.h
#ifndef INI_PARSER_H
#define INI_PARSER_H
#include
#include
#include
#include
#include
#include
using namespace std;
//其他部分省略
//主要的INIParser类
class INIParser
{
public:
int ReadINI(string path);
string GetValue(string root, string key);
vector::size_type GetSize(){return map_ini.size();}
vector::size_type SetValue(string root, string key, string value);
int WriteINI(string path);
void Clear(){map_ini.clear();}
private:
map map_ini;
};
#endif // INI_PARSER_HINIParser.cpp
#include "iniparser.h"
//remove all blank space
string &TrimString(string &str)
{
string::size_type pos = 0;
while(str.npos != (pos = str.find(" ")))
str = str.replace(pos, pos+1, "");
return str;
}
//read in INI file and parse it
int INIParser::ReadINI(string path)
{
ifstream in_conf_file(path.c_str());
if(!in_conf_file) return 0;
string str_line = "";
string str_root = "";
vector vec_ini;
while(getline(in_conf_file, str_line))
{
string::size_type left_pos = 0;
string::size_type right_pos = 0;
string::size_type equal_div_pos = 0;
string str_key = "";
string str_value = "";
if((str_line.npos != (left_pos = str_line.find("["))) && (str_line.npos != (right_pos = str_line.find("]"))))
{
//cout |