Debian Codegraph @

src/mpt/path/path.hpp

raw
1 /* SPDX-License-Identifier: BSL-1.0 OR BSD-3-Clause */
2
3 #ifndef MPT_PATH_PATH_HPP
4 #define MPT_PATH_PATH_HPP
5
6
7
8 #include "mpt/base/detect.hpp"
9 #include "mpt/base/namespace.hpp"
10 #include "mpt/path/os_path.hpp"
11 #include "mpt/string/types.hpp"
12 #include "mpt/string_transcode/transcode.hpp"
13
14 #if !defined(MPT_COMPILER_QUIRK_NO_FILESYSTEM)
15 #include <filesystem>
16 #endif // !MPT_COMPILER_QUIRK_NO_FILESYSTEM
17 #include <type_traits>
18 #include <utility>
19 #if MPT_OS_WINDOWS && defined(MPT_COMPILER_QUIRK_NO_FILESYSTEM)
20 #include <vector>
21 #endif // MPT_OS_WINDOWS && MPT_COMPILER_QUIRK_NO_FILESYSTEM
22
23 #if MPT_OS_WINDOWS
24 #include <windows.h>
25 #endif // MPT_OS_WINDOWS
26
27
28
29 namespace mpt {
30 inline namespace MPT_INLINE_NS {
31
32
33
34 #if defined(MPT_COMPILER_QUIRK_NO_FILESYSTEM)
35
36
37
38 using path = mpt::os_path;
39 #define MPT_PATH_CHAR(x) MPT_OSPATH_CHAR(x)
40 #define MPT_PATH_LITERAL(x) MPT_OSPATH_LITERAL(x)
41 #define MPT_PATH(x) MPT_OSPATH(x)
42
43
44
45
46 #else // !MPT_COMPILER_QUIRK_NO_FILESYSTEM
47
48
49
50 template <>
51 struct make_string_type<std::filesystem::path> {
52 using type = std::filesystem::path;
53 };
54
55
56 template <>
57 struct is_string_type<std::filesystem::path> : public std::true_type { };
58
59
60
61 template <>
62 struct string_transcoder<std::filesystem::path> {
63 using string_type = std::filesystem::path;
64 static inline mpt::widestring decode(const string_type & src) {
65 if constexpr (std::is_same<std::filesystem::path::value_type, char>::value) {
66 // In contrast to standard recommendation and cppreference,
67 // native encoding on unix-like systems with libstdc++ or libc++ is actually *NOT* UTF8,
68 // but instead the conventional std::locale::locale("") encoding (which happens to be UTF8 on all modern systems, but this is not guaranteed).
69 // Note: libstdc++ and libc++ however assume that their internal representation is UTF8,
70 // which implies that wstring/u32string/u16string/u8string conversions are actually broken and MUST NOT be used, ever.
71 return mpt::transcode<mpt::widestring>(mpt::logical_encoding::locale, src.string());
72 #if !defined(MPT_COMPILER_QUIRK_NO_WCHAR)
73 } else if constexpr (std::is_same<std::filesystem::path::value_type, wchar_t>::value) {
74 return mpt::transcode<mpt::widestring>(src.wstring());
75 #endif // !MPT_COMPILER_QUIRK_NO_WCHAR
76 } else if constexpr (std::is_same<std::filesystem::path::value_type, char32_t>::value) {
77 return mpt::transcode<mpt::widestring>(src.u32string());
78 } else if constexpr (std::is_same<std::filesystem::path::value_type, char16_t>::value) {
79 return mpt::transcode<mpt::widestring>(src.u16string());
80 #if MPT_CXX_AT_LEAST(20)
81 } else if constexpr (std::is_same<std::filesystem::path::value_type, char8_t>::value) {
82 return mpt::transcode<mpt::widestring>(src.u8string());
83 #endif
84 } else {
85 #if MPT_OS_WINDOWS && !defined(MPT_COMPILER_QUIRK_NO_WCHAR)
86 return mpt::transcode<mpt::widestring>(src.wstring());
87 #elif MPT_OS_WINDOWS
88 return mpt::transcode<mpt::widestring>(mpt::logical_encoding::locale, src.string());
89 #else
90 // completely unknown implementation, assume it can sanely convert to/from UTF16/UTF32
91 if constexpr (sizeof(mpt::widechar) == sizeof(char32_t)) {
92 return mpt::transcode<mpt::widestring>(src.u32string());
93 } else if constexpr (sizeof(mpt::widechar) == sizeof(char16_t)) {
94 return mpt::transcode<mpt::widestring>(src.u16string());
95 } else {
96 return mpt::transcode<mpt::widestring>(src.u32string());
97 }
98 #endif
99 }
100 }
101 static inline string_type encode(const mpt::widestring & src, std::filesystem::path::format fmt) {
102 if constexpr (std::is_same<std::filesystem::path::value_type, char>::value) {
103 // In contrast to standard recommendation and cppreference,
104 // native encoding on unix-like systems with libstdc++ or libc++ is actually *NOT* UTF8,
105 // but instead the conventional std::locale::locale("") encoding (which happens to be UTF8 on all modern systems, but this is not guaranteed).
106 // Note: libstdc++ and libc++ however assume that their internal representation is UTF8,
107 // which implies that wstring/u32string/u16string/u8string conversions are actually broken and MUST NOT be used, ever.
108 return std::filesystem::path{mpt::transcode<std::string>(mpt::logical_encoding::locale, src), fmt};
109 #if !defined(MPT_COMPILER_QUIRK_NO_WCHAR)
110 } else if constexpr (std::is_same<std::filesystem::path::value_type, wchar_t>::value) {
111 return std::filesystem::path{mpt::transcode<std::wstring>(src), fmt};
112 #endif // !MPT_COMPILER_QUIRK_NO_WCHAR
113 } else if constexpr (std::is_same<std::filesystem::path::value_type, char32_t>::value) {
114 return std::filesystem::path{mpt::transcode<std::u32string>(src), fmt};
115 } else if constexpr (std::is_same<std::filesystem::path::value_type, char16_t>::value) {
116 return std::filesystem::path{mpt::transcode<std::u16string>(src), fmt};
117 #if MPT_CXX_AT_LEAST(20)
118 } else if constexpr (std::is_same<std::filesystem::path::value_type, char8_t>::value) {
119 return std::filesystem::path{mpt::transcode<std::u8string>(src), fmt};
120 #endif
121 } else {
122 #if MPT_OS_WINDOWS && !defined(MPT_COMPILER_QUIRK_NO_WCHAR)
123 return std::filesystem::path{mpt::transcode<std::wstring>(src), fmt};
124 #elif MPT_OS_WINDOWS
125 return std::filesystem::path{mpt::transcode<std::string>(mpt::logical_encoding::locale, src), fmt};
126 #else
127 // completely unknown implementation, assume it can sanely convert to/from UTF16/UTF32
128 if constexpr (sizeof(mpt::widechar) == sizeof(char32_t)) {
129 return std::filesystem::path{mpt::transcode<std::u32string>(src), fmt};
130 } else if constexpr (sizeof(mpt::widechar) == sizeof(char16_t)) {
131 return std::filesystem::path{mpt::transcode<std::u16string>(src), fmt};
132 } else {
133 return std::filesystem::path{mpt::transcode<std::u32string>(src), fmt};
134 }
135 #endif
136 }
137 }
138 static inline string_type encode(const mpt::widestring & src) {
139 return encode(src, std::filesystem::path::auto_format);
140 }
141 };
142
143
144
145 // Best heuristics we can come up with to define std::filesystem::path literals that do not involve (or at least only non-lossy) runtime conversion.
146
147 #if MPT_OS_WINDOWS && !defined(MPT_COMPILER_QUIRK_NO_WCHAR)
148 #define MPT_STDPATH_CHAR(x) L##x
149 #define MPT_STDPATH_LITERAL(x) L##x
150 #define MPT_STDPATH(x) \
151 std::filesystem::path { \
152 L##x \
153 }
154 #elif MPT_OS_WINDOWS
155 #define MPT_STDPATH_CHAR(x) x
156 #define MPT_STDPATH_LITERAL(x) x
157 #define MPT_STDPATH(x) \
158 std::filesystem::path { \
159 x \
160 }
161 #elif MPT_CXX_AT_LEAST(20)
162 #define MPT_STDPATH_CHAR(x) u8##x
163 #define MPT_STDPATH_LITERAL(x) u8##x
164 #define MPT_STDPATH(x) \
165 std::filesystem::path { \
166 u8##x \
167 }
168 #else
169 #define MPT_STDPATH_CHAR(x) U##x
170 #define MPT_STDPATH_LITERAL(x) U##x
171 #define MPT_STDPATH(x) \
172 std::filesystem::path { \
173 U##x \
174 }
175 #endif
176
177
178
179 // std::filesystem::path offers implicit conversions to/from types of which it is confused about their encodings.
180 // The only way to work around this problem is to implement our own mpt::path that does not do such broken nonsense.
181 // We offer no implicit conversions and only explicit conversions from std::filesystem::path and mpt::os_path.
182
183 class path {
184 public:
185 using format = std::filesystem::path::format;
186 using std_value_type = std::filesystem::path::value_type;
187 using std_string_type = std::filesystem::path;
188 static constexpr inline std_value_type std_preferred_separator = std::filesystem::path::preferred_separator;
189 using os_value_type = os_path::value_type;
190 using os_string_type = os_path;
191 static constexpr inline os_value_type os_preferred_separator = static_cast<os_value_type>(std::filesystem::path::preferred_separator);
192
193 private:
194 std::filesystem::path m_path;
195
196 private:
197 template <typename path_type, std::enable_if_t<std::is_same<path_type, std::filesystem::path>::value, bool> = true>
198 explicit path(const path_type & p)
199 : m_path(p) {
200 return;
201 }
202 template <typename path_type, std::enable_if_t<std::is_same<path_type, std::filesystem::path>::value, bool> = true>
203 explicit path(path_type && p)
204 : m_path(std::forward<path_type>(p)) {
205 return;
206 }
207
208 public:
209 struct internal {
210 static inline path make_path(std::filesystem::path && p) {
211 return path{std::move(p)};
212 }
213 };
214
215 public:
216 template <typename path_type, std::enable_if_t<std::is_same<path_type, std::filesystem::path>::value, bool> = true>
217 static path from_stdpath(const path_type & p) {
218 return path{p};
219 }
220 static std::filesystem::path to_stdpath(const path & p) {
221 return p.m_path;
222 }
223 static os_path to_ospath(const path & p) {
224 return mpt::transcode<os_path>(p.m_path);
225 }
226 static std::filesystem::path from_ospath(const os_path & s, std::filesystem::path::format fmt = std::filesystem::path::auto_format) {
227 return string_transcoder<std::filesystem::path>{}.encode(mpt::transcode<mpt::widestring>(s), fmt);
228 }
229
230 public:
231 path() noexcept = default;
232 path(const path & p)
233 : m_path(p.m_path) {
234 return;
235 }
236 path(path && p)
237 : m_path(std::move(p.m_path)) {
238 return;
239 }
240 explicit path(const os_path & s, std::filesystem::path::format fmt = std::filesystem::path::auto_format)
241 : m_path(from_ospath(s, fmt)) {
242 return;
243 }
244 path & operator=(const path & p) {
245 m_path = p.m_path;
246 return *this;
247 }
248 path & operator=(path && p) {
249 m_path = std::move(p.m_path);
250 return *this;
251 }
252 path & assign(const path & p) {
253 m_path = p.m_path;
254 return *this;
255 }
256 path & assign(path && p) {
257 m_path = std::move(p.m_path);
258 return *this;
259 }
260 path & operator/=(const path & p) {
261 m_path /= p.m_path;
262 return *this;
263 }
264 path & operator/=(path && p) {
265 m_path /= std::move(p.m_path);
266 return *this;
267 }
268 // concatenation
269 path & append(const path & p) {
270 m_path /= p.m_path;
271 return *this;
272 }
273 path & append(path && p) {
274 m_path /= std::move(p.m_path);
275 return *this;
276 }
277 path & operator+=(const path & p) {
278 m_path += p.m_path;
279 return *this;
280 }
281 path & operator+=(path && p) {
282 m_path += std::move(p.m_path);
283 return *this;
284 }
285 path & concat(const path & p) {
286 m_path += p.m_path;
287 return *this;
288 }
289 path & concat(path && p) {
290 m_path += std::move(p.m_path);
291 return *this;
292 }
293 // modifiers
294 void clear() noexcept {
295 m_path.clear();
296 }
297 path & make_preferred() {
298 m_path.make_preferred();
299 return *this;
300 }
301 path & remove_filename() {
302 m_path.remove_filename();
303 return *this;
304 }
305 path & replace_filename(const path & replacement) {
306 m_path.replace_filename(replacement.m_path);
307 return *this;
308 }
309 path & replace_extension(const path & replacement = path()) {
310 m_path.replace_extension(replacement.m_path);
311 return *this;
312 }
313 void swap(path & other) {
314 m_path.swap(other.m_path);
315 }
316 // format observers
317 std::filesystem::path stdpath() const {
318 return m_path;
319 }
320 os_path ospath() const {
321 return to_ospath(*this);
322 }
323 // compare
324 int compare(const path & p) const noexcept {
325 return m_path.compare(p.m_path);
326 }
327 // generation
328 path lexically_normal() const {
329 return path{m_path.lexically_normal()};
330 }
331 path lexically_relative(const path & base) const {
332 return path{m_path.lexically_relative(base.m_path)};
333 }
334 path lexically_proximate(const path & base) const {
335 return path{m_path.lexically_proximate(base.m_path)};
336 }
337 // decomposition
338 path root_name() const {
339 return path{m_path.root_name()};
340 }
341 path root_directory() const {
342 return path{m_path.root_directory()};
343 }
344 path root_path() const {
345 return path{m_path.root_path()};
346 }
347 path relative_path() const {
348 return path{m_path.relative_path()};
349 }
350 path parent_path() const {
351 return path{m_path.parent_path()};
352 }
353 path filename() const {
354 return path{m_path.filename()};
355 }
356 path stem() const {
357 return path{m_path.stem()};
358 }
359 path extension() const {
360 return path{m_path.extension()};
361 }
362 // queries
363 [[nodiscard]] bool empty() const noexcept {
364 return m_path.empty();
365 }
366 bool has_root_path() const {
367 return m_path.has_root_path();
368 }
369 bool has_root_name() const {
370 return m_path.has_root_name();
371 }
372 bool has_root_directory() const {
373 return m_path.has_root_directory();
374 }
375 bool has_relative_path() const {
376 return m_path.has_relative_path();
377 }
378 bool has_parent_path() const {
379 return m_path.has_parent_path();
380 }
381 bool has_filename() const {
382 return m_path.has_filename();
383 }
384 bool has_stem() const {
385 return m_path.has_stem();
386 }
387 bool has_extension() const {
388 return m_path.has_extension();
389 }
390 bool is_absolute() const {
391 return m_path.is_absolute();
392 }
393 bool is_relative() const {
394 return m_path.is_relative();
395 }
396 // comparison operators
397 friend bool operator==(const path & lhs, const path & rhs) noexcept {
398 return lhs.m_path == rhs.m_path;
399 }
400 friend bool operator!=(const path & lhs, const path & rhs) noexcept {
401 return lhs.m_path != rhs.m_path;
402 }
403 friend bool operator<(const path & lhs, const path & rhs) noexcept {
404 return lhs.m_path < rhs.m_path;
405 }
406 friend bool operator<=(const path & lhs, const path & rhs) noexcept {
407 return lhs.m_path <= rhs.m_path;
408 }
409 friend bool operator>(const path & lhs, const path & rhs) noexcept {
410 return lhs.m_path > rhs.m_path;
411 }
412 friend bool operator>=(const path & lhs, const path & rhs) noexcept {
413 return lhs.m_path >= rhs.m_path;
414 }
415 // copncatenation operator
416 friend path operator/(const path & lhs, const path & rhs) {
417 return path{lhs.m_path / rhs.m_path};
418 }
419 };
420
421
422
423 // Best heuristics we can come up with to define mpt::path literals that do not involve (or at least only non-lossy) runtime conversion.
424
425 #if MPT_OS_WINDOWS && !defined(MPT_COMPILER_QUIRK_NO_WCHAR)
426 #define MPT_PATH_CHAR(x) L##x
427 #define MPT_PATH_LITERAL(x) L##x
428 #define MPT_PATH(x) mpt::path::internal::make_path(L##x)
429 #elif MPT_OS_WINDOWS
430 #define MPT_PATH_CHAR(x) x
431 #define MPT_PATH_LITERAL(x) x
432 #define MPT_PATH(x) mpt::path::internal::make_path(x)
433 #elif MPT_CXX_AT_LEAST(20)
434 #define MPT_PATH_CHAR(x) u8##x
435 #define MPT_PATH_LITERAL(x) u8##x
436 #define MPT_PATH(x) mpt::path::internal::make_path(u8##x)
437 #else
438 #define MPT_PATH_CHAR(x) U##x
439 #define MPT_PATH_LITERAL(x) U##x
440 #define MPT_PATH(x) mpt::path::internal::make_path(U##x)
441 #endif
442
443
444
445 template <>
446 struct make_string_type<mpt::path> {
447 using type = mpt::path;
448 };
449
450
451 template <>
452 struct is_string_type<mpt::path> : public std::true_type { };
453
454
455
456 template <>
457 struct string_transcoder<mpt::path> {
458 using string_type = mpt::path;
459 static inline mpt::widestring decode(const string_type & src) {
460 return mpt::transcode<mpt::widestring>(src.ospath());
461 }
462 static inline string_type encode(const mpt::widestring & src) {
463 return mpt::path{mpt::transcode<mpt::os_path>(src)};
464 }
465 };
466
467
468
469 #endif // MPT_COMPILER_QUIRK_NO_FILESYSTEM
470
471
472
473 } // namespace MPT_INLINE_NS
474 } // namespace mpt
475
476
477
478 #endif // MPT_PATH_PATH_HPP