Add `extractNativePath()` as counterpart to `makeNativePath()`
This commit is contained in:
parent
283c416a59
commit
6ec77a1d1c
54
io/path.h
54
io/path.h
|
@ -10,6 +10,9 @@
|
|||
#include <list>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
#ifdef CPP_UTILITIES_USE_STANDARD_FILESYSTEM
|
||||
#include <filesystem>
|
||||
#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<typename std::filesystem::path::value_type, PathValueType>);
|
||||
#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<PathValueType>;
|
||||
/// \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<PathValueType>;
|
||||
/// \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<const char *>(path.data()), path.size() * 2);
|
||||
return std::string(res.first.get(), res.second);
|
||||
#else
|
||||
return path;
|
||||
#endif
|
||||
}
|
||||
|
||||
} // namespace CppUtilities
|
||||
|
||||
#endif // IOUTILITIES_PATHHELPER_H
|
||||
|
|
Loading…
Reference in New Issue