update experimental::filesystem and remove file_iterator
This commit is contained in:
@@ -1,41 +0,0 @@
|
||||
/*
|
||||
* A File Iterator Implementation
|
||||
* Copyright(C) 2003-2013 Jinhao(cnjinhao@hotmail.com)(cnjinhao@hotmail.com)
|
||||
*
|
||||
* Distributed under 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)
|
||||
*
|
||||
* @file: nana/filesystem/file_iterator.cpp
|
||||
* @description:
|
||||
* file_iterator is a toolkit for applying each file and directory in a
|
||||
* specified path.
|
||||
*/
|
||||
|
||||
#include <nana/filesystem/file_iterator.hpp>
|
||||
|
||||
namespace nana
|
||||
{
|
||||
namespace filesystem
|
||||
{
|
||||
//struct fileinfo
|
||||
fileinfo::fileinfo()
|
||||
:size(0), directory(false)
|
||||
{}
|
||||
#if defined(NANA_WINDOWS)
|
||||
fileinfo::fileinfo(const WIN32_FIND_DATA& wfd)
|
||||
: name(utf8_cast(wfd.cFileName)), size(wfd.nFileSizeLow),
|
||||
directory((FILE_ATTRIBUTE_DIRECTORY & wfd.dwFileAttributes) == FILE_ATTRIBUTE_DIRECTORY)
|
||||
{
|
||||
}
|
||||
#elif defined(NANA_LINUX) || defined(NANA_MACOS)
|
||||
fileinfo::fileinfo(const ::std::string& name, const struct stat& fst)
|
||||
:name(name), size(fst.st_size), directory(0 != S_ISDIR(fst.st_mode))
|
||||
{
|
||||
}
|
||||
#endif
|
||||
//end struct fileinfo
|
||||
|
||||
}//end namespace filesystem
|
||||
}//end namespace nana
|
||||
|
||||
@@ -24,7 +24,7 @@
|
||||
|
||||
#include <shlobj.h>
|
||||
#include <nana/datetime.hpp>
|
||||
#elif defined(NANA_LINUX) || defined(NANA_MACOS)
|
||||
#elif defined(NANA_POSIX)
|
||||
#include <nana/charset.hpp>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/types.h>
|
||||
@@ -36,9 +36,38 @@
|
||||
#include <stdlib.h>
|
||||
#endif
|
||||
|
||||
|
||||
namespace nana { namespace experimental {
|
||||
namespace filesystem
|
||||
{
|
||||
//class filesystem_error
|
||||
filesystem_error::filesystem_error(const std::string& msg, std::error_code err)
|
||||
: std::system_error(err, msg)
|
||||
{}
|
||||
|
||||
filesystem_error::filesystem_error(const std::string& msg, const path& path1, std::error_code err)
|
||||
: std::system_error(err, msg),
|
||||
path1_(path1)
|
||||
{}
|
||||
|
||||
filesystem_error::filesystem_error(const std::string& msg, const path& path1, const path& path2, std::error_code err)
|
||||
: std::system_error(err, msg),
|
||||
path1_(path1),
|
||||
path2_(path2)
|
||||
{}
|
||||
|
||||
const path& filesystem_error::path1() const
|
||||
{
|
||||
return path1_;
|
||||
}
|
||||
|
||||
const path&filesystem_error::path2() const
|
||||
{
|
||||
return path2_;
|
||||
}
|
||||
//end class filesystem_error
|
||||
|
||||
|
||||
//Because of No wide character version of POSIX
|
||||
#if defined(NANA_LINUX) || defined(NANA_MACOS)
|
||||
const char* splstr = "/";
|
||||
@@ -72,9 +101,7 @@ namespace nana { namespace experimental {
|
||||
}
|
||||
//end filestatus
|
||||
|
||||
//class path
|
||||
path::path() {}
|
||||
|
||||
//class path
|
||||
int path::compare(const path& p) const
|
||||
{
|
||||
return pathstr_.compare(p.pathstr_);
|
||||
@@ -158,6 +185,41 @@ namespace nana { namespace experimental {
|
||||
return{ pathstr_ };
|
||||
}
|
||||
|
||||
path& path::remove_filename()
|
||||
{
|
||||
#ifdef NANA_WINDOWS
|
||||
wchar_t seps[] = L"/\\";
|
||||
#else
|
||||
char seps[] = "/\\";
|
||||
#endif
|
||||
auto pos = pathstr_.find_last_of(seps);
|
||||
if (pos != pathstr_.npos)
|
||||
{
|
||||
pos = pathstr_.find_last_not_of(seps, pos);
|
||||
if (pos != pathstr_.npos)
|
||||
{
|
||||
#ifdef NANA_WINDOWS
|
||||
if (pathstr_[pos] == L':')
|
||||
{
|
||||
pathstr_.erase(pos + 1);
|
||||
pathstr_ += L'\\';
|
||||
|
||||
return *this;
|
||||
}
|
||||
#endif
|
||||
++pos;
|
||||
}
|
||||
else
|
||||
pos = 0;
|
||||
}
|
||||
else
|
||||
pos = 0;
|
||||
|
||||
pathstr_.erase(pos);
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
const path::value_type* path::c_str() const
|
||||
{
|
||||
return native().c_str();
|
||||
@@ -188,6 +250,36 @@ namespace nana { namespace experimental {
|
||||
return to_utf8(pathstr_);
|
||||
}
|
||||
|
||||
path & path::operator/=(const path& p)
|
||||
{
|
||||
if (p.empty())
|
||||
return *this;
|
||||
|
||||
if (this == &p)
|
||||
{
|
||||
auto other = p.pathstr_;
|
||||
if ((other.front() != '/') && (other.front() == path::preferred_separator))
|
||||
{
|
||||
if (!this->pathstr_.empty() && (pathstr_.back() != '/' && pathstr_.back() == path::preferred_separator))
|
||||
pathstr_.append(path::preferred_separator, 1);
|
||||
}
|
||||
|
||||
pathstr_ += other;
|
||||
}
|
||||
else
|
||||
{
|
||||
auto & other = p.pathstr_;
|
||||
if ((other.front() != '/') && (other.front() == path::preferred_separator))
|
||||
{
|
||||
if (!this->pathstr_.empty() && (pathstr_.back() != '/' && pathstr_.back() == path::preferred_separator))
|
||||
pathstr_.append(path::preferred_separator, 1);
|
||||
}
|
||||
|
||||
pathstr_ += p.pathstr_;
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
void path::_m_assign(const std::string& source_utf8)
|
||||
{
|
||||
#if defined(NANA_WINDOWS)
|
||||
@@ -227,52 +319,251 @@ namespace nana { namespace experimental {
|
||||
return (rhs.compare(lhs) < 0);
|
||||
}
|
||||
|
||||
path operator/(const path& lhs, const path& rhs)
|
||||
{
|
||||
auto path = lhs;
|
||||
return (path /= rhs);
|
||||
}
|
||||
|
||||
//class directory_entry
|
||||
directory_entry::directory_entry(const filesystem::path& p)
|
||||
:path_{ p }
|
||||
{}
|
||||
|
||||
//modifiers
|
||||
void directory_entry::assign(const filesystem::path& p)
|
||||
{
|
||||
path_ = p;
|
||||
}
|
||||
|
||||
void directory_entry::replace_filename(const filesystem::path& p)
|
||||
{
|
||||
path_ = path_.parent_path() / p;
|
||||
}
|
||||
|
||||
//observers
|
||||
file_status directory_entry::status() const
|
||||
{
|
||||
return filesystem::status(path_);
|
||||
}
|
||||
|
||||
directory_entry::operator const filesystem::path&() const
|
||||
{
|
||||
return path_;
|
||||
}
|
||||
|
||||
const path& directory_entry::path() const
|
||||
{
|
||||
return path_;
|
||||
}
|
||||
//end class directory_entry
|
||||
|
||||
|
||||
//class directory_iterator
|
||||
struct inner_handle_deleter
|
||||
{
|
||||
void operator()(void** handle)
|
||||
{
|
||||
#if defined(NANA_WINDOWS)
|
||||
::FindClose(*handle);
|
||||
#elif defined(NANA_LINUX) || defined(NANA_MACOS)
|
||||
::closedir(*reinterpret_cast<DIR**>(handle));
|
||||
#endif
|
||||
}
|
||||
};
|
||||
|
||||
directory_iterator::directory_iterator()
|
||||
: end_(true),
|
||||
handle_(nullptr)
|
||||
{}
|
||||
|
||||
directory_iterator::directory_iterator(const path& file_path)
|
||||
{
|
||||
_m_prepare(file_path);
|
||||
}
|
||||
|
||||
const directory_iterator::value_type& directory_iterator::operator*() const { return value_; }
|
||||
|
||||
const directory_iterator::value_type*
|
||||
directory_iterator::operator->() const { return &(operator*()); }
|
||||
|
||||
directory_iterator& directory_iterator::operator++()
|
||||
{
|
||||
_m_read(); return *this;
|
||||
}
|
||||
|
||||
directory_iterator directory_iterator::operator++(int)
|
||||
{
|
||||
directory_iterator tmp = *this;
|
||||
_m_read();
|
||||
return tmp;
|
||||
}
|
||||
|
||||
bool directory_iterator::equal(const directory_iterator& x) const
|
||||
{
|
||||
if (end_ && (end_ == x.end_)) return true;
|
||||
return (value_.path().filename() == x.value_.path().filename());
|
||||
}
|
||||
|
||||
|
||||
// enable directory_iterator range-based for statements
|
||||
directory_iterator directory_iterator::begin() { return *this; }
|
||||
directory_iterator directory_iterator::end() { return{}; }
|
||||
|
||||
void directory_iterator::_m_prepare(const path& file_path)
|
||||
{
|
||||
path_ = file_path.native();
|
||||
#if defined(NANA_WINDOWS)
|
||||
if (!path_.empty() && (path_.back() != L'/' && path_.back() != L'\\'))
|
||||
path_ += L'\\';
|
||||
|
||||
auto pat = path_;
|
||||
DWORD attr = ::GetFileAttributes(pat.data());
|
||||
if ((attr != INVALID_FILE_ATTRIBUTES) && (attr & FILE_ATTRIBUTE_DIRECTORY))
|
||||
pat += L"*";
|
||||
|
||||
WIN32_FIND_DATAW wfd;
|
||||
::HANDLE handle = ::FindFirstFile(pat.data(), &wfd);
|
||||
|
||||
if (handle == INVALID_HANDLE_VALUE)
|
||||
{
|
||||
end_ = true;
|
||||
return;
|
||||
}
|
||||
|
||||
while (_m_ignore(wfd.cFileName))
|
||||
{
|
||||
if (::FindNextFile(handle, &wfd) == 0)
|
||||
{
|
||||
end_ = true;
|
||||
::FindClose(handle);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
value_ = value_type(path(path_ + wfd.cFileName));
|
||||
|
||||
#elif defined(NANA_POSIX)
|
||||
if (path_.size() && (path_.back() != '/'))
|
||||
path_ += '/';
|
||||
auto handle = opendir(path_.c_str());
|
||||
end_ = true;
|
||||
if (handle)
|
||||
{
|
||||
struct dirent * dnt = readdir(handle);
|
||||
if (dnt)
|
||||
{
|
||||
while (_m_ignore(dnt->d_name))
|
||||
{
|
||||
dnt = readdir(handle);
|
||||
if (dnt == 0)
|
||||
{
|
||||
closedir(handle);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
value_ = value_type(path_ + dnt->d_name);
|
||||
end_ = false;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
if (false == end_)
|
||||
{
|
||||
find_ptr_ = std::shared_ptr<find_handle>(new find_handle(handle), inner_handle_deleter());
|
||||
handle_ = handle;
|
||||
}
|
||||
}
|
||||
|
||||
void directory_iterator::_m_read()
|
||||
{
|
||||
if (handle_)
|
||||
{
|
||||
#if defined(NANA_WINDOWS)
|
||||
WIN32_FIND_DATAW wfd;
|
||||
if (::FindNextFile(handle_, &wfd) != 0)
|
||||
{
|
||||
while (_m_ignore(wfd.cFileName))
|
||||
{
|
||||
if (::FindNextFile(handle_, &wfd) == 0)
|
||||
{
|
||||
end_ = true;
|
||||
return;
|
||||
}
|
||||
}
|
||||
value_ = value_type(path(path_ + wfd.cFileName));
|
||||
}
|
||||
else
|
||||
end_ = true;
|
||||
#elif defined(NANA_POSIX)
|
||||
struct dirent * dnt = readdir(reinterpret_cast<DIR*>(handle_));
|
||||
if (dnt)
|
||||
{
|
||||
while (_m_ignore(dnt->d_name))
|
||||
{
|
||||
dnt = readdir(reinterpret_cast<DIR*>(handle_));
|
||||
if (!dnt)
|
||||
{
|
||||
end_ = true;
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
value_ = value_type(path(path_ + dnt->d_name));
|
||||
}
|
||||
else
|
||||
end_ = true;
|
||||
#endif
|
||||
}
|
||||
}
|
||||
//end class directory_iterator
|
||||
|
||||
namespace detail
|
||||
{
|
||||
//rm_dir_recursive
|
||||
//@brief: remove a directory, if it is not empty, recursively remove it's subfiles and sub directories
|
||||
template<typename CharT>
|
||||
bool rm_dir_recursive(const CharT* dir)
|
||||
//rm_dir_recursive
|
||||
//@brief: remove a directory, if it is not empty, recursively remove it's subfiles and sub directories
|
||||
template<typename CharT>
|
||||
bool rm_dir_recursive(const CharT* dir)
|
||||
{
|
||||
std::vector<directory_iterator::value_type> files;
|
||||
std::basic_string<CharT> path = dir;
|
||||
path += '\\';
|
||||
|
||||
std::copy(directory_iterator(dir), directory_iterator(), std::back_inserter(files));
|
||||
|
||||
for (auto & f : files)
|
||||
{
|
||||
std::vector<directory_iterator::value_type> files;
|
||||
std::basic_string<CharT> path = dir;
|
||||
path += '\\';
|
||||
auto subpath = path + f.path().filename().native();
|
||||
|
||||
std::copy(directory_iterator(dir), directory_iterator(), std::back_inserter(files));
|
||||
|
||||
for (auto & f : files)
|
||||
{
|
||||
auto subpath = path + f.path().filename().native();
|
||||
if (f.attr.directory)
|
||||
rm_dir_recursive(subpath.c_str());
|
||||
else
|
||||
rmfile(subpath.c_str());
|
||||
}
|
||||
|
||||
return rmdir(dir, true);
|
||||
if (is_directory(f))
|
||||
rm_dir_recursive(subpath.c_str());
|
||||
else
|
||||
rmfile(subpath.c_str());
|
||||
}
|
||||
return rmdir(dir, true);
|
||||
}
|
||||
|
||||
#if defined(NANA_WINDOWS)
|
||||
void filetime_to_c_tm(FILETIME& ft, struct tm& t)
|
||||
void filetime_to_c_tm(FILETIME& ft, struct tm& t)
|
||||
{
|
||||
FILETIME local_file_time;
|
||||
if (::FileTimeToLocalFileTime(&ft, &local_file_time))
|
||||
{
|
||||
FILETIME local_file_time;
|
||||
if (::FileTimeToLocalFileTime(&ft, &local_file_time))
|
||||
{
|
||||
SYSTEMTIME st;
|
||||
::FileTimeToSystemTime(&local_file_time, &st);
|
||||
t.tm_year = st.wYear - 1900;
|
||||
t.tm_mon = st.wMonth - 1;
|
||||
t.tm_mday = st.wDay;
|
||||
t.tm_wday = st.wDayOfWeek - 1;
|
||||
t.tm_yday = nana::date::day_in_year(st.wYear, st.wMonth, st.wDay);
|
||||
SYSTEMTIME st;
|
||||
::FileTimeToSystemTime(&local_file_time, &st);
|
||||
t.tm_year = st.wYear - 1900;
|
||||
t.tm_mon = st.wMonth - 1;
|
||||
t.tm_mday = st.wDay;
|
||||
t.tm_wday = st.wDayOfWeek - 1;
|
||||
t.tm_yday = nana::date::day_in_year(st.wYear, st.wMonth, st.wDay);
|
||||
|
||||
t.tm_hour = st.wHour;
|
||||
t.tm_min = st.wMinute;
|
||||
t.tm_sec = st.wSecond;
|
||||
}
|
||||
t.tm_hour = st.wHour;
|
||||
t.tm_min = st.wMinute;
|
||||
t.tm_sec = st.wSecond;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
}//end namespace detail
|
||||
}//end namespace detail
|
||||
|
||||
bool not_found_error(int errval)
|
||||
{
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
* A FileSystem Utility Implementation
|
||||
* Copyright(C) 2003-2013 Jinhao(cnjinhao@hotmail.com)
|
||||
* Copyright(C) 2003-2016 Jinhao(cnjinhao@hotmail.com)
|
||||
*
|
||||
* Distributed under the Boost Software License, Version 1.0.
|
||||
* (See accompanying file LICENSE_1_0.txt or copy at
|
||||
@@ -12,7 +12,6 @@
|
||||
*/
|
||||
|
||||
#include <nana/filesystem/fs_utility.hpp>
|
||||
#include <nana/filesystem/file_iterator.hpp>
|
||||
#include <nana/deploy.hpp>
|
||||
#include <vector>
|
||||
#if defined(NANA_WINDOWS)
|
||||
@@ -53,27 +52,6 @@ namespace filesystem
|
||||
|
||||
namespace detail
|
||||
{
|
||||
//rm_dir_recursive
|
||||
//@brief: remove a directory, if it is not empty, recursively remove it's subfiles and sub directories
|
||||
bool rm_dir_recursive(std::string&& dir)
|
||||
{
|
||||
std::vector<file_iterator::value_type> files;
|
||||
auto path = dir;
|
||||
path += '\\';
|
||||
|
||||
std::copy(file_iterator(dir), file_iterator(), std::back_inserter(files));
|
||||
|
||||
for(auto & f : files)
|
||||
{
|
||||
if(f.directory)
|
||||
rm_dir_recursive(path + f.name);
|
||||
else
|
||||
rmfile((path + f.name).c_str());
|
||||
}
|
||||
|
||||
return rmdir(dir.c_str(), true);
|
||||
}
|
||||
|
||||
#if defined(NANA_WINDOWS)
|
||||
void filetime_to_c_tm(FILETIME& ft, struct tm& t)
|
||||
{
|
||||
@@ -150,41 +128,18 @@ namespace filesystem
|
||||
#endif
|
||||
}
|
||||
|
||||
bool rmdir(const char* dir, bool fails_if_not_empty)
|
||||
{
|
||||
bool ret = false;
|
||||
if(dir)
|
||||
{
|
||||
#if defined(NANA_WINDOWS)
|
||||
ret = (::RemoveDirectory(utf8_cast(dir).c_str()) == TRUE);
|
||||
if(!fails_if_not_empty && (::GetLastError() == ERROR_DIR_NOT_EMPTY))
|
||||
ret = detail::rm_dir_recursive(dir);
|
||||
#elif defined(NANA_LINUX) || defined(NANA_MACOS)
|
||||
std::string mbstr = nana::charset(dir);
|
||||
if(::rmdir(mbstr.c_str()))
|
||||
{
|
||||
if(!fails_if_not_empty && (errno == EEXIST || errno == ENOTEMPTY))
|
||||
ret = detail::rm_dir_recursive(dir);
|
||||
}
|
||||
else
|
||||
ret = true;
|
||||
#endif
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
std::wstring path_user()
|
||||
std::string path_user()
|
||||
{
|
||||
#if defined(NANA_WINDOWS)
|
||||
wchar_t path[MAX_PATH];
|
||||
if(SUCCEEDED(SHGetFolderPath(0, CSIDL_PROFILE, 0, SHGFP_TYPE_CURRENT, path)))
|
||||
return path;
|
||||
return to_utf8(path);
|
||||
#elif defined(NANA_LINUX) || defined(NANA_MACOS)
|
||||
const char * s = ::getenv("HOME");
|
||||
if(s)
|
||||
return nana::charset(std::string(s, std::strlen(s)), nana::unicode::utf8);
|
||||
return s;
|
||||
#endif
|
||||
return std::wstring();
|
||||
return std::string();
|
||||
}
|
||||
}//end namespace filesystem
|
||||
}//end namespace nana
|
||||
|
||||
Reference in New Issue
Block a user