diff --git a/SModule b/SModule index 845eb17..6af9347 100644 --- a/SModule +++ b/SModule @@ -16,6 +16,7 @@ mijin_sources = Split(""" source/mijin/platform/folders.cpp source/mijin/util/os.cpp source/mijin/types/name.cpp + source/mijin/types/path.cpp source/mijin/virtual_filesystem/filesystem.cpp source/mijin/virtual_filesystem/memory.cpp source/mijin/virtual_filesystem/stacked.cpp diff --git a/source/mijin/container/vector_map.hpp b/source/mijin/container/vector_map.hpp index 53e5fb3..21b319a 100644 --- a/source/mijin/container/vector_map.hpp +++ b/source/mijin/container/vector_map.hpp @@ -251,12 +251,13 @@ public: return eraseImpl(idx, count); } + template T> [[nodiscard]] - iterator find(const TKey& key) MIJIN_NOEXCEPT + iterator find(const T& keyValue) MIJIN_NOEXCEPT { for (std::size_t idx = 0; idx < keys_.size(); ++idx) { - if (keys_[idx] == key) + if (keys_[idx] == keyValue) { return iterator(&keys_[idx], &values_[idx]); } @@ -264,12 +265,13 @@ public: return end(); } + template T> [[nodiscard]] - const_iterator find(const TKey& key) const MIJIN_NOEXCEPT + const_iterator find(const T& keyValue) const MIJIN_NOEXCEPT { for (std::size_t idx = 0; idx < keys_.size(); ++idx) { - if (keys_[idx] == key) + if (keys_[idx] == keyValue) { return const_iterator(&keys_[idx], &values_[idx]); } @@ -277,10 +279,11 @@ public: return end(); } + template T> [[nodiscard]] - bool contains(const TKey& key) const MIJIN_NOEXCEPT + bool contains(const T& keyValue) const MIJIN_NOEXCEPT { - return std::ranges::contains(keys_, key); + return std::ranges::contains(keys_, keyValue); } private: iterator eraseImpl(std::ptrdiff_t idx, std::ptrdiff_t count = 1) MIJIN_NOEXCEPT diff --git a/source/mijin/types/path.cpp b/source/mijin/types/path.cpp new file mode 100644 index 0000000..f855788 --- /dev/null +++ b/source/mijin/types/path.cpp @@ -0,0 +1,40 @@ + +#include "./path.hpp" + +#include + +namespace fs = std::filesystem; + +namespace mijin +{ + +// +// internal defines +// + +// +// internal constants +// + +// +// internal types +// + +// +// internal variables +// + +// +// internal functions +// + + +// +// public functions +// + +NativePath getWorkingDirectory() +{ + return fs::current_path().generic_string(); +} +} // namespace mijin diff --git a/source/mijin/types/path.hpp b/source/mijin/types/path.hpp index f585870..7d89aac 100644 --- a/source/mijin/types/path.hpp +++ b/source/mijin/types/path.hpp @@ -4,55 +4,384 @@ #if !defined(MIJIN_TYPES_PATH_HPP_INCLUDED) #define MIJIN_TYPES_PATH_HPP_INCLUDED 1 +#include "../debug/assert.hpp" #include "../internal/common.hpp" +#include "../util/traits.hpp" +#include #include #include namespace mijin { -template, template typename TAllocator = MIJIN_DEFAULT_ALLOCATOR> -class BasePath + +template +concept BasePathType = requires(const T& path) +{ + typename T::traits_t; + typename T::char_t; + typename T::char_traits_t; + typename T::allocator_t; + typename T::string_t; + typename T::string_view_t; + typename T::size_type; + typename T::difference_type; + typename T::path_view_t; + { T::SEPARATOR } -> std::convertible_to; + { path.stringView() } -> std::convertible_to; +}; + +template +concept PathType = BasePathType && std::is_same_v; + +template +concept WPathType = BasePathType && std::is_same_v; + +template +struct DefaultPathTraits +{ + using char_t = TChar; + using char_traits_t = std::char_traits; + using allocator_t = std::allocator; + using string_t = std::basic_string; + using string_view_t = std::basic_string_view; + using size_type = string_view_t::size_type; + using difference_type = string_view_t::difference_type; + static constexpr char_t SEPARATOR = separator; + + static constexpr bool isAbsolute(string_view_t path) MIJIN_NOEXCEPT + { + return !path.empty() && path[0] == SEPARATOR; + } +}; + +template> +class BasePathView; + +template> +class MixinPath; + +namespace impl +{ +template +class BasePathIterator { -public: - using string_t = std::basic_string>; - using size_type = string_t::size_type; - using difference_type = string_t::difference_type; private: - string_t storage_; + std::string_view full_; + std::string_view::iterator pos_; + std::string_view::iterator next_; + + BasePathIterator(std::string_view full, std::string_view::iterator pos) MIJIN_NOEXCEPT : full_(full), pos_(pos) { + if (pos != full_.end() && *pos == separator) { + ++pos; + } + findNext(); + } public: - BasePath() = default; - BasePath(const BasePath&) = default; - BasePath(BasePath&&) = default; - BasePath(string_t string) MIJIN_NOEXCEPT_IF(std::is_nothrow_move_constructible()) : storage_(std::move(string)) { - simplify(); + BasePathIterator() MIJIN_NOEXCEPT : pos_(full_.end()), next_(full_.end()) {} + BasePathIterator(const BasePathIterator&) noexcept = default; + + BasePathIterator& operator=(const BasePathIterator&) noexcept = default; + + bool operator==(const BasePathIterator& other) MIJIN_NOEXCEPT { MIJIN_ASSERT(full_ == other.full_, "Comparing unrelated iterators."); return pos_ == other.pos_; } + bool operator!=(const BasePathIterator& other) MIJIN_NOEXCEPT { MIJIN_ASSERT(full_ == other.full_, "Comparing unrelated iterators."); return pos_ != other.pos_; } + bool operator<(const BasePathIterator& other) MIJIN_NOEXCEPT { MIJIN_ASSERT(full_ == other.full_, "Comparing unrelated iterators."); return pos_ < other.pos_; } + bool operator<=(const BasePathIterator& other) MIJIN_NOEXCEPT { MIJIN_ASSERT(full_ == other.full_, "Comparing unrelated iterators."); return pos_ <= other.pos_; } + bool operator>(const BasePathIterator& other) MIJIN_NOEXCEPT { MIJIN_ASSERT(full_ == other.full_, "Comparing unrelated iterators."); return pos_ > other.pos_; } + bool operator>=(const BasePathIterator& other) MIJIN_NOEXCEPT { MIJIN_ASSERT(full_ == other.full_, "Comparing unrelated iterators."); return pos_ >= other.pos_; } + + std::string_view operator*() const MIJIN_NOEXCEPT + { + MIJIN_ASSERT(pos_ != full_.end(), "Dereferencing an invalid iterator."); + return {pos_, next_}; } - BasePath& operator=(const BasePath&) = default; - BasePath& operator=(BasePath&&) = default; + BasePathIterator& operator++() MIJIN_NOEXCEPT + { + MIJIN_ASSERT(pos_ != full_.end(), "Iterating past end."); + if (next_ == full_.end()) { + pos_ = full_.end(); + } + else + { + pos_ = std::next(next_); + findNext(); + } + return *this; + } - auto operator<=>(const BasePath&) const noexcept = default; + BasePathIterator operator++(int) const MIJIN_NOEXCEPT + { + BasePathIterator copy(*this); + ++copy; + return copy; + } + + // TODO + // BasePathIterator& operator--() MIJIN_NOEXCEPT + // { + // MIJIN_ASSERT(pos_ != full_.begin(), "Iterating past begin."); + // next_ = std::prev(pos_); + // pos_ = std::find(std::reverse_iterator(next_), std::reverse_iterator(full_.begin()), separator).base(); + // } +private: + void findNext() + { + next_ = std::find(pos_, full_.end(), separator); + } + + template + friend class ::mijin::MixinPath; +}; +} + +template +class MixinPath +{ +public: + using traits_t = TTraits; + using char_t = typename traits_t::char_t; + using char_traits_t = typename traits_t::char_traits_t; + using allocator_t = typename traits_t::allocator_t; + using string_t = typename traits_t::string_t; + using string_view_t = typename traits_t::string_view_t; + using size_type = typename traits_t::size_type; + using difference_type = typename traits_t::difference_type; + using path_view_t = BasePathView; + using iterator = impl::BasePathIterator; + using const_iterator = iterator; + static constexpr char_t SEPARATOR = traits_t::SEPARATOR; + + constexpr bool operator==(string_view_t stringView) const MIJIN_NOEXCEPT { return stringView() == stringView; } + constexpr bool operator==(const TChar* cString) const MIJIN_NOEXCEPT { return stringView() == cString; } [[nodiscard]] - const string_t& string() const MIJIN_NOEXCEPT + constexpr string_view_t getName() const MIJIN_NOEXCEPT; + + [[nodiscard]] + constexpr path_view_t getParent() const MIJIN_NOEXCEPT; + + [[nodiscard]] + constexpr string_view_t stringView() const MIJIN_NOEXCEPT + { + return static_cast(this)->stringViewImpl(); + } + + [[nodiscard]] + constexpr bool empty() const MIJIN_NOEXCEPT { return stringView().empty(); } + + [[nodiscard]] + constexpr size_type size() const MIJIN_NOEXCEPT { return stringView().size(); } + + [[nodiscard]] + constexpr bool isAbsolute() const MIJIN_NOEXCEPT + { + return traits_t::isAbsolute(stringView()); + } + + [[nodiscard]] + iterator begin() const MIJIN_NOEXCEPT { return iterator(stringView(), stringView().begin()); } + + [[nodiscard]] + iterator end() const MIJIN_NOEXCEPT { return iterator(stringView(), stringView().end()); } +private: + + template + friend class MixinPath; +}; + +template +constexpr bool operator==(const MixinPath& pathA, const MixinPath& pathB) MIJIN_NOEXCEPT +{ + return pathA.stringView() == pathB.stringView(); +} + +template> +class BasePath : public MixinPath, TChar, TTraits> +{ +public: + using mixin_t = MixinPath, TChar, TTraits>; + using typename mixin_t::traits_t; + using typename mixin_t::char_t; + using typename mixin_t::char_traits_t; + using typename mixin_t::allocator_t; + using typename mixin_t::string_t; + using typename mixin_t::string_view_t; + using typename mixin_t::difference_type; + using typename mixin_t::path_view_t; + static constexpr char_t SEPARATOR = mixin_t::SEPARATOR; +private: + struct NoSimplify {}; + + string_t storage_; + + constexpr BasePath(string_t storage, NoSimplify) MIJIN_NOEXCEPT_IF(std::is_nothrow_move_constructible()) : storage_(std::move(storage)) {} +public: + constexpr BasePath() = default; + constexpr BasePath(const BasePath&) = default; + constexpr BasePath(BasePath&&) = default; + constexpr BasePath(string_t string) MIJIN_NOEXCEPT_IF(std::is_nothrow_move_constructible()) : storage_(std::move(string)) { + simplify(); + } + constexpr BasePath(const char_t* cString, allocator_t allocator = {}) : BasePath(string_t(cString, std::move(allocator))) {} + constexpr BasePath(string_view_t stringView, allocator_t allocator = {}) : BasePath(string_t(stringView, std::move(allocator))) {} + template requires (!std::is_same_v, TConcreteOther>) + explicit constexpr BasePath(const MixinPath other, allocator_t allocator = {}) : BasePath(string_t(other.stringView(), std::move(allocator)), NoSimplify()) {} + + constexpr BasePath& operator=(const BasePath&) = default; + constexpr BasePath& operator=(BasePath&&) = default; + + constexpr auto operator<=>(const BasePath&) const noexcept = default; + using mixin_t::operator==; + + template + BasePath& operator/=(const MixinPath& other); + BasePath& operator/=(string_view_t more); + + template + BasePath operator/(const MixinPath& other) const; + BasePath operator/(string_view_t more) const; + + [[nodiscard]] + constexpr const string_t& string() const MIJIN_NOEXCEPT + { + return storage_; + } + + [[nodiscard]] + constexpr string_view_t stringViewImpl() const MIJIN_NOEXCEPT { return storage_; } private: - void simplify() MIJIN_NOEXCEPT; + constexpr void simplify() MIJIN_NOEXCEPT; }; using Path = BasePath; using WPath = BasePath; -template typename TAllocator> -void BasePath::simplify() MIJIN_NOEXCEPT +using NativePath = Path; // TODO + +template +class BasePathView : public MixinPath, TChar, TTraits> +{ +public: + using mixin_t = MixinPath, TChar, TTraits>; + using typename mixin_t::char_t; + using typename mixin_t::string_view_t; +private: + string_view_t view_; +public: + constexpr BasePathView() noexcept = default; + constexpr BasePathView(const BasePathView&) noexcept = default; + explicit constexpr BasePathView(string_view_t view) MIJIN_NOEXCEPT : view_(view) {} + template + constexpr BasePathView(const TIterator& begin, const TIterator& end) MIJIN_NOEXCEPT : view_(begin, end) {} + template requires (!std::is_same_v, TConcreteOther>) + constexpr BasePathView(const MixinPath& other) MIJIN_NOEXCEPT : view_(other.stringView()) {} + + constexpr BasePathView& operator=(const BasePathView&) noexcept = default; + + constexpr auto operator<=>(const BasePathView&) const noexcept = default; + using mixin_t::operator==; + + [[nodiscard]] + constexpr string_view_t stringViewImpl() const MIJIN_NOEXCEPT + { + return view_; + } +}; + +using PathView = BasePathView; +using WPathView = BasePathView; + +using NativePathView = PathView; // TODO + +template +constexpr auto MixinPath::getName() const MIJIN_NOEXCEPT -> string_view_t +{ + const string_view_t strView = stringView(); + auto it = std::ranges::find(std::ranges::reverse_view(strView), SEPARATOR).base(); + return {it, strView.end()}; +} + +template +constexpr auto MixinPath::getParent() const MIJIN_NOEXCEPT -> path_view_t +{ + const string_view_t strView = stringView(); + auto it = std::ranges::find(std::ranges::reverse_view(strView), SEPARATOR).base(); + if (it == strView.begin()) { + return {}; + } + if (std::prev(it) == strView.begin()) { + return {strView.begin(), it}; // edge case, return "/" instead of nothing + } + return {strView.begin(), std::prev(it)}; +} + +template +template +auto BasePath::operator/=(const MixinPath& other) -> BasePath& +{ + if (other.isAbsolute()) + { + storage_ = other.stringView(); + } + else if (!other.empty()) + { + if (other.stringView()[0] != SEPARATOR) + { + storage_.reserve(storage_.size() + other.size() + 1); + storage_.push_back(SEPARATOR); + } + storage_.append_range(other.stringView()); + } + return *this; +} + +template +auto BasePath::operator/=(string_view_t more) -> BasePath& +{ + operator/=(path_view_t(more)); + simplify(); + return *this; +} + +template +template +auto BasePath::operator/(const MixinPath& other) const -> BasePath +{ + if (other.isAbsolute() || other.empty()) + { + return BasePath(string_t(other.stringView(), storage_.get_allocator()), NoSimplify()); + } + const bool addSeparator = other.stringView()[0] != SEPARATOR; + string_t combined(storage_.get_allocator()); + combined.reserve(storage_.size() + other.stringView().size() + (addSeparator ? 1 : 0)); + combined.append_range(storage_); + if (addSeparator) { + combined.push_back(SEPARATOR); + } + combined.append_range(other.stringView()); + return BasePath(std::move(combined), NoSimplify()); +} + +template +auto BasePath::operator/(string_view_t other) const -> BasePath +{ + BasePath combined = (*this / path_view_t(other)); + combined.simplify(); + return combined; +} + +template +constexpr void BasePath::simplify() MIJIN_NOEXCEPT { // step 1: remove double slashes difference_type moveBy = 0; for (auto it = std::next(storage_.begin()); it < storage_.end(); ++it) { - const bool doubleSlash = (*it == separator && *std::prev(it) == separator); // check this before moving the current character, as that might create a double slash itself + const bool doubleSlash = (*it == SEPARATOR && *std::prev(it) == SEPARATOR); // check this before moving the current character, as that might create a double slash itself if (moveBy > 0) { *std::prev(it, moveBy) = *it; } @@ -61,7 +390,7 @@ void BasePath::simplify() MIJIN_NOEXCEPT } } // step 1.5: remove trailing slash (but only if it's not the only remaining char) - if (moveBy < static_cast(storage_.size() - 1) && storage_[storage_.size() - moveBy - 1] == separator) { + if (moveBy < static_cast(storage_.size() - 1) && storage_[storage_.size() - moveBy - 1] == SEPARATOR) { ++moveBy; } storage_.resize(storage_.size() - moveBy); @@ -74,8 +403,8 @@ void BasePath::simplify() MIJIN_NOEXCEPT { *std::prev(it, moveBy) = *it; } - if (*std::prev(it, moveBy) == TChar('.') && *std::prev(it, moveBy + 1) == TChar('.') && *std::prev(it, moveBy + 2) == separator - && (std::next(it) == storage_.end() || *std::next(it) == separator)) + if (*std::prev(it, moveBy) == TChar('.') && *std::prev(it, moveBy + 1) == TChar('.') && *std::prev(it, moveBy + 2) == SEPARATOR + && (std::next(it) == storage_.end() || *std::next(it) == SEPARATOR)) { if (std::prev(it, moveBy + 2) == storage_.begin()) { @@ -86,7 +415,7 @@ void BasePath::simplify() MIJIN_NOEXCEPT // find the start of the preceeding segment for (auto itStart = std::prev(it, moveBy + 3);; --itStart) { - if (*std::prev(itStart) == separator || std::prev(itStart) == storage_.begin()) + if (*std::prev(itStart) == SEPARATOR || std::prev(itStart) == storage_.begin()) { // /path/with/../double/dot // itStart --A A @@ -110,8 +439,8 @@ void BasePath::simplify() MIJIN_NOEXCEPT const bool atStart = (it == storage_.begin()); const bool atEnd = (std::next(it) == storage_.end()); const bool emptyEle = (*it == TChar('.') // char is a dot - && (atStart || *std::prev(it, moveBy + 1) == separator) // previous is a slash or doesn't exist - && (atEnd || *std::next(it) == separator)); // next is a slash or doesn't exist + && (atStart || *std::prev(it, moveBy + 1) == SEPARATOR) // previous is a slash or doesn't exist + && (atEnd || *std::next(it) == SEPARATOR)); // next is a slash or doesn't exist if (moveBy > 0) { *std::prev(it, moveBy) = *it; } @@ -124,6 +453,59 @@ void BasePath::simplify() MIJIN_NOEXCEPT } storage_.resize(storage_.size() - moveBy); } -} // namespace shiken + +template> +constexpr bool verifyPathString(std::basic_string_view stringView) MIJIN_NOEXCEPT +{ + for (auto it = std::next(stringView.begin()); it < stringView.end(); ++it) + { + if(*it == separator && *std::prev(it) == separator) { + return false; // double slash + } + if (it != std::next(stringView.begin()) + && *it == TChar('.') && *std::prev(it) == TChar('.') && *std::prev(it, 2) == separator + && (std::next(it) == stringView.end() || *std::next(it) == separator)) { + return false; // "/.." + } + const bool atStart = (it == stringView.begin()); + const bool atEnd = (std::next(it) == stringView.end()); + if(*it == TChar('.') // char is a dot + && (atStart || *std::prev(it) == separator) // previous is a slash or doesn't exist + && (atEnd || *std::next(it) == separator)) // next is a slash or doesn't exist + { + return false; // "/." + } + } + return true; +} + +consteval PathView operator""_pv(const char* cString, std::size_t len) +{ + if (!verifyPathString(std::string_view(cString, len))) { + throw "Invalid path string."; + } + return PathView(std::string_view(cString, len)); +} + +consteval WPathView operator""_pv(const wchar_t* cString, std::size_t len) +{ + if (!verifyPathString(std::wstring_view(cString, len))) { + throw "Invalid path string."; + } + return WPathView(std::wstring_view(cString, len)); +} + +[[nodiscard]] +NativePath getWorkingDirectory(); +} // namespace mijin + +template +struct std::hash +{ + std::size_t operator()(const TPath& path) const MIJIN_NOEXCEPT + { + return std::hash()(path.stringView()); + } +}; #endif // !defined(MIJIN_TYPES_PATH_HPP_INCLUDED) diff --git a/source/mijin/virtual_filesystem/filesystem.cpp b/source/mijin/virtual_filesystem/filesystem.cpp index 4960d9e..a1de33a 100644 --- a/source/mijin/virtual_filesystem/filesystem.cpp +++ b/source/mijin/virtual_filesystem/filesystem.cpp @@ -3,6 +3,10 @@ #include "../platform/folders.hpp" +#include + +namespace fs = std::filesystem; + namespace mijin { @@ -26,100 +30,91 @@ namespace mijin // internal functions // +namespace +{ +void doGetFileInfo(const fs::path& stlPath, FileInfo& outInfo) +{ + std::error_code err; + + outInfo.isFolder = fs::is_directory(stlPath, err); + outInfo.isSymlink = fs::is_symlink(stlPath, err); + outInfo.isSpecial = !outInfo.isFolder && !fs::is_regular_file(stlPath, err); + outInfo.isHidden = stlPath.c_str()[0] == '.'; // at least for Linux + if (outInfo.isFolder) + { + const fs::directory_iterator dirIt(stlPath, err); + if (err != std::error_code{}) + { + outInfo.size = std::distance(dirIt, fs::directory_iterator()); + } + else + { + outInfo.size = 0; + } + } + else if (!outInfo.isSpecial) + { + outInfo.size = fs::file_size(stlPath, err); + if (err) + { + outInfo.size = 0; + } + } +} +} + // // public functions // -fs::path OSFileSystemAdapter::getHomeFolder() +std::vector OSFileSystemAdapter::listFiles(PathView folder) { - return getKnownFolder(KnownFolder::USER_HOME); -} - -std::vector OSFileSystemAdapter::listFiles(const fs::path& folder) -{ - std::vector entries; + std::vector entries; std::error_code err; - const fs::directory_iterator iterator(folder, fs::directory_options::skip_permission_denied, err); + + const fs::path stlFolder(folder.stringView()); + const fs::directory_iterator iterator(stlFolder, fs::directory_options::skip_permission_denied, err); if (err) { return {}; // TODO: propagate? } - for (const fs::directory_entry& entry : iterator) + for (const fs::directory_entry& stlEntry : iterator) { - FileInfo& info = entries.emplace_back(); - info.path = entry.path(); - info.exists = true; - info.isFolder = entry.is_directory(err); - info.isSymlink = entry.is_symlink(err); - info.isSpecial = !info.isFolder && !entry.is_regular_file(err); - info.isHidden = info.path.filename().string().starts_with('.'); // at least for Linux - if (info.isFolder) - { - std::error_code errorCode; - fs::directory_iterator dirIt(info.path, errorCode); - if (errorCode != std::error_code{}) - { - info.size = std::distance(dirIt, fs::directory_iterator()); - } - else - { - info.size = 0; - } - } - else if (!info.isSpecial) - { - info.size = entry.file_size(err); - if (err) - { - info.size = 0; - } - } + FolderEntry& entry = entries.emplace_back(); + entry.path = stlEntry.path().generic_string(); + entry.info.exists = true; + doGetFileInfo(stlEntry.path(), entry.info); } return entries; } -FileInfo OSFileSystemAdapter::getFileInfo(const fs::path& file) +FileInfo OSFileSystemAdapter::getFileInfo(PathView file) { + const fs::path stlFile(file.stringView()); + FileInfo info = {}; std::error_code err; - info.path = file; - info.exists = fs::exists(file, err); + info.exists = fs::exists(stlFile, err); if (info.exists) { - info.isFolder = fs::is_directory(file, err); - info.isSymlink = fs::is_symlink(file, err); - info.isSpecial = !info.isFolder && !fs::is_regular_file(file, err); - info.isHidden = info.path.filename().string().starts_with('.'); // at least for Linux - if (info.isFolder) { - MIJIN_TRY - { - info.size = std::distance(fs::directory_iterator(info.path), fs::directory_iterator()); - } - MIJIN_CATCH(std::runtime_error&) - { - info.size = 0; - } - } - else if (!info.isSpecial) - { - info.size = fs::file_size(file, err); - if (err) { - info.size = 0; - } - } + doGetFileInfo(fs::path(file.stringView()), info); } return info; } -Optional OSFileSystemAdapter::getNativePath(const fs::path& file) +Optional OSFileSystemAdapter::getNativePath(PathView file) { - return file; + return NativePath(file); } -StreamError OSFileSystemAdapter::open(const fs::path& path, FileOpenMode mode, std::unique_ptr& outStream) +StreamError OSFileSystemAdapter::open(PathView path, FileOpenMode mode, std::unique_ptr& outStream) { - const std::string pathStr = path.string(); + const PathView::string_view_t pathSv = path.stringView(); + char* pathStr = static_cast(alloca(pathSv.size() + 1)); + std::memcpy(pathStr, pathSv.data(), pathSv.size()); + pathStr[pathSv.size()] = '\0'; + auto stream = std::make_unique(); - const StreamError error = stream->open(pathStr.c_str(), mode); + const StreamError error = stream->open(pathStr, mode); if (error != StreamError::SUCCESS) { return error; } diff --git a/source/mijin/virtual_filesystem/filesystem.hpp b/source/mijin/virtual_filesystem/filesystem.hpp index bd1322c..cdc6ea4 100644 --- a/source/mijin/virtual_filesystem/filesystem.hpp +++ b/source/mijin/virtual_filesystem/filesystem.hpp @@ -6,17 +6,15 @@ #include #include -#include #include #include #include #include "../container/optional.hpp" #include "../io/stream.hpp" #include "../internal/common.hpp" +#include "../types/path.hpp" #include "../util/hash.hpp" -namespace fs = std::filesystem; - namespace mijin { @@ -34,7 +32,6 @@ namespace mijin struct FileInfo { - fs::path path; /// either file size in bytes, or number of entries if folder std::size_t size = 0; bool exists : 1 = false; @@ -44,17 +41,23 @@ struct FileInfo bool isHidden : 1 = false; }; +struct FolderEntry +{ + Path path; + FileInfo info; +}; + // basically just a thin wrapper around adapter + path, for utility class PathReference { private: class FileSystemAdapter* adapter_ = nullptr; - fs::path path_ = {}; + Path path_ = {}; public: PathReference() = default; PathReference(const PathReference&) = default; PathReference(PathReference&&) = default; - PathReference(class FileSystemAdapter* adapter, fs::path path) MIJIN_NOEXCEPT : adapter_(adapter), path_(std::move(path)) {} + PathReference(class FileSystemAdapter* adapter, Path path) MIJIN_NOEXCEPT : adapter_(adapter), path_(std::move(path)) {} PathReference& operator=(const PathReference&) = default; PathReference& operator=(PathReference&&) MIJIN_NOEXCEPT = default; @@ -63,13 +66,13 @@ public: [[nodiscard]] bool empty() const MIJIN_NOEXCEPT { return adapter_ == nullptr; } [[nodiscard]] class FileSystemAdapter* getAdapter() const noexcept { return adapter_; } - [[nodiscard]] const fs::path& getPath() const MIJIN_NOEXCEPT { return path_; } + [[nodiscard]] const Path& getPath() const MIJIN_NOEXCEPT { return path_; } [[nodiscard]] inline FileInfo getInfo() const; - [[nodiscard]] inline Optional getNativePath() const; + [[nodiscard]] inline Optional getNativePath() const; [[nodiscard]] inline StreamError open(FileOpenMode mode, std::unique_ptr& outStream) const; [[nodiscard]] - PathReference operator/(const fs::path& more) const + PathReference operator/(const Path& more) const { return PathReference(adapter_, path_ / more); } @@ -80,16 +83,14 @@ class FileSystemAdapter public: virtual ~FileSystemAdapter() = default; - [[deprecated("Will be removed ASAP")]] - [[nodiscard]] virtual fs::path getHomeFolder() { return {}; } // TODO: get rid of this ... - [[nodiscard]] virtual std::vector listFiles(const fs::path& folder) = 0; - [[nodiscard]] virtual FileInfo getFileInfo(const fs::path& file) = 0; - [[nodiscard]] virtual Optional getNativePath(const fs::path& /* file */) { return NULL_OPTIONAL; } - [[nodiscard]] virtual StreamError open(const fs::path& path, FileOpenMode mode, std::unique_ptr& outStream) = 0; - virtual void getAllPaths(const fs::path& path, std::vector& outPaths) { outPaths.push_back(getPath(path)); } + [[nodiscard]] virtual std::vector listFiles(PathView folder) = 0; + [[nodiscard]] virtual FileInfo getFileInfo(PathView file) = 0; + [[nodiscard]] virtual Optional getNativePath(PathView /* file */) { return NULL_OPTIONAL; } + [[nodiscard]] virtual StreamError open(PathView path, FileOpenMode mode, std::unique_ptr& outStream) = 0; + virtual void getAllPaths(PathView path, std::vector& outPaths) { outPaths.push_back(getPath(Path(path))); } - [[nodiscard]] PathReference getPath(fs::path path) MIJIN_NOEXCEPT { return PathReference(this, std::move(path)); } - [[nodiscard]] std::vector getAllPaths(const fs::path& path) + [[nodiscard]] PathReference getPath(Path path) MIJIN_NOEXCEPT { return PathReference(this, std::move(path)); } + [[nodiscard]] std::vector getAllPaths(PathView path) { std::vector paths; getAllPaths(path, paths); @@ -100,11 +101,10 @@ public: class OSFileSystemAdapter : public FileSystemAdapter { public: - fs::path getHomeFolder() override; - std::vector listFiles(const fs::path& folder) override; - FileInfo getFileInfo(const fs::path& file) override; - Optional getNativePath(const fs::path& file) override; - StreamError open(const fs::path& path, FileOpenMode mode, std::unique_ptr& outStream) override; + std::vector listFiles(PathView folder) override; + FileInfo getFileInfo(PathView file) override; + Optional getNativePath(PathView file) override; + StreamError open(PathView, FileOpenMode mode, std::unique_ptr& outStream) override; static OSFileSystemAdapter& getInstance(); }; @@ -118,7 +118,7 @@ inline FileInfo PathReference::getInfo() const return adapter_->getFileInfo(path_); } -Optional PathReference::getNativePath() const +Optional PathReference::getNativePath() const { return adapter_->getNativePath(path_); } diff --git a/source/mijin/virtual_filesystem/memory.cpp b/source/mijin/virtual_filesystem/memory.cpp index 8e1c6fd..15f03e0 100644 --- a/source/mijin/virtual_filesystem/memory.cpp +++ b/source/mijin/virtual_filesystem/memory.cpp @@ -5,30 +5,31 @@ namespace mijin { -std::vector MemoryFileSystemAdapter::listFiles(const fs::path& folder) +std::vector MemoryFileSystemAdapter::listFiles(PathView folder) { const detail::MemoryFolder* folderObj = findFolder(folder); if (folderObj == nullptr) { return {}; } - std::vector result; + std::vector result; result.reserve(folderObj->folders.size() + folderObj->files.size()); + const Path folderPath(folder); for (const auto& [name, subFolder] : folderObj->folders) { - result.push_back(folderInfo(folder / name, subFolder)); + result.emplace_back(folderPath / name, folderInfo(subFolder)); } for (const auto& [name, file] : folderObj->files) { - result.push_back(fileInfo(folder / name, file)); + result.emplace_back(folderPath / name, fileInfo(file)); } return result; } -FileInfo MemoryFileSystemAdapter::getFileInfo(const fs::path& file) +FileInfo MemoryFileSystemAdapter::getFileInfo(PathView file) { #if 0 // shouldn't be necessary // empty means root @@ -43,38 +44,38 @@ FileInfo MemoryFileSystemAdapter::getFileInfo(const fs::path& file) } #endif - const detail::MemoryFolder* folderObj = findFolder(file.parent_path()); + const detail::MemoryFolder* folderObj = findFolder(file.getParent()); if (folderObj == nullptr) { return {}; } - const std::string filename = file.filename().generic_string(); + const std::string_view filename = file.getName(); if (auto itFolder = folderObj->folders.find(filename); itFolder != folderObj->folders.end()) { - return folderInfo(file, itFolder->second); + return folderInfo(itFolder->second); } if (auto itFile = folderObj->files.find(filename); itFile != folderObj->files.end()) { - return fileInfo(file, itFile->second); + return fileInfo(itFile->second); } return {}; } -StreamError MemoryFileSystemAdapter::open(const fs::path& path, FileOpenMode mode, std::unique_ptr& outStream) +StreamError MemoryFileSystemAdapter::open(PathView path, FileOpenMode mode, std::unique_ptr& outStream) { if (mode != FileOpenMode::READ) { return StreamError::IO_ERROR; } - const detail::MemoryFolder* folderObj = findFolder(path.parent_path()); + const detail::MemoryFolder* folderObj = findFolder(path.getParent()); if (folderObj == nullptr) { return StreamError::IO_ERROR; } - auto itFile = folderObj->files.find(path.filename().generic_string()); + auto itFile = folderObj->files.find(path.getName()); if (itFile == folderObj->files.end()) { return StreamError::IO_ERROR; @@ -87,10 +88,10 @@ StreamError MemoryFileSystemAdapter::open(const fs::path& path, FileOpenMode mod return StreamError::SUCCESS; } -bool MemoryFileSystemAdapter::addFile(const fs::path& path, std::span data, Overwrite overwrite, CopyData copyData) +bool MemoryFileSystemAdapter::addFile(PathView path, std::span data, Overwrite overwrite, CopyData copyData) { - detail::MemoryFolder& folder = *findFolder(path.parent_path(), true); - std::string filename = path.filename().generic_string(); + detail::MemoryFolder& folder = *findFolder(path.getParent(), true); + const std::string_view filename = path.getName(); if (folder.folders.contains(filename)) { @@ -107,29 +108,28 @@ bool MemoryFileSystemAdapter::addFile(const fs::path& path, std::spanfolders.find(partname); + auto it = folder->folders.find(part); if (it == folder->folders.end()) { if (!create) { return nullptr; } - folder = &folder->folders[std::move(partname)]; + folder = &folder->folders[std::string(part)]; } else { @@ -139,20 +139,18 @@ detail::MemoryFolder* MemoryFileSystemAdapter::findFolder(const fs::path& path, return folder; } -FileInfo MemoryFileSystemAdapter::folderInfo(const fs::path& path, const detail::MemoryFolder& folder) const MIJIN_NOEXCEPT +FileInfo MemoryFileSystemAdapter::folderInfo(const detail::MemoryFolder& folder) const MIJIN_NOEXCEPT { return { - .path = path, .size = folder.folders.size() + folder.files.size(), .exists = true, .isFolder = true }; } -FileInfo MemoryFileSystemAdapter::fileInfo(const fs::path& path, const detail::MemoryFile& file) const MIJIN_NOEXCEPT +FileInfo MemoryFileSystemAdapter::fileInfo(const detail::MemoryFile& file) const MIJIN_NOEXCEPT { return { - .path = path, .size = file.data.size(), .exists = true }; diff --git a/source/mijin/virtual_filesystem/memory.hpp b/source/mijin/virtual_filesystem/memory.hpp index c7736bc..febbeb0 100644 --- a/source/mijin/virtual_filesystem/memory.hpp +++ b/source/mijin/virtual_filesystem/memory.hpp @@ -35,20 +35,20 @@ private: detail::MemoryFolder root_; std::vector> fileData_; public: - [[nodiscard]] std::vector listFiles(const fs::path& folder) override; - [[nodiscard]] FileInfo getFileInfo(const fs::path& file) override; - [[nodiscard]] StreamError open(const fs::path& path, FileOpenMode mode, std::unique_ptr& outStream) override; + [[nodiscard]] std::vector listFiles(PathView folder) override; + [[nodiscard]] FileInfo getFileInfo(PathView file) override; + [[nodiscard]] StreamError open(PathView path, FileOpenMode mode, std::unique_ptr& outStream) override; - bool addFile(const fs::path& path, std::span data, Overwrite overwrite = Overwrite::NO, CopyData copyData = CopyData::NO); - bool addFile(const fs::path& path, std::span data, CopyData copyData) + bool addFile(PathView path, std::span data, Overwrite overwrite = Overwrite::NO, CopyData copyData = CopyData::NO); + bool addFile(PathView path, std::span data, CopyData copyData) { return addFile(path, data, Overwrite::NO, copyData); } - void addFolder(const fs::path& path); + void addFolder(PathView path); private: - detail::MemoryFolder* findFolder(const fs::path& path, bool create = false) MIJIN_NOEXCEPT; - FileInfo folderInfo(const fs::path& path, const detail::MemoryFolder& folder) const MIJIN_NOEXCEPT; - FileInfo fileInfo(const fs::path& path, const detail::MemoryFile& file) const MIJIN_NOEXCEPT; + detail::MemoryFolder* findFolder(PathView path, bool create = false) MIJIN_NOEXCEPT; + FileInfo folderInfo(const detail::MemoryFolder& folder) const MIJIN_NOEXCEPT; + FileInfo fileInfo(const detail::MemoryFile& file) const MIJIN_NOEXCEPT; }; } // namespace mijin diff --git a/source/mijin/virtual_filesystem/relative.hpp b/source/mijin/virtual_filesystem/relative.hpp index 5a024db..c31b98e 100644 --- a/source/mijin/virtual_filesystem/relative.hpp +++ b/source/mijin/virtual_filesystem/relative.hpp @@ -27,10 +27,10 @@ class RelativeFileSystemAdapter : public FileSystemAdapter { private: TWrapped wrapped_; - fs::path root_; + Path root_; public: template - explicit RelativeFileSystemAdapter(fs::path root, TArgs&&... args) + explicit RelativeFileSystemAdapter(Path root, TArgs&&... args) : wrapped_(std::forward(args)...), root_(std::move(root)) {} RelativeFileSystemAdapter(const RelativeFileSystemAdapter&) = default; RelativeFileSystemAdapter(RelativeFileSystemAdapter&&) MIJIN_NOEXCEPT = default; @@ -38,13 +38,12 @@ public: RelativeFileSystemAdapter& operator=(const RelativeFileSystemAdapter&) = default; RelativeFileSystemAdapter& operator=(RelativeFileSystemAdapter&&) MIJIN_NOEXCEPT = default; - fs::path getHomeFolder() override; - std::vector listFiles(const fs::path& folder) override; - FileInfo getFileInfo(const fs::path& file) override; - Optional getNativePath(const fs::path& file) override; - StreamError open(const fs::path& path, FileOpenMode mode, std::unique_ptr& outStream) override; + std::vector listFiles(PathView folder) override; + FileInfo getFileInfo(PathView file) override; + Optional getNativePath(PathView file) override; + StreamError open(PathView path, FileOpenMode mode, std::unique_ptr& outStream) override; private: - fs::path appendPath(const fs::path& other) const MIJIN_NOEXCEPT; + Path appendPath(PathView other) const MIJIN_NOEXCEPT; }; // @@ -52,51 +51,44 @@ private: // template -fs::path RelativeFileSystemAdapter::getHomeFolder() +std::vector RelativeFileSystemAdapter::listFiles(PathView folder) { - return root_; -} - -template -std::vector RelativeFileSystemAdapter::listFiles(const fs::path& folder) -{ - std::vector result; + std::vector result; result = wrapped_.listFiles(appendPath(folder)); - for (FileInfo& fileInfo : result) { - fileInfo.path = "/" / fileInfo.path.lexically_relative(root_); + for (FolderEntry& fileInfo : result) { + fileInfo.path = Path("/") / fileInfo.path.stringView().substr(root_.size()); } return result; } template -FileInfo RelativeFileSystemAdapter::getFileInfo(const fs::path& file) +FileInfo RelativeFileSystemAdapter::getFileInfo(PathView file) { return wrapped_.getFileInfo(appendPath(file)); } template -Optional RelativeFileSystemAdapter::getNativePath(const fs::path& file) +Optional RelativeFileSystemAdapter::getNativePath(PathView file) { return wrapped_.getNativePath(appendPath(file)); } template -StreamError RelativeFileSystemAdapter::open(const fs::path& path, FileOpenMode mode, std::unique_ptr& outStream) +StreamError RelativeFileSystemAdapter::open(PathView path, FileOpenMode mode, std::unique_ptr& outStream) { return wrapped_.open(appendPath(path), mode, outStream); } template -fs::path RelativeFileSystemAdapter::appendPath(const fs::path& other) const MIJIN_NOEXCEPT +Path RelativeFileSystemAdapter::appendPath(PathView other) const MIJIN_NOEXCEPT { - fs::path combinedPath = root_; - if (other.is_absolute()) { - combinedPath += other; - } - else { - combinedPath /= other; + Path combinedPath = root_; + std::string_view otherSv(other.stringView()); + if (!otherSv.empty() && otherSv[0] == Path::SEPARATOR) { + otherSv = otherSv.substr(1); } + combinedPath /= otherSv; return combinedPath; } @@ -105,7 +97,7 @@ namespace vfs_pipe template struct RelativeBuilder : Builder, RelativeFileSystemAdapter> { - fs::path root; + Path root; TBase base; std::unique_ptr> build() @@ -116,11 +108,11 @@ struct RelativeBuilder : Builder, RelativeFileSystemAdapt struct RelativeOptions { - fs::path root; + Path root; }; [[nodiscard]] -inline RelativeOptions relative_to(fs::path root) noexcept +inline RelativeOptions relative_to(Path root) noexcept { return {.root = std::move(root) }; } diff --git a/source/mijin/virtual_filesystem/stacked.cpp b/source/mijin/virtual_filesystem/stacked.cpp index 89a33c1..4c3c3b2 100644 --- a/source/mijin/virtual_filesystem/stacked.cpp +++ b/source/mijin/virtual_filesystem/stacked.cpp @@ -31,40 +31,20 @@ namespace mijin // public functions // -fs::path StackedFileSystemAdapter::getHomeFolder() +std::vector StackedFileSystemAdapter::listFiles(PathView folder) { - if (adapters_.empty()) { - return fs::path(); - } -#if MIJIN_COMPILER == MIJIN_COMPILER_MSVC -#pragma warning(push) -#pragma warning(disable : 4996) // yeah, we're using a deprecated function here, in order to implement another deprecated function ¯\_(ツ)_/¯ -#elif MIJIN_COMPILER == MIJIN_COMPILER_GCC || MIJIN_COMPILER == MIJIN_COMPILER_CLANG -#pragma GCC diagnostic push -#pragma GCC diagnostic ignored "-Wdeprecated-declarations" -#endif - return adapters_.front()->getHomeFolder(); -#if MIJIN_COMPILER == MIJIN_COMPILER_MSVC -#pragma warning(pop) -#elif MIJIN_COMPILER == MIJIN_COMPILER_GCC || MIJIN_COMPILER == MIJIN_COMPILER_CLANG -#pragma GCC diagnostic pop -#endif -} - -std::vector StackedFileSystemAdapter::listFiles(const fs::path& folder) -{ - std::vector files; + std::vector files; for (auto& adapter : adapters_) { - for (const FileInfo& file : adapter->listFiles(folder)) + for (const FolderEntry& entry : adapter->listFiles(folder)) { - auto it = std::find_if(files.begin(), files.end(), [&](const FileInfo& existing) + auto it = std::ranges::find_if(files, [&](const FolderEntry& existing) { - return existing.path == file.path; + return existing.path == entry.path; }); if (it == files.end()) { - files.push_back(file); + files.push_back(entry); } } } @@ -72,7 +52,7 @@ std::vector StackedFileSystemAdapter::listFiles(const fs::path& folder return files; } -FileInfo StackedFileSystemAdapter::getFileInfo(const fs::path& file) +FileInfo StackedFileSystemAdapter::getFileInfo(PathView file) { for (auto& adapter : adapters_) { @@ -85,11 +65,11 @@ FileInfo StackedFileSystemAdapter::getFileInfo(const fs::path& file) return {}; } -Optional StackedFileSystemAdapter::getNativePath(const fs::path& file) +Optional StackedFileSystemAdapter::getNativePath(PathView file) { for (auto& adapter : adapters_) { - Optional result = adapter->getNativePath(file); + Optional result = adapter->getNativePath(file); if (!result.empty()) { return result; @@ -98,7 +78,7 @@ Optional StackedFileSystemAdapter::getNativePath(const fs::path& file) return NULL_OPTIONAL; } -StreamError StackedFileSystemAdapter::open(const fs::path& path, FileOpenMode mode, std::unique_ptr& outStream) +StreamError StackedFileSystemAdapter::open(PathView path, FileOpenMode mode, std::unique_ptr& outStream) { // try to open existing files first for (auto& adapter : adapters_) @@ -125,7 +105,7 @@ StreamError StackedFileSystemAdapter::open(const fs::path& path, FileOpenMode mo return StreamError::IO_ERROR; } -void StackedFileSystemAdapter::getAllPaths(const fs::path& path, std::vector& outPaths) +void StackedFileSystemAdapter::getAllPaths(PathView path, std::vector& outPaths) { for (auto& adapter : adapters_) { diff --git a/source/mijin/virtual_filesystem/stacked.hpp b/source/mijin/virtual_filesystem/stacked.hpp index 70038bf..568bbd2 100644 --- a/source/mijin/virtual_filesystem/stacked.hpp +++ b/source/mijin/virtual_filesystem/stacked.hpp @@ -28,12 +28,11 @@ class StackedFileSystemAdapter : public FileSystemAdapter private: std::vector> adapters_; public: - fs::path getHomeFolder() override; - std::vector listFiles(const fs::path& folder) override; - FileInfo getFileInfo(const fs::path& file) override; - Optional getNativePath(const fs::path& file) override; - StreamError open(const fs::path& path, FileOpenMode mode, std::unique_ptr& outStream) override; - void getAllPaths(const fs::path &path, std::vector &outPaths) override; + std::vector listFiles(PathView folder) override; + FileInfo getFileInfo(PathView file) override; + Optional getNativePath(PathView file) override; + StreamError open(PathView path, FileOpenMode mode, std::unique_ptr& outStream) override; + void getAllPaths(PathView path, std::vector &outPaths) override; using FileSystemAdapter::getAllPaths; inline FileSystemAdapter* addAdapter(std::unique_ptr&& adapter) {