diff --git a/io/path.h b/io/path.h index f739382..3610b33 100644 --- a/io/path.h +++ b/io/path.h @@ -10,6 +10,9 @@ #include #include #include +#ifdef CPP_UTILITIES_USE_STANDARD_FILESYSTEM +#include +#endif #ifdef PLATFORM_WINDOWS #define PATH_SEP_CHAR '\\' @@ -33,6 +36,32 @@ CPP_UTILITIES_EXPORT std::string_view directory(std::string_view path); #endif CPP_UTILITIES_EXPORT void removeInvalidChars(std::string &fileName); +/// \brief The native type used by std::filesystem:path. +/// \remarks The current implementation requires this to be always wchar_t on Windows and char otherwise. +using PathValueType = +#ifdef PLATFORM_WINDOWS + wchar_t +#else + char +#endif + ; +#ifdef CPP_UTILITIES_USE_STANDARD_FILESYSTEM +static_assert(std::is_same_v); +#endif + +/// \brief The string type used to store paths in the native encoding. +/// \remarks This type is used to store paths when interfacing with native APIs. +using NativePathString = std::basic_string; +/// \brief The string view type used to pass paths in the native encoding. +/// \remarks This type is used to pass paths when interfacing with native APIs. +using NativePathStringView = std::basic_string_view; +/// \brief The string type used to store paths UTF-8 encoded. +/// \remarks This type is used to store paths everywhere except when interfacing directly with native APIs. +using PathString = std::string; +/// \brief The string view type used to pass paths UTF-8 encoded. +/// \remarks This type is used to pass paths everywhere except when interfacing directly with native APIs. +using PathStringView = std::string_view; + /*! * \brief Returns \a path in the platform's native encoding. * \remarks @@ -46,11 +75,11 @@ CPP_UTILITIES_EXPORT void removeInvalidChars(std::string &fileName); */ inline #ifdef PLATFORM_WINDOWS - std::wstring + NativePathString #else - std::string_view + NativePathStringView #endif - makeNativePath(std::string_view path) + makeNativePath(PathStringView path) { #ifdef PLATFORM_WINDOWS auto ec = std::error_code(); @@ -60,6 +89,25 @@ inline #endif } +/*! + * \brief Returns \a path as UTF-8 string or string view. + * \sa This is the opposite of makeNativePath() so checkout remarks of that function for details. + */ +inline +#ifdef PLATFORM_WINDOWS + PathString +#else + PathStringView +#endif + extractNativePath(NativePathStringView path) { +#ifdef PLATFORM_WINDOWS + auto res = convertUtf16LEToUtf8(reinterpret_cast(path.data()), path.size() * 2); + return std::string(res.first.get(), res.second); +#else + return path; +#endif +} + } // namespace CppUtilities #endif // IOUTILITIES_PATHHELPER_H