4

I need to parse ini file using C++ with boost library. This file contains the multi keys. For example,

[section_1]
key_1=value_1
key_1=value_2
...
key_n=value_n
[section_2]
key1=value_1
key1=value_2
...
key_n=value_1
key_n=value_2
[]
...
[section_n]
...

I tried use the functional of boost library: the function boost::property_tree::ini_parser::read_ini(), but it can't contain the multikey in ini file and return the exception. So I tried use the function boost::program_options::parse_config_file(), but it's not what I need.

What functionality should I use to parse the ini file and for each section I can to get own structure with relevant key values?

Mike Kinghan
  • 55,740
  • 12
  • 153
  • 182
voiko
  • 43
  • 5
  • You fail to explain what "the multi key" is. The example is inconsistent (it looks like you want to have `key_1`, `key_2`..., `key_n` but it could just as well be `key1`, `key2`..., `key` or even `key1`, `key1`, `key1` for that matter. The appearance of `[]` is totally unexplained. – sehe Jan 20 '17 at 09:00
  • Multi key is two or more same key values. And I just gave an example for understanding what constitutes ini file in my case. The purpose of my problem using boost functionality (if it's available) to access the parsed values of the keys and corresponding values without collisions keys (or multikeys). It's that what I mean. – voiko Jan 20 '17 at 18:26
  • You need to show us what sort of data structure you expect to *get* as a result of parsing such a file, whether with some boost library or anything else. Add an illustration of the desired output for the input you've posted. As it stands, it's unclear what you're asking. – Mike Kinghan Jan 20 '17 at 18:40
  • Ok, I will try to describe my purpose. I have ini file, which get outside. It's contain information about certificate chain (final certificate, internal certificates, CRLs etc.), i.e. my ini file: [cert chain 1] final cert = name cert1 internal cert=name cert2 ... internal cert=name cert_n other keys = ... [cert chain 2] ... And I need to parse this ini file of such a type, but I met the problem with parsing internal certificates whick can't parsing with using boost's function property_tree::read_ini(). I want to know ways to parse this file using boost. P.S. I can't edit this file. – voiko Jan 20 '17 at 19:11

2 Answers2

1

Your input is simply not an INI file, as INI files do not permit duplicate values. You can write your own parser, e.g. using the code I wrote here:¹

If you replace the section_t map

typedef std::map<textnode_t, textnode_t>    section_t;

with multimap:

typedef std::multimap<textnode_t, textnode_t>    section_t;

you can parse repeated keys:

[section_1]
key_1=value_1
key_1=value_2
key_n=value_n
[section_2]
key1=value_1
key2=value_2
key_n=value_1
key_n=value_2
[section_n]

See full code here: https://gist.github.com/sehe/068b1ae81547b98a3cec02a530f220df

¹ or Learning Boost.Spirit: parsing INI and http://coliru.stacked-crooked.com/view?id=cd1d516ae0b19bd6f9af1e3f1b132211-0d2159870a1c6cb0cd1457b292b97230 and possibly others

sehe
  • 374,641
  • 47
  • 450
  • 633
0

A SSCCE that might help you

Live On Coliru

#include <boost/property_tree/ini_parser.hpp>
#include <iostream>
using boost::property_tree::ptree;

int main() {
    std::istringstream iss(R"([section_1]
key_1=value_1
key_2=value_2
key_n=value_n
[section_2]
key1=value_1
key2=value_2
key_n=value_n
key_m=value_m
[]
[section_n])");

    ptree pt;
    read_ini(iss, pt);

    for (auto& section : pt) {
        std::cout << "[" << section.first << "]\n";
        for (auto& key : section.second) {
            std::cout << key.first << "=" << key.second.get_value("") << "\n";
        }
    }
}

Prints

[section_1]
key_1=value_1
key_2=value_2
key_n=value_n
[section_2]
key1=value_1
key2=value_2
key_n=value_n
key_m=value_m
sehe
  • 374,641
  • 47
  • 450
  • 633