Skip to content

Commit 6ec77a1

Browse files
committed
Add extractNativePath() as counterpart to makeNativePath()
1 parent 283c416 commit 6ec77a1

File tree

1 file changed

+51
-3
lines changed

1 file changed

+51
-3
lines changed

io/path.h

Lines changed: 51 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
#include <list>
1111
#include <string>
1212
#include <string_view>
13+
#ifdef CPP_UTILITIES_USE_STANDARD_FILESYSTEM
14+
#include <filesystem>
15+
#endif
1316

1417
#ifdef PLATFORM_WINDOWS
1518
#define PATH_SEP_CHAR '\\'
@@ -33,6 +36,32 @@ CPP_UTILITIES_EXPORT std::string_view directory(std::string_view path);
3336
#endif
3437
CPP_UTILITIES_EXPORT void removeInvalidChars(std::string &fileName);
3538

39+
/// \brief The native type used by std::filesystem:path.
40+
/// \remarks The current implementation requires this to be always wchar_t on Windows and char otherwise.
41+
using PathValueType =
42+
#ifdef PLATFORM_WINDOWS
43+
wchar_t
44+
#else
45+
char
46+
#endif
47+
;
48+
#ifdef CPP_UTILITIES_USE_STANDARD_FILESYSTEM
49+
static_assert(std::is_same_v<typename std::filesystem::path::value_type, PathValueType>);
50+
#endif
51+
52+
/// \brief The string type used to store paths in the native encoding.
53+
/// \remarks This type is used to store paths when interfacing with native APIs.
54+
using NativePathString = std::basic_string<PathValueType>;
55+
/// \brief The string view type used to pass paths in the native encoding.
56+
/// \remarks This type is used to pass paths when interfacing with native APIs.
57+
using NativePathStringView = std::basic_string_view<PathValueType>;
58+
/// \brief The string type used to store paths UTF-8 encoded.
59+
/// \remarks This type is used to store paths everywhere except when interfacing directly with native APIs.
60+
using PathString = std::string;
61+
/// \brief The string view type used to pass paths UTF-8 encoded.
62+
/// \remarks This type is used to pass paths everywhere except when interfacing directly with native APIs.
63+
using PathStringView = std::string_view;
64+
3665
/*!
3766
* \brief Returns \a path in the platform's native encoding.
3867
* \remarks
@@ -46,11 +75,11 @@ CPP_UTILITIES_EXPORT void removeInvalidChars(std::string &fileName);
4675
*/
4776
inline
4877
#ifdef PLATFORM_WINDOWS
49-
std::wstring
78+
NativePathString
5079
#else
51-
std::string_view
80+
NativePathStringView
5281
#endif
53-
makeNativePath(std::string_view path)
82+
makeNativePath(PathStringView path)
5483
{
5584
#ifdef PLATFORM_WINDOWS
5685
auto ec = std::error_code();
@@ -60,6 +89,25 @@ inline
6089
#endif
6190
}
6291

92+
/*!
93+
* \brief Returns \a path as UTF-8 string or string view.
94+
* \sa This is the opposite of makeNativePath() so checkout remarks of that function for details.
95+
*/
96+
inline
97+
#ifdef PLATFORM_WINDOWS
98+
PathString
99+
#else
100+
PathStringView
101+
#endif
102+
extractNativePath(NativePathStringView path) {
103+
#ifdef PLATFORM_WINDOWS
104+
auto res = convertUtf16LEToUtf8(reinterpret_cast<const char *>(path.data()), path.size() * 2);
105+
return std::string(res.first.get(), res.second);
106+
#else
107+
return path;
108+
#endif
109+
}
110+
63111
} // namespace CppUtilities
64112

65113
#endif // IOUTILITIES_PATHHELPER_H

0 commit comments

Comments
 (0)