From fd47e511dd7b456713b2ecbe8acd155377ace817 Mon Sep 17 00:00:00 2001 From: Martin Ridgers Date: Mon, 18 Oct 2021 14:02:43 +0200 Subject: Simple class for building paths based off an ExtendableString --- zencore/include/zencore/filesystem.h | 40 ++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) (limited to 'zencore/include') diff --git a/zencore/include/zencore/filesystem.h b/zencore/include/zencore/filesystem.h index c7ac7140d..68b55f86e 100644 --- a/zencore/include/zencore/filesystem.h +++ b/zencore/include/zencore/filesystem.h @@ -5,6 +5,7 @@ #include "zencore.h" #include +#include #include #include @@ -57,6 +58,45 @@ ZENCORE_API bool SupportsBlockRefCounting(std::filesystem::path Path); ZENCORE_API std::string ToUtf8(const std::filesystem::path& Path); +/** + * Helper class for building paths. Backed by an extendable string builder. + * + */ +#if ZEN_PLATFORM_WINDOWS + template + using ExtendablePathBuilderBase = ExtendableWideStringBuilder; +#else + template + using ExtendablePathBuilderBase = ExtendableStringBuilder; +#endif + +template +class ExtendablePathBuilder + : public ExtendablePathBuilderBase +{ + using Super = ExtendablePathBuilderBase; + +public: + void Append(const std::filesystem::path& Rhs) { Super::Append(Rhs.c_str()); } + void operator /= (const std::filesystem::path& Rhs) { this->operator /= (Rhs.c_str()); }; + void operator /= (const char* Rhs) { AppendSeparator(); *this << (Rhs); } + void operator /= (const wchar_t* Rhs) { AppendSeparator(); Super::Append(Rhs); } + std::filesystem::path ToPath() const { return std::filesystem::path(Super::ToView()); } + + void AppendSeparator() + { + if ( + Super::ToView().ends_with(std::filesystem::path::preferred_separator) +#if ZEN_PLATFORM_WINDOWS + || Super::ToView().ends_with('/') +#endif + ) + return; + + Super::Append(std::filesystem::path::preferred_separator); + } +}; + /** * Efficient file system traversal * -- cgit v1.2.3