summaryrefslogtreecommitdiff
path: root/boost/filesystem/v2/convenience.hpp
blob: 30b6adee7ddf6cae5770b92c36af658cddc985c7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
//  boost/filesystem/convenience.hpp  ----------------------------------------//

//  Copyright Beman Dawes, 2002-2005
//  Copyright Vladimir Prus, 2002
//  Use, modification, and distribution is subject to the Boost Software
//  License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
//  http://www.boost.org/LICENSE_1_0.txt)

//  See library home page at http://www.boost.org/libs/filesystem

//----------------------------------------------------------------------------// 

#ifndef BOOST_FILESYSTEM2_CONVENIENCE_HPP
#define BOOST_FILESYSTEM2_CONVENIENCE_HPP

#include <boost/filesystem/v2/config.hpp>
#include <boost/filesystem/v2/operations.hpp>
#include <boost/system/error_code.hpp>
#include <vector>
#include <stack>

#include <boost/config/abi_prefix.hpp> // must be the last #include

# ifndef BOOST_FILESYSTEM2_NARROW_ONLY
#   define BOOST_FS_FUNC(BOOST_FS_TYPE) \
      template<class Path> typename boost::enable_if<is_basic_path<Path>, \
      BOOST_FS_TYPE>::type
#   define BOOST_FS_FUNC_STRING BOOST_FS_FUNC(typename Path::string_type)
#   define BOOST_FS_TYPENAME typename
# else
#   define BOOST_FS_FUNC(BOOST_FS_TYPE) inline BOOST_FS_TYPE 
    typedef boost::filesystem::path Path;
#   define BOOST_FS_FUNC_STRING inline std::string
#   define BOOST_FS_TYPENAME
# endif

namespace boost
{
  namespace filesystem2
  {

    BOOST_FS_FUNC(bool) create_directories(const Path& ph)
    {
         if (ph.empty() || exists(ph))
         {
           if ( !ph.empty() && !is_directory(ph) )
               boost::throw_exception( basic_filesystem_error<Path>(
                 "boost::filesystem::create_directories", ph,
                 make_error_code( boost::system::errc::file_exists ) ) );
           return false;
         }

         // First create branch, by calling ourself recursively
         create_directories(ph.parent_path());
         // Now that parent's path exists, create the directory
         create_directory(ph);
         return true;
     }

# ifndef BOOST_FILESYSTEM_NO_DEPRECATED

    BOOST_FS_FUNC_STRING extension(const Path& ph)
    {
      typedef BOOST_FS_TYPENAME Path::string_type string_type;
      string_type filename = ph.filename();

      BOOST_FS_TYPENAME string_type::size_type n = filename.rfind('.');
      if (n != string_type::npos)
        return filename.substr(n);
      else
        return string_type();
    }

    BOOST_FS_FUNC_STRING basename(const Path& ph)
    {
      typedef BOOST_FS_TYPENAME Path::string_type string_type;
      string_type filename = ph.filename();
      BOOST_FS_TYPENAME string_type::size_type n = filename.rfind('.');
      return filename.substr(0, n);
    }


    BOOST_FS_FUNC(Path) change_extension( const Path & ph,
      const BOOST_FS_TYPENAME Path::string_type & new_extension )
    {
#   if !defined(_STLPORT_VERSION)
      return ph.parent_path() / (basename(ph) + new_extension); 
#   else
      typedef BOOST_FS_TYPENAME Path::string_type string_type; 
      string_type filename = basename(ph) + new_extension; 
      return ph.parent_path() / filename;
#   endif
    } 
# endif

# ifndef BOOST_FILESYSTEM2_NARROW_ONLY

    // "do-the-right-thing" overloads  ---------------------------------------//

    inline bool create_directories(const path& ph)
      { return create_directories<path>(ph); }
    inline bool create_directories(const wpath& ph)
      { return create_directories<wpath>(ph); }

# ifndef BOOST_FILESYSTEM_NO_DEPRECATED
    inline std::string extension(const path& ph)
      { return extension<path>(ph); }
    inline std::wstring extension(const wpath& ph)
      { return extension<wpath>(ph); }

    inline std::string basename(const path& ph)
      { return basename<path>( ph ); }
    inline std::wstring basename(const wpath& ph)
      { return basename<wpath>( ph ); }

    inline path change_extension( const path & ph, const std::string& new_ex )
      { return change_extension<path>( ph, new_ex ); }
    inline wpath change_extension( const wpath & ph, const std::wstring& new_ex )
      { return change_extension<wpath>( ph, new_ex ); }
# endif

# endif


    //  basic_recursive_directory_iterator helpers  --------------------------//

    namespace detail
    {
      template< class Path >
      struct recur_dir_itr_imp
      {
        typedef basic_directory_iterator< Path > element_type;
        std::stack< element_type, std::vector< element_type > > m_stack;
        int  m_level;
        bool m_no_push;
        bool m_no_throw;

        recur_dir_itr_imp() : m_level(0), m_no_push(false), m_no_throw(false) {}
      };

    } // namespace detail

    //  basic_recursive_directory_iterator  ----------------------------------//

    template< class Path >
    class basic_recursive_directory_iterator
      : public boost::iterator_facade<
          basic_recursive_directory_iterator<Path>,
          basic_directory_entry<Path>,
          boost::single_pass_traversal_tag >
    {
    public:
      typedef Path path_type;

      basic_recursive_directory_iterator(){}  // creates the "end" iterator

      explicit basic_recursive_directory_iterator( const Path & dir_path );
      basic_recursive_directory_iterator( const Path & dir_path,
        system::error_code & ec );

      int level() const { return m_imp->m_level; }

      void pop();
      void no_push()
      {
        BOOST_ASSERT( m_imp.get() && "attempt to no_push() on end iterator" );
        m_imp->m_no_push = true;
      }

      file_status status() const
      {
        BOOST_ASSERT( m_imp.get()
          && "attempt to call status() on end recursive_iterator" );
        return m_imp->m_stack.top()->status();
      }

      file_status symlink_status() const
      {
        BOOST_ASSERT( m_imp.get()
          && "attempt to call symlink_status() on end recursive_iterator" );
        return m_imp->m_stack.top()->symlink_status();
      }

    private:

      // shared_ptr provides shallow-copy semantics required for InputIterators.
      // m_imp.get()==0 indicates the end iterator.
      boost::shared_ptr< detail::recur_dir_itr_imp< Path > >  m_imp;

      friend class boost::iterator_core_access;

      typename boost::iterator_facade< 
        basic_recursive_directory_iterator<Path>,
        basic_directory_entry<Path>,
        boost::single_pass_traversal_tag >::reference
      dereference() const 
      {
        BOOST_ASSERT( m_imp.get() && "attempt to dereference end iterator" );
        return *m_imp->m_stack.top();
      }

      void increment();

      bool equal( const basic_recursive_directory_iterator & rhs ) const
        { return m_imp == rhs.m_imp; }

    };

    typedef basic_recursive_directory_iterator<path> recursive_directory_iterator;
# ifndef BOOST_FILESYSTEM2_NARROW_ONLY
    typedef basic_recursive_directory_iterator<wpath> wrecursive_directory_iterator;
# endif

    //  basic_recursive_directory_iterator implementation  -------------------//

    //  constructors
    template<class Path>
    basic_recursive_directory_iterator<Path>::
      basic_recursive_directory_iterator( const Path & dir_path )
      : m_imp( new detail::recur_dir_itr_imp<Path> )
    {
      m_imp->m_stack.push( basic_directory_iterator<Path>( dir_path ) );
      if ( m_imp->m_stack.top () == basic_directory_iterator<Path>() )
        { m_imp.reset (); }
    }

    template<class Path>
    basic_recursive_directory_iterator<Path>::
      basic_recursive_directory_iterator( const Path & dir_path,
        system::error_code & ec )
      : m_imp( new detail::recur_dir_itr_imp<Path> )
    {
      m_imp->m_no_throw = true;
      m_imp->m_stack.push( basic_directory_iterator<Path>( dir_path, ec ) );
      if ( m_imp->m_stack.top () == basic_directory_iterator<Path>() )
        { m_imp.reset (); }
    }

    //  increment
    template<class Path>
    void basic_recursive_directory_iterator<Path>::increment()
    {
      BOOST_ASSERT( m_imp.get() && "increment on end iterator" );
      
      static const basic_directory_iterator<Path> end_itr;

      if ( m_imp->m_no_push )
        { m_imp->m_no_push = false; }
      else if ( is_directory( m_imp->m_stack.top()->status() ) )
      {
        system::error_code ec;
#if BOOST_WORKAROUND(__CODEGEARC__, BOOST_TESTED_AT(0x610))
        if( m_imp->m_no_throw ) {
            m_imp->m_stack.push(
                basic_directory_iterator<Path>( *m_imp->m_stack.top(), ec )
            );
        }
        else {
            m_imp->m_stack.push(
                basic_directory_iterator<Path>( *m_imp->m_stack.top() )
            );
        }
#else
        m_imp->m_stack.push(
          m_imp->m_no_throw
            ? basic_directory_iterator<Path>( *m_imp->m_stack.top(), ec )
            : basic_directory_iterator<Path>( *m_imp->m_stack.top() ) );
#endif
        if ( m_imp->m_stack.top() != end_itr )
        {
          ++m_imp->m_level;
          return;
        }
        m_imp->m_stack.pop();
      }

      while ( !m_imp->m_stack.empty()
        && ++m_imp->m_stack.top() == end_itr )
      {
        m_imp->m_stack.pop();
        --m_imp->m_level;
      }

      if ( m_imp->m_stack.empty() ) m_imp.reset(); // done, so make end iterator
    }

    //  pop
    template<class Path>
    void basic_recursive_directory_iterator<Path>::pop()
    {
      BOOST_ASSERT( m_imp.get() && "pop on end iterator" );
      BOOST_ASSERT( m_imp->m_level > 0 && "pop with level < 1" );

      static const basic_directory_iterator<Path> end_itr;

      do
      {
        m_imp->m_stack.pop();
        --m_imp->m_level;
      }
      while ( !m_imp->m_stack.empty()
        && ++m_imp->m_stack.top() == end_itr );

      if ( m_imp->m_stack.empty() ) m_imp.reset(); // done, so make end iterator
    }

  } // namespace filesystem2
} // namespace boost

#undef BOOST_FS_FUNC_STRING
#undef BOOST_FS_FUNC

//----------------------------------------------------------------------------//

namespace boost
{
  namespace filesystem
  {
    using filesystem2::create_directories;
    using filesystem2::basic_recursive_directory_iterator;
    using filesystem2::recursive_directory_iterator;

# ifndef BOOST_FILESYSTEM_NO_DEPRECATED
    using filesystem2::extension;
    using filesystem2::basename;
    using filesystem2::change_extension;
# endif

# ifndef BOOST_FILESYSTEM2_NARROW_ONLY
    using filesystem2::wrecursive_directory_iterator;
# endif

  }
}

//----------------------------------------------------------------------------//

#include <boost/config/abi_suffix.hpp> // pops abi_prefix.hpp pragmas
#endif // BOOST_FILESYSTEM2_CONVENIENCE_HPP