如果有任何注释附加到键值,Boost属性树ini解析会出错

Boost property tree ini parsing gives error if any comment is appended to key-value

本文关键字:ini 属性 出错 Boost 注释 任何 键值 如果      更新时间:2023-10-16

我有一个工作示例程序,它使用boost property_tree解析ini格式的数据。当我将注释附加到键值对时,我得到了coredump。我在property_tree上搜索了任何注释修剪函数,但找不到任何内容。

工作程序:

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;
}

附加了config:的注释

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)

不支持注释样式**。

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

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

测试程序显示#实际上只在第一个非空白列中很特别:LiveOnColiru

#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);
}