📄 operations.hpp
字号:
// boost/filesystem/operations.hpp -----------------------------------------//
// Copyright 2002-2005 Beman Dawes
// Copyright 2002 Jan Langer
// Copyright 2001 Dietmar Kuehl
//
// 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_FILESYSTEM_OPERATIONS_HPP
#define BOOST_FILESYSTEM_OPERATIONS_HPP
#include <boost/filesystem/path.hpp> // includes <boost/filesystem/config.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/utility/enable_if.hpp>
#include <boost/type_traits/is_same.hpp>
#include <boost/iterator.hpp>
#include <boost/cstdint.hpp>
#include <boost/assert.hpp>
#include <string>
#include <utility> // for pair
#include <ctime>
#ifdef BOOST_WINDOWS_API
# include <fstream>
# if !defined(_WIN32_WINNT) || _WIN32_WINNT >= 0x0500
# define BOOST_FS_HARD_LINK // Default for Windows 2K or later
# endif
#endif
#include <boost/config/abi_prefix.hpp> // must be the last #include
# ifdef BOOST_NO_STDC_NAMESPACE
namespace std { using ::time_t; }
# endif
# ifndef BOOST_FILESYSTEM_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_INLINE_FS_FUNC(BOOST_FS_TYPE) \
template<class Path> inline typename boost::enable_if<is_basic_path<Path>, \
BOOST_FS_TYPE>::type
# define BOOST_FS_TYPENAME typename
# else
# define BOOST_FS_FUNC(BOOST_FS_TYPE) inline BOOST_FS_TYPE
# define BOOST_INLINE_FS_FUNC(BOOST_FS_TYPE) inline BOOST_FS_TYPE
typedef boost::filesystem::path Path;
# define BOOST_FS_TYPENAME
# endif
//----------------------------------------------------------------------------//
namespace boost
{
namespace filesystem
{
template<class Path> class basic_directory_iterator;
// BOOST_FILESYSTEM_NARROW_ONLY needs this:
typedef basic_directory_iterator<path> directory_iterator;
template<class Path> class basic_directory_entry;
enum file_type
{
status_unknown,
file_not_found,
regular_file,
directory_file,
// the following will never be reported by some operating or file systems
symlink_file,
block_file,
character_file,
fifo_file,
socket_file,
type_unknown // file does exist, but isn't one of the above types
};
class file_status
{
public:
explicit file_status( file_type v = status_unknown ) : m_value(v) {}
void type( file_type v ) { m_value = v; }
file_type type() const { return m_value; }
private:
// the internal representation is unspecified so that additional state
// information such as permissions can be added in the future; this
// implementation just uses status_type as the internal representation
file_type m_value;
};
inline bool status_known( file_status f ) { return f.type() != status_unknown; }
inline bool exists( file_status f ) { return f.type() != status_unknown && f.type() != file_not_found; }
inline bool is_regular( file_status f ) { return f.type() == regular_file; }
inline bool is_directory( file_status f ) { return f.type() == directory_file; }
inline bool is_symlink( file_status f ) { return f.type() == symlink_file; }
inline bool is_other( file_status f ) { return exists(f) && !is_regular(f) && !is_directory(f) && !is_symlink(f); }
struct space_info
{
// all values are byte counts
boost::uintmax_t capacity;
boost::uintmax_t free; // <= capacity
boost::uintmax_t available; // <= free
};
namespace detail
{
typedef std::pair< boost::filesystem::system_error_type, bool >
query_pair;
typedef std::pair< boost::filesystem::system_error_type, boost::uintmax_t >
uintmax_pair;
typedef std::pair< boost::filesystem::system_error_type, std::time_t >
time_pair;
typedef std::pair< boost::filesystem::system_error_type, space_info >
space_pair;
template< class Path >
struct directory_pair
{
typedef std::pair< boost::filesystem::system_error_type,
typename Path::external_string_type > type;
};
# ifndef BOOST_FILESYSTEM_NO_DEPRECATED
BOOST_FILESYSTEM_DECL bool
symbolic_link_exists_api( const std::string & ); // deprecated
# endif
BOOST_FILESYSTEM_DECL file_status
status_api( const std::string & ph, system_error_type & ec );
# ifndef BOOST_WINDOWS_API
BOOST_FILESYSTEM_DECL file_status
symlink_status_api( const std::string & ph, system_error_type & ec );
# endif
BOOST_FILESYSTEM_DECL query_pair
is_empty_api( const std::string & ph );
BOOST_FILESYSTEM_DECL query_pair
equivalent_api( const std::string & ph1, const std::string & ph2 );
BOOST_FILESYSTEM_DECL uintmax_pair
file_size_api( const std::string & ph );
BOOST_FILESYSTEM_DECL space_pair
space_api( const std::string & ph );
BOOST_FILESYSTEM_DECL time_pair
last_write_time_api( const std::string & ph );
BOOST_FILESYSTEM_DECL boost::filesystem::system_error_type
last_write_time_api( const std::string & ph, std::time_t new_value );
BOOST_FILESYSTEM_DECL boost::filesystem::system_error_type
get_current_path_api( std::string & ph );
BOOST_FILESYSTEM_DECL query_pair
create_directory_api( const std::string & ph );
BOOST_FILESYSTEM_DECL boost::filesystem::system_error_type
create_hard_link_api( const std::string & to_ph,
const std::string & from_ph );
BOOST_FILESYSTEM_DECL boost::filesystem::system_error_type
create_symlink_api( const std::string & to_ph,
const std::string & from_ph );
BOOST_FILESYSTEM_DECL boost::filesystem::system_error_type
remove_api( const std::string & ph );
BOOST_FILESYSTEM_DECL boost::filesystem::system_error_type
rename_api( const std::string & from, const std::string & to );
BOOST_FILESYSTEM_DECL boost::filesystem::system_error_type
copy_file_api( const std::string & from, const std::string & to );
# if defined(BOOST_WINDOWS_API)
BOOST_FILESYSTEM_DECL boost::filesystem::system_error_type
get_full_path_name_api( const std::string & ph, std::string & target );
# if !defined(BOOST_FILESYSTEM_NARROW_ONLY)
BOOST_FILESYSTEM_DECL boost::filesystem::file_status
status_api( const std::wstring & ph, system_error_type & ec );
BOOST_FILESYSTEM_DECL query_pair
is_empty_api( const std::wstring & ph );
BOOST_FILESYSTEM_DECL query_pair
equivalent_api( const std::wstring & ph1, const std::wstring & ph2 );
BOOST_FILESYSTEM_DECL uintmax_pair
file_size_api( const std::wstring & ph );
BOOST_FILESYSTEM_DECL space_pair
space_api( const std::wstring & ph );
BOOST_FILESYSTEM_DECL boost::filesystem::system_error_type
get_full_path_name_api( const std::wstring & ph, std::wstring & target );
BOOST_FILESYSTEM_DECL time_pair
last_write_time_api( const std::wstring & ph );
BOOST_FILESYSTEM_DECL boost::filesystem::system_error_type
last_write_time_api( const std::wstring & ph, std::time_t new_value );
BOOST_FILESYSTEM_DECL boost::filesystem::system_error_type
get_current_path_api( std::wstring & ph );
BOOST_FILESYSTEM_DECL query_pair
create_directory_api( const std::wstring & ph );
# ifdef BOOST_FS_HARD_LINK
BOOST_FILESYSTEM_DECL boost::filesystem::system_error_type
create_hard_link_api( const std::wstring & existing_ph,
const std::wstring & new_ph );
# endif
BOOST_FILESYSTEM_DECL boost::filesystem::system_error_type
create_symlink_api( const std::wstring & to_ph,
const std::wstring & from_ph );
BOOST_FILESYSTEM_DECL boost::filesystem::system_error_type
remove_api( const std::wstring & ph );
BOOST_FILESYSTEM_DECL boost::filesystem::system_error_type
rename_api( const std::wstring & from, const std::wstring & to );
BOOST_FILESYSTEM_DECL boost::filesystem::system_error_type
copy_file_api( const std::wstring & from, const std::wstring & to );
# endif
# endif
template<class Path>
unsigned long remove_all_aux( const Path & ph );
} // namespace detail
// operations functions ----------------------------------------------------//
// The non-template overloads enable automatic conversion from std and
// C-style strings. See basic_path constructors. The enable_if for the
// templates implements the famous "do-the-right-thing" rule.
// query functions ---------------------------------------------------------//
BOOST_INLINE_FS_FUNC(file_status)
status( const Path & ph, system_error_type & ec )
{ return detail::status_api( ph.external_file_string(), ec ); }
BOOST_FS_FUNC(file_status)
status( const Path & ph )
{
system_error_type ec;
file_status result( detail::status_api( ph.external_file_string(), ec ) );
if ( ec )
boost::throw_exception( basic_filesystem_error<Path>(
"boost::filesystem::status", ph, ec ) );
return result;
}
BOOST_INLINE_FS_FUNC(file_status)
symlink_status( const Path & ph, system_error_type & ec )
# ifdef BOOST_WINDOWS_API
{ return detail::status_api( ph.external_file_string(), ec ); }
# else
{ return detail::symlink_status_api( ph.external_file_string(), ec ); }
# endif
BOOST_FS_FUNC(file_status)
symlink_status( const Path & ph )
{
system_error_type ec;
file_status result( symlink_status( ph, ec ) );
if ( ec )
boost::throw_exception( basic_filesystem_error<Path>(
"boost::filesystem::symlink_status", ph, ec ) );
return result;
}
# ifndef BOOST_FILESYSTEM_NO_DEPRECATED
inline bool symbolic_link_exists( const path & ph )
{ return is_symlink( symlink_status(ph) ); }
#endif
BOOST_FS_FUNC(bool) exists( const Path & ph )
{
system_error_type ec;
file_status result( detail::status_api( ph.external_file_string(), ec ) );
if ( ec )
boost::throw_exception( basic_filesystem_error<Path>(
"boost::filesystem::exists", ph, ec ) );
return exists( result );
}
BOOST_FS_FUNC(bool) is_directory( const Path & ph )
{
system_error_type ec;
file_status result( detail::status_api( ph.external_file_string(), ec ) );
if ( ec )
boost::throw_exception( basic_filesystem_error<Path>(
"boost::filesystem::is_directory", ph, ec ) );
return is_directory( result );
}
BOOST_FS_FUNC(bool) is_regular( const Path & ph )
{
system_error_type ec;
file_status result( detail::status_api( ph.external_file_string(), ec ) );
if ( ec )
boost::throw_exception( basic_filesystem_error<Path>(
"boost::filesystem::is_regular", ph, ec ) );
return is_regular( result );
}
BOOST_FS_FUNC(bool) is_other( const Path & ph )
{
system_error_type ec;
file_status result( detail::status_api( ph.external_file_string(), ec ) );
if ( ec )
boost::throw_exception( basic_filesystem_error<Path>(
"boost::filesystem::is_other", ph, ec ) );
return is_other( result );
}
BOOST_FS_FUNC(bool) is_symlink(
# ifdef BOOST_WINDOWS_API
const Path & )
{
return false;
# else
const Path & ph)
{
system_error_type ec;
file_status result( detail::symlink_status_api( ph.external_file_string(), ec ) );
if ( ec )
boost::throw_exception( basic_filesystem_error<Path>(
"boost::filesystem::is_symlink", ph, ec ) );
return is_symlink( result );
# endif
}
// VC++ 7.0 and earlier has a serious namespace bug that causes a clash
// between boost::filesystem::is_empty and the unrelated type trait
// boost::is_empty.
# if !defined( BOOST_MSVC ) || BOOST_MSVC > 1300
BOOST_FS_FUNC(bool) is_empty( const Path & ph )
# else
BOOST_FS_FUNC(bool) _is_empty( const Path & ph )
# endif
{
detail::query_pair result = detail::is_empty_api( ph.external_file_string() );
if ( result.first != 0 )
boost::throw_exception( basic_filesystem_error<Path>(
"boost::filesystem::is_empty", ph, result.first ) );
return result.second;
}
BOOST_FS_FUNC(bool) equivalent( const Path & ph1, const Path & ph2 )
{
detail::query_pair result = detail::equivalent_api(
ph1.external_file_string(), ph2.external_file_string() );
if ( result.first != 0 )
boost::throw_exception( basic_filesystem_error<Path>(
"boost::filesystem::equivalent", ph1, ph2, result.first ) );
return result.second;
}
BOOST_FS_FUNC(boost::uintmax_t) file_size( const Path & ph )
{
detail::uintmax_pair result
= detail::file_size_api( ph.external_file_string() );
if ( result.first != 0 )
boost::throw_exception( basic_filesystem_error<Path>(
"boost::filesystem::file_size", ph, result.first ) );
return result.second;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -