1

I am having trouble with this library... My code works fine, the parsers/creator works too, but an err appears, I don't know why:

#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/xml_parser.hpp>
#include <boost/thread.hpp>
#include <string>
#include <exception>
#include <set>
#include <iostream>
#include "journal.h"

unsigned short port                 = 2013;
unsigned short maxConnec            = 250;

unsigned short fPort()                 {return port;}
unsigned short fMaxConnec()            {return maxConnec;}

bool load(const std::string &file)
{
    using boost::property_tree::ptree;
    ptree objectXML;
    std::cout << "bbb";
    read_xml(file, objectXML);
    std::cout << "aaa";
    if (file.length() == 0) // By the way, no way to do that better ? "if file doesn't exist..."
    {
        return 0;
    }
    else
    {
        port                    = objectXML.get<unsigned short>("configuration.server.port");
        maxConnec            = objectXML.get<unsigned short>("configuration.server.maxConnections");
        return 1;
    }
}

bool save(const std::string &file)
{
    try
    {
        using boost::property_tree::ptree;
        ptree objectXML;
        objetXML.put("configuration.server.port", port);
        objetXML.put("configuration.server.maxConnections", maxConnec);
        write_xml(file, objectXML, std::locale(), boost::property_tree::xml_writer_make_settings<ptree::key_type>(' ', 4));
        return 1;
    }
    catch (std::exception e)
    {
        return 0;
    }
}

void generate()
{

    std::string file = "configuration.xml";
    try{
        if (!load(fichier))
        {
            save(file);
        }
    }
    catch (std::exception &e)
    {
        load(file);
    }
}

Get a bad path, I totally don't know why because when I try to read data I can and it gets the data in configuration.xml even if I change it...

Josef
  • 13
  • 4

1 Answers1

1

The ptree_bad_path exception is raised from the throwing version of get and signals that "configuration.server.port" or "configuration.server.maxConnections" path to the XML element doesn't exist.

The error isn't related to the configuration.xml file path.

So you should check the element name or, for optional elements, use the default-value / optional-value version of get.

manlio
  • 18,345
  • 14
  • 76
  • 126
  • Well, this is exactly what I though when I just pressed the "post" button and I was feeling so sily, because I had to go. But anyway, that was it, thank you. In fact, I deleted some stuff to post, to make the code more readable, and it was configuration.time.save (I was looking for configuration.time.saving). Well again thank you anyway :). – Josef Jan 05 '15 at 18:23