update experimental::filesystem

This commit is contained in:
Jinhao
2015-12-29 01:11:03 +08:00
parent 7cc173022a
commit 443b3dcd87
5 changed files with 300 additions and 195 deletions

View File

@@ -36,6 +36,7 @@
#include <memory> #include <memory>
#include <chrono> #include <chrono>
#include <cstddef> #include <cstddef>
#include <cstdint>
#include <nana/deploy.hpp> #include <nana/deploy.hpp>
@@ -55,25 +56,27 @@ namespace nana { namespace experimental
{ {
namespace filesystem namespace filesystem
{ {
enum class file_type enum class file_type
{ {
none = 0, ///< has not been determined or an error occurred while trying to determine none = 0, ///< has not been determined or an error occurred while trying to determine
not_found = -1, ///< Pseudo-type: file was not found. Is not considered an error not_found = -1, ///< Pseudo-type: file was not found. Is not considered an error
regular = 1, regular = 1,
directory = 2 , directory = 2 ,
symlink =3, ///< Symbolic link file symlink =3, ///< Symbolic link file
block =4, ///< Block special file block =4, ///< Block special file
character= 5 , ///< Character special file character= 5 , ///< Character special file
fifo = 6 , ///< FIFO or pipe file fifo = 6 , ///< FIFO or pipe file
socket =7, socket =7,
unknown= 8 ///< The file does exist, but is of an operating system dependent type not covered by any of the other unknown= 8 ///< The file does exist, but is of an operating system dependent type not covered by any of the other
}; };
enum class perms enum class perms
{ {
none =0, ///< There are no permissions set for the file. none = 0, ///< There are no permissions set for the file.
unknown = 0xFFFF ///< not known, such as when a file_status object is created without specifying the permissions all = 0x1FF, ///< owner_all | group_all | others_all
}; mask = 0xFFF, ///< all | set_uid | set_gid | sticky_bit.
unknown = 0xFFFF ///< not known, such as when a file_status object is created without specifying the permissions
};
//enum class copy_options; //enum class copy_options;
//enum class directory_options; //enum class directory_options;
@@ -96,35 +99,28 @@ namespace filesystem
uintmax_t free; uintmax_t free;
uintmax_t available; uintmax_t available;
}; };
using file_time_type = std::chrono::time_point< std::chrono::system_clock>;// trivial-clock> ;
class file_status using file_time_type = std::chrono::time_point< std::chrono::system_clock>;// trivial-clock> ;
{
file_type m_ft = file_type::none;
perms m_prms = perms::unknown;
public: class file_status
explicit file_status(file_type ft = file_type::none, perms prms = perms::unknown) {
:m_ft{ft}, m_prms{prms} file_type m_ft = file_type::none;
{} perms m_prms = perms::unknown;
file_status(const file_status& fs) : m_ft{fs.m_ft}, m_prms{fs.m_prms}{} // = default; public:
file_status(file_status&& fs) : m_ft{fs.m_ft}, m_prms{fs.m_prms}{} // = default; explicit file_status(file_type ft = file_type::none, perms prms = perms::unknown);
~file_status(){}; // observers
file_status& operator=(const file_status&) = default; file_type type() const;
file_status& operator=(file_status&&fs) // = default; perms permissions() const;
{
m_ft=fs.m_ft; m_prms = fs.m_prms; // modifiers
return *this; void type(file_type ft);
} void permissions(perms prms);
// observers private:
file_type type() const { return m_ft;} file_type value_;
perms permissions() const { return m_prms;} perms perms_;
// modifiers };
void type (file_type ft) { m_ft=ft ;}
void permissions(perms prms) { m_prms = prms; }
};
/// concerned only with lexical and syntactic aspects and does not necessarily exist in /// concerned only with lexical and syntactic aspects and does not necessarily exist in
/// external storage, and the pathname is not necessarily valid for the current operating system /// external storage, and the pathname is not necessarily valid for the current operating system
@@ -418,11 +414,11 @@ namespace filesystem
} }
// file_status status(const path& p); file_status status(const path& p);
bool file_attrib(const nana::string& file, attribute&); bool file_attrib(const nana::string& file, attribute&);
inline bool is_directory(file_status s) { return s.type() == file_type::directory ;} inline bool is_directory(file_status s) { return s.type() == file_type::directory ;}
inline bool is_directory(const path& p) { return directory_iterator(p)->attr.directory; }//works?? bool is_directory(const path& p);
inline bool is_directory(const directory_entry& d) { return d.attr.directory; } inline bool is_directory(const directory_entry& d) { return d.attr.directory; }
//bool is_directory(const path& p, error_code& ec) noexcept; //bool is_directory(const path& p, error_code& ec) noexcept;
@@ -436,35 +432,34 @@ namespace filesystem
} }
//bool is_empty(const path& p, error_code& ec) noexcept; //bool is_empty(const path& p, error_code& ec) noexcept;
uintmax_t file_size(const nana::string& file); // deprecate? std::uintmax_t file_size(const path& p);
inline uintmax_t file_size(const path& p){return file_size(p.filename());} //uintmax_t file_size(const path& p, error_code& ec) noexcept;
//uintmax_t file_size(const path& p, error_code& ec) noexcept;
//long long filesize(const nana::string& file); //long long filesize(const nana::string& file);
bool create_directories(const path& p); bool create_directories(const path& p);
//bool create_directories(const path& p, error_code& ec) noexcept; //bool create_directories(const path& p, error_code& ec) noexcept;
bool create_directory(const path& p); bool create_directory(const path& p);
//bool create_directory(const path& p, error_code& ec) noexcept; //bool create_directory(const path& p, error_code& ec) noexcept;
bool create_directory(const path& p, const path& attributes); bool create_directory(const path& p, const path& attributes);
//bool create_directory(const path& p, const path& attributes, error_code& ec) noexcept; //bool create_directory(const path& p, const path& attributes, error_code& ec) noexcept;
bool create_directory(const std::wstring& p, bool & if_exist);
/*
bool create_directory(const std::wstring& p, bool & if_exist); //deprecated
inline bool create_directory(const path& p, bool & if_exist) inline bool create_directory(const path& p, bool & if_exist)
{ {
return create_directory(p.filename(), if_exist); return create_directory(p.filename(), if_exist);
}; }
*/
bool modified_file_time(const std::wstring& file, struct tm&);
bool modified_file_time(const std::wstring& file, struct tm&);
path path_user(); path path_user();
path current_path();
path current_path(); //path current_path(error_code& ec);
//path current_path(error_code& ec); void current_path(const path& p);
void current_path(const path& p); //void current_path(const path& p, error_code& ec) noexcept;
//void current_path(const path& p, error_code& ec) noexcept; //nana::string path_current();
//nana::string path_current();
//bool remove(const path& p); //bool remove(const path& p);

View File

@@ -25,9 +25,9 @@ namespace filesystem
}; };
bool file_attrib(const ::std::string& file, attribute&); bool file_attrib(const ::std::string& file, attribute&);
long long filesize(const nana::string& file); //long long filesize(const nana::string& file); //deprecated
bool mkdir(const ::std::string& dir, bool & if_exist); //bool mkdir(const ::std::string& dir, bool & if_exist); //deprecated
bool modified_file_time(const ::std::string& file, struct tm&); bool modified_file_time(const ::std::string& file, struct tm&);
std::wstring path_user(); std::wstring path_user();
@@ -37,6 +37,7 @@ namespace filesystem
bool rmdir(const char* dir, bool fails_if_not_empty); bool rmdir(const char* dir, bool fails_if_not_empty);
nana::string root(const nana::string& path); nana::string root(const nana::string& path);
/*
class path class path
{ {
public: public:
@@ -59,6 +60,7 @@ namespace filesystem
std::string text_; std::string text_;
#endif #endif
}; };
*/
}//end namespace filesystem }//end namespace filesystem
}//end namespace nana }//end namespace nana

View File

@@ -36,160 +36,184 @@
#include <stdlib.h> #include <stdlib.h>
#endif #endif
namespace nana { namespace nana { namespace experimental {
namespace experimental namespace filesystem
{ {
namespace filesystem //Because of No wide character version of POSIX
{
//Because of No wide character version of POSIX
#if defined(NANA_LINUX) || defined(NANA_MACOS) #if defined(NANA_LINUX) || defined(NANA_MACOS)
const char* splstr = "/"; const char* splstr = "/";
#else #else
const wchar_t* splstr = L"/\\"; const wchar_t* splstr = L"/\\";
#endif #endif
//class path //class file_status
path::path() {} file_status::file_status(file_type ft, perms prms)
: value_{ft}, perms_{prms}
{}
int path::compare(const path& p) const file_type file_status::type() const
{ {
return pathstr_.compare(p.pathstr_); return value_;
} }
bool path::empty() const void file_status::type(file_type ft)
{ {
value_ = ft;
}
perms file_status::permissions() const
{
return perms_;
}
void file_status::permissions(perms prms)
{
perms_ = prms;
}
//end filestatus
//class path
path::path() {}
int path::compare(const path& p) const
{
return pathstr_.compare(p.pathstr_);
}
bool path::empty() const
{
#if defined(NANA_WINDOWS) #if defined(NANA_WINDOWS)
return (::GetFileAttributes(pathstr_.c_str()) == INVALID_FILE_ATTRIBUTES); return (::GetFileAttributes(pathstr_.c_str()) == INVALID_FILE_ATTRIBUTES);
#elif defined(NANA_LINUX) || defined(NANA_MACOS) #elif defined(NANA_LINUX) || defined(NANA_MACOS)
struct stat sta; struct stat sta;
return (::stat(pathstr_.c_str(), &sta) == -1); return (::stat(pathstr_.c_str(), &sta) == -1);
#endif #endif
} }
path path::extension() const path path::extension() const
{ {
#if defined(NANA_WINDOWS) #if defined(NANA_WINDOWS)
auto pos = pathstr_.find_last_of(L"\\/."); auto pos = pathstr_.find_last_of(L"\\/.");
#else #else
auto pos = pathstr_.find_last_of("\\/."); auto pos = pathstr_.find_last_of("\\/.");
#endif #endif
if ((pos == pathstr_.npos) || (pathstr_[pos] != '.')) if ((pos == pathstr_.npos) || (pathstr_[pos] != '.'))
return path(); return path();
if (pos + 1 == pathstr_.size()) if (pos + 1 == pathstr_.size())
return path(); return path();
return path(pathstr_.substr(pos)); return path(pathstr_.substr(pos));
} }
path path::parent_path() const path path::parent_path() const
{ {
return{filesystem::parent_path(pathstr_)}; return{filesystem::parent_path(pathstr_)};
} }
file_type path::what() const file_type path::what() const
{ {
#if defined(NANA_WINDOWS) #if defined(NANA_WINDOWS)
unsigned long attr = ::GetFileAttributes(pathstr_.c_str()); unsigned long attr = ::GetFileAttributes(pathstr_.c_str());
if (INVALID_FILE_ATTRIBUTES == attr) if (INVALID_FILE_ATTRIBUTES == attr)
return file_type::not_found; //?? return file_type::not_found; //??
if (FILE_ATTRIBUTE_DIRECTORY & attr) if (FILE_ATTRIBUTE_DIRECTORY & attr)
return file_type::directory; return file_type::directory;
return file_type::regular; return file_type::regular;
#elif defined(NANA_LINUX) || defined(NANA_MACOS) #elif defined(NANA_LINUX) || defined(NANA_MACOS)
struct stat sta; struct stat sta;
if (-1 == ::stat(pathstr_.c_str(), &sta)) if (-1 == ::stat(pathstr_.c_str(), &sta))
return file_type::not_found; //?? return file_type::not_found; //??
if ((S_IFDIR & sta.st_mode) == S_IFDIR) if ((S_IFDIR & sta.st_mode) == S_IFDIR)
return file_type::directory; return file_type::directory;
if ((S_IFREG & sta.st_mode) == S_IFREG) if ((S_IFREG & sta.st_mode) == S_IFREG)
return file_type::regular; return file_type::regular;
return file_type::none; return file_type::none;
#endif #endif
} }
path path::filename() const path path::filename() const
{
auto pos = pathstr_.find_last_of(splstr);
if (pos != pathstr_.npos)
{ {
auto pos = pathstr_.find_last_of(splstr); if (pos + 1 == pathstr_.size())
if (pos != pathstr_.npos)
{ {
if (pos + 1 == pathstr_.size()) value_type tmp[2] = {preferred_separator, 0};
{
value_type tmp[2] = {preferred_separator, 0};
if (pathstr_.npos != pathstr_.find_last_not_of(splstr, pos)) if (pathstr_.npos != pathstr_.find_last_not_of(splstr, pos))
tmp[0] = '.'; tmp[0] = '.';
return{ tmp }; return{ tmp };
}
return{ pathstr_.substr(pos + 1) };
} }
return{ pathstr_.substr(pos + 1) };
return{ pathstr_ };
} }
const path::value_type* path::c_str() const return{ pathstr_ };
{ }
return native().c_str();
}
const path::string_type& path::native() const const path::value_type* path::c_str() const
{ {
return pathstr_; return native().c_str();
} }
const path::string_type& path::native() const
{
return pathstr_;
}
path::operator string_type() const path::operator string_type() const
{ {
return native(); return native();
} }
void path::_m_assign(const std::string& source_utf8) void path::_m_assign(const std::string& source_utf8)
{ {
#if defined(NANA_WINDOWS) #if defined(NANA_WINDOWS)
pathstr_ = utf8_cast(source_utf8); pathstr_ = utf8_cast(source_utf8);
#else #else
pathstr_ = source_utf8; pathstr_ = source_utf8;
#endif #endif
} }
void path::_m_assign(const std::wstring& source) void path::_m_assign(const std::wstring& source)
{ {
#if defined(NANA_WINDOWS) #if defined(NANA_WINDOWS)
pathstr_ = source; pathstr_ = source;
#else #else
pathstr_ = utf8_cast(source); pathstr_ = utf8_cast(source);
#endif #endif
} }
//end class path //end class path
bool operator==(const path& lhs, const path& rhs) bool operator==(const path& lhs, const path& rhs)
{ {
return (lhs.compare(rhs) == 0); return (lhs.compare(rhs) == 0);
} }
bool operator!=(const path& lhs, const path& rhs) bool operator!=(const path& lhs, const path& rhs)
{ {
return (lhs.native() != rhs.native()); return (lhs.native() != rhs.native());
} }
bool operator<(const path& lhs, const path& rhs) bool operator<(const path& lhs, const path& rhs)
{ {
return (lhs.compare(rhs) < 0); return (lhs.compare(rhs) < 0);
} }
bool operator>(const path& lhs, const path& rhs) bool operator>(const path& lhs, const path& rhs)
{ {
return (rhs.compare(lhs) < 0); return (rhs.compare(lhs) < 0);
} }
namespace detail namespace detail
{ {
//rm_dir_recursive //rm_dir_recursive
//@brief: remove a directory, if it is not empty, recursively remove it's subfiles and sub directories //@brief: remove a directory, if it is not empty, recursively remove it's subfiles and sub directories
template<typename CharT> template<typename CharT>
@@ -257,6 +281,51 @@ namespace nana {
#endif #endif
}//end namespace detail }//end namespace detail
file_status status(const path& p)
{
#if defined(NANA_WINDOWS)
auto attr = ::GetFileAttributesW(p.c_str());
if(INVALID_FILE_ATTRIBUTES == attr)
return file_status{file_type::unknown};
return file_status{(FILE_ATTRIBUTE_DIRECTORY & attr) ? file_type::directory : file_type::regular, perms::all};
#elif defined(NANA_POSIX)
struct stat path_stat;
if(0 != ::stat(p.c_str(), &path_stat))
{
if(errno == ENOENT || errno == ENOTDIR)
return file_status{file_type::not_found};
return file_status{file_type::unknown};
}
auto prms = static_cast<perms>(path_stat.st_mode & static_cast<unsigned>(perms::mask));
if(S_ISREG(path_stat.st_mode))
return file_status{file_type::regular, prms};
if(S_ISDIR(path_stat.st_mode))
return file_status{file_type::directory, prms};
if(S_ISLNK(path_stat.st_mode))
return file_status{file_type::symlink, prms};
if(S_ISBLK(path_stat.st_mode))
return file_status{file_type::block, prms};
if(S_ISCHR(path_stat.st_mode))
return file_status{file_type::character, prms};
if(S_ISFIFO(path_stat.st_mode))
return file_status{file_type::fifo, prms};
if(S_ISSOCK(path_stat.st_mode))
return file_status{file_type::socket, prms};
return file_status{file_type::unknown};
#endif
}
bool file_attrib(const nana::string& file, attribute& attr) bool file_attrib(const nana::string& file, attribute& attr)
{ {
#if defined(NANA_WINDOWS) #if defined(NANA_WINDOWS)
@@ -284,7 +353,12 @@ namespace nana {
return false; return false;
} }
uintmax_t file_size(const nana::string& file) bool is_directory(const path& p)
{
return (status(p).type() == file_type::directory);
}
std::uintmax_t file_size(const path& p)
{ {
#if defined(NANA_WINDOWS) #if defined(NANA_WINDOWS)
//Some compilation environment may fail to link to GetFileSizeEx //Some compilation environment may fail to link to GetFileSizeEx
@@ -292,7 +366,7 @@ namespace nana {
GetFileSizeEx_fptr_t get_file_size_ex = reinterpret_cast<GetFileSizeEx_fptr_t>(::GetProcAddress(::GetModuleHandleA("Kernel32.DLL"), "GetFileSizeEx")); GetFileSizeEx_fptr_t get_file_size_ex = reinterpret_cast<GetFileSizeEx_fptr_t>(::GetProcAddress(::GetModuleHandleA("Kernel32.DLL"), "GetFileSizeEx"));
if (get_file_size_ex) if (get_file_size_ex)
{ {
HANDLE handle = ::CreateFile(file.c_str(), GENERIC_READ, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0); HANDLE handle = ::CreateFile(p.c_str(), GENERIC_READ, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
if (INVALID_HANDLE_VALUE != handle) if (INVALID_HANDLE_VALUE != handle)
{ {
LARGE_INTEGER li; LARGE_INTEGER li;
@@ -304,26 +378,21 @@ namespace nana {
} }
} }
return 0; return 0;
#elif defined(NANA_LINUX) #elif defined(NANA_POSIX)
FILE * stream = ::fopen(static_cast<std::string>(nana::charset(file)).c_str(), "rb"); FILE * stream = ::fopen(p.c_str(), "rb");
long long size = 0; long long size = 0;
if (stream) if (stream)
{ {
# if defined(NANA_LINUX)
fseeko64(stream, 0, SEEK_END); fseeko64(stream, 0, SEEK_END);
size = ftello64(stream); size = ftello64(stream);
fclose(stream); # elif defined(NANA_MACOS)
}
return size;
#elif defined(NANA_MACOS)
FILE * stream = ::fopen(static_cast<std::string>(nana::charset(file)).c_str(), "rb");
long long size = 0;
if (stream)
{
fseeko(stream, 0, SEEK_END); fseeko(stream, 0, SEEK_END);
size = ftello(stream); size = ftello(stream);
fclose(stream); # endif
::fclose(stream);
} }
return size; return size;
#endif #endif
} }
@@ -361,7 +430,18 @@ namespace nana {
return false; return false;
} }
bool create_directory(const std::wstring& path, bool & if_exist)
bool create_directory(const path& p)
{
#if defined(NANA_WINDOWS)
return (FALSE != ::CreateDirectoryW(p.c_str(), 0));
#elif defined(NANA_POSIX)
return (0 == ::mkdir(p.c_str(), static_cast<int>(perms::all)));
#endif
}
/*
bool create_directory(const std::wstring& path, bool & if_exist) //deprecated
{ {
if_exist = false; if_exist = false;
if (path.size() == 0) return false; if (path.size() == 0) return false;
@@ -411,7 +491,7 @@ namespace nana {
} }
return mkstat; return mkstat;
} }
*/
bool rmfile(const path& p) bool rmfile(const path& p)
{ {

View File

@@ -47,10 +47,11 @@ namespace filesystem
typedef std::string string_t; typedef std::string string_t;
const char* splstr = "/\\"; const char* splstr = "/\\";
#else #else
typedef nana::string string_t; typedef std::wstring string_t;
const nana::char_t* splstr = L"/\\"; const wchar_t* splstr = L"/\\";
#endif #endif
/*
//class path //class path
path::path(){} path::path(){}
@@ -122,7 +123,7 @@ namespace filesystem
#endif #endif
} }
//end class path //end class path
*/
namespace detail namespace detail
{ {
@@ -219,7 +220,8 @@ namespace filesystem
return false; return false;
} }
long long filesize(const std::string& file) /*
long long filesize(const std::string& file) //deprecated
{ {
#if defined(NANA_WINDOWS) #if defined(NANA_WINDOWS)
//Some compilation environment may fail to link to GetFileSizeEx //Some compilation environment may fail to link to GetFileSizeEx
@@ -256,6 +258,7 @@ namespace filesystem
return size; return size;
#endif #endif
} }
*/
bool modified_file_time(const ::std::string& file, struct tm& t) bool modified_file_time(const ::std::string& file, struct tm& t)
{ {
@@ -291,7 +294,8 @@ namespace filesystem
return false; return false;
} }
bool mkdir(const std::string& path, bool & if_exist) /*
bool mkdir(const std::string& path, bool & if_exist) //deprecated
{ {
if_exist = false; if_exist = false;
if(path.size() == 0) return false; if(path.size() == 0) return false;
@@ -341,6 +345,7 @@ namespace filesystem
} }
return mkstat; return mkstat;
} }
*/
bool rmfile(const char* file) bool rmfile(const char* file)
{ {

View File

@@ -13,6 +13,7 @@
#include <nana/gui.hpp> #include <nana/gui.hpp>
#include <nana/gui/filebox.hpp> #include <nana/gui/filebox.hpp>
#include <nana/filesystem/fs_utility.hpp> #include <nana/filesystem/fs_utility.hpp>
#include <nana/filesystem/filesystem.hpp>
#if defined(NANA_WINDOWS) #if defined(NANA_WINDOWS)
#include <windows.h> #include <windows.h>
@@ -644,9 +645,30 @@ namespace nana
mb(); mb();
return; return;
} }
using file_type = nana::experimental::filesystem::file_type;
experimental::filesystem::path fspath(fb_.addr_.filesystem + path);
auto fs = experimental::filesystem::status(fspath);
if(fs.type() != file_type::not_found && fs.type() != file_type::none)
{
mb<<L"The folder is existing, please rename it.";
mb();
return;
}
if(false == experimental::filesystem::create_directory(fspath))
{
mb<<L"Failed to create the folder, please rename it.";
mb();
return;
}
/*
bool if_exist; bool if_exist;
if(false == nana::filesystem::mkdir(fb_.addr_.filesystem + path, if_exist)) if(false == nana::filesystem::mkdir(fb_.addr_.filesystem + path, if_exist)) //deprecated
{ {
if(if_exist) if(if_exist)
mb<<L"The folder is existing, please rename it."; mb<<L"The folder is existing, please rename it.";
@@ -655,6 +677,7 @@ namespace nana
mb(); mb();
return; return;
} }
*/
fb_._m_load_cat_path(fb_.addr_.filesystem); fb_._m_load_cat_path(fb_.addr_.filesystem);
fm_.close(); fm_.close();
} }