Question

I'm considering creating an input/log file for my code for two main purposes.

1) Be able to log all key parameters that the code was run with so that I can reproduce the same results in the future if I needed to. 2) Be able to start/resume the code without the need to hard-code the parameters in the code.

I'm interested in a header-only approach so that I could simply carry the header files along with my code thus I have turned into boost.property_tree. From the documentation it seems to serve the purpose. My question is which file-format serves my need the best? I need something that:

1) is easily readable/editable by a human 2) can support nested sections 3) can support comments

By default I have turned to xml but I'm not sure that's the best option considering parsing capabilities of boost.property_tree. For instance I want to be able to parse something like this:

<Grid Type = "AMR">
    <Domain> -1.0 1.0 -1.0 1.0 </Domain>
    <Levels> 10 5 </Levels>
    <Path> /path/to/data.bin </Path>
</Grid>

I want to be able to read in Type property as well as Domain, Levels, and Path sections correctly and separately (for instance Domain has 4 separate floating points corresponding to the edges of a rectangle). My code looks like this:

using boost::property_tree::ptree;
ptree pt;

double x = pt.get<double>("Grid.Domain");

std::cout << x << std::endl;

but I keep getting conversion of data to type "d" failed. How should I fix it? Also, is xml the best option I have or should I consider changing to another format?

Was it helpful?

Solution

boost::property_tree also supports serialization/deserialization to and from JSON. JSON allows arrays and thus it is very easy to store multiple values for a section. You can even store structures in JSON (which may be handy if you wanted to persist the state of an object from your program).

Important caveat: boost::property_tree treats values internally as strings, so if you want valid JSON that might be read outside of your program, you will not be able to read numeric values directly from JSON you create with boost::property_tree without performing some sort of cast (depending on the reading code).

If retaining the types of numeric values in your persisted file is important to you, I would check out JSONCpp. I have used it myself and it is very easy to get up and running, and it is very lightweight.

JSONCpp also supports comments. From the SourceForge page:

Notes: Comments used to be supported in JSON but where removed for portability (C like comments are not supported in Python). Since comments are useful in configuration/input file, this feature was preserved.

OTHER TIPS

Ok I think I found a way to handle multiple values. One way would be to read the whole thing as std::string and then using std::istringstream you can separate each part very easily. Something like this:

std::istringstream iss(pt.get<std::string>("Grid.Domain"));
double xmin, xmax, ymin, ymax;
iss >> xmin >> xmax >> ymin >> ymax;

works pretty well. Yet I do not know of a way to get the Type property ...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top