using nana filesystem

This commit is contained in:
qPCR4vir 2019-11-05 21:00:33 +01:00
parent 3eee1f76b9
commit a8b5e92947
4 changed files with 726 additions and 479 deletions

View File

@ -38,10 +38,9 @@
#define NANA_USING_STD_FILESYSTEM 0
#define NANA_USING_BOOST_FILESYSTEM 0
#define NANA_FILESYSTEM_FORCE 1
//#define NANA_FILESYSTEM_FORCE 1
#if (defined(NANA_FILESYSTEM_FORCE) || ( (defined(STD_FILESYSTEM_NOT_SUPPORTED) && !defined(BOOST_FILESYSTEM_AVAILABLE)) && !(defined(BOOST_FILESYSTEM_FORCE) || defined(STD_FILESYSTEM_FORCE)) ) )
#undef NANA_USING_NANA_FILESYSTEM
#define NANA_USING_NANA_FILESYSTEM 1
@ -186,11 +185,14 @@ namespace nana {
// observers
file_type type() const;
perms permissions() const;
// modifiers
void type(file_type ft);
void permissions(perms prms);
private:
file_type value_;
perms perms_;
@ -225,7 +227,9 @@ namespace nana {
// modifiers
void clear() noexcept;
path &make_preferred();
path &remove_filename();
//path& replace_filename(const path& replacement);
//path& replace_extension(const path& replacement = path());
@ -233,25 +237,45 @@ namespace nana {
// decomposition
path root_name() const;
path root_directory() const;
path root_path() const;
path relative_path() const;
path parent_path() const;
path filename() const;
path stem() const;
path extension() const;
// query
bool empty() const noexcept;
bool has_root_name() const { return !root_name().empty(); }
bool has_root_directory() const { return !root_directory().empty(); }
bool has_root_path() const { return !root_path().empty(); }
bool has_relative_path() const { return !relative_path().empty(); }
bool has_parent_path() const { return !parent_path().empty(); }; // temp;;
bool has_filename() const { return !filename().empty(); }; // temp;
bool has_root_name() const
{ return !root_name().empty(); }
bool has_root_directory() const
{ return !root_directory().empty(); }
bool has_root_path() const
{ return !root_path().empty(); }
bool has_relative_path() const
{ return !relative_path().empty(); }
bool has_parent_path() const
{ return !parent_path().empty(); }; // temp;;
bool has_filename() const
{ return !filename().empty(); }; // temp;
//bool has_stem() const;
bool has_extension() const { return !extension().empty(); }; // temp
bool has_extension() const
{ return !extension().empty(); }; // temp
bool is_absolute() const;
bool is_relative() const;
int compare(const path &other) const;
@ -259,7 +283,9 @@ namespace nana {
file_type what() const;
const value_type *c_str() const;
const string_type &native() const;
operator string_type() const;
std::string string() const;
@ -296,17 +322,24 @@ namespace nana {
path other(source);
return this->operator/=(other);
}
private:
void _m_assign(const std::string &source_utf8);
void _m_assign(const std::wstring &source);
private:
string_type pathstr_;
};
bool operator==(const path &lhs, const path &rhs);
bool operator!=(const path &lhs, const path &rhs);
bool operator<(const path &lhs, const path &rhs);
bool operator>(const path &lhs, const path &rhs);
path operator/(const path &lhs, const path &rhs);
@ -317,9 +350,11 @@ namespace nana {
explicit filesystem_error(const std::string &msg, std::error_code);
filesystem_error(const std::string &msg, const path &path1, std::error_code err);
filesystem_error(const std::string &msg, const path &path1, const path &path2, std::error_code err);
const path &path1() const noexcept;
const path &path2() const noexcept;
// const char* what() const noexcept;
private:
@ -332,18 +367,24 @@ namespace nana {
{
public:
directory_entry() = default;
explicit directory_entry(const ::nana::experimental::filesystem::path&);
explicit directory_entry(const filesystem::path &);
//modifiers
void assign(const ::nana::experimental::filesystem::path&);
void replace_filename(const ::nana::experimental::filesystem::path&);
void assign(const filesystem::path &);
void replace_filename(const filesystem::path &);
//observers
file_status status() const;
operator const filesystem::path&() const { return path_; };
operator const filesystem::path &() const
{ return path_; };
const filesystem::path &path() const;
private:
::nana::experimental::filesystem::path path_;
filesystem::path path_;
};
/// InputIterator that iterate over the sequence of directory_entry elements representing the files in a directory, not an recursive_directory_iterator
@ -353,13 +394,17 @@ namespace nana {
public:
directory_iterator() noexcept;
explicit directory_iterator(const path &p);
directory_iterator(const path &p, directory_options opt);
const value_type &operator*() const;
const value_type *operator->() const;
directory_iterator &operator++();
directory_iterator operator++(int); ///< extention
bool equal(const directory_iterator &x) const;
@ -374,7 +419,9 @@ namespace nana {
}
void _m_prepare(const path &file_path);
void _m_read();
private:
bool end_{false};
path::string_type path_;
@ -384,6 +431,7 @@ namespace nana {
find_handle handle_{nullptr};
value_type value_;
};
/// enable directory_iterator range-based for statements
inline directory_iterator begin(directory_iterator iter) noexcept
{
@ -415,15 +463,18 @@ namespace nana {
file_status status(const path &p);
file_status status(const path &p, std::error_code &);
std::uintmax_t file_size(const path &p);
std::uintmax_t file_size(const path &p, std::error_code &ec) noexcept;
inline bool is_directory(file_status s) noexcept
{ return s.type() == file_type::directory; }
bool is_directory(const path &p);
bool is_directory(const path &p, std::error_code &ec) noexcept;
inline bool is_regular_file(file_status s) noexcept
@ -451,8 +502,10 @@ namespace nana {
bool create_directories(const path &p);
//bool create_directories(const path& p, error_code& ec) noexcept;
bool create_directory(const path &p);
//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;
@ -466,11 +519,13 @@ namespace nana {
path current_path();
//path current_path(error_code& ec);
void current_path(const path &p); ///< chdir
//void current_path(const path& p, error_code& ec) noexcept;
bool remove(const path &p);
bool remove(const path &p, std::error_code &ec); // noexcept;
//uintmax_t remove_all(const path& p);
@ -529,6 +584,17 @@ namespace std
}
}
#else //#if NANA_USING_NANA_FILESYSTEM
//Implements the missing functions for various version of experimental/filesystem
namespace std
{
namespace filesystem
{
#if defined(_MSC_VER) && ((!defined(_MSVC_LANG)) || (_MSVC_LANG < 201703))
path absolute(const path& p);
path absolute(const path& p, std::error_code& err);
path canonical(const path& p);
path canonical(const path& p, std::error_code& err);
@ -536,19 +602,12 @@ namespace std
path weakly_canonical(const path& p, std::error_code& err);
#endif
#if defined(NANA_FILESYSTEM_FORCE) || defined(NANA_MINGW)
#if defined(NANA_MINGW) // todo ??
bool exists( std::filesystem::file_status s ) noexcept;
bool exists( const std::filesystem::path& p );
bool exists( const std::filesystem::path& p, std::error_code& ec ) noexcept;
#endif
}
} // std
#else //#if NANA_USING_NANA_FILESYSTEM
//Implements the missing functions for various version of experimental/filesystem
namespace std
{
namespace filesystem
{
//Visual Studio 2017
#if (defined(NANA_USING_STD_EXPERIMENTAL_FILESYSTEM) && defined(_MSC_VER) && (_MSC_VER > 1912)) || \
(!defined(__clang__) && defined(__GNUC__) && (__cplusplus < 201603 || (__GNUC__* 100 + __GNUC_MINOR__ < 801)))

View File

@ -6,8 +6,8 @@
* (See accompanying file LICENSE_1_0.txt or copy at
* http://www.boost.org/LICENSE_1_0.txt)
*
* @file: nana/filesystem/filesystem.cpp
* @description:
* @file nana/filesystem/filesystem.cpp
* @description
* provide some interface for file management
*/
@ -183,7 +183,6 @@ namespace filesystem
}
//end class filesystem_error
//Because of No wide character version of POSIX
#if defined(NANA_POSIX)
const char* separators = "/";
@ -510,10 +509,10 @@ namespace filesystem
return to_wstring(pathstr_);
}
std::string path::u8string() const
/*std::string path::u8string() const
{
return to_utf8(pathstr_);
}
}*/
std::string path::generic_string() const
{
auto str = string();
@ -526,12 +525,12 @@ namespace filesystem
std::replace(str.begin(), str.end(), L'\\', L'/');
return str;
}
std::string path::generic_u8string() const // uppss ...
/*std::string path::generic_u8string() const // uppss ...
{
auto str = pathstr_;
std::replace(str.begin(), str.end(), '\\', '/'); // uppss ... revise this !!!!!
return to_utf8(str);
}
}*/
path path::lexically_normal() const
{
@ -976,7 +975,6 @@ namespace filesystem
#endif
}//end namespace detail
file_status status(const path& p)
{
std::error_code err;
@ -1180,19 +1178,216 @@ namespace filesystem
#endif
}
#ifndef CXX_NO_INLINE_NAMESPACE
} //end namespace v1
path absolute(const path& p)
{
if (p.empty())
return p;
auto abs_base = current_path();
// store expensive to compute values that are needed multiple times
path p_root_name(p.root_name());
path base_root_name(abs_base.root_name());
path p_root_directory(p.root_directory());
if (!p_root_name.empty()) // p.has_root_name()
{
if (p_root_directory.empty()) // !p.has_root_directory()
return p_root_name / abs_base.root_directory()
/ abs_base.relative_path() / p.relative_path();
// p is absolute, so fall through to return p at end of block
}
else if (!p_root_directory.empty()) // p.has_root_directory()
{
#ifdef NANA_POSIX
// POSIX can have root name it it is a network path
if (base_root_name.empty()) // !abs_base.has_root_name()
return p;
#endif
return base_root_name / p;
}
else
return abs_base / p;
return p; // p.is_absolute() is true
}
path absolute(const path& p, std::error_code& /*err*/)
{
return absolute(p);
}
path canonical(const path& p, std::error_code* err)
{
path source(p.is_absolute() ? p : absolute(p));
path root(source.root_path());
path result;
std::error_code local_ec;
file_status stat(status(source, local_ec));
if (stat.type() == file_type::not_found)
{
if (nullptr == err)
throw (filesystem_error(
"nana::filesystem::canonical", source,
std::error_code(static_cast<int>(std::errc::no_such_file_or_directory), std::generic_category())));
err->assign(static_cast<int>(std::errc::no_such_file_or_directory), std::generic_category());
return result;
}
else if (local_ec)
{
if (nullptr == err)
throw (filesystem_error(
"nana::filesystem::canonical", source, local_ec));
*err = local_ec;
return result;
}
auto tmp_p = source;
std::vector<path> source_elements;
while (tmp_p != root)
{
source_elements.emplace(source_elements.begin(), tmp_p.filename());
tmp_p.remove_filename();
}
result = root;
for(auto & e : source_elements)
{
auto str = e.string();
if("." == str)
continue;
else if(".." == str)
{
if(result != root)
result.remove_filename();
continue;
}
result /= e;
}
if (err)
err->clear();
return result;
}
path canonical(const path& p)
{
return canonical(p, nullptr);
}
path canonical(const path& p, std::error_code& err)
{
return canonical(p, &err);
}
bool try_throw(int err_val, const path& p, std::error_code* ec, const char* message)
{
if (0 == err_val)
{
if (ec) ec->clear();
}
else
{ //error
if (nullptr == ec)
throw (filesystem_error(
message, p,
std::error_code(err_val, std::generic_category())));
else
ec->assign(err_val, std::system_category());
}
return err_val != 0;
}
path weakly_canonical(const path& p, std::error_code* err)
{
path head{ p };
std::error_code tmp_err;
std::vector<path> elements;
while (!head.empty())
{
auto head_status = status(head, tmp_err);
if (head_status.type() == file_type::unknown)
{
if (try_throw(static_cast<int>(std::errc::invalid_argument), head, err, "nana::filesystem::weakly_canonical"))
return path{};
}
if (head_status.type() != file_type::not_found)
break;
elements.emplace_back(head.filename());
head.remove_filename();
}
bool tail_has_dots = false;
path tail;
for (auto & e : elements)
{
tail /= e;
// for a later optimization, track if any dot or dot-dot elements are present
if (e.native().size() <= 2
&& e.native()[0] == '.'
&& (e.native().size() == 1 || e.native()[1] == '.'))
tail_has_dots = true;
}
if (head.empty())
return p.lexically_normal();
head = canonical(head, tmp_err);
if (try_throw(tmp_err.value(), head, err, "nana::filesystem::weakly_canonical"))
return path();
return tail.empty()
? head
: (tail_has_dots // optimization: only normalize if tail had dot or dot-dot element
? (head / tail).lexically_normal()
: head / tail);
}
path weakly_canonical(const path& p)
{
return weakly_canonical(p, nullptr);
}
path weakly_canonical(const path& p, std::error_code& err)
{
return weakly_canonical(p, &err);
}
bool exists( std::filesystem::file_status s ) noexcept
{
return s.type() != file_type::not_found;
}
bool exists( const std::filesystem::path& p )
{
return exists(status(p));
}
bool exists( const std::filesystem::path& p, std::error_code& ec ) noexcept
{
return exists(status(p, ec));
}
} //end namespace filesystem
} //end namespace experimental
}//end namespace nana
#else
namespace std
{
namespace filesystem
{
#if defined(NANA_FILESYSTEM_FORCE) || \
(defined(_MSC_VER) && ((!defined(_MSVC_LANG)) || (_MSVC_LANG < 201703)))
#if defined(_MSC_VER) && ((!defined(_MSVC_LANG)) || (_MSVC_LANG < 201703))
path absolute(const path& p)
{
if (p.empty())
@ -1377,8 +1572,8 @@ namespace std
return weakly_canonical(p, &err);
}
#endif
#if defined(NANA_FILESYSTEM_FORCE) || defined(NANA_MINGW)
/*
#if defined(NANA_MINGW)
bool exists( std::filesystem::file_status s ) noexcept
{
return s.type() != file_type::not_found;
@ -1393,17 +1588,10 @@ namespace std
{
return exists(status(p, ec));
}
#endif
*/
}//end namespace filesystem
}//end namespace std
#else //else NANA_USING_NANA_FILESYSTEM
//Defines the functions that are not provided by experimental/filesystem
namespace std
{
namespace filesystem
{
#if (defined(NANA_USING_STD_EXPERIMENTAL_FILESYSTEM) && defined(_MSC_VER) && (_MSC_VER > 1912)) || \
(!defined(__clang__) && defined(__GNUC__) && (__cplusplus < 201603 || (__GNUC__* 100 + __GNUC_MINOR__ < 801)))

View File

@ -1522,7 +1522,7 @@ namespace nana
if (!*str)
{
targets.emplace_back(parent_path);
impl_->path = parent_path.parent_path().u8string();
impl_->path = parent_path.parent_path().string();
}
else
{
@ -1532,7 +1532,7 @@ namespace nana
targets.emplace_back(parent_path / path_type{str});
str += (len + 1);
}
impl_->path = parent_path.u8string();
impl_->path = parent_path.string();
}
}
else
@ -1540,7 +1540,7 @@ namespace nana
wfile.resize(std::wcslen(wfile.data()));
targets.emplace_back(wfile);
impl_->path = targets.front().parent_path().u8string();
impl_->path = targets.front().parent_path().string();
}
#elif defined(NANA_POSIX)

View File

@ -1267,7 +1267,7 @@ namespace nana
auto files = impl->fbox.show();
if(!files.empty())
{
impl->value = files.front().u8string();
impl->value = files.front().string();
impl->path_edit.caption(impl->value);
}
});