SAGA Adaptor CPI v.1.0
filesystem.hpp
Go to the documentation of this file.
00001 //  Copyright (c) 2008 João Abecasis
00002 //
00003 //  Distributed under the Boost Software License, Version 1.0. (See accompanying
00004 //  file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
00005 
00006 #ifndef SAGA_ADAPTORS_UTILS_FILESYSTEM_HPP_INCLUDED
00007 #define SAGA_ADAPTORS_UTILS_FILESYSTEM_HPP_INCLUDED
00008 
00009 //
00010 //  Here, we attempt to hide idiosyncrasies of Boost.Filesystem's evolution.
00011 //  This is not meant to be complete, stuff should be added *only* as needed.
00012 //
00013 
00014 #include <boost/version.hpp>
00015 #include <saga/saga-defs.hpp>
00016 
00017 #if SAGA_BOOST_FILESYSTEM_VERSION > 103500
00018 #   include <boost/filesystem.hpp>
00019 #else
00020 
00021 //  There are serious issues with this version of the API and the workarounds in
00022 //  this file. E.g. file_status has race-conditions which can't be avoided other
00023 //  than by reimplementing Boost.Filesystem.
00024 
00025 #   include <boost/filesystem/path.hpp>
00026 #   include <boost/filesystem/operations.hpp>
00027 #   include <boost/filesystem/exception.hpp>
00028 
00029 #endif
00030 
00031 namespace boost { namespace system {
00032 
00033     // Error reporting was split into Boost.System in Boost 1.35
00034 
00035 #if SAGA_BOOST_FILESYSTEM_VERSION < 103400
00036     // An enum in Boost 1.33
00037     typedef boost::filesystem::error_code error_code;
00038 #elif SAGA_BOOST_FILESYSTEM_VERSION < 103500
00039     // An integer type in Boost 1.34
00040     typedef boost::filesystem::system_error_type error_code;
00041 #endif
00042 
00043 }} // boost::system
00044 
00045 namespace boost { namespace filesystem {
00046 
00047 #if SAGA_BOOST_FILESYSTEM_VERSION < 103400
00048     // Introduced in Boost 1.34
00049     struct file_status
00050     {
00051         file_status()
00052             : error_(other_error)
00053         {
00054         }
00055 
00056         file_status(path const &p)
00057             : error_(no_error)
00058             , exists_(exists(p))
00059         {
00060             // Yes, there's a race condition here. Upgrade your Boost!
00061             if (exists_)
00062             {
00063                 is_directory_ = is_directory(p);
00064                 is_symbolic_link_ = symbolic_link_exists(p);
00065             }
00066         }
00067 
00068         error_code error_;
00069         bool exists_;
00070         bool is_symbolic_link_;
00071         bool is_directory_;
00072     };
00073 
00074     // Introduced in Boost 1.34
00075     inline file_status status(path const &p)
00076     {
00077         return file_status(p);
00078     }
00079 
00080     // Introduced in Boost 1.34
00081     inline file_status status(path const &p, boost::system::error_code &err)
00082     {
00083         file_status status;
00084         err = no_error;
00085 
00086         try
00087         {
00088             status = file_status(p);
00089         }
00090         catch (boost::filesystem::filesystem_error const &exc)
00091         {
00092             err = exc.error();
00093             status.error_ = err;
00094         }
00095 
00096         return status;
00097     }
00098 
00099     // Introduced in Boost 1.34
00100     inline bool status_known(file_status status)
00101     {
00102         return status.error_ == no_error;
00103     }
00104 
00105     // Introduced in Boost 1.34
00106     inline bool exists(file_status status)
00107     {
00108         return status.exists_;
00109     }
00110 
00111     // Introduced in Boost 1.34
00112     inline bool is_directory(file_status status)
00113     {
00114         return status.is_directory_;
00115     }
00116 
00117     // is_regular(file_status) was introduced in Boost 1.34 and later renamed to
00118     // is_regular_file.
00119     inline bool is_regular_file(file_status status)
00120     {
00121         // This implementation of is_regular_file is less strict than what later
00122         // versions of Boost provide.
00123         //
00124         // If you have a problem with this, upgrade your Boost! ;-)
00125 
00126         return status.exists_
00127             && !status.is_symbolic_link_
00128             && !status.is_directory_;
00129     }
00130 
00131 #elif SAGA_BOOST_FILESYSTEM_VERSION <= 103500
00132 
00133     // Introduced in Boost 1.36, deprecating is_regular(file_status)
00134     inline bool is_regular_file(file_status status)
00135     {
00136         return is_regular(status);
00137     }
00138 
00139 #endif
00140 
00142     // Evolution of basic_path member functions is catered to with inline
00143     // forwarding non-member functions that can be found through ADL.
00145 
00146     // basic_path::leaf() was deprecated in Boost 1.36 and renamed to filename
00147 #if SAGA_BOOST_FILESYSTEM_VERSION < 103400
00148     inline std::string filename(path const &p)
00149 #else
00150     template <class Path>
00151     inline typename Path::string_type filename(Path const &p)
00152 #endif
00153     {
00154     #if SAGA_BOOST_FILESYSTEM_VERSION < 103600
00155         return p.leaf();
00156     #else
00157         #if BOOST_FILESYSTEM_VERSION == 3
00158         return p.filename().string();
00159         #else
00160         return p.filename();
00161         #endif    
00162     #endif
00163     }
00164 
00165     // basic_path::branch_path() was deprecated in Boost 1.36 and renamed to
00166     // parent_path
00167     template <class Path>
00168     inline Path parent_path(Path const &p)
00169     {
00170     #if SAGA_BOOST_FILESYSTEM_VERSION < 103600
00171         return p.branch_path();
00172     #else
00173         return p.parent_path();
00174     #endif
00175     }
00176 
00177 }} // namespace boost::filesystem
00178 
00179 #endif // include guard
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines