10
10
#include < list>
11
11
#include < string>
12
12
#include < string_view>
13
+ #ifdef CPP_UTILITIES_USE_STANDARD_FILESYSTEM
14
+ #include < filesystem>
15
+ #endif
13
16
14
17
#ifdef PLATFORM_WINDOWS
15
18
#define PATH_SEP_CHAR ' \\ '
@@ -33,6 +36,32 @@ CPP_UTILITIES_EXPORT std::string_view directory(std::string_view path);
33
36
#endif
34
37
CPP_UTILITIES_EXPORT void removeInvalidChars (std::string &fileName);
35
38
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
+
36
65
/* !
37
66
* \brief Returns \a path in the platform's native encoding.
38
67
* \remarks
@@ -46,11 +75,11 @@ CPP_UTILITIES_EXPORT void removeInvalidChars(std::string &fileName);
46
75
*/
47
76
inline
48
77
#ifdef PLATFORM_WINDOWS
49
- std::wstring
78
+ NativePathString
50
79
#else
51
- std::string_view
80
+ NativePathStringView
52
81
#endif
53
- makeNativePath (std::string_view path)
82
+ makeNativePath (PathStringView path)
54
83
{
55
84
#ifdef PLATFORM_WINDOWS
56
85
auto ec = std::error_code ();
@@ -60,6 +89,25 @@ inline
60
89
#endif
61
90
}
62
91
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
+
63
111
} // namespace CppUtilities
64
112
65
113
#endif // IOUTILITIES_PATHHELPER_H
0 commit comments