libstdc++
fs_dir.h
Go to the documentation of this file.
00001 // Filesystem directory utilities -*- C++ -*-
00002 
00003 // Copyright (C) 2014-2015 Free Software Foundation, Inc.
00004 //
00005 // This file is part of the GNU ISO C++ Library.  This library is free
00006 // software; you can redistribute it and/or modify it under the
00007 // terms of the GNU General Public License as published by the
00008 // Free Software Foundation; either version 3, or (at your option)
00009 // any later version.
00010 
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 // GNU General Public License for more details.
00015 
00016 // Under Section 7 of GPL version 3, you are granted additional
00017 // permissions described in the GCC Runtime Library Exception, version
00018 // 3.1, as published by the Free Software Foundation.
00019 
00020 // You should have received a copy of the GNU General Public License and
00021 // a copy of the GCC Runtime Library Exception along with this program;
00022 // see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
00023 // <http://www.gnu.org/licenses/>.
00024 
00025 /** @file experimental/fs_dir.h
00026  *  This is an internal header file, included by other library headers.
00027  *  Do not attempt to use it directly. @headername{experimental/filesystem}
00028  */
00029 
00030 #ifndef _GLIBCXX_EXPERIMENTAL_FS_DIR_H
00031 #define _GLIBCXX_EXPERIMENTAL_FS_DIR_H 1
00032 
00033 #if __cplusplus < 201103L
00034 # include <bits/c++0x_warning.h>
00035 #else
00036 # include <typeinfo>
00037 # include <ext/concurrence.h>
00038 # include <bits/unique_ptr.h>
00039 # include <bits/shared_ptr.h>
00040 
00041 namespace std _GLIBCXX_VISIBILITY(default)
00042 {
00043 namespace experimental
00044 {
00045 namespace filesystem
00046 {
00047 inline namespace v1
00048 {
00049 _GLIBCXX_BEGIN_NAMESPACE_VERSION
00050 
00051   /**
00052    * @ingroup filesystem
00053    * @{
00054    */
00055 
00056   class file_status
00057   {
00058   public:
00059     // constructors
00060     explicit
00061     file_status(file_type __ft = file_type::none,
00062                 perms __prms = perms::unknown) noexcept
00063     : _M_type(__ft), _M_perms(__prms) { }
00064 
00065     file_status(const file_status&) noexcept = default;
00066     file_status(file_status&&) noexcept = default;
00067     ~file_status() = default;
00068 
00069     file_status& operator=(const file_status&) noexcept = default;
00070     file_status& operator=(file_status&&) noexcept = default;
00071 
00072     // observers
00073     file_type  type() const noexcept { return _M_type; }
00074     perms      permissions() const noexcept { return _M_perms; }
00075 
00076     // modifiers
00077     void       type(file_type __ft) noexcept { _M_type = __ft; }
00078     void       permissions(perms __prms) noexcept { _M_perms = __prms; }
00079 
00080   private:
00081     file_type   _M_type;
00082     perms       _M_perms;
00083   };
00084 
00085 _GLIBCXX_BEGIN_NAMESPACE_CXX11
00086 
00087   class directory_entry
00088   {
00089   public:
00090     // constructors and destructor
00091     directory_entry() noexcept = default;
00092     directory_entry(const directory_entry&) = default;
00093     directory_entry(directory_entry&&) noexcept = default;
00094     explicit directory_entry(const filesystem::path& __p) : _M_path(__p) { }
00095     ~directory_entry() = default;
00096 
00097     // modifiers
00098     directory_entry& operator=(const directory_entry&) = default;
00099     directory_entry& operator=(directory_entry&&) noexcept = default;
00100 
00101     void assign(const filesystem::path& __p) { _M_path = __p; }
00102 
00103     void
00104     replace_filename(const filesystem::path& __p)
00105     { _M_path = _M_path.parent_path() / __p; }
00106 
00107     // observers
00108     const filesystem::path&  path() const noexcept { return _M_path; }
00109     operator const filesystem::path&() const noexcept { return _M_path; }
00110 
00111     file_status
00112     status() const
00113     { return filesystem::status(_M_path); }
00114 
00115     file_status
00116     status(error_code& __ec) const noexcept
00117     { return filesystem::status(_M_path, __ec); }
00118 
00119     file_status
00120     symlink_status() const
00121     { return filesystem::symlink_status(_M_path); }
00122 
00123     file_status
00124     symlink_status(error_code& __ec) const noexcept
00125     { return filesystem::symlink_status(_M_path, __ec); }
00126 
00127     bool
00128     operator< (const directory_entry& __rhs) const noexcept
00129     { return _M_path < __rhs._M_path; }
00130 
00131     bool
00132     operator==(const directory_entry& __rhs) const noexcept
00133     { return _M_path == __rhs._M_path; }
00134 
00135     bool
00136     operator!=(const directory_entry& __rhs) const noexcept
00137     { return _M_path != __rhs._M_path; }
00138 
00139     bool
00140     operator<=(const directory_entry& __rhs) const noexcept
00141     { return _M_path <= __rhs._M_path; }
00142 
00143     bool
00144     operator> (const directory_entry& __rhs) const noexcept
00145     { return _M_path > __rhs._M_path; }
00146 
00147     bool
00148     operator>=(const directory_entry& __rhs) const noexcept
00149     { return _M_path >= __rhs._M_path; }
00150 
00151   private:
00152     filesystem::path    _M_path;
00153   };
00154 
00155   struct _Dir;
00156   class recursive_directory_iterator;
00157 
00158   class directory_iterator
00159   {
00160   public:
00161     typedef directory_entry        value_type;
00162     typedef ptrdiff_t              difference_type;
00163     typedef const directory_entry* pointer;
00164     typedef const directory_entry& reference;
00165     typedef input_iterator_tag     iterator_category;
00166 
00167     directory_iterator() noexcept = default;
00168 
00169     explicit
00170     directory_iterator(const path& __p)
00171     : directory_iterator(__p, directory_options::none, nullptr) { }
00172 
00173     directory_iterator(const path& __p, directory_options __options)
00174     : directory_iterator(__p, __options, nullptr) { }
00175 
00176     directory_iterator(const path& __p, error_code& __ec) noexcept
00177     : directory_iterator(__p, directory_options::none, __ec) { }
00178 
00179     directory_iterator(const path& __p,
00180       directory_options __options, error_code& __ec) noexcept
00181     : directory_iterator(__p, __options, &__ec) { }
00182 
00183     directory_iterator(const directory_iterator& __rhs) = default;
00184 
00185     directory_iterator(directory_iterator&& __rhs) noexcept = default;
00186 
00187     ~directory_iterator() = default;
00188 
00189     directory_iterator& operator=(const directory_iterator& __rhs) = default;
00190     directory_iterator& operator=(directory_iterator&& __rhs) noexcept = default;
00191 
00192     const directory_entry& operator*() const;
00193     const directory_entry* operator->() const { return &**this; }
00194     directory_iterator&    operator++();
00195     directory_iterator&    increment(error_code& __ec) noexcept;
00196 
00197     directory_iterator operator++(int)
00198     {
00199       auto __tmp = *this;
00200       ++*this;
00201       return __tmp;
00202     }
00203 
00204   private:
00205     directory_iterator(const path&, directory_options, error_code*);
00206 
00207     friend bool
00208     operator==(const directory_iterator& __lhs,
00209                const directory_iterator& __rhs);
00210 
00211     friend class recursive_directory_iterator;
00212 
00213     std::shared_ptr<_Dir> _M_dir;
00214   };
00215 
00216   inline directory_iterator
00217   begin(directory_iterator __iter) { return __iter; }
00218 
00219   inline directory_iterator
00220   end(directory_iterator) { return directory_iterator(); }
00221 
00222   inline bool
00223   operator==(const directory_iterator& __lhs, const directory_iterator& __rhs)
00224   {
00225     return !__rhs._M_dir.owner_before(__lhs._M_dir)
00226       && !__lhs._M_dir.owner_before(__rhs._M_dir);
00227   }
00228 
00229   inline bool
00230   operator!=(const directory_iterator& __lhs, const directory_iterator& __rhs)
00231   { return !(__lhs == __rhs); }
00232 
00233   class recursive_directory_iterator
00234   {
00235   public:
00236     typedef directory_entry        value_type;
00237     typedef ptrdiff_t              difference_type;
00238     typedef const directory_entry* pointer;
00239     typedef const directory_entry& reference;
00240     typedef input_iterator_tag     iterator_category;
00241 
00242     recursive_directory_iterator() noexcept = default;
00243 
00244     explicit
00245     recursive_directory_iterator(const path& __p)
00246     : recursive_directory_iterator(__p, directory_options::none, nullptr) { }
00247 
00248     recursive_directory_iterator(const path& __p, directory_options __options)
00249     : recursive_directory_iterator(__p, __options, nullptr) { }
00250 
00251     recursive_directory_iterator(const path& __p,
00252                                  directory_options __options,
00253                                  error_code& __ec) noexcept
00254     : recursive_directory_iterator(__p, __options, &__ec) { }
00255 
00256     recursive_directory_iterator(const path& __p, error_code& __ec) noexcept
00257     : recursive_directory_iterator(__p, directory_options::none, &__ec) { }
00258 
00259     recursive_directory_iterator(
00260         const recursive_directory_iterator&) = default;
00261 
00262     recursive_directory_iterator(
00263         recursive_directory_iterator&&) noexcept = default;
00264 
00265     ~recursive_directory_iterator();
00266 
00267     // observers
00268     directory_options  options() const { return _M_options; }
00269     int                depth() const;
00270     bool               recursion_pending() const { return _M_pending; }
00271 
00272     const directory_entry& operator*() const;
00273     const directory_entry* operator->() const { return &**this; }
00274 
00275     // modifiers
00276     recursive_directory_iterator&
00277       operator=(const recursive_directory_iterator& __rhs) noexcept;
00278     recursive_directory_iterator&
00279       operator=(recursive_directory_iterator&& __rhs) noexcept;
00280 
00281     recursive_directory_iterator& operator++();
00282     recursive_directory_iterator& increment(error_code& __ec) noexcept;
00283 
00284     recursive_directory_iterator operator++(int)
00285     {
00286       auto __tmp = *this;
00287       ++*this;
00288       return __tmp;
00289     }
00290 
00291     void pop();
00292 
00293     void disable_recursion_pending() { _M_pending = false; }
00294 
00295   private:
00296     recursive_directory_iterator(const path&, directory_options, error_code*);
00297 
00298     friend bool
00299     operator==(const recursive_directory_iterator& __lhs,
00300                const recursive_directory_iterator& __rhs);
00301 
00302     struct _Dir_stack;
00303     std::shared_ptr<_Dir_stack> _M_dirs;
00304     directory_options _M_options;
00305     bool _M_pending;
00306   };
00307 
00308   inline recursive_directory_iterator
00309   begin(recursive_directory_iterator __iter) { return __iter; }
00310 
00311   inline recursive_directory_iterator
00312   end(recursive_directory_iterator) { return recursive_directory_iterator(); }
00313 
00314   inline bool
00315   operator==(const recursive_directory_iterator& __lhs,
00316              const recursive_directory_iterator& __rhs)
00317   {
00318     return !__rhs._M_dirs.owner_before(__lhs._M_dirs)
00319       && !__lhs._M_dirs.owner_before(__rhs._M_dirs);
00320   }
00321 
00322   inline bool
00323   operator!=(const recursive_directory_iterator& __lhs,
00324              const recursive_directory_iterator& __rhs)
00325   { return !(__lhs == __rhs); }
00326 
00327 _GLIBCXX_END_NAMESPACE_CXX11
00328 
00329   // @} group filesystem
00330 _GLIBCXX_END_NAMESPACE_VERSION
00331 } // namespace v1
00332 } // namespace filesystem
00333 } // namespace experimental
00334 } // namespace std
00335 
00336 #endif // C++11
00337 
00338 #endif // _GLIBCXX_EXPERIMENTAL_FS_DIR_H