基于C++11的ini文件解析

  • Post author:
  • Post category:其他


inipraser.h



#pragma once
#ifndef INI_PARSER_H
#define INI_PARSER_H

#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <cstdlib>
#include <map>
using namespace std;
class ININode
{
public:
    ININode(string root, string key, string value)
    {
        this->root = root;
        this->key = key;
        this->value = value;
    }
    string root;
    string key;
    string value;
};
class SubNode
{
public:
    void InsertElement(string key, string value)
    {
        sub_node.insert(pair<string, string>(key, value));
    }
    void clear() {
        sub_node.clear();
    }
    void updata(string key, string value) {
        sub_node[key] = value;
    }

    map<string, string> sub_node;
};
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);

    //清除ini
    void Clear() { map_ini.clear(); }

    //删除root
    void clear_root(string root);
    //删除key
    void clear_key(string root, string key);



private:
    map<string, SubNode> map_ini;

};
#endif // INI_PARSER_H

inipraser.cpp

#include"iniparser.h"

#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <cstdlib>
#include <map>
using namespace std;
//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;
}


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, ""));
    }
    for (map<string, string>::iterator itr = map_tmp.begin(); itr != map_tmp.end(); ++itr)
    {
        SubNode sn;
        //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;
}

string INIParser::GetValue(string root, string key)
{
    map<string, SubNode>::iterator itr = map_ini.find(root);
    if (map_ini.end() != itr) {
        map<string, string>::iterator sub_itr = itr->second.sub_node.find(key);
        if (!(sub_itr->second).empty())
            return sub_itr->second;
    }
    return "";
}

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) {
        map_ini[root].updata(key, value);
        //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();
}
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;
}

//删除root
void INIParser::clear_root(string root) {
    //map_ini[root].clear();
    map_ini.erase(root);
}
//删除key
void INIParser::clear_key(string root, string key) {
    map<string, SubNode>::iterator itr = map_ini.find(root);
    if (map_ini.end() != itr) {
        for (map<string, string>::iterator sub_itr = itr->second.sub_node.begin(); sub_itr != itr->second.sub_node.end(); ++sub_itr)
        {
            if (sub_itr->first == key) {
                map_ini[root].sub_node.erase(key);
                break;
            }
        }
    }
}



版权声明:本文为qq_45434544原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。