Linux、Window下INI文件的读写操作C++类

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 <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <cstdlib>
#include <map>
using namespace std;
//其他部分省略
//主要的INIParser类
class INIParser
{
public:
    int ReadINI(string path);
    string GetValue(string root, string key);
    vector<ININode>::size_type GetSize(){return map_ini.size();}
    vector<ININode>::size_type SetValue(string root, string key, string value);
    int WriteINI(string path);
    void Clear(){map_ini.clear();}
private:
    map<string, SubNode> map_ini;
};
 
#endif // INI_PARSER_H
INIParser.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<ININode> 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 << str_line.substr(left_pos+1, right_pos-1) << endl;
            str_root = str_line.substr(left_pos+1, right_pos-1);
        }
        if(str_line.npos != (equal_div_pos = str_line.find("=")))
        {
           str_key = str_line.substr(0, equal_div_pos);
           str_value = str_line.substr(equal_div_pos+1, str_line.size()-1);
           str_key = TrimString(str_key);
           str_value = TrimString(str_value);
           //cout << str_key << "=" << str_value << endl;
        }
        if((!str_root.empty()) && (!str_key.empty()) && (!str_value.empty()))
        {
           ININode ini_node(str_root, str_key, str_value);
           vec_ini.push_back(ini_node);
           //cout << vec_ini.size() << endl;
        }
    }
    in_conf_file.close();
    in_conf_file.clear();
    //vector convert to map
    map<string, string> map_tmp;
    for(vector<ININode>::iterator itr = vec_ini.begin(); itr != vec_ini.end(); ++itr)
    {
        map_tmp.insert(pair<string, string>(itr->root, ""));
    }
    SubNode sn;
    for(map<string, string>::iterator itr = map_tmp.begin(); itr != map_tmp.end(); ++itr)
    {
       //cout << itr->first << endl;
       for(vector<ININode>::iterator sub_itr = vec_ini.begin(); sub_itr != vec_ini.end(); ++sub_itr)
       {
           if(sub_itr->root == itr->first)
           {
               //cout << sub_itr->key << "=" << sub_itr->value << endl;
               sn.InsertElement(sub_itr->key, sub_itr->value);
           }
       }
       map_ini.insert(pair<string, SubNode>(itr->first, sn));
    }
    return 1;
}
//get value by root and key
string INIParser::GetValue(string root, string key)
{
    map<string, SubNode>::iterator itr = map_ini.find(root);
    map<string, string>::iterator sub_itr = itr->second.sub_node.find(key);
    if(!(sub_itr->second).empty())
        return sub_itr->second;
    return "";
}
//write ini file
int INIParser::WriteINI(string path)
{
    ofstream out_conf_file(path.c_str());
    if(!out_conf_file)
        return -1;
    //cout << map_ini.size() << endl;
    for(map<string, SubNode>::iterator itr = map_ini.begin(); itr != map_ini.end(); ++itr)
    {
       //cout << itr->first << endl;
       out_conf_file << "[" << itr->first << "]" << endl;
       for(map<string, string>::iterator sub_itr = itr->second.sub_node.begin(); sub_itr != itr->second.sub_node.end(); ++sub_itr)
       {
           //cout << sub_itr->first << "=" << sub_itr->second << endl;
           out_conf_file << sub_itr->first << "=" << sub_itr->second << endl;
       }
    }
    out_conf_file.close();
    out_conf_file.clear();
    return 1;
}
//set value
vector<ININode>::size_type INIParser::SetValue(string root, string key, string value)
{
    map<string, SubNode>::iterator itr = map_ini.find(root);
    if(map_ini.end() != itr)
        itr->second.sub_node.insert(pair<string, string>(key, value));
    else
    {
        SubNode sn;
        sn.InsertElement(key, value);
        map_ini.insert(pair<string, SubNode>(root, sn));
    }
    return map_ini.size();
}


评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值