Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Boost Property Tree with filename as key

I am trying to use filenames as the key in boost::PropertyTree

However, the '.' character in a filename such as "example.txt" causes an additional layer to be added within the property tree. The most obvious solution would be to replace '.' with another character, but there is likely a better way to do this, such as with an escape character.

In the following example, the value 10 will be put in the node 'txt', a child of 'example'. Instead, I want the value 10 to be stored in the node 'example.txt'.

ptree pt;
pt.put("example.txt", 10);

How can I use the full filename for a single node?

Thanks in advance for your help!

like image 956
Andrew Hundt Avatar asked Dec 22 '09 19:12

Andrew Hundt


2 Answers

Just insert the tree explicitly:

pt.push_back(ptree::value_type("example.txt", ptree(10)));

The put method is simply there for convenience, which is why it automatically parses . as an additional layer. Constructing the value_type explicitly like I have shown above avoids this problem.

An alternative way to solve the problem is to use an extra argument in put and get, which changes the delimeter.

pt.put('/', "example.txt", "10");
pt.get<string>('/', "example.txt");

For the record, I've never used this class before in my life. I got all this information right from the page you linked to ; )

like image 146
Peter Alexander Avatar answered Nov 05 '22 05:11

Peter Alexander


The problem was that the documentation was outdated. A path type object must be created as follows, with another character that is invalid for file paths specified as the delimiter as follows:

pt.put(boost::property_tree::ptree::path_type("example.txt", '|'), 10);

I found a path to the solution from the boost mailing list at the newsgroup gmane.comp.lib.boost.devel posted by Philippe Vaucher.

like image 8
Andrew Hundt Avatar answered Nov 05 '22 05:11

Andrew Hundt