peerplays-fc/include/fc/filesystem.hpp

225 lines
6.8 KiB
C++
Raw Normal View History

2012-12-16 05:31:43 +00:00
#pragma once
#include <utility>
2012-09-08 21:37:25 +00:00
#include <fc/string.hpp>
#include <fc/reflect/typename.hpp>
#include <fc/optional.hpp>
2012-09-08 21:37:25 +00:00
#include <fc/fwd.hpp>
namespace boost {
namespace filesystem {
class path;
2012-10-10 01:40:29 +00:00
class directory_iterator;
2012-12-19 17:23:12 +00:00
class recursive_directory_iterator;
2012-09-08 21:37:25 +00:00
}
}
2012-09-08 06:41:28 +00:00
namespace fc {
/**
* @brief wraps boost::filesystem::path to provide platform independent path manipulation.
*
* Most calls are simply a passthrough to boost::filesystem::path, however exceptions are
* wrapped in an fc::error_report and the additional helper method fc::path::windows_string(),
* can be used to calculate paths intended for systems different than the host.
*
* @note Serializes to a fc::value() as the result of generic_string()
*/
2012-09-08 21:37:25 +00:00
class path {
public:
path();
~path();
path( const boost::filesystem::path& );
path( const fc::string& p );
/// Constructor to build path using unicode native characters.
path(const std::wstring& p);
2012-09-09 03:46:19 +00:00
path( const char* );
2012-09-08 21:37:25 +00:00
path( const path& p );
path( path&& p );
path& operator =( const path& );
path& operator =( path&& );
path& operator /=( const fc::path& );
friend path operator /( const fc::path& p, const fc::path& );
2012-09-14 04:56:20 +00:00
friend bool operator ==( const fc::path& p, const fc::path& );
friend bool operator !=( const fc::path& p, const fc::path& );
friend bool operator < ( const fc::path& p, const fc::path& );
2012-09-08 21:37:25 +00:00
operator boost::filesystem::path& ();
operator const boost::filesystem::path& ()const;
void replace_extension( const fc::path& e );
fc::path stem()const;
fc::path extension()const;
fc::path filename()const;
fc::path parent_path()const;
fc::string string()const;
fc::string generic_string()const;
/** On windows, returns a path where all path separators are '\' suitable for displaying
* to users. On other platforms, it does the same as generic_string()
*/
fc::string preferred_string() const;
std::wstring wstring() const;
std::wstring generic_wstring() const;
std::wstring preferred_wstring() const;
/** Retrieves native string path representation and next converts it into
ANSI UTF-8 representation.
It is needed since not all parts of fc library accept unicode paths
(fc::file_mapping).
*/
2014-01-14 08:56:15 +00:00
std::string to_native_ansi_path() const;
/**
* @brief replaces '/' with '\' in the result of generic_string()
*
* @note not part of boost::filesystem::path
*/
fc::string windows_string()const;
2012-12-29 17:00:19 +00:00
bool is_relative()const;
bool is_absolute()const;
static char separator_char;
2012-09-08 21:37:25 +00:00
private:
2013-11-13 19:35:12 +00:00
#ifdef _WIN64
fwd<boost::filesystem::path,40> _p;
#else
2012-12-03 19:51:31 +00:00
fwd<boost::filesystem::path,32> _p;
2013-11-13 19:35:12 +00:00
#endif
2012-09-08 21:37:25 +00:00
};
2014-06-20 14:38:21 +00:00
namespace detail
{
class path_wrapper
{
public:
path_wrapper(path p) :
_path(p)
{
}
const path* operator->() const
{
return &_path;
}
private:
path _path;
};
}
2012-10-10 01:40:29 +00:00
class directory_iterator {
public:
directory_iterator( const fc::path& p );
directory_iterator();
~directory_iterator();
fc::path operator*()const;
2014-06-20 14:38:21 +00:00
detail::path_wrapper operator->() const;
2012-10-10 01:40:29 +00:00
directory_iterator& operator++(int);
directory_iterator& operator++();
friend bool operator==( const directory_iterator&, const directory_iterator& );
friend bool operator!=( const directory_iterator&, const directory_iterator& );
private:
fwd<boost::filesystem::directory_iterator,16> _p;
2012-10-10 01:40:29 +00:00
};
2012-12-19 17:23:12 +00:00
class recursive_directory_iterator {
public:
recursive_directory_iterator( const fc::path& p );
recursive_directory_iterator();
~recursive_directory_iterator();
fc::path operator*()const;
recursive_directory_iterator& operator++(int);
recursive_directory_iterator& operator++();
void pop();
int level();
2012-12-19 17:23:12 +00:00
friend bool operator==( const recursive_directory_iterator&, const recursive_directory_iterator& );
friend bool operator!=( const recursive_directory_iterator&, const recursive_directory_iterator& );
private:
fwd<boost::filesystem::recursive_directory_iterator,16> _p;
};
2012-10-10 01:40:29 +00:00
2012-09-14 04:56:20 +00:00
bool exists( const path& p );
bool is_directory( const path& p );
2012-10-10 01:40:29 +00:00
bool is_regular_file( const path& p );
2012-09-14 04:56:20 +00:00
void create_directories( const path& p );
2012-12-20 20:39:35 +00:00
void remove_all( const path& p );
2012-12-12 18:26:41 +00:00
path absolute( const path& p );
path make_relative(const path& from, const path& to);
2012-11-12 03:04:24 +00:00
path canonical( const path& p );
2012-12-20 20:39:35 +00:00
uint64_t file_size( const path& p );
2012-11-15 16:55:36 +00:00
bool remove( const path& p );
void copy( const path& from, const path& to );
void rename( const path& from, const path& to );
2013-07-13 02:05:38 +00:00
void resize_file( const path& file, size_t s );
2012-12-19 17:23:12 +00:00
void create_hard_link( const path& from, const path& to );
path unique_path();
path temp_directory_path();
/** @return the home directory on Linux and OS X and the Profile directory on Windows */
const path& home_path();
/** @return the home_path() on Linux, home_path()/Library/Application Support/ on OS X,
* and APPDATA on windows
*/
const path& app_path();
/** @return application executable path */
const fc::path& current_path();
class variant;
void to_variant( const fc::path&, fc::variant& );
void from_variant( const fc::variant& , fc::path& );
template<> struct get_typename<path> { static const char* name() { return "path"; } };
/**
* Class which creates a temporary directory inside an existing temporary directory.
*/
class temp_file_base
{
public:
inline ~temp_file_base() { remove(); }
inline operator bool() const { return _path.valid(); }
inline bool operator!() const { return !_path; }
const fc::path& path() const;
void remove();
void release();
protected:
typedef fc::optional<fc::path> path_t;
inline temp_file_base(const path_t& path) : _path(path) {}
inline temp_file_base(path_t&& path) : _path(std::move(path)) {}
path_t _path;
};
/**
* Class which creates a temporary directory inside an existing temporary directory.
*/
class temp_file : public temp_file_base
{
public:
temp_file(temp_file&& other);
temp_file& operator=(temp_file&& other);
temp_file(const fc::path& tempFolder = fc::temp_directory_path(), bool create = false);
};
/**
* Class which creates a temporary directory inside an existing temporary directory.
*/
2013-07-05 23:48:59 +00:00
class temp_directory : public temp_file_base
{
public:
temp_directory(temp_directory&& other);
temp_directory& operator=(temp_directory&& other);
temp_directory(const fc::path& tempFolder = fc::temp_directory_path());
};
2012-09-08 06:41:28 +00:00
}