Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Writing comments to ini file with boost::property_tree::ptree

Is there any way to write comments in an ini file using boost::property::ptree?

Something like that:

  void save_ini(const std::string& path)
  {
      boost::property_tree::ptree pt;
      int first_value = 1;
      int second_value = 2;
      
      // Write a comment to describe what the first_value is here
      pt.put("something.first_value", );
      // Write a second comment here
      pt.put("something.second_value", second_value);

      boost::property_tree::write_ini(path, pt);
  }

The documentation here doesn't give the info. Did boost implement that?

like image 417
Jeff Bencteux Avatar asked Dec 08 '14 09:12

Jeff Bencteux


1 Answers

boost::property_tree::ptree is used for a variety of "property tree" types (INFO, INI, XML, JSON, etc.), so it does not inherently support anything other than being a fancy container for allowing key=>value settings. Your final line (which should be):

boost::property_tree::ini_parser::write_ini(path, pt);

is the only thing that is defining what you're doing as INI instead of one of those other formats. You could easily replace that line with writing to XML, for example, and it would also work. Therefore, you can see that the property_tree::ptree cannot have things specific to a specific type of file.


The best you could do would be to add a "comments" setting for each of your children -- something like this:

pt.put("something.comments", "Here are the comments for the 'something' section");

You can have properties for any child with any name you want...and simply ignore them when iterating through during read. So, there's no reason not to be creative like this if you wish -- it's your program!

like image 141
Amadeus Avatar answered Oct 03 '22 08:10

Amadeus