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 <chrono>
#include <cstddef>
#include <cstdint>
#include <nana/deploy.hpp>
@@ -72,6 +73,8 @@ namespace filesystem
enum class perms
{
none = 0, ///< There are no permissions set for the file.
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;
@@ -96,6 +99,7 @@ namespace filesystem
uintmax_t free;
uintmax_t available;
};
using file_time_type = std::chrono::time_point< std::chrono::system_clock>;// trivial-clock> ;
class file_status
@@ -104,26 +108,18 @@ namespace filesystem
perms m_prms = perms::unknown;
public:
explicit file_status(file_type ft = file_type::none, perms prms = perms::unknown)
:m_ft{ft}, m_prms{prms}
{}
explicit file_status(file_type ft = file_type::none, perms prms = perms::unknown);
file_status(const file_status& fs) : m_ft{fs.m_ft}, m_prms{fs.m_prms}{} // = default;
file_status(file_status&& fs) : m_ft{fs.m_ft}, m_prms{fs.m_prms}{} // = default;
~file_status(){};
file_status& operator=(const file_status&) = default;
file_status& operator=(file_status&&fs) // = default;
{
m_ft=fs.m_ft; m_prms = fs.m_prms;
return *this;
}
// observers
file_type type() const { return m_ft;}
perms permissions() const { return m_prms;}
file_type type() const;
perms permissions() const;
// modifiers
void type (file_type ft) { m_ft=ft ;}
void permissions(perms prms) { m_prms = prms; }
void type(file_type ft);
void permissions(perms prms);
private:
file_type value_;
perms perms_;
};
/// concerned only with lexical and syntactic aspects and does not necessarily exist in
@@ -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&);
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; }
//bool is_directory(const path& p, error_code& ec) noexcept;
@@ -436,8 +432,7 @@ namespace filesystem
}
//bool is_empty(const path& p, error_code& ec) noexcept;
uintmax_t file_size(const nana::string& file); // deprecate?
inline uintmax_t file_size(const path& p){return file_size(p.filename());}
std::uintmax_t file_size(const path& p);
//uintmax_t file_size(const path& p, error_code& ec) noexcept;
//long long filesize(const nana::string& file);
@@ -448,18 +443,18 @@ namespace filesystem
//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, 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)
{
return create_directory(p.filename(), if_exist);
};
}
*/
bool modified_file_time(const std::wstring& file, struct tm&);
path path_user();
path current_path();
//path current_path(error_code& ec);
void current_path(const path& p);

View File

@@ -25,9 +25,9 @@ namespace filesystem
};
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&);
std::wstring path_user();
@@ -37,6 +37,7 @@ namespace filesystem
bool rmdir(const char* dir, bool fails_if_not_empty);
nana::string root(const nana::string& path);
/*
class path
{
public:
@@ -59,6 +60,7 @@ namespace filesystem
std::string text_;
#endif
};
*/
}//end namespace filesystem
}//end namespace nana

View File

@@ -36,9 +36,7 @@
#include <stdlib.h>
#endif
namespace nana {
namespace experimental
{
namespace nana { namespace experimental {
namespace filesystem
{
//Because of No wide character version of POSIX
@@ -48,6 +46,32 @@ namespace nana {
const wchar_t* splstr = L"/\\";
#endif
//class file_status
file_status::file_status(file_type ft, perms prms)
: value_{ft}, perms_{prms}
{}
file_type file_status::type() const
{
return value_;
}
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() {}
@@ -257,6 +281,51 @@ namespace nana {
#endif
}//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)
{
#if defined(NANA_WINDOWS)
@@ -284,7 +353,12 @@ namespace nana {
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)
//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"));
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)
{
LARGE_INTEGER li;
@@ -304,24 +378,19 @@ namespace nana {
}
}
return 0;
#elif defined(NANA_LINUX)
FILE * stream = ::fopen(static_cast<std::string>(nana::charset(file)).c_str(), "rb");
#elif defined(NANA_POSIX)
FILE * stream = ::fopen(p.c_str(), "rb");
long long size = 0;
if (stream)
{
# if defined(NANA_LINUX)
fseeko64(stream, 0, SEEK_END);
size = ftello64(stream);
fclose(stream);
}
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);
size = ftello(stream);
fclose(stream);
# endif
::fclose(stream);
}
return size;
#endif
@@ -361,7 +430,18 @@ namespace nana {
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 (path.size() == 0) return false;
@@ -411,7 +491,7 @@ namespace nana {
}
return mkstat;
}
*/
bool rmfile(const path& p)
{

View File

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

View File

@@ -13,6 +13,7 @@
#include <nana/gui.hpp>
#include <nana/gui/filebox.hpp>
#include <nana/filesystem/fs_utility.hpp>
#include <nana/filesystem/filesystem.hpp>
#if defined(NANA_WINDOWS)
#include <windows.h>
@@ -645,8 +646,29 @@ namespace nana
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;
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)
mb<<L"The folder is existing, please rename it.";
@@ -655,6 +677,7 @@ namespace nana
mb();
return;
}
*/
fb_._m_load_cat_path(fb_.addr_.filesystem);
fm_.close();
}