Added getNativePath() method to get the OS version of a virtual path.

This commit is contained in:
Patrick 2024-07-27 11:04:38 +02:00
parent 9b4425c495
commit e82c697d2e
5 changed files with 35 additions and 0 deletions

View File

@ -107,6 +107,11 @@ FileInfo OSFileSystemAdapter::getFileInfo(const fs::path& file)
return info; return info;
} }
Optional<fs::path> OSFileSystemAdapter::getNativePath(const fs::path& file)
{
return file;
}
StreamError OSFileSystemAdapter::open(const fs::path& path, FileOpenMode mode, std::unique_ptr<Stream>& outStream) StreamError OSFileSystemAdapter::open(const fs::path& path, FileOpenMode mode, std::unique_ptr<Stream>& outStream)
{ {
const std::string pathStr = path.string(); const std::string pathStr = path.string();

View File

@ -10,6 +10,7 @@
#include <sstream> #include <sstream>
#include <string> #include <string>
#include <vector> #include <vector>
#include "../container/optional.hpp"
#include "../io/stream.hpp" #include "../io/stream.hpp"
#include "../util/hash.hpp" #include "../util/hash.hpp"
@ -62,6 +63,7 @@ public:
[[nodiscard]] class FileSystemAdapter* getAdapter() const noexcept { return adapter_; } [[nodiscard]] class FileSystemAdapter* getAdapter() const noexcept { return adapter_; }
[[nodiscard]] const fs::path& getPath() const noexcept { return path_; } [[nodiscard]] const fs::path& getPath() const noexcept { return path_; }
[[nodiscard]] inline FileInfo getInfo() const; [[nodiscard]] inline FileInfo getInfo() const;
[[nodiscard]] inline Optional<fs::path> getNativePath() const;
[[nodiscard]] inline StreamError open(FileOpenMode mode, std::unique_ptr<Stream>& outStream) const; [[nodiscard]] inline StreamError open(FileOpenMode mode, std::unique_ptr<Stream>& outStream) const;
}; };
@ -74,6 +76,7 @@ public:
[[nodiscard]] virtual fs::path getHomeFolder() = 0; [[nodiscard]] virtual fs::path getHomeFolder() = 0;
[[nodiscard]] virtual std::vector<FileInfo> listFiles(const fs::path& folder) = 0; [[nodiscard]] virtual std::vector<FileInfo> listFiles(const fs::path& folder) = 0;
[[nodiscard]] virtual FileInfo getFileInfo(const fs::path& file) = 0; [[nodiscard]] virtual FileInfo getFileInfo(const fs::path& file) = 0;
[[nodiscard]] virtual Optional<fs::path> getNativePath(const fs::path& /* file */) { return NULL_OPTIONAL; }
[[nodiscard]] virtual StreamError open(const fs::path& path, FileOpenMode mode, std::unique_ptr<Stream>& outStream) = 0; [[nodiscard]] virtual StreamError open(const fs::path& path, FileOpenMode mode, std::unique_ptr<Stream>& outStream) = 0;
[[nodiscard]] PathReference getPath(fs::path path) noexcept { return PathReference(this, std::move(path)); } [[nodiscard]] PathReference getPath(fs::path path) noexcept { return PathReference(this, std::move(path)); }
@ -86,6 +89,7 @@ public:
fs::path getHomeFolder() override; fs::path getHomeFolder() override;
std::vector<FileInfo> listFiles(const fs::path& folder) override; std::vector<FileInfo> listFiles(const fs::path& folder) override;
FileInfo getFileInfo(const fs::path& file) override; FileInfo getFileInfo(const fs::path& file) override;
Optional<fs::path> getNativePath(const fs::path& file) override;
StreamError open(const fs::path& path, FileOpenMode mode, std::unique_ptr<Stream>& outStream) override; StreamError open(const fs::path& path, FileOpenMode mode, std::unique_ptr<Stream>& outStream) override;
static OSFileSystemAdapter& getInstance(); static OSFileSystemAdapter& getInstance();
@ -100,6 +104,11 @@ inline FileInfo PathReference::getInfo() const
return adapter_->getFileInfo(path_); return adapter_->getFileInfo(path_);
} }
Optional<fs::path> PathReference::getNativePath() const
{
return adapter_->getNativePath(path_);
}
inline StreamError PathReference::open(FileOpenMode mode, std::unique_ptr<Stream>& outStream) const inline StreamError PathReference::open(FileOpenMode mode, std::unique_ptr<Stream>& outStream) const
{ {
return adapter_->open(path_, mode, outStream); return adapter_->open(path_, mode, outStream);

View File

@ -41,6 +41,7 @@ public:
fs::path getHomeFolder() override; fs::path getHomeFolder() override;
std::vector<FileInfo> listFiles(const fs::path& folder) override; std::vector<FileInfo> listFiles(const fs::path& folder) override;
FileInfo getFileInfo(const fs::path& file) override; FileInfo getFileInfo(const fs::path& file) override;
Optional<fs::path> getNativePath(const fs::path& file) override;
StreamError open(const fs::path& path, FileOpenMode mode, std::unique_ptr<Stream>& outStream) override; StreamError open(const fs::path& path, FileOpenMode mode, std::unique_ptr<Stream>& outStream) override;
private: private:
fs::path appendPath(const fs::path& other) const noexcept; fs::path appendPath(const fs::path& other) const noexcept;
@ -80,6 +81,12 @@ FileInfo RelativeFileSystemAdapter<TWrapped>::getFileInfo(const fs::path& file)
return wrapped_.getFileInfo(appendPath(file)); return wrapped_.getFileInfo(appendPath(file));
} }
template<typename TWrapped>
Optional<fs::path> RelativeFileSystemAdapter<TWrapped>::getNativePath(const fs::path& file)
{
return wrapped_.getNativePath(appendPath(file));
}
template<typename TWrapped> template<typename TWrapped>
StreamError RelativeFileSystemAdapter<TWrapped>::open(const fs::path& path, FileOpenMode mode, std::unique_ptr<Stream>& outStream) StreamError RelativeFileSystemAdapter<TWrapped>::open(const fs::path& path, FileOpenMode mode, std::unique_ptr<Stream>& outStream)
{ {

View File

@ -90,6 +90,19 @@ FileInfo StackedFileSystemAdapter::getFileInfo(const fs::path& file)
return {}; return {};
} }
Optional<fs::path> StackedFileSystemAdapter::getNativePath(const fs::path& file)
{
for (auto& adapter : adapters_)
{
Optional<fs::path> result = adapter->getNativePath(file);
if (!result.empty())
{
return result;
}
}
return NULL_OPTIONAL;
}
StreamError StackedFileSystemAdapter::open(const fs::path& path, FileOpenMode mode, std::unique_ptr<Stream>& outStream) StreamError StackedFileSystemAdapter::open(const fs::path& path, FileOpenMode mode, std::unique_ptr<Stream>& outStream)
{ {
for (auto& adapter : adapters_) for (auto& adapter : adapters_)

View File

@ -32,6 +32,7 @@ public:
fs::path getHomeFolder() override; fs::path getHomeFolder() override;
std::vector<FileInfo> listFiles(const fs::path& folder) override; std::vector<FileInfo> listFiles(const fs::path& folder) override;
FileInfo getFileInfo(const fs::path& file) override; FileInfo getFileInfo(const fs::path& file) override;
Optional<fs::path> getNativePath(const fs::path& file) override;
StreamError open(const fs::path& path, FileOpenMode mode, std::unique_ptr<Stream>& outStream) override; StreamError open(const fs::path& path, FileOpenMode mode, std::unique_ptr<Stream>& outStream) override;
inline void addAdapter(std::unique_ptr<FileSystemAdapter>&& adapter) { inline void addAdapter(std::unique_ptr<FileSystemAdapter>&& adapter) {