如果将任何注释附加到键值,Boost属性树ini解析将给出错误 [英] Boost property tree ini parsing gives error if any comment is appended to key-value

查看:96
本文介绍了如果将任何注释附加到键值,Boost属性树ini解析将给出错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个工作的示例程序,该程序使用boost property_tree解析ini格式的数据. 当我在键值对上添加注释时,我得到了coredump.我已经搜索了property_tree上的任何注释修剪函数,但找不到任何东西.

I have a working example program which parses ini formatted data with boost property_tree. When I append a comment to the key-value pairs I get coredump. I have searched any comment trim function on property_tree but cannot find anything.

工作程序:

static std::string inidata =
R"(
    # comment

    [SECTION1]  
    key1 = 15
    key2=val        
)";

void read_data(std::istream &is)
{
    using boost::property_tree::ptree;

    ptree pt;

    read_ini(is, pt);

    boost::optional<uint32_t>    sect1_key1 = pt.get_optional<uint32_t>(ptree::path_type("SECTION1/key1", '/'));
    boost::optional<std::string> sect1_key2 = pt.get_optional<std::string>(ptree::path_type("SECTION1/key2", '/'));

    std::cout << "SECTION1.key1: " << *sect1_key1 << std::endl;
    std::cout << "SECTION1.key2: " << *sect1_key2 << std::endl;
}

注释附上配置:

static std::string inidata =
R"(
    # comment

    [SECTION1]  
    key1 = 15           # COMMENT ADDED!
    key2=val        
)";

核心转储输出:

/usr/local/include/boost/optional/optional.hpp:992: boost::optional<T>::reference_type boost::optional<T>::get() [with T = unsigned int; boost::optional<T>::reference_type = unsigned int&]: Assertion `this->is_initialized()' failed.
Aborted (core dumped)

推荐答案

注释样式** 您可以通过在文本值上移动注释来看到这一点,结果是:

You can see this by moving the comment on the text value, which results in:

SECTION1.key1: 15
SECTION1.key2: val  # woah

显示#实际上仅在第一个非空白列中是特殊的测试器:

Tester that shows that # is really only special in the first non-whitespace column: Live On Coliru

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

static std::string inidata =
R"(
    # comment

    [SECTION1]  
    key1 = 15
    key2=val  # woah       
    k#ey3=whoops
)";

using boost::property_tree::ptree;

void read_data(std::istream &is)
{

    ptree pt;
    read_ini(is, pt);

    for (auto section : pt)
        for (auto key : section.second)
            std::cout << "DEBUG: " << key.first << "=" << key.second.get_value<std::string>() << "\n";
}

#include <sstream>

int main()
{
    std::istringstream iss(inidata);
    read_data(iss);
}

这篇关于如果将任何注释附加到键值,Boost属性树ini解析将给出错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆