读取文件配置类

基于Boost库的读取文件配置类

#pragma once
#include "const.h"
struct SectionInfo
{
    std::map<std::string, std::string> _section_datas;
    SectionInfo() = default;
    ~SectionInfo(){
        _section_datas.clear();
    }
    SectionInfo(const SectionInfo &src){
        _section_datas = src._section_datas;
    }
    SectionInfo& operator=(const SectionInfo& src)
    {
        if (this != &src)
        {
            this->_section_datas = src._section_datas;
        }
        return *this;
    }

    std::string operator[](const std::string& key)
    {
        if (_section_datas.find(key) == _section_datas.end())
        {
            return "";
        }
        return _section_datas[key];
    }
};
class ConfigMgr
{
public:
    static ConfigMgr& Inst() {
        static ConfigMgr cfg_mgr;
        return cfg_mgr;
    }

    ConfigMgr& operator=(const ConfigMgr& src) {
        if (&src == this) {
            return *this;
        }
        this->_config_map = src._config_map;
        return *this;
    };
    ConfigMgr(const ConfigMgr& src) {
        this->_config_map = src._config_map;
    }

    ~ConfigMgr() {
        _config_map.clear();
    }
    SectionInfo operator[](const std::string& section) {
        if (_config_map.find(section) == _config_map.end())
        {
            return SectionInfo();
        }
        return _config_map[section];
    }

private:

    ConfigMgr()
{
    // 获取当前目录
    boost::filesystem::path current_path = boost::filesystem::current_path();
    boost::filesystem::path config_path = current_path / "config.ini";
    std::cout << "Config path : " << config_path << std::endl;

    // 按树来读取
    boost::property_tree::ptree pt;
    boost::property_tree::read_ini(config_path.string(), pt);

    for (const auto& section_pair : pt)
    {
        const ::std::string& section_name = section_pair.first;//GateServer
        const boost::property_tree::ptree& section_tree = section_pair.second;//Port=8080的集合

         // 对于属性子树每个结点,遍历其所有的key-value对  
        std::map<std::string, std::string> section_config;
        for (const auto& key_value_pair : section_tree)
        {
            const std::string& key = key_value_pair.first;//key值
            const std::string& value = key_value_pair.second.get_value<std::string>();
            section_config[key] = value;
        }
        SectionInfo sectionInfo;
        sectionInfo._section_datas = section_config;
        // 将section的key-value对保存到config_map中  
        _config_map[section_name] = sectionInfo;
    }
    // 输出所有的section和key-value对  
    for (const auto& section_entry : _config_map) {
        const std::string& section_name = section_entry.first;
        SectionInfo section_config = section_entry.second;
        std::cout << "[" << section_name << "]" << std::endl;
        for (const auto& key_value_pair : section_config._section_datas) {
            std::cout << key_value_pair.first << "=" << key_value_pair.second << std::endl;
        }
    }

}

    std::map<std::string, SectionInfo> _config_map;
};