Imported Upstream version 6.10.0.49

Former-commit-id: 1d6753294b2993e1fbf92de9366bb9544db4189b
This commit is contained in:
Xamarin Public Jenkins (auto-signing)
2020-01-16 16:38:04 +00:00
parent d94e79959b
commit 468663ddbb
48518 changed files with 2789335 additions and 61176 deletions

View File

@ -0,0 +1,342 @@
//===------------------ directory_iterator.cpp ----------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is dual licensed under the MIT and the University of Illinois Open
// Source Licenses. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include "experimental/filesystem"
#include "__config"
#if defined(_LIBCPP_WIN32API)
#define WIN32_LEAN_AND_MEAN
#include <Windows.h>
#else
#include <dirent.h>
#endif
#include <errno.h>
_LIBCPP_BEGIN_NAMESPACE_EXPERIMENTAL_FILESYSTEM
namespace { namespace detail {
#if !defined(_LIBCPP_WIN32API)
inline error_code capture_errno() {
_LIBCPP_ASSERT(errno, "Expected errno to be non-zero");
return error_code{errno, std::generic_category()};
}
#endif
template <class ...Args>
inline bool set_or_throw(std::error_code& my_ec,
std::error_code* user_ec,
const char* msg, Args&&... args)
{
if (user_ec) {
*user_ec = my_ec;
return true;
}
__throw_filesystem_error(msg, std::forward<Args>(args)..., my_ec);
return false;
}
#if !defined(_LIBCPP_WIN32API)
inline path::string_type posix_readdir(DIR *dir_stream, error_code& ec) {
struct dirent* dir_entry_ptr = nullptr;
errno = 0; // zero errno in order to detect errors
ec.clear();
if ((dir_entry_ptr = ::readdir(dir_stream)) == nullptr) {
if (errno)
ec = capture_errno();
return {};
} else {
return dir_entry_ptr->d_name;
}
}
#endif
}} // namespace detail
using detail::set_or_throw;
#if defined(_LIBCPP_WIN32API)
class __dir_stream {
public:
__dir_stream() = delete;
__dir_stream& operator=(const __dir_stream&) = delete;
__dir_stream(__dir_stream&& __ds) noexcept
: __stream_(__ds.__stream_), __root_(std::move(__ds.__root_)),
__entry_(std::move(__ds.__entry_)) {
__ds.__stream_ = INVALID_HANDLE_VALUE;
}
__dir_stream(const path& root, directory_options opts, error_code& ec)
: __stream_(INVALID_HANDLE_VALUE), __root_(root) {
__stream_ = ::FindFirstFile(root.c_str(), &__data_);
if (__stream_ == INVALID_HANDLE_VALUE) {
ec = error_code(::GetLastError(), std::generic_category());
const bool ignore_permission_denied =
bool(opts & directory_options::skip_permission_denied);
if (ignore_permission_denied && ec.value() == ERROR_ACCESS_DENIED)
ec.clear();
return;
}
}
~__dir_stream() noexcept {
if (__stream_ == INVALID_HANDLE_VALUE)
return;
close();
}
bool good() const noexcept { return __stream_ != INVALID_HANDLE_VALUE; }
bool advance(error_code& ec) {
while (::FindNextFile(__stream_, &__data_)) {
if (!strcmp(__data_.cFileName, ".") || strcmp(__data_.cFileName, ".."))
continue;
__entry_.assign(__root_ / __data_.cFileName);
return true;
}
ec = error_code(::GetLastError(), std::generic_category());
close();
return false;
}
private:
std::error_code close() noexcept {
std::error_code ec;
if (!::FindClose(__stream_))
ec = error_code(::GetLastError(), std::generic_category());
__stream_ = INVALID_HANDLE_VALUE;
return ec;
}
HANDLE __stream_{INVALID_HANDLE_VALUE};
WIN32_FIND_DATA __data_;
public:
path __root_;
directory_entry __entry_;
};
#else
class __dir_stream {
public:
__dir_stream() = delete;
__dir_stream& operator=(const __dir_stream&) = delete;
__dir_stream(__dir_stream&& other) noexcept
: __stream_(other.__stream_), __root_(std::move(other.__root_)),
__entry_(std::move(other.__entry_))
{
other.__stream_ = nullptr;
}
__dir_stream(const path& root, directory_options opts, error_code& ec)
: __stream_(nullptr),
__root_(root)
{
if ((__stream_ = ::opendir(root.c_str())) == nullptr) {
ec = detail::capture_errno();
const bool allow_eacess =
bool(opts & directory_options::skip_permission_denied);
if (allow_eacess && ec.value() == EACCES)
ec.clear();
return;
}
advance(ec);
}
~__dir_stream() noexcept
{ if (__stream_) close(); }
bool good() const noexcept { return __stream_ != nullptr; }
bool advance(error_code &ec) {
while (true) {
auto str = detail::posix_readdir(__stream_, ec);
if (str == "." || str == "..") {
continue;
} else if (ec || str.empty()) {
close();
return false;
} else {
__entry_.assign(__root_ / str);
return true;
}
}
}
private:
std::error_code close() noexcept {
std::error_code m_ec;
if (::closedir(__stream_) == -1)
m_ec = detail::capture_errno();
__stream_ = nullptr;
return m_ec;
}
DIR * __stream_{nullptr};
public:
path __root_;
directory_entry __entry_;
};
#endif
// directory_iterator
directory_iterator::directory_iterator(const path& p, error_code *ec,
directory_options opts)
{
std::error_code m_ec;
__imp_ = make_shared<__dir_stream>(p, opts, m_ec);
if (ec) *ec = m_ec;
if (!__imp_->good()) {
__imp_.reset();
if (m_ec)
set_or_throw(m_ec, ec,
"directory_iterator::directory_iterator(...)", p);
}
}
directory_iterator& directory_iterator::__increment(error_code *ec)
{
_LIBCPP_ASSERT(__imp_, "Attempting to increment an invalid iterator");
std::error_code m_ec;
if (!__imp_->advance(m_ec)) {
__imp_.reset();
if (m_ec)
set_or_throw(m_ec, ec, "directory_iterator::operator++()");
} else {
if (ec) ec->clear();
}
return *this;
}
directory_entry const& directory_iterator::__dereference() const {
_LIBCPP_ASSERT(__imp_, "Attempting to dereference an invalid iterator");
return __imp_->__entry_;
}
// recursive_directory_iterator
struct recursive_directory_iterator::__shared_imp {
stack<__dir_stream> __stack_;
directory_options __options_;
};
recursive_directory_iterator::recursive_directory_iterator(const path& p,
directory_options opt, error_code *ec)
: __imp_(nullptr), __rec_(true)
{
if (ec) ec->clear();
std::error_code m_ec;
__dir_stream new_s(p, opt, m_ec);
if (m_ec) set_or_throw(m_ec, ec, "recursive_directory_iterator", p);
if (m_ec || !new_s.good()) return;
__imp_ = _VSTD::make_shared<__shared_imp>();
__imp_->__options_ = opt;
__imp_->__stack_.push(_VSTD::move(new_s));
}
void recursive_directory_iterator::__pop(error_code* ec)
{
_LIBCPP_ASSERT(__imp_, "Popping the end iterator");
if (ec) ec->clear();
__imp_->__stack_.pop();
if (__imp_->__stack_.size() == 0)
__imp_.reset();
else
__advance(ec);
}
directory_options recursive_directory_iterator::options() const {
return __imp_->__options_;
}
int recursive_directory_iterator::depth() const {
return __imp_->__stack_.size() - 1;
}
const directory_entry& recursive_directory_iterator::__dereference() const {
return __imp_->__stack_.top().__entry_;
}
recursive_directory_iterator&
recursive_directory_iterator::__increment(error_code *ec)
{
if (ec) ec->clear();
if (recursion_pending()) {
if (__try_recursion(ec) || (ec && *ec))
return *this;
}
__rec_ = true;
__advance(ec);
return *this;
}
void recursive_directory_iterator::__advance(error_code* ec) {
// REQUIRES: ec must be cleared before calling this function.
const directory_iterator end_it;
auto& stack = __imp_->__stack_;
std::error_code m_ec;
while (stack.size() > 0) {
if (stack.top().advance(m_ec))
return;
if (m_ec) break;
stack.pop();
}
__imp_.reset();
if (m_ec)
set_or_throw(m_ec, ec, "recursive_directory_iterator::operator++()");
}
bool recursive_directory_iterator::__try_recursion(error_code *ec) {
bool rec_sym =
bool(options() & directory_options::follow_directory_symlink);
auto& curr_it = __imp_->__stack_.top();
bool skip_rec = false;
std::error_code m_ec;
if (!rec_sym) {
file_status st = curr_it.__entry_.symlink_status(m_ec);
if (m_ec && status_known(st))
m_ec.clear();
if (m_ec || is_symlink(st) || !is_directory(st))
skip_rec = true;
} else {
file_status st = curr_it.__entry_.status(m_ec);
if (m_ec && status_known(st))
m_ec.clear();
if (m_ec || !is_directory(st))
skip_rec = true;
}
if (!skip_rec) {
__dir_stream new_it(curr_it.__entry_.path(), __imp_->__options_, m_ec);
if (new_it.good()) {
__imp_->__stack_.push(_VSTD::move(new_it));
return true;
}
}
if (m_ec) {
const bool allow_eacess = bool(__imp_->__options_
& directory_options::skip_permission_denied);
if (m_ec.value() == EACCES && allow_eacess) {
if (ec) ec->clear();
} else {
__imp_.reset();
set_or_throw(m_ec, ec,
"recursive_directory_iterator::operator++()");
}
}
return false;
}
_LIBCPP_END_NAMESPACE_EXPERIMENTAL_FILESYSTEM

View File

@ -0,0 +1,173 @@
//===----------------------------------------------------------------------===////
//
// The LLVM Compiler Infrastructure
//
// This file is dual licensed under the MIT and the University of Illinois Open
// Source Licenses. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===////
#ifndef FILESYSTEM_TIME_HELPER_H
#define FILESYSTEM_TIME_HELPER_H
#include "experimental/__config"
#include "chrono"
#include "cstdlib"
#include "climits"
#include <unistd.h>
#include <sys/stat.h>
#if !defined(UTIME_OMIT)
#include <sys/time.h> // for ::utimes as used in __last_write_time
#endif
_LIBCPP_BEGIN_NAMESPACE_EXPERIMENTAL_FILESYSTEM
namespace time_detail { namespace {
using namespace chrono;
template <class FileTimeT,
bool IsFloat = is_floating_point<typename FileTimeT::rep>::value>
struct fs_time_util_base {
static constexpr auto max_seconds =
duration_cast<seconds>(FileTimeT::duration::max()).count();
static constexpr auto max_nsec =
duration_cast<nanoseconds>(FileTimeT::duration::max() -
seconds(max_seconds))
.count();
static constexpr auto min_seconds =
duration_cast<seconds>(FileTimeT::duration::min()).count();
static constexpr auto min_nsec_timespec =
duration_cast<nanoseconds>(
(FileTimeT::duration::min() - seconds(min_seconds)) + seconds(1))
.count();
// Static assert that these values properly round trip.
static_assert((seconds(min_seconds) +
duration_cast<microseconds>(nanoseconds(min_nsec_timespec))) -
duration_cast<microseconds>(seconds(1)) ==
FileTimeT::duration::min(),
"");
};
template <class FileTimeT>
struct fs_time_util_base<FileTimeT, true> {
static const long long max_seconds;
static const long long max_nsec;
static const long long min_seconds;
static const long long min_nsec_timespec;
};
template <class FileTimeT>
const long long fs_time_util_base<FileTimeT, true>::max_seconds =
duration_cast<seconds>(FileTimeT::duration::max()).count();
template <class FileTimeT>
const long long fs_time_util_base<FileTimeT, true>::max_nsec =
duration_cast<nanoseconds>(FileTimeT::duration::max() -
seconds(max_seconds))
.count();
template <class FileTimeT>
const long long fs_time_util_base<FileTimeT, true>::min_seconds =
duration_cast<seconds>(FileTimeT::duration::min()).count();
template <class FileTimeT>
const long long fs_time_util_base<FileTimeT, true>::min_nsec_timespec =
duration_cast<nanoseconds>((FileTimeT::duration::min() -
seconds(min_seconds)) +
seconds(1))
.count();
template <class FileTimeT, class TimeT, class TimeSpecT>
struct fs_time_util : fs_time_util_base<FileTimeT> {
using Base = fs_time_util_base<FileTimeT>;
using Base::max_nsec;
using Base::max_seconds;
using Base::min_nsec_timespec;
using Base::min_seconds;
public:
template <class CType, class ChronoType>
static bool checked_set(CType* out, ChronoType time) {
using Lim = numeric_limits<CType>;
if (time > Lim::max() || time < Lim::min())
return false;
*out = static_cast<CType>(time);
return true;
}
static _LIBCPP_CONSTEXPR_AFTER_CXX11 bool is_representable(TimeSpecT tm) {
if (tm.tv_sec >= 0) {
return (tm.tv_sec < max_seconds) ||
(tm.tv_sec == max_seconds && tm.tv_nsec <= max_nsec);
} else if (tm.tv_sec == (min_seconds - 1)) {
return tm.tv_nsec >= min_nsec_timespec;
} else {
return (tm.tv_sec >= min_seconds);
}
}
static _LIBCPP_CONSTEXPR_AFTER_CXX11 bool is_representable(FileTimeT tm) {
auto secs = duration_cast<seconds>(tm.time_since_epoch());
auto nsecs = duration_cast<nanoseconds>(tm.time_since_epoch() - secs);
if (nsecs.count() < 0) {
secs = secs + seconds(1);
nsecs = nsecs + seconds(1);
}
using TLim = numeric_limits<TimeT>;
if (secs.count() >= 0)
return secs.count() <= TLim::max();
return secs.count() >= TLim::min();
}
static _LIBCPP_CONSTEXPR_AFTER_CXX11 FileTimeT
convert_timespec(TimeSpecT tm) {
auto adj_msec = duration_cast<microseconds>(nanoseconds(tm.tv_nsec));
if (tm.tv_sec >= 0) {
auto Dur = seconds(tm.tv_sec) + microseconds(adj_msec);
return FileTimeT(Dur);
} else if (duration_cast<microseconds>(nanoseconds(tm.tv_nsec)).count() ==
0) {
return FileTimeT(seconds(tm.tv_sec));
} else { // tm.tv_sec < 0
auto adj_subsec =
duration_cast<microseconds>(seconds(1) - nanoseconds(tm.tv_nsec));
auto Dur = seconds(tm.tv_sec + 1) - adj_subsec;
return FileTimeT(Dur);
}
}
template <class SubSecDurT, class SubSecT>
static bool set_times_checked(TimeT* sec_out, SubSecT* subsec_out,
FileTimeT tp) {
using namespace chrono;
auto dur = tp.time_since_epoch();
auto sec_dur = duration_cast<seconds>(dur);
auto subsec_dur = duration_cast<SubSecDurT>(dur - sec_dur);
// The tv_nsec and tv_usec fields must not be negative so adjust accordingly
if (subsec_dur.count() < 0) {
if (sec_dur.count() > min_seconds) {
sec_dur -= seconds(1);
subsec_dur += seconds(1);
} else {
subsec_dur = SubSecDurT::zero();
}
}
return checked_set(sec_out, sec_dur.count()) &&
checked_set(subsec_out, subsec_dur.count());
}
};
} // end namespace
} // end namespace time_detail
using time_detail::fs_time_util;
_LIBCPP_END_NAMESPACE_EXPERIMENTAL_FILESYSTEM
#endif // FILESYSTEM_TIME_HELPER_H

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,448 @@
//===--------------------- filesystem/path.cpp ----------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is dual licensed under the MIT and the University of Illinois Open
// Source Licenses. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include "experimental/filesystem"
#include "string_view"
#include "utility"
namespace { namespace parser
{
using namespace std;
using namespace std::experimental::filesystem;
using string_view_t = path::__string_view;
using string_view_pair = pair<string_view_t, string_view_t>;
using PosPtr = path::value_type const*;
struct PathParser {
enum ParserState : unsigned char {
// Zero is a special sentinel value used by default constructed iterators.
PS_BeforeBegin = 1,
PS_InRootName,
PS_InRootDir,
PS_InFilenames,
PS_InTrailingSep,
PS_AtEnd
};
const string_view_t Path;
string_view_t RawEntry;
ParserState State;
private:
PathParser(string_view_t P, ParserState State) noexcept
: Path(P), State(State) {}
public:
PathParser(string_view_t P, string_view_t E, unsigned char S)
: Path(P), RawEntry(E), State(static_cast<ParserState>(S)) {
// S cannot be '0' or PS_BeforeBegin.
}
static PathParser CreateBegin(string_view_t P) noexcept {
PathParser PP(P, PS_BeforeBegin);
PP.increment();
return PP;
}
static PathParser CreateEnd(string_view_t P) noexcept {
PathParser PP(P, PS_AtEnd);
return PP;
}
PosPtr peek() const noexcept {
auto TkEnd = getNextTokenStartPos();
auto End = getAfterBack();
return TkEnd == End ? nullptr : TkEnd;
}
void increment() noexcept {
const PosPtr End = getAfterBack();
const PosPtr Start = getNextTokenStartPos();
if (Start == End)
return makeState(PS_AtEnd);
switch (State) {
case PS_BeforeBegin: {
PosPtr TkEnd = consumeSeparator(Start, End);
// If we consumed exactly two separators we have a root name.
if (TkEnd && TkEnd == Start + 2) {
// FIXME Do we need to consume a name or is '//' a root name on its own?
// what about '//.', '//..', '//...'?
auto NameEnd = consumeName(TkEnd, End);
if (NameEnd)
TkEnd = NameEnd;
return makeState(PS_InRootName, Start, TkEnd);
}
else if (TkEnd)
return makeState(PS_InRootDir, Start, TkEnd);
else
return makeState(PS_InFilenames, Start, consumeName(Start, End));
}
case PS_InRootName:
return makeState(PS_InRootDir, Start, consumeSeparator(Start, End));
case PS_InRootDir:
return makeState(PS_InFilenames, Start, consumeName(Start, End));
case PS_InFilenames: {
PosPtr SepEnd = consumeSeparator(Start, End);
if (SepEnd != End) {
PosPtr TkEnd = consumeName(SepEnd, End);
if (TkEnd)
return makeState(PS_InFilenames, SepEnd, TkEnd);
}
return makeState(PS_InTrailingSep, Start, SepEnd);
}
case PS_InTrailingSep:
return makeState(PS_AtEnd);
case PS_AtEnd:
_LIBCPP_UNREACHABLE();
}
}
void decrement() noexcept {
const PosPtr REnd = getBeforeFront();
const PosPtr RStart = getCurrentTokenStartPos() - 1;
switch (State) {
case PS_AtEnd: {
// Try to consume a trailing separator or root directory first.
if (PosPtr SepEnd = consumeSeparator(RStart, REnd)) {
if (SepEnd == REnd)
return makeState((RStart == REnd + 2) ? PS_InRootName : PS_InRootDir,
Path.data(), RStart + 1);
// Check if we're seeing the root directory separator
auto PP = CreateBegin(Path);
bool InRootDir = PP.State == PS_InRootName &&
&PP.RawEntry.back() == SepEnd;
return makeState(InRootDir ? PS_InRootDir : PS_InTrailingSep,
SepEnd + 1, RStart + 1);
} else {
PosPtr TkStart = consumeName(RStart, REnd);
if (TkStart == REnd + 2 && consumeSeparator(TkStart, REnd) == REnd)
return makeState(PS_InRootName, Path.data(), RStart + 1);
else
return makeState(PS_InFilenames, TkStart + 1, RStart + 1);
}
}
case PS_InTrailingSep:
return makeState(PS_InFilenames, consumeName(RStart, REnd) + 1, RStart + 1);
case PS_InFilenames: {
PosPtr SepEnd = consumeSeparator(RStart, REnd);
if (SepEnd == REnd)
return makeState((RStart == REnd + 2) ? PS_InRootName : PS_InRootDir,
Path.data(), RStart + 1);
PosPtr TkEnd = consumeName(SepEnd, REnd);
if (TkEnd == REnd + 2 && consumeSeparator(TkEnd, REnd) == REnd)
return makeState(PS_InRootDir, SepEnd + 1, RStart + 1);
return makeState(PS_InFilenames, TkEnd + 1, SepEnd + 1);
}
case PS_InRootDir:
return makeState(PS_InRootName, Path.data(), RStart + 1);
case PS_InRootName:
case PS_BeforeBegin:
_LIBCPP_UNREACHABLE();
}
}
/// \brief Return a view with the "preferred representation" of the current
/// element. For example trailing separators are represented as a '.'
string_view_t operator*() const noexcept {
switch (State) {
case PS_BeforeBegin:
case PS_AtEnd:
return "";
case PS_InRootDir:
return "/";
case PS_InTrailingSep:
return ".";
case PS_InRootName:
case PS_InFilenames:
return RawEntry;
}
_LIBCPP_UNREACHABLE();
}
explicit operator bool() const noexcept {
return State != PS_BeforeBegin && State != PS_AtEnd;
}
PathParser& operator++() noexcept {
increment();
return *this;
}
PathParser& operator--() noexcept {
decrement();
return *this;
}
private:
void makeState(ParserState NewState, PosPtr Start, PosPtr End) noexcept {
State = NewState;
RawEntry = string_view_t(Start, End - Start);
}
void makeState(ParserState NewState) noexcept {
State = NewState;
RawEntry = {};
}
PosPtr getAfterBack() const noexcept {
return Path.data() + Path.size();
}
PosPtr getBeforeFront() const noexcept {
return Path.data() - 1;
}
/// \brief Return a pointer to the first character after the currently
/// lexed element.
PosPtr getNextTokenStartPos() const noexcept {
switch (State) {
case PS_BeforeBegin:
return Path.data();
case PS_InRootName:
case PS_InRootDir:
case PS_InFilenames:
return &RawEntry.back() + 1;
case PS_InTrailingSep:
case PS_AtEnd:
return getAfterBack();
}
_LIBCPP_UNREACHABLE();
}
/// \brief Return a pointer to the first character in the currently lexed
/// element.
PosPtr getCurrentTokenStartPos() const noexcept {
switch (State) {
case PS_BeforeBegin:
case PS_InRootName:
return &Path.front();
case PS_InRootDir:
case PS_InFilenames:
case PS_InTrailingSep:
return &RawEntry.front();
case PS_AtEnd:
return &Path.back() + 1;
}
_LIBCPP_UNREACHABLE();
}
PosPtr consumeSeparator(PosPtr P, PosPtr End) const noexcept {
if (P == End || *P != '/')
return nullptr;
const int Inc = P < End ? 1 : -1;
P += Inc;
while (P != End && *P == '/')
P += Inc;
return P;
}
PosPtr consumeName(PosPtr P, PosPtr End) const noexcept {
if (P == End || *P == '/')
return nullptr;
const int Inc = P < End ? 1 : -1;
P += Inc;
while (P != End && *P != '/')
P += Inc;
return P;
}
};
string_view_pair separate_filename(string_view_t const & s) {
if (s == "." || s == ".." || s.empty()) return string_view_pair{s, ""};
auto pos = s.find_last_of('.');
if (pos == string_view_t::npos)
return string_view_pair{s, string_view_t{}};
return string_view_pair{s.substr(0, pos), s.substr(pos)};
}
string_view_t createView(PosPtr S, PosPtr E) noexcept {
return {S, static_cast<size_t>(E - S) + 1};
}
}} // namespace parser
_LIBCPP_BEGIN_NAMESPACE_EXPERIMENTAL_FILESYSTEM
using parser::string_view_t;
using parser::string_view_pair;
using parser::PathParser;
using parser::createView;
///////////////////////////////////////////////////////////////////////////////
// path definitions
///////////////////////////////////////////////////////////////////////////////
constexpr path::value_type path::preferred_separator;
path & path::replace_extension(path const & replacement)
{
path p = extension();
if (not p.empty()) {
__pn_.erase(__pn_.size() - p.native().size());
}
if (!replacement.empty()) {
if (replacement.native()[0] != '.') {
__pn_ += ".";
}
__pn_.append(replacement.__pn_);
}
return *this;
}
///////////////////////////////////////////////////////////////////////////////
// path.decompose
string_view_t path::__root_name() const
{
auto PP = PathParser::CreateBegin(__pn_);
if (PP.State == PathParser::PS_InRootName)
return *PP;
return {};
}
string_view_t path::__root_directory() const
{
auto PP = PathParser::CreateBegin(__pn_);
if (PP.State == PathParser::PS_InRootName)
++PP;
if (PP.State == PathParser::PS_InRootDir)
return *PP;
return {};
}
string_view_t path::__root_path_raw() const
{
auto PP = PathParser::CreateBegin(__pn_);
if (PP.State == PathParser::PS_InRootName) {
auto NextCh = PP.peek();
if (NextCh && *NextCh == '/') {
++PP;
return createView(__pn_.data(), &PP.RawEntry.back());
}
return PP.RawEntry;
}
if (PP.State == PathParser::PS_InRootDir)
return *PP;
return {};
}
string_view_t path::__relative_path() const
{
auto PP = PathParser::CreateBegin(__pn_);
while (PP.State <= PathParser::PS_InRootDir)
++PP;
if (PP.State == PathParser::PS_AtEnd)
return {};
return createView(PP.RawEntry.data(), &__pn_.back());
}
string_view_t path::__parent_path() const
{
if (empty())
return {};
auto PP = PathParser::CreateEnd(__pn_);
--PP;
if (PP.RawEntry.data() == __pn_.data())
return {};
--PP;
return createView(__pn_.data(), &PP.RawEntry.back());
}
string_view_t path::__filename() const
{
if (empty()) return {};
return *(--PathParser::CreateEnd(__pn_));
}
string_view_t path::__stem() const
{
return parser::separate_filename(__filename()).first;
}
string_view_t path::__extension() const
{
return parser::separate_filename(__filename()).second;
}
////////////////////////////////////////////////////////////////////////////
// path.comparisons
int path::__compare(string_view_t __s) const {
auto PP = PathParser::CreateBegin(__pn_);
auto PP2 = PathParser::CreateBegin(__s);
while (PP && PP2) {
int res = (*PP).compare(*PP2);
if (res != 0) return res;
++PP; ++PP2;
}
if (PP.State == PP2.State && PP.State == PathParser::PS_AtEnd)
return 0;
if (PP.State == PathParser::PS_AtEnd)
return -1;
return 1;
}
////////////////////////////////////////////////////////////////////////////
// path.nonmembers
size_t hash_value(const path& __p) noexcept {
auto PP = PathParser::CreateBegin(__p.native());
size_t hash_value = 0;
std::hash<string_view_t> hasher;
while (PP) {
hash_value = __hash_combine(hash_value, hasher(*PP));
++PP;
}
return hash_value;
}
////////////////////////////////////////////////////////////////////////////
// path.itr
path::iterator path::begin() const
{
auto PP = PathParser::CreateBegin(__pn_);
iterator it;
it.__path_ptr_ = this;
it.__state_ = PP.State;
it.__entry_ = PP.RawEntry;
it.__stashed_elem_.__assign_view(*PP);
return it;
}
path::iterator path::end() const
{
iterator it{};
it.__state_ = PathParser::PS_AtEnd;
it.__path_ptr_ = this;
return it;
}
path::iterator& path::iterator::__increment() {
static_assert(__at_end == PathParser::PS_AtEnd, "");
PathParser PP(__path_ptr_->native(), __entry_, __state_);
++PP;
__state_ = PP.State;
__entry_ = PP.RawEntry;
__stashed_elem_.__assign_view(*PP);
return *this;
}
path::iterator& path::iterator::__decrement() {
PathParser PP(__path_ptr_->native(), __entry_, __state_);
--PP;
__state_ = PP.State;
__entry_ = PP.RawEntry;
__stashed_elem_.__assign_view(*PP);
return *this;
}
_LIBCPP_END_NAMESPACE_EXPERIMENTAL_FILESYSTEM

View File

@ -0,0 +1,143 @@
//===------------------------ memory_resource.cpp -------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is dual licensed under the MIT and the University of Illinois Open
// Source Licenses. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include "experimental/memory_resource"
#ifndef _LIBCPP_HAS_NO_ATOMIC_HEADER
#include "atomic"
#elif !defined(_LIBCPP_HAS_NO_THREADS)
#include "mutex"
#endif
_LIBCPP_BEGIN_NAMESPACE_LFTS_PMR
// memory_resource
//memory_resource::~memory_resource() {}
// new_delete_resource()
class _LIBCPP_TYPE_VIS __new_delete_memory_resource_imp
: public memory_resource
{
public:
~__new_delete_memory_resource_imp() = default;
protected:
virtual void* do_allocate(size_t __size, size_t __align)
{ return __allocate(__size); }
virtual void do_deallocate(void * __p, size_t, size_t)
{ _VSTD::__libcpp_deallocate(__p); }
virtual bool do_is_equal(memory_resource const & __other) const _NOEXCEPT
{ return &__other == this; }
};
// null_memory_resource()
class _LIBCPP_TYPE_VIS __null_memory_resource_imp
: public memory_resource
{
public:
~__null_memory_resource_imp() = default;
protected:
virtual void* do_allocate(size_t, size_t) {
__throw_bad_alloc();
}
virtual void do_deallocate(void *, size_t, size_t) {}
virtual bool do_is_equal(memory_resource const & __other) const _NOEXCEPT
{ return &__other == this; }
};
namespace {
union ResourceInitHelper {
struct {
__new_delete_memory_resource_imp new_delete_res;
__null_memory_resource_imp null_res;
} resources;
char dummy;
_LIBCPP_CONSTEXPR_AFTER_CXX11 ResourceInitHelper() : resources() {}
~ResourceInitHelper() {}
};
// When compiled in C++14 this initialization should be a constant expression.
// Only in C++11 is "init_priority" needed to ensure initialization order.
#if _LIBCPP_STD_VER > 11
_LIBCPP_SAFE_STATIC
#endif
ResourceInitHelper res_init __attribute__((init_priority (101)));
} // end namespace
memory_resource * new_delete_resource() _NOEXCEPT {
return &res_init.resources.new_delete_res;
}
memory_resource * null_memory_resource() _NOEXCEPT {
return &res_init.resources.null_res;
}
// default_memory_resource()
static memory_resource *
__default_memory_resource(bool set = false, memory_resource * new_res = nullptr) _NOEXCEPT
{
#ifndef _LIBCPP_HAS_NO_ATOMIC_HEADER
_LIBCPP_SAFE_STATIC static atomic<memory_resource*> __res =
ATOMIC_VAR_INIT(&res_init.resources.new_delete_res);
if (set) {
new_res = new_res ? new_res : new_delete_resource();
// TODO: Can a weaker ordering be used?
return _VSTD::atomic_exchange_explicit(
&__res, new_res, memory_order::memory_order_acq_rel);
}
else {
return _VSTD::atomic_load_explicit(
&__res, memory_order::memory_order_acquire);
}
#elif !defined(_LIBCPP_HAS_NO_THREADS)
_LIBCPP_SAFE_STATIC static memory_resource * res = &res_init.resources.new_delete_res;
static mutex res_lock;
if (set) {
new_res = new_res ? new_res : new_delete_resource();
lock_guard<mutex> guard(res_lock);
memory_resource * old_res = res;
res = new_res;
return old_res;
} else {
lock_guard<mutex> guard(res_lock);
return res;
}
#else
_LIBCPP_SAFE_STATIC static memory_resource* res = &res_init.resources.new_delete_res;
if (set) {
new_res = new_res ? new_res : new_delete_resource();
memory_resource * old_res = res;
res = new_res;
return old_res;
} else {
return res;
}
#endif
}
memory_resource * get_default_resource() _NOEXCEPT
{
return __default_memory_resource();
}
memory_resource * set_default_resource(memory_resource * __new_res) _NOEXCEPT
{
return __default_memory_resource(true, __new_res);
}
_LIBCPP_END_NAMESPACE_LFTS_PMR