Reputation: 442
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.
The working program:
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;
}
The comment appended config:
static std::string inidata =
R"(
# comment
[SECTION1]
key1 = 15 # COMMENT ADDED!
key2=val
)";
The core dump output:
/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)
Upvotes: 2
Views: 2463
Reputation: 393174
The comment style **is not supported*.
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);
}
Upvotes: 2