[Boost-bugs] [Boost C++ Libraries] #12368: Improve property_tree documentation for path_of

Subject: [Boost-bugs] [Boost C++ Libraries] #12368: Improve property_tree documentation for path_of
From: Boost C++ Libraries (noreply_at_[hidden])
Date: 2016-08-02 08:44:13


#12368: Improve property_tree documentation for path_of
------------------------------+---------------------------
 Reporter: harris.pc@… | Owner: cornedbee
     Type: Feature Requests | Status: new
Milestone: To Be Determined | Component: property_tree
  Version: Boost 1.61.0 | Severity: Problem
 Keywords: |
------------------------------+---------------------------
 There is no documentation on the path_of system,

 I did find this helpful email:
 http://lists.boost.org/Archives/boost/2009/06/152883.php

 It would be great if something like that were in the official
 documentation.

 In the end, I managed to create my own extension for storing a tree of
 filenames,
 here is the key code, just in case you feel its a good example for the
 documentation.

 (Code is still not fully tested)


 {{{
 // a custom path-string,
 // I am not using fs::path because I need to treat a directory and a file
 // differently in terms of the name, where
 // a directory has a / on the end of its name.
 // This allows me to have a tree with a file and folder existing in the
 tree
 // at the same time (the file may have been replaced with the folder, so
 one
 // is deleted and the other is created).
 //
 class PathString
 {
    string filename;
 public:
    PathString( string const& p ) : filename(p) {}

    bool is_dir() const { return not empty() and
 filename[filename.size()-1] == '/'; }

    string const& str() const { return filename; }

    bool empty() const { return filename.empty(); }

    bool operator<( PathString const& b ) const
    {
       return filename < b.filename;
    }

    bool operator==( PathString const& b ) const
    {
       return filename == b.filename;
    }
 };




 namespace boost {
    namespace property_tree {

       // specialisation of path_of for PathString
       template <>
       struct path_of<PathString>
       {
          struct type
          {
             std::string filename;
             std::string::size_type start, size;

          public:
             type( std::string const& s ) :
                filename(s),
                start(0),
                size(filename.size())
             {}

             std::string dump() const
             {
                return filename.substr(start);
             }

             bool empty() const
             {
                return start == size;
             }

             // aka pop_front() and return the fragment
             // modifies this value
             std::string reduce()
             {
                assert(not empty());

                std::string::size_type next_sep = filename.find('/',
 start);

                // no separator, or separator found at the end
                if (next_sep == std::string::npos or next_sep+1 == size)
                {
                   std::string part;
                   part.swap(filename);
                   return part;
                }

                // include the trailing separator
                std::string::size_type old_start = start;
                start = next_sep+1;

                return filename.substr(old_start, start-old_start);
             }


             bool single() const
             {
                // will return true if empty, it should not be asked for
 empty paths anyway

                // it is a "single" path if there is a slash BEFORE the
 last character.
                // if the last character is /, then it can be a "single"
 directory.
                //
                // so check if idx < filename.size()-1 --therefore--> idx+1
 < filename.size()
                std::string::size_type idx = filename.find('/');
                return idx == std::string::npos or idx+1 < filename.size();
             }
          };
       };
 }}}

-- 
Ticket URL: <https://svn.boost.org/trac/boost/ticket/12368>
Boost C++ Libraries <http://www.boost.org/>
Boost provides free peer-reviewed portable C++ source libraries.

This archive was generated by hypermail 2.1.7 : 2017-02-16 18:50:20 UTC