首页 > 解决方案 > C ++ / Boost :: Propery_Tree - 是否可以使用名称中的点来获取ini配置值

问题描述

这是我的代码:

我的Config.h

#include <string>
#include <boost/property_tree/ini_parser.hpp>

namespace pt = boost::property_tree;

class Config {
public:
    Config(std::string conf);

    template<typename T>
    T GetConfigValue(std::string configKey, T value = 0){
        pt::ptree tree;
        pt::read_ini(_configFile, tree);
        return tree.get<T>(configKey, value);
    }

private:
    std::string _configFile;
};

我是这样称呼它的:

#include "Logger.h"
#include "Config.h"
int main() {
    Config * config = new Config("/WorldServer.conf.dist");
    std::string def = "Nothing there.";
    Logger::Log(config->GetConfigValue("WorldServer.MaxPingTime", def));
}

以下是 的内容WorldServer.conf.dist

[WorldServer]
MaxPingTime = 30
MySQL.User = "root"

当我执行

Logger::Log(config->GetConfigValue("WorldServer.MaxPingTime", def));

我看到30结果是正确的,但是当我执行时:

Logger::Log(config->GetConfigValue("WorldServer.MySQL.User", def));

我看到了结果Nothing there.

据我了解.MySQL.User导致问题的原因。

我的问题是我可以使用 boost 属性树以某种方式使其与 dot 一起使用来解析 ini 配置吗?

标签: c++boostboost-propertytree

解决方案


推荐阅读