1

I want to write a boost::property_tree::ptree binary to a file, and read it out again into a ptree.

Since i´m not very comfort with the ptree and binary writing/reading them.. I thought you guys can lead me into the right direction.

Writing strings, int/float/double isn´t that great problem, but how to store a whole ptree (with unknown keys and values, so it is generic) to a file and read it back in cpp using if-/ofstream?

The Filer Extention will be "*.tgasset" and the File will contain more than the ptree as data.

To make things easier to me.. these are my dummy functions to write/read the data:

void TGS_File::PTreeToFile(std::ofstream &_file, boost::property_tree::ptree _data) {

}

boost::property_tree::ptree TGS_File::PTreeFromFile(std::ifstream &_file) {
    boost::property_tree::ptree _data;

    return _data;
}

(done that with strings, ints, floats and doubles the same way)

  • The read/write should be binary with if-/ofstream, since the ptree is not the only data in this file, later. Means, I can't use the XML and json parsers. – BDC_Patrick Nov 14 '21 at 14:01
  • @BDC_Patrick the problem might boil down to map everything to POCs, which can be simply stored in binary format. That's a hard thing to do, specifically if any pointer linkage is used. You'll probably better off, providing appropriate json, or XML parsers for your class types and use a mapper. – πάντα ῥεῖ Nov 14 '21 at 14:10
  • The ptree data is only made of types like std::string, int, float, double and bools.. no pointer, just plain data. thought of iterating over the keys/paths and write them + the value to the file.. but dunno how. – BDC_Patrick Nov 14 '21 at 15:06
  • Yes, that's why I said. You'll need an appropriate mapper class, `std::string` isn't a POD type, which can be serialized as a binary directly. – πάντα ῥεῖ Nov 14 '21 at 15:10
  • yes. my StringToFile function creates a char* buffer out of the string and stores that in the file . OK.. will search for that. – BDC_Patrick Nov 14 '21 at 15:21
  • I think there might be a logical error here. Text is effectively a subset of binary. Sure, there's some minor stuff like line endings, but JSON and XML don't care about that. – MSalters Nov 23 '21 at 13:43

1 Answers1

0

You will have to select a format: INI, Json, XML or INFO. Each has their own set of limitations: https://www.boost.org/doc/libs/1_77_0/doc/html/property_tree/parsers.html

E.g. if you choose JSON:

#include <boost/property_tree/json_parser.hpp>

void TGS_File::PTreeToFile(std::ofstream &_file, boost::property_tree::ptree _data) {
     write_json(_file, _data);
}

boost::property_tree::ptree TGS_File::PTreeFromFile(std::ifstream &_file) {
    boost::property_tree::ptree _data;
    read_json(_file, _data);
    return _data;
}

The procedure is 99% the same for other formats. Roughly speaking (from memory) I suppose the INFO format loses the least amount of information over a roundtrip.

sehe
  • 374,641
  • 47
  • 450
  • 633
  • Yes.. but that would write a *.json, *.xml or such.. but i need to write more infos into the same file than just the ptree, using an own extention (see OP). – BDC_Patrick Nov 17 '21 at 11:02
  • Then you have to _decide_ how to. It depends 100% on your other needs, the format of the surrounding elements. Since you didn't tell me/us anything about that, we can't help. I can make something up, but chances are you'll just go "That doesn't work for me because of magic surprise XYZ" – sehe Nov 17 '21 at 12:52
  • For fun, this is such a solution which naively encodes every value in base64 and quotes the individual values. This format doesn't relay any structure, but it does guarantee encoding neutrality and message framing: [Live On Coliru](http://coliru.stacked-crooked.com/a/052d76480acc0bce) I'm 100% sure you will find a reason why "this doesn't work for you". That's because you need to design your own file format! – sehe Nov 17 '21 at 12:54
  • Added a roundtrip test (fixed lossy serialization of the double, file under "Turd Polishing"): http://coliru.stacked-crooked.com/a/47e9fe3b2572b88d – sehe Nov 17 '21 at 15:59
  • Didn't know that the parser can be used to write the json string directly to an other file. That changes some stuff. thanks @sehe – BDC_Patrick Nov 18 '21 at 05:16