add lexically_normal and weakly_canonical to nana.fs
This commit is contained in:
parent
6c7211e77d
commit
01b7f6ff09
@ -290,6 +290,8 @@ namespace nana { namespace experimental { namespace filesystem
|
||||
// std::u16string generic_u16string() const;
|
||||
// std::u32string generic_u32string() const;
|
||||
|
||||
path lexically_normal() const;
|
||||
|
||||
//appends
|
||||
path& operator/=(const path& other);
|
||||
|
||||
@ -544,6 +546,9 @@ namespace std {
|
||||
|
||||
path canonical(const path& p);
|
||||
path canonical(const path& p, std::error_code& err);
|
||||
|
||||
path weakly_canonical(const path& p);
|
||||
path weakly_canonical(const path& p, std::error_code& err);
|
||||
#endif
|
||||
|
||||
#if defined(NANA_FILESYSTEM_FORCE) || defined(NANA_MINGW)
|
||||
|
@ -225,9 +225,11 @@ namespace nana { namespace experimental { namespace filesystem
|
||||
//Because of No wide character version of POSIX
|
||||
#if defined(NANA_POSIX)
|
||||
const char* separators = "/";
|
||||
const char separator = '/';
|
||||
const char* punt = ".";
|
||||
#else
|
||||
const wchar_t* separators = L"/\\";
|
||||
const char separator = '\\';
|
||||
const wchar_t* punt = L".";
|
||||
#endif
|
||||
|
||||
@ -568,6 +570,71 @@ namespace nana { namespace experimental { namespace filesystem
|
||||
std::replace(str.begin(), str.end(), '\\', '/'); // uppss ... revise this !!!!!
|
||||
return to_utf8(str);
|
||||
}
|
||||
|
||||
path path::lexically_normal() const
|
||||
{
|
||||
if (pathstr_.empty())
|
||||
return *this;
|
||||
|
||||
std::vector<path> elements;
|
||||
path temp{ pathstr_ };
|
||||
while (!temp.empty())
|
||||
{
|
||||
elements.emplace_back(temp.filename());
|
||||
temp.remove_filename();
|
||||
}
|
||||
|
||||
auto start = elements.begin();
|
||||
auto last = elements.end();
|
||||
auto stop = last--;
|
||||
for (auto itr(start); itr != stop; ++itr)
|
||||
{
|
||||
// ignore "." except at start and last
|
||||
if (itr->native().size() == 1
|
||||
&& (itr->native())[0] == '.'
|
||||
&& itr != start
|
||||
&& itr != last) continue;
|
||||
|
||||
// ignore a name and following ".."
|
||||
if (!temp.empty()
|
||||
&& itr->native().size() == 2
|
||||
&& (itr->native())[0] == '.'
|
||||
&& (itr->native())[1] == '.') // dot dot
|
||||
{
|
||||
string_type lf(temp.filename().native());
|
||||
if (lf.size() > 0
|
||||
&& (lf.size() != 1
|
||||
|| (lf[0] != '.'
|
||||
&& (lf[0] != '/' && lf[0] != '\\')))
|
||||
&& (lf.size() != 2
|
||||
|| (lf[0] != '.'
|
||||
&& lf[1] != '.'
|
||||
# ifdef NANA_WINDOWS
|
||||
&& lf[1] != ':'
|
||||
# endif
|
||||
)
|
||||
)
|
||||
)
|
||||
{
|
||||
temp.remove_filename();
|
||||
auto next = itr;
|
||||
if (temp.empty() && ++next != stop
|
||||
&& next == last && last->string() == ".")
|
||||
{
|
||||
temp /= ".";
|
||||
}
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
temp /= *itr;
|
||||
};
|
||||
|
||||
if (temp.empty())
|
||||
temp = ".";
|
||||
return temp;
|
||||
}
|
||||
|
||||
path & path::operator/=(const path& p)
|
||||
{
|
||||
if (p.empty())
|
||||
@ -1272,6 +1339,81 @@ namespace std
|
||||
{
|
||||
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(
|
||||
"nana::filesystem::canonical", p,
|
||||
error_code(err_val, generic_category())));
|
||||
else
|
||||
ec->assign(err_val, 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>(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);
|
||||
}
|
||||
#endif
|
||||
|
||||
#if defined(NANA_FILESYSTEM_FORCE) || defined(NANA_MINGW)
|
||||
|
Loading…
x
Reference in New Issue
Block a user