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 directory_iterator;
00157   class recursive_directory_iterator;
00158 
00159   struct __directory_iterator_proxy
00160   {
00161     const directory_entry& operator*() const& noexcept { return _M_entry; }
00162 
00163     directory_entry operator*() && noexcept { return std::move(_M_entry); }
00164 
00165   private:
00166     friend class directory_iterator;
00167     friend class recursive_directory_iterator;
00168 
00169     explicit
00170     __directory_iterator_proxy(const directory_entry& __e) : _M_entry(__e) { }
00171 
00172     directory_entry _M_entry;
00173   };
00174 
00175   class directory_iterator
00176   {
00177   public:
00178     typedef directory_entry        value_type;
00179     typedef ptrdiff_t              difference_type;
00180     typedef const directory_entry* pointer;
00181     typedef const directory_entry& reference;
00182     typedef input_iterator_tag     iterator_category;
00183 
00184     directory_iterator() noexcept = default;
00185 
00186     explicit
00187     directory_iterator(const path& __p)
00188     : directory_iterator(__p, directory_options::none, nullptr) { }
00189 
00190     directory_iterator(const path& __p, directory_options __options)
00191     : directory_iterator(__p, __options, nullptr) { }
00192 
00193     directory_iterator(const path& __p, error_code& __ec) noexcept
00194     : directory_iterator(__p, directory_options::none, __ec) { }
00195 
00196     directory_iterator(const path& __p,
00197                        directory_options __options,
00198                        error_code& __ec) noexcept
00199     : directory_iterator(__p, __options, &__ec) { }
00200 
00201     directory_iterator(const directory_iterator& __rhs) = default;
00202 
00203     directory_iterator(directory_iterator&& __rhs) noexcept = default;
00204 
00205     ~directory_iterator() = default;
00206 
00207     directory_iterator&
00208     operator=(const directory_iterator& __rhs) = default;
00209 
00210     directory_iterator&
00211     operator=(directory_iterator&& __rhs) noexcept = default;
00212 
00213     const directory_entry& operator*() const;
00214     const directory_entry* operator->() const { return &**this; }
00215     directory_iterator&    operator++();
00216     directory_iterator&    increment(error_code& __ec) noexcept;
00217 
00218     __directory_iterator_proxy operator++(int)
00219     {
00220       __directory_iterator_proxy __pr{**this};
00221       ++*this;
00222       return __pr;
00223     }
00224 
00225   private:
00226     directory_iterator(const path&, directory_options, error_code*);
00227 
00228     friend bool
00229     operator==(const directory_iterator& __lhs,
00230                const directory_iterator& __rhs);
00231 
00232     friend class recursive_directory_iterator;
00233 
00234     std::shared_ptr<_Dir> _M_dir;
00235   };
00236 
00237   inline directory_iterator
00238   begin(directory_iterator __iter) noexcept
00239   { return __iter; }
00240 
00241   inline directory_iterator
00242   end(directory_iterator) noexcept
00243   { return directory_iterator(); }
00244 
00245   inline bool
00246   operator==(const directory_iterator& __lhs, const directory_iterator& __rhs)
00247   {
00248     return !__rhs._M_dir.owner_before(__lhs._M_dir)
00249       && !__lhs._M_dir.owner_before(__rhs._M_dir);
00250   }
00251 
00252   inline bool
00253   operator!=(const directory_iterator& __lhs, const directory_iterator& __rhs)
00254   { return !(__lhs == __rhs); }
00255 
00256   class recursive_directory_iterator
00257   {
00258   public:
00259     typedef directory_entry        value_type;
00260     typedef ptrdiff_t              difference_type;
00261     typedef const directory_entry* pointer;
00262     typedef const directory_entry& reference;
00263     typedef input_iterator_tag     iterator_category;
00264 
00265     recursive_directory_iterator() noexcept = default;
00266 
00267     explicit
00268     recursive_directory_iterator(const path& __p)
00269     : recursive_directory_iterator(__p, directory_options::none, nullptr) { }
00270 
00271     recursive_directory_iterator(const path& __p, directory_options __options)
00272     : recursive_directory_iterator(__p, __options, nullptr) { }
00273 
00274     recursive_directory_iterator(const path& __p,
00275                                  directory_options __options,
00276                                  error_code& __ec) noexcept
00277     : recursive_directory_iterator(__p, __options, &__ec) { }
00278 
00279     recursive_directory_iterator(const path& __p, error_code& __ec) noexcept
00280     : recursive_directory_iterator(__p, directory_options::none, &__ec) { }
00281 
00282     recursive_directory_iterator(
00283         const recursive_directory_iterator&) = default;
00284 
00285     recursive_directory_iterator(
00286         recursive_directory_iterator&&) noexcept = default;
00287 
00288     ~recursive_directory_iterator();
00289 
00290     // observers
00291     directory_options  options() const { return _M_options; }
00292     int                depth() const;
00293     bool               recursion_pending() const { return _M_pending; }
00294 
00295     const directory_entry& operator*() const;
00296     const directory_entry* operator->() const { return &**this; }
00297 
00298     // modifiers
00299     recursive_directory_iterator&
00300     operator=(const recursive_directory_iterator& __rhs) noexcept;
00301     recursive_directory_iterator&
00302     operator=(recursive_directory_iterator&& __rhs) noexcept;
00303 
00304     recursive_directory_iterator& operator++();
00305     recursive_directory_iterator& increment(error_code& __ec) noexcept;
00306 
00307     __directory_iterator_proxy operator++(int)
00308     {
00309       __directory_iterator_proxy __pr{**this};
00310       ++*this;
00311       return __pr;
00312     }
00313 
00314     void pop();
00315 
00316     void disable_recursion_pending() { _M_pending = false; }
00317 
00318   private:
00319     recursive_directory_iterator(const path&, directory_options, error_code*);
00320 
00321     friend bool
00322     operator==(const recursive_directory_iterator& __lhs,
00323                const recursive_directory_iterator& __rhs);
00324 
00325     struct _Dir_stack;
00326     std::shared_ptr<_Dir_stack> _M_dirs;
00327     directory_options _M_options = {};
00328     bool _M_pending = false;
00329   };
00330 
00331   inline recursive_directory_iterator
00332   begin(recursive_directory_iterator __iter) noexcept
00333   { return __iter; }
00334 
00335   inline recursive_directory_iterator
00336   end(recursive_directory_iterator) noexcept
00337   { return recursive_directory_iterator(); }
00338 
00339   inline bool
00340   operator==(const recursive_directory_iterator& __lhs,
00341              const recursive_directory_iterator& __rhs)
00342   {
00343     return !__rhs._M_dirs.owner_before(__lhs._M_dirs)
00344       && !__lhs._M_dirs.owner_before(__rhs._M_dirs);
00345   }
00346 
00347   inline bool
00348   operator!=(const recursive_directory_iterator& __lhs,
00349              const recursive_directory_iterator& __rhs)
00350   { return !(__lhs == __rhs); }
00351 
00352 _GLIBCXX_END_NAMESPACE_CXX11
00353 
00354   // @} group filesystem
00355 _GLIBCXX_END_NAMESPACE_VERSION
00356 } // namespace v1
00357 } // namespace filesystem
00358 } // namespace experimental
00359 } // namespace std
00360 
00361 #endif // C++11
00362 
00363 #endif // _GLIBCXX_EXPERIMENTAL_FS_DIR_H