Josef
Josef

Reputation: 13

Boost property tree Bad path for nothing

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...

Upvotes: 1

Views: 1969

Answers (1)

manlio
manlio

Reputation: 18902

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.

Upvotes: 1

Related Questions