Boost logo

Boost :

Subject: [boost] [property_tree] handling of filesystem::path
From: Johan Råde (rade_at_[hidden])
Date: 2010-06-13 02:47:26


property_tree::ptree does not handle filesystem::path
values correctly if the path contains spaces.

Here is a proposed fix and a test.
The test succeeds, but if you comment out the patch, then the test fails.

----------------------------------------------------------------------------------

#include <boost/property_tree/ptree.hpp>

// patch that fixes the problem

namespace boost { namespace filesystem {
     template<class String, class PathTraits> class basic_path;
}};

namespace boost { namespace property_tree {

     template<typename String, typename PathTraits> struct path_translator
     {
         typedef boost::filesystem::basic_path<String, PathTraits> Path;

         typedef String internal_type;
         typedef Path external_type;

         boost::optional<Path> get_value(const String& s) { return Path(s); }
         boost::optional<String> put_value(const Path &p) { return p.string(); }
     };

     template <typename Char, typename StringTraits, typename Alloc, typename PathTraits>
     struct translator_between<
         std::basic_string<Char, StringTraits, Alloc>,
         boost::filesystem::basic_path<std::basic_string<Char, StringTraits, Alloc>, PathTraits>
>
     {
         typedef std::basic_string<Char, StringTraits, Alloc> String;
         typedef path_translator<String, PathTraits> type;
     };
}}

// a simple test

#include <boost/filesystem/path.hpp>
#include <boost/optional.hpp>

using boost::filesystem::path;
using boost::optional;
using boost::property_tree::ptree;

int main()
{
     path p1("a b");
     ptree t;
     t.put_value(p1);
     optional<path> p2 = t.get_value_optional<path>();
     assert(p2);
     assert(p1 == *p2);
}


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk