Added getAllPaths() method to get combined results from stacked adapters.

This commit is contained in:
Patrick 2025-03-02 20:02:03 +01:00
parent 3d0f5b9a3f
commit b6657189d3
5 changed files with 75 additions and 2 deletions

View File

@ -1,12 +1,12 @@
#include "./folders.hpp" #include "./folders.hpp"
#include <cstdlib>
#include <ranges>
#include "../detect.hpp" #include "../detect.hpp"
#include "../debug/assert.hpp" #include "../debug/assert.hpp"
#include <cstdlib>
#if MIJIN_TARGET_OS == MIJIN_OS_WINDOWS #if MIJIN_TARGET_OS == MIJIN_OS_WINDOWS
# include <Shlobj.h> # include <Shlobj.h>
#endif #endif
@ -103,4 +103,48 @@ fs::path getKnownFolder(KnownFolder folder) MIJIN_NOEXCEPT
return {}; // don't know :/ return {}; // don't know :/
#endif #endif
} }
std::vector<fs::path> getAllConfigFolders(IncludeUser includeUser, IncludeSystem includeSystem) MIJIN_NOEXCEPT
{
std::vector<fs::path> result;
if (includeUser)
{
result.push_back(getKnownFolder(KnownFolder::USER_CONFIG_ROOT));
}
#if MIJIN_TARGET_OS == MIJIN_OS_LINUX
if (includeSystem)
{
if (const char* val = std::getenv("XDG_CONFIG_DIRS"); val != nullptr)
{
for (auto part : std::ranges::split_view(std::string_view(val), ':'))
{
result.emplace_back(part.begin(), part.end());
}
}
}
#endif
return result;
}
std::vector<fs::path> getAllDataFolders(IncludeUser includeUser, IncludeSystem includeSystem) MIJIN_NOEXCEPT
{
std::vector<fs::path> result;
if (includeUser)
{
result.push_back(getKnownFolder(KnownFolder::USER_DATA_ROOT));
}
#if MIJIN_TARGET_OS == MIJIN_OS_LINUX
if (includeSystem)
{
if (const char* val = std::getenv("XDG_DATA_DIRS"); val != nullptr)
{
for (auto part : std::ranges::split_view(std::string_view(val), ':'))
{
result.emplace_back(part.begin(), part.end());
}
}
}
#endif
return result;
}
} }

View File

@ -4,8 +4,10 @@
#define MIJIN_PLATFORM_FOLDERS_HPP_INCLUDED 1 #define MIJIN_PLATFORM_FOLDERS_HPP_INCLUDED 1
#include "../internal/common.hpp" #include "../internal/common.hpp"
#include "../util/flag.hpp"
#include <filesystem> #include <filesystem>
#include <vector>
namespace fs = std::filesystem; namespace fs = std::filesystem;
@ -20,8 +22,19 @@ enum class KnownFolder
USER_DATA_ROOT USER_DATA_ROOT
}; };
MIJIN_DEFINE_FLAG(IncludeUser);
MIJIN_DEFINE_FLAG(IncludeSystem);
[[nodiscard]] [[nodiscard]]
fs::path getKnownFolder(KnownFolder folder) MIJIN_NOEXCEPT; fs::path getKnownFolder(KnownFolder folder) MIJIN_NOEXCEPT;
[[nodiscard]]
std::vector<fs::path> getAllConfigFolders(IncludeUser includeUser = IncludeUser::YES,
IncludeSystem includeSystem = IncludeSystem::YES) MIJIN_NOEXCEPT;
[[nodiscard]]
std::vector<fs::path> getAllDataFolders(IncludeUser includeUser = IncludeUser::YES,
IncludeSystem includeSystem = IncludeSystem::YES) MIJIN_NOEXCEPT;
} }
#endif // defined(MIJIN_PLATFORM_FOLDERS_HPP_INCLUDED) #endif // defined(MIJIN_PLATFORM_FOLDERS_HPP_INCLUDED)

View File

@ -84,8 +84,15 @@ public:
[[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 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;
virtual void getAllPaths(const fs::path& path, std::vector<PathReference>& outPaths) { outPaths.push_back(getPath(path)); }
[[nodiscard]] PathReference getPath(fs::path path) MIJIN_NOEXCEPT { return PathReference(this, std::move(path)); } [[nodiscard]] PathReference getPath(fs::path path) MIJIN_NOEXCEPT { return PathReference(this, std::move(path)); }
[[nodiscard]] std::vector<PathReference> getAllPaths(const fs::path& path)
{
std::vector<PathReference> paths;
getAllPaths(path, paths);
return paths;
}
}; };
class OSFileSystemAdapter : public FileSystemAdapter class OSFileSystemAdapter : public FileSystemAdapter

View File

@ -112,4 +112,12 @@ StreamError StackedFileSystemAdapter::open(const fs::path& path, FileOpenMode mo
return StreamError::IO_ERROR; return StreamError::IO_ERROR;
} }
void StackedFileSystemAdapter::getAllPaths(const fs::path& path, std::vector<PathReference>& outPaths)
{
for (auto& adapter : adapters_)
{
adapter->getAllPaths(path, outPaths);
}
}
} // namespace mijin } // namespace mijin

View File

@ -33,6 +33,7 @@ public:
FileInfo getFileInfo(const fs::path& file) override; FileInfo getFileInfo(const fs::path& file) override;
Optional<fs::path> getNativePath(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;
void getAllPaths(const fs::path &path, std::vector<PathReference> &outPaths) override;
inline void addAdapter(std::unique_ptr<FileSystemAdapter>&& adapter) { inline void addAdapter(std::unique_ptr<FileSystemAdapter>&& adapter) {
adapters_.push_back(std::move(adapter)); adapters_.push_back(std::move(adapter));