Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>If you want the property tree to reflect the hierarchy, then it requires writing a custom parser. Per the INI parser <a href="http://www.boost.org/doc/libs/1_53_0/doc/html/boost_propertytree/parsers.html#boost_propertytree.parsers.ini_parser" rel="nofollow">documentation</a>:</p> <blockquote> <p>INI is a simple key-value format with a single level of sectioning. [...] not all property trees can be serialized as INI files.</p> </blockquote> <p>Because of the single level sectioning, <code>my.section.subsection1</code> must be treated as a key, rather than a hierarchy path. For example, the <code>my.section.subsection1.someProp1</code> path could be broken down into:</p> <pre class="lang-none prettyprint-override"><code> key separator value .----------^---------. ^ .---^---. |my.section.subsection1|.|someProp1| </code></pre> <p>Because "." is part of the key, the <a href="http://www.boost.org/doc/libs/1_53_0/doc/html/boost/property_tree/string_path.html" rel="nofollow"><code>boost::property_tree::string_path</code></a> type must be explicitly instantiated with a different separator. It is available as a <code>path_type</code> typedef on <code>ptree</code>. In this case, I have opted to use "/":</p> <pre><code> ptree::path_type("my.section.subsection1/someProp1", '/') </code></pre> <hr> <p>With example.ini containing:</p> <pre class="lang-none prettyprint-override"><code>[my.section.subsection1] someProp1=0 [my.section.subsection2] anotherProp=1 </code></pre> <p>The following program:</p> <pre class="lang-cpp prettyprint-override"><code>#include &lt;iostream&gt; #include &lt;boost/property_tree/ptree.hpp&gt; #include &lt;boost/property_tree/ini_parser.hpp&gt; int main() { namespace pt = boost::property_tree; pt::ptree propTree; read_ini("example.ini", propTree); boost::optional&lt;uint32_t&gt; someProp1 = propTree.get_optional&lt;uint32_t&gt;( pt::ptree::path_type( "my.section.subsection1/someProp1", '/')); boost::optional&lt;uint32_t&gt; anotherProp = propTree.get_optional&lt;uint32_t&gt;( pt::ptree::path_type( "my.section.subsection2/anotherProp", '/')); std::cout &lt;&lt; "someProp1 = " &lt;&lt; *someProp1 &lt;&lt; "\nanotherProp = " &lt;&lt; *anotherProp &lt;&lt; std::endl; } </code></pre> <p>Produces the following output:</p> <pre class="lang-none prettyprint-override"><code>someProp1 = 0 anotherProp = 1 </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload