views:

581

answers:

2

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!

+4  A: 

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

Peter Alexander
Small mistake in your line, it needs to be pt.push_back(ptree::value_type("example.txt", ptree("10"))); This also begs the question, how do I get this child now that the get operation also fails due to the '.' character, and how do I put without giving it a data value such as example.txt>wordCount>10?
doomdayx
Edited my post to answer your question.
Peter Alexander
Unfortunately, put does not support alternate delimiters. so pt.put('/', "example.txt", "10"); cannot be used. While I did replacement of '.' with another character myself outside of the function, it does not look like that is built in.
doomdayx
+2  A: 

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.

doomdayx