Debian Codegraph @

src/mpt/path/basic_path.hpp

raw
1 /* SPDX-License-Identifier: BSL-1.0 OR BSD-3-Clause */
2
3 #ifndef MPT_PATH_BASIC_PATH_HPP
4 #define MPT_PATH_BASIC_PATH_HPP
5
6
7
8 #include "mpt/base/detect.hpp"
9 #include "mpt/base/macros.hpp"
10 #include "mpt/base/namespace.hpp"
11 #include "mpt/detect/mfc.hpp"
12 #include "mpt/string_transcode/transcode.hpp"
13
14 #include <stdexcept>
15 #include <string_view>
16 #include <type_traits>
17 #include <utility>
18 #include <vector>
19
20 #include <cstddef>
21
22
23
24 namespace mpt {
25 inline namespace MPT_INLINE_NS {
26
27
28
29 namespace path_literals {
30
31 template <typename Tchar>
32 struct literals;
33
34 template <typename Tchar>
35 MPT_CONSTEVAL Tchar L(char x) {
36 return path_literals::literals<Tchar>::L(x);
37 }
38
39 template <typename Tchar, std::size_t N>
40 MPT_CONSTEVAL const Tchar * L(const char (&x)[N]) {
41 return path_literals::literals<Tchar>::L(x);
42 }
43
44 template <>
45 struct literals<char> {
46 using char_type = char;
47 static MPT_CONSTEVAL char_type L(char c) {
48 if (c == '\0')
49 return '\0';
50 if (c == '\\')
51 return '\\';
52 if (c == '/')
53 return '/';
54 if (c == '.')
55 return '.';
56 if (c == ':')
57 return ':';
58 if (c == '?')
59 return '?';
60 if (c == '*')
61 return '*';
62 if (c == '|')
63 return '|';
64 if (c == '<')
65 return '<';
66 if (c == '>')
67 return '>';
68 if (c == '\"')
69 return '\"';
70 if (c == '_')
71 return '_';
72 #if defined(MPT_COMPILER_QUIRK_NO_CONSTEXPR_THROW)
73 else
74 return false ? 0 : throw std::domain_error("invalid path literal");
75 #else
76 throw std::domain_error("invalid path literal");
77 #endif
78 }
79 template <std::size_t N>
80 static MPT_CONSTEVAL const char_type * L(const char (&s)[N]) {
81 if (std::string_view(s) == std::string_view(""))
82 return "";
83 if (std::string_view(s) == std::string_view("/"))
84 return "/";
85 if (std::string_view(s) == std::string_view("."))
86 return ".";
87 if (std::string_view(s) == std::string_view("\\"))
88 return "\\";
89 if (std::string_view(s) == std::string_view(".."))
90 return "..";
91 if (std::string_view(s) == std::string_view("//"))
92 return "//";
93 if (std::string_view(s) == std::string_view("./"))
94 return "./";
95 if (std::string_view(s) == std::string_view(".\\"))
96 return ".\\";
97 if (std::string_view(s) == std::string_view("\\/"))
98 return "\\/";
99 if (std::string_view(s) == std::string_view("/\\"))
100 return "/\\";
101 if (std::string_view(s) == std::string_view("\\\\"))
102 return "\\\\";
103 if (std::string_view(s) == std::string_view("\\\\?\\"))
104 return "\\\\?\\";
105 if (std::string_view(s) == std::string_view("\\\\?\\UNC"))
106 return "\\\\?\\UNC";
107 if (std::string_view(s) == std::string_view("\\\\?\\UNC\\"))
108 return "\\\\?\\UNC\\";
109 #if defined(MPT_COMPILER_QUIRK_NO_CONSTEXPR_THROW)
110 else
111 return false ? nullptr : throw std::domain_error("invalid path literal");
112 #else
113 throw std::domain_error("invalid path literal");
114 #endif
115 }
116 };
117
118 #if !defined(MPT_COMPILER_QUIRK_NO_WCHAR)
119 template <>
120 struct literals<wchar_t> {
121 using char_type = wchar_t;
122 static MPT_CONSTEVAL char_type L(char c) {
123 if (c == '\0')
124 return L'\0';
125 if (c == '\\')
126 return L'\\';
127 if (c == '/')
128 return L'/';
129 if (c == '.')
130 return L'.';
131 if (c == ':')
132 return L':';
133 if (c == '?')
134 return L'?';
135 if (c == '*')
136 return L'*';
137 if (c == '|')
138 return L'|';
139 if (c == '<')
140 return L'<';
141 if (c == '>')
142 return L'>';
143 if (c == '\"')
144 return L'\"';
145 if (c == '_')
146 return L'_';
147 #if defined(MPT_COMPILER_QUIRK_NO_CONSTEXPR_THROW)
148 else
149 return false ? 0 : throw std::domain_error("invalid path literal");
150 #else
151 throw std::domain_error("invalid path literal");
152 #endif
153 }
154 template <std::size_t N>
155 static MPT_CONSTEVAL const char_type * L(const char (&s)[N]) {
156 if (std::string_view(s) == std::string_view(""))
157 return L"";
158 if (std::string_view(s) == std::string_view("/"))
159 return L"/";
160 if (std::string_view(s) == std::string_view("."))
161 return L".";
162 if (std::string_view(s) == std::string_view("\\"))
163 return L"\\";
164 if (std::string_view(s) == std::string_view(".."))
165 return L"..";
166 if (std::string_view(s) == std::string_view("//"))
167 return L"//";
168 if (std::string_view(s) == std::string_view("./"))
169 return L"./";
170 if (std::string_view(s) == std::string_view(".\\"))
171 return L".\\";
172 if (std::string_view(s) == std::string_view("\\/"))
173 return L"\\/";
174 if (std::string_view(s) == std::string_view("/\\"))
175 return L"/\\";
176 if (std::string_view(s) == std::string_view("\\\\"))
177 return L"\\\\";
178 if (std::string_view(s) == std::string_view("\\\\?\\"))
179 return L"\\\\?\\";
180 if (std::string_view(s) == std::string_view("\\\\?\\UNC"))
181 return L"\\\\?\\UNC";
182 if (std::string_view(s) == std::string_view("\\\\?\\UNC\\"))
183 return L"\\\\?\\UNC\\";
184 #if defined(MPT_COMPILER_QUIRK_NO_CONSTEXPR_THROW)
185 else
186 return false ? nullptr : throw std::domain_error("invalid path literal");
187 #else
188 throw std::domain_error("invalid path literal");
189 #endif
190 }
191 };
192 #endif // !MPT_COMPILER_QUIRK_NO_WCHAR
193
194 #if MPT_CXX_AT_LEAST(20)
195 template <>
196 struct literals<char8_t> {
197 using char_type = char8_t;
198 static MPT_CONSTEVAL char_type L(char c) {
199 if (c == '\0')
200 return u8'\0';
201 if (c == '\\')
202 return u8'\\';
203 if (c == '/')
204 return u8'/';
205 if (c == '.')
206 return u8'.';
207 if (c == ':')
208 return u8':';
209 if (c == '?')
210 return u8'?';
211 if (c == '*')
212 return u8'*';
213 if (c == '|')
214 return u8'|';
215 if (c == '<')
216 return u8'<';
217 if (c == '>')
218 return u8'>';
219 if (c == '\"')
220 return u8'\"';
221 if (c == '_')
222 return u8'_';
223 #if defined(MPT_COMPILER_QUIRK_NO_CONSTEXPR_THROW)
224 else
225 return false ? 0 : throw std::domain_error("invalid path literal");
226 #else
227 throw std::domain_error("invalid path literal");
228 #endif
229 }
230 template <std::size_t N>
231 static MPT_CONSTEVAL const char_type * L(const char (&s)[N]) {
232 if (std::string_view(s) == std::string_view(""))
233 return u8"";
234 if (std::string_view(s) == std::string_view("/"))
235 return u8"/";
236 if (std::string_view(s) == std::string_view("."))
237 return u8".";
238 if (std::string_view(s) == std::string_view("\\"))
239 return u8"\\";
240 if (std::string_view(s) == std::string_view(".."))
241 return u8"..";
242 if (std::string_view(s) == std::string_view("//"))
243 return u8"//";
244 if (std::string_view(s) == std::string_view("./"))
245 return u8"./";
246 if (std::string_view(s) == std::string_view(".\\"))
247 return u8".\\";
248 if (std::string_view(s) == std::string_view("\\/"))
249 return u8"\\/";
250 if (std::string_view(s) == std::string_view("/\\"))
251 return u8"/\\";
252 if (std::string_view(s) == std::string_view("\\\\"))
253 return u8"\\\\";
254 if (std::string_view(s) == std::string_view("\\\\?\\"))
255 return u8"\\\\?\\";
256 if (std::string_view(s) == std::string_view("\\\\?\\UNC"))
257 return u8"\\\\?\\UNC";
258 if (std::string_view(s) == std::string_view("\\\\?\\UNC\\"))
259 return u8"\\\\?\\UNC\\";
260 #if defined(MPT_COMPILER_QUIRK_NO_CONSTEXPR_THROW)
261 else
262 return false ? nullptr : throw std::domain_error("invalid path literal");
263 #else
264 throw std::domain_error("invalid path literal");
265 #endif
266 }
267 };
268 #endif // C++20
269
270 template <>
271 struct literals<char16_t> {
272 using char_type = char16_t;
273 static MPT_CONSTEVAL char_type L(char c) {
274 if (c == '\0')
275 return u'\0';
276 if (c == '\\')
277 return u'\\';
278 if (c == '/')
279 return u'/';
280 if (c == '.')
281 return u'.';
282 if (c == ':')
283 return u':';
284 if (c == '?')
285 return u'?';
286 if (c == '*')
287 return u'*';
288 if (c == '|')
289 return u'|';
290 if (c == '<')
291 return u'<';
292 if (c == '>')
293 return u'>';
294 if (c == '\"')
295 return u'\"';
296 if (c == '_')
297 return u'_';
298 #if defined(MPT_COMPILER_QUIRK_NO_CONSTEXPR_THROW)
299 else
300 return false ? 0 : throw std::domain_error("invalid path literal");
301 #else
302 throw std::domain_error("invalid path literal");
303 #endif
304 }
305 template <std::size_t N>
306 static MPT_CONSTEVAL const char_type * L(const char (&s)[N]) {
307 if (std::string_view(s) == std::string_view(""))
308 return u"";
309 if (std::string_view(s) == std::string_view("/"))
310 return u"/";
311 if (std::string_view(s) == std::string_view("."))
312 return u".";
313 if (std::string_view(s) == std::string_view("\\"))
314 return u"\\";
315 if (std::string_view(s) == std::string_view(".."))
316 return u"..";
317 if (std::string_view(s) == std::string_view("//"))
318 return u"//";
319 if (std::string_view(s) == std::string_view("./"))
320 return u"./";
321 if (std::string_view(s) == std::string_view(".\\"))
322 return u".\\";
323 if (std::string_view(s) == std::string_view("\\/"))
324 return u"\\/";
325 if (std::string_view(s) == std::string_view("/\\"))
326 return u"/\\";
327 if (std::string_view(s) == std::string_view("\\\\"))
328 return u"\\\\";
329 if (std::string_view(s) == std::string_view("\\\\?\\"))
330 return u"\\\\?\\";
331 if (std::string_view(s) == std::string_view("\\\\?\\UNC"))
332 return u"\\\\?\\UNC";
333 if (std::string_view(s) == std::string_view("\\\\?\\UNC\\"))
334 return u"\\\\?\\UNC\\";
335 #if defined(MPT_COMPILER_QUIRK_NO_CONSTEXPR_THROW)
336 else
337 return false ? nullptr : throw std::domain_error("invalid path literal");
338 #else
339 throw std::domain_error("invalid path literal");
340 #endif
341 }
342 };
343
344 template <>
345 struct literals<char32_t> {
346 using char_type = char32_t;
347 static MPT_CONSTEVAL char_type L(char c) {
348 if (c == '\0')
349 return U'\0';
350 if (c == '\\')
351 return U'\\';
352 if (c == '/')
353 return U'/';
354 if (c == '.')
355 return U'.';
356 if (c == ':')
357 return U':';
358 if (c == '?')
359 return U'?';
360 if (c == '*')
361 return U'*';
362 if (c == '|')
363 return U'|';
364 if (c == '<')
365 return U'<';
366 if (c == '>')
367 return U'>';
368 if (c == '\"')
369 return U'\"';
370 if (c == '_')
371 return U'_';
372 #if defined(MPT_COMPILER_QUIRK_NO_CONSTEXPR_THROW)
373 else
374 return false ? 0 : throw std::domain_error("invalid path literal");
375 #else
376 throw std::domain_error("invalid path literal");
377 #endif
378 }
379 template <std::size_t N>
380 static MPT_CONSTEVAL const char_type * L(const char (&s)[N]) {
381 if (std::string_view(s) == std::string_view(""))
382 return U"";
383 if (std::string_view(s) == std::string_view("/"))
384 return U"/";
385 if (std::string_view(s) == std::string_view("."))
386 return U".";
387 if (std::string_view(s) == std::string_view("\\"))
388 return U"\\";
389 if (std::string_view(s) == std::string_view(".."))
390 return U"..";
391 if (std::string_view(s) == std::string_view("//"))
392 return U"//";
393 if (std::string_view(s) == std::string_view("./"))
394 return U"./";
395 if (std::string_view(s) == std::string_view(".\\"))
396 return U".\\";
397 if (std::string_view(s) == std::string_view("\\/"))
398 return U"\\/";
399 if (std::string_view(s) == std::string_view("/\\"))
400 return U"/\\";
401 if (std::string_view(s) == std::string_view("\\\\"))
402 return U"\\\\";
403 if (std::string_view(s) == std::string_view("\\\\?\\"))
404 return U"\\\\?\\";
405 if (std::string_view(s) == std::string_view("\\\\?\\UNC"))
406 return U"\\\\?\\UNC";
407 if (std::string_view(s) == std::string_view("\\\\?\\UNC\\"))
408 return U"\\\\?\\UNC\\";
409 #if defined(MPT_COMPILER_QUIRK_NO_CONSTEXPR_THROW)
410 else
411 return false ? nullptr : throw std::domain_error("invalid path literal");
412 #else
413 throw std::domain_error("invalid path literal");
414 #endif
415 }
416 };
417
418 } // namespace path_literals
419
420
421
422 enum class PathStyle {
423 Posix,
424 DOS_DJGPP,
425 Windows9x,
426 WindowsNT,
427 };
428
429 template <PathStyle EStyle>
430 struct PathStyleTag {
431 static inline constexpr PathStyle path_style = EStyle;
432 };
433
434
435
436 template <typename TRawPath, typename PathStyleTag>
437 struct PathTraits {
438
439
440
441 static inline constexpr PathStyle path_style = PathStyleTag::path_style;
442 using raw_path_type = TRawPath;
443 using char_type = typename raw_path_type::value_type;
444
445
446
447 static bool IsPathSeparator(char_type c) {
448 using namespace path_literals;
449 bool result{};
450 if constexpr ((path_style == PathStyle::WindowsNT) || (path_style == PathStyle::Windows9x) || (path_style == PathStyle::DOS_DJGPP)) {
451 result = (c == L<char_type>('\\')) || (c == L<char_type>('/'));
452 } else if constexpr (path_style == PathStyle::Posix) {
453 result = (c == L<char_type>('/'));
454 } else {
455 //static_assert(false);
456 }
457 return result;
458 }
459
460
461
462 static char_type GetDefaultPathSeparator() {
463 using namespace path_literals;
464 char_type result{};
465 if constexpr ((path_style == PathStyle::WindowsNT) || (path_style == PathStyle::Windows9x) || (path_style == PathStyle::DOS_DJGPP)) {
466 result = L<char_type>('\\');
467 } else if constexpr (path_style == PathStyle::Posix) {
468 result = L<char_type>('/');
469 } else {
470 //static_assert(false);
471 }
472 return result;
473 }
474
475
476
477 static bool IsValidComponentChar(char_type c) {
478 using namespace path_literals;
479 bool result = true;
480 if constexpr ((path_style == PathStyle::WindowsNT) || (path_style == PathStyle::Windows9x) || (path_style == PathStyle::DOS_DJGPP)) {
481 if (c == L<char_type>('\\') || c == L<char_type>('\"') || c == L<char_type>('/') || c == L<char_type>(':') || c == L<char_type>('?') || c == L<char_type>('<') || c == L<char_type>('>') || c == L<char_type>('|') || c == L<char_type>('*')) {
482 result = false;
483 } else {
484 result = true;
485 }
486 } else if constexpr (path_style == PathStyle::Posix) {
487 result = (c != L<char_type>('/'));
488 } else {
489 // nothing
490 }
491 return result;
492 }
493
494
495
496 static char_type InvalidComponentCharReplacement() {
497 using namespace path_literals;
498 return L<char_type>('_');
499 }
500
501
502
503 #if MPT_GCC_AT_LEAST(12, 0, 0) && MPT_GCC_BEFORE(13, 1, 0)
504 // Work-around <https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105329> /
505 // <https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105651>.
506 #pragma GCC push_options
507 #if defined(__OPTIMIZE__)
508 #pragma GCC optimize("O1")
509 #endif
510 // Work-around brain-damaged GCC warning 'void operator delete(void*, std::size_t)' called on a pointer to an unallocated object '"\\\000\\\000\000"'.
511 // Probably a duplicate of one of the many incarnations of <https://gcc.gnu.org/bugzilla/show_bug.cgi?id=99098>.
512 #pragma GCC diagnostic push
513 #pragma GCC diagnostic ignored "-Wfree-nonheap-object"
514 #endif
515 static void SplitPath(raw_path_type p, raw_path_type * prefix, raw_path_type * drive, raw_path_type * dir, raw_path_type * fbase, raw_path_type * fext) {
516
517 using namespace path_literals;
518
519 if (prefix) {
520 *prefix = raw_path_type();
521 }
522 if (drive) {
523 *drive = raw_path_type();
524 }
525 if (dir) {
526 *dir = raw_path_type();
527 }
528 if (fbase) {
529 *fbase = raw_path_type();
530 }
531 if (fext) {
532 *fext = raw_path_type();
533 }
534
535 if constexpr ((path_style == PathStyle::WindowsNT) || (path_style == PathStyle::Windows9x) || (path_style == PathStyle::DOS_DJGPP)) {
536
537 // We cannot use CRT splitpath here, because:
538 // * limited to _MAX_PATH or similar
539 // * no support for UNC paths
540 // * no support for \\?\ prefixed paths
541
542 if constexpr (path_style == PathStyle::WindowsNT) {
543 // remove \\?\\ prefix
544 if (p.substr(0, 8) == L<char_type>("\\\\?\\UNC\\")) {
545 if (prefix) {
546 *prefix = L<char_type>("\\\\?\\UNC");
547 }
548 p = L<char_type>("\\\\") + p.substr(8);
549 } else if (p.substr(0, 4) == L<char_type>("\\\\?\\")) {
550 if (prefix) {
551 *prefix = L<char_type>("\\\\?\\");
552 }
553 p = p.substr(4);
554 }
555 }
556
557 MPT_MAYBE_CONSTANT_IF (((path_style == PathStyle::WindowsNT) || (path_style == PathStyle::Windows9x)) && (p.length() >= 2) && (p.substr(0, 2) == L<char_type>("\\\\") || p.substr(0, 2) == L<char_type>("\\/") || p.substr(0, 2) == L<char_type>("/\\") || p.substr(0, 2) == L<char_type>("//"))) {
558 // UNC
559 typename raw_path_type::size_type first_slash = p.substr(2).find_first_of(L<char_type>("\\/"));
560 if (first_slash != raw_path_type::npos) {
561 typename raw_path_type::size_type second_slash = p.substr(2 + first_slash + 1).find_first_of(L<char_type>("\\/"));
562 if (second_slash != raw_path_type::npos) {
563 if (drive) {
564 *drive = p.substr(0, 2 + first_slash + 1 + second_slash);
565 }
566 p = p.substr(2 + first_slash + 1 + second_slash);
567 } else {
568 if (drive) {
569 *drive = p;
570 }
571 p = raw_path_type();
572 }
573 } else {
574 if (drive) {
575 *drive = p;
576 }
577 p = raw_path_type();
578 }
579 } else {
580 // local
581 if (p.length() >= 2 && (p[1] == L<char_type>(':'))) {
582 if (drive) {
583 *drive = p.substr(0, 2);
584 }
585 p = p.substr(2);
586 } else {
587 if (drive) {
588 *drive = raw_path_type();
589 }
590 }
591 }
592 typename raw_path_type::size_type last_slash = p.find_last_of(L<char_type>("\\/"));
593 if (last_slash != raw_path_type::npos) {
594 if (dir) {
595 *dir = p.substr(0, last_slash + 1);
596 }
597 p = p.substr(last_slash + 1);
598 } else {
599 if (dir) {
600 *dir = raw_path_type();
601 }
602 }
603 typename raw_path_type::size_type last_dot = p.find_last_of(L<char_type>("."));
604 if (last_dot == raw_path_type::npos) {
605 if (fbase) {
606 *fbase = p;
607 }
608 if (fext) {
609 *fext = raw_path_type();
610 }
611 } else if (last_dot == 0) {
612 if (fbase) {
613 *fbase = p;
614 }
615 if (fext) {
616 *fext = raw_path_type();
617 }
618 } else if (p == L<char_type>(".") || p == L<char_type>("..")) {
619 if (fbase) {
620 *fbase = p;
621 }
622 if (fext) {
623 *fext = raw_path_type();
624 }
625 } else {
626 if (fbase) {
627 *fbase = p.substr(0, last_dot);
628 }
629 if (fext) {
630 *fext = p.substr(last_dot);
631 }
632 }
633
634 } else if constexpr (path_style == PathStyle::Posix) {
635
636 typename raw_path_type::size_type last_slash = p.find_last_of(L<char_type>("/"));
637 if (last_slash != raw_path_type::npos) {
638 if (dir) {
639 *dir = p.substr(0, last_slash + 1);
640 }
641 p = p.substr(last_slash + 1);
642 } else {
643 if (dir) {
644 *dir = raw_path_type();
645 }
646 }
647 typename raw_path_type::size_type last_dot = p.find_last_of(L<char_type>("."));
648 if (last_dot == raw_path_type::npos) {
649 if (fbase) {
650 *fbase = p;
651 }
652 if (fext) {
653 *fext = raw_path_type();
654 }
655 } else if (last_dot == 0) {
656 if (fbase) {
657 *fbase = p;
658 }
659 if (fext) {
660 *fext = raw_path_type();
661 }
662 } else if (p == L<char_type>(".") || p == L<char_type>("..")) {
663 if (fbase) {
664 *fbase = p;
665 }
666 if (fext) {
667 *fext = raw_path_type();
668 }
669 } else {
670 if (fbase) {
671 *fbase = p.substr(0, last_dot);
672 }
673 if (fext) {
674 *fext = p.substr(last_dot);
675 }
676 }
677
678 } else {
679
680 //static_assert(false);
681 }
682 }
683 #if MPT_GCC_AT_LEAST(12, 0, 0) && MPT_GCC_BEFORE(13, 1, 0)
684 #pragma GCC diagnostic pop
685 #pragma GCC pop_options
686 #endif
687
688
689
690 #if MPT_GCC_AT_LEAST(12, 0, 0) && MPT_GCC_BEFORE(13, 1, 0)
691 // Work-around <https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105329> /
692 // <https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105651>.
693 #pragma GCC push_options
694 #if defined(__OPTIMIZE__)
695 #pragma GCC optimize("O1")
696 #endif
697 #endif
698 // Convert a path to its simplified form, i.e. remove ".\" and "..\" entries
699 // Note: We use our own implementation as PathCanonicalize is limited to MAX_PATH
700 // and unlimited versions are only available on Windows 8 and later.
701 // Furthermore, we also convert forward-slashes to backslashes and always remove trailing slashes.
702 static raw_path_type Simplify(const raw_path_type & path) {
703
704 using namespace path_literals;
705
706 raw_path_type result{};
707
708 if (path.empty()) {
709 return result;
710 }
711
712 std::vector<raw_path_type> components;
713
714 if constexpr ((path_style == PathStyle::WindowsNT) || (path_style == PathStyle::Windows9x) || (path_style == PathStyle::DOS_DJGPP)) {
715
716 raw_path_type root;
717 typename raw_path_type::size_type startPos = 0;
718 if (path.size() >= 2 && path[1] == L<char_type>(':')) {
719 // Drive letter
720 root = path.substr(0, 2) + L<char_type>('\\');
721 startPos = 2;
722 } else MPT_MAYBE_CONSTANT_IF (((path_style == PathStyle::WindowsNT) || (path_style == PathStyle::Windows9x)) && (path.substr(0, 2) == L<char_type>("\\\\"))) {
723 // Network share
724 root = L<char_type>("\\\\");
725 startPos = 2;
726 } else if (path.substr(0, 2) == L<char_type>(".\\") || path.substr(0, 2) == L<char_type>("./")) {
727 // Special case for relative paths
728 root = L<char_type>(".\\");
729 startPos = 2;
730 } else if (path.size() >= 1 && (path[0] == L<char_type>('\\') || path[0] == L<char_type>('/'))) {
731 // Special case for relative paths
732 root = L<char_type>("\\");
733 startPos = 1;
734 }
735
736 while (startPos < path.size()) {
737 auto pos = path.find_first_of(L<char_type>("\\/"), startPos);
738 if (pos == raw_path_type::npos) {
739 pos = path.size();
740 }
741 raw_path_type dir = path.substr(startPos, pos - startPos);
742 if (dir == L<char_type>("..")) {
743 // Go back one directory
744 if (!components.empty()) {
745 components.pop_back();
746 }
747 } else if (dir == L<char_type>(".")) {
748 // nop
749 } else if (!dir.empty()) {
750 components.push_back(std::move(dir));
751 }
752 startPos = pos + 1;
753 }
754
755 result = root;
756 result.reserve(path.size());
757 for (const auto & component : components) {
758 result += component + L<char_type>("\\");
759 }
760 if (!components.empty()) {
761 result.pop_back();
762 }
763
764 } else if constexpr (path_style == PathStyle::Posix) {
765
766 raw_path_type root;
767 typename raw_path_type::size_type startPos = 0;
768 if (path.substr(0, 2) == L<char_type>("./")) {
769 // Special case for relative paths
770 root = L<char_type>("./");
771 startPos = 2;
772 } else if (path.size() >= 1 && (path[0] == L<char_type>('/'))) {
773 // Special case for relative paths
774 root = L<char_type>("/");
775 startPos = 1;
776 }
777
778 while (startPos < path.size()) {
779 auto pos = path.find_first_of(L<char_type>("/"), startPos);
780 if (pos == raw_path_type::npos) {
781 pos = path.size();
782 }
783 raw_path_type dir = path.substr(startPos, pos - startPos);
784 if (dir == L<char_type>("..")) {
785 // Go back one directory
786 if (!components.empty()) {
787 components.pop_back();
788 }
789 } else if (dir == L<char_type>(".")) {
790 // nop
791 } else if (!dir.empty()) {
792 components.push_back(std::move(dir));
793 }
794 startPos = pos + 1;
795 }
796
797 result = root;
798 result.reserve(path.size());
799 for (const auto & component : components) {
800 result += component + L<char_type>("/");
801 }
802 if (!components.empty()) {
803 result.pop_back();
804 }
805
806 } else {
807
808 //static_assert(false);
809 }
810
811 return result;
812 }
813 #if MPT_GCC_AT_LEAST(12, 0, 0) && MPT_GCC_BEFORE(13, 1, 0)
814 #pragma GCC pop_options
815 #endif
816
817
818
819 #if MPT_GCC_AT_LEAST(12, 0, 0) && MPT_GCC_BEFORE(13, 1, 0)
820 // Work-around <https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105329> /
821 // <https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105651>.
822 #pragma GCC push_options
823 #if defined(__OPTIMIZE__)
824 #pragma GCC optimize("O1")
825 #endif
826 #endif
827 static bool IsAbsolute(const raw_path_type & path) {
828 using namespace path_literals;
829 bool result{};
830 if constexpr (path_style == PathStyle::WindowsNT) {
831 if (path.substr(0, 8) == L<char_type>("\\\\?\\UNC\\")) {
832 return true;
833 }
834 if (path.substr(0, 4) == L<char_type>("\\\\?\\")) {
835 return true;
836 }
837 if (path.substr(0, 2) == L<char_type>("\\\\")) {
838 return true; // UNC
839 }
840 if (path.substr(0, 2) == L<char_type>("//")) {
841 return true; // UNC
842 }
843 result = (path.length()) >= 3 && (path[1] == L<char_type>(':')) && IsPathSeparator(path[2]);
844 } else if constexpr (path_style == PathStyle::Windows9x) {
845 if (path.substr(0, 2) == L<char_type>("\\\\")) {
846 return true; // UNC
847 }
848 if (path.substr(0, 2) == L<char_type>("//")) {
849 return true; // UNC
850 }
851 result = (path.length()) >= 3 && (path[1] == L<char_type>(':')) && IsPathSeparator(path[2]);
852 } else if constexpr (path_style == PathStyle::DOS_DJGPP) {
853 result = (path.length()) >= 3 && (path[1] == L<char_type>(':')) && IsPathSeparator(path[2]);
854 } else if constexpr (path_style == PathStyle::Posix) {
855 result = (path.length() >= 1) && IsPathSeparator(path[0]);
856 } else {
857 //static_assert(false);
858 }
859 return result;
860 }
861 #if MPT_GCC_AT_LEAST(12, 0, 0) && MPT_GCC_BEFORE(13, 1, 0)
862 #pragma GCC pop_options
863 #endif
864 };
865
866
867
868 template <typename Traits, bool allow_transcode_locale = true>
869 class BasicPathString {
870
871 public:
872
873 using path_traits = Traits;
874 using raw_path_type = typename path_traits::raw_path_type;
875 using char_type = typename raw_path_type::value_type;
876
877 private:
878
879 raw_path_type path;
880
881 public:
882
883 template <typename Tpath, typename std::enable_if<std::is_same<Tpath, raw_path_type>::value, bool>::type = true>
884 BasicPathString(const Tpath & path_)
885 : path(path_) {
886 return;
887 }
888
889 template <typename Tpath, typename std::enable_if<std::is_same<Tpath, raw_path_type>::value, bool>::type = true>
890 operator Tpath() const {
891 return path;
892 }
893
894 public:
895
896 BasicPathString() = default;
897 BasicPathString(const BasicPathString &) = default;
898 BasicPathString(BasicPathString &&) noexcept = default;
899
900 BasicPathString & assign(const BasicPathString & other) {
901 path = other.path;
902 return *this;
903 }
904
905 BasicPathString & assign(BasicPathString && other) noexcept {
906 path = std::move(other.path);
907 return *this;
908 }
909
910 BasicPathString & operator=(const BasicPathString & other) {
911 return assign(other);
912 }
913
914 BasicPathString & operator=(BasicPathString && other) noexcept {
915 return assign(std::move(other));
916 }
917
918 BasicPathString & append(const BasicPathString & other) {
919 path.append(other.path);
920 return *this;
921 }
922
923 BasicPathString & operator+=(const BasicPathString & other) {
924 return append(other);
925 }
926
927 friend BasicPathString operator+(const BasicPathString & a, const BasicPathString & b) {
928 return BasicPathString(a).append(b);
929 }
930
931 friend bool operator<(const BasicPathString & a, const BasicPathString & b) {
932 return a.AsNative() < b.AsNative();
933 }
934
935 friend bool operator==(const BasicPathString & a, const BasicPathString & b) {
936 return a.AsNative() == b.AsNative();
937 }
938
939 friend bool operator!=(const BasicPathString & a, const BasicPathString & b) {
940 return a.AsNative() != b.AsNative();
941 }
942
943 bool empty() const {
944 return path.empty();
945 }
946
947 std::size_t length() const {
948 return path.size();
949 }
950
951 std::size_t Length() const {
952 return path.size();
953 }
954
955 public:
956
957 raw_path_type AsNative() const {
958 return path;
959 }
960
961 static BasicPathString FromNative(const raw_path_type & path) {
962 return BasicPathString(path);
963 }
964
965 mpt::ustring ToUnicode() const {
966 return mpt::transcode<mpt::ustring>(path);
967 }
968
969 static BasicPathString FromUnicode(const mpt::ustring & path) {
970 return BasicPathString(mpt::transcode<raw_path_type>(path));
971 }
972
973 std::string ToUTF8() const {
974 return mpt::transcode<std::string>(mpt::common_encoding::utf8, path);
975 }
976
977 static BasicPathString FromUTF8(const std::string & path) {
978 return BasicPathString(mpt::transcode<raw_path_type>(mpt::common_encoding::utf8, path));
979 }
980
981 #if !defined(MPT_COMPILER_QUIRK_NO_WCHAR)
982
983 std::wstring ToWide() const {
984 return mpt::transcode<std::wstring>(path);
985 }
986
987 static BasicPathString FromWide(const std::wstring & path) {
988 return BasicPathString(mpt::transcode<raw_path_type>(path));
989 }
990
991 #endif // !MPT_COMPILER_QUIRK_NO_WCHAR
992
993 template <bool enable = allow_transcode_locale, typename std::enable_if<enable, bool>::type = true>
994 std::string ToLocale() const {
995 return mpt::transcode<std::string>(mpt::logical_encoding::locale, path);
996 }
997
998 template <bool enable = allow_transcode_locale, typename std::enable_if<enable, bool>::type = true>
999 static BasicPathString FromLocale(const std::string & path) {
1000 return BasicPathString(mpt::transcode<raw_path_type>(mpt::logical_encoding::locale, path));
1001 }
1002
1003 #if MPT_DETECTED_MFC
1004
1005 CString ToCString() const {
1006 return mpt::transcode<CString>(path);
1007 }
1008
1009 static BasicPathString FromCString(const CString & path) {
1010 return BasicPathString(mpt::transcode<raw_path_type>(path));
1011 }
1012
1013 #endif // MPT_DETECTED_MFC
1014
1015 public:
1016
1017 static bool IsPathSeparator(char_type c) {
1018 return path_traits::IsPathSeparator(c);
1019 }
1020
1021 static char_type GetDefaultPathSeparator() {
1022 return path_traits::GetDefaultPathSeparator();
1023 }
1024
1025 bool HasTrailingSlash() const {
1026 if (path.empty()) {
1027 return false;
1028 }
1029 char_type c = path[path.length() - 1];
1030 return IsPathSeparator(c);
1031 }
1032
1033 BasicPathString AsSanitizedComponent() const {
1034 BasicPathString result = *this;
1035 for (auto & c : result.path) {
1036 if (!path_traits::IsValidComponentChar(c)) {
1037 c = path_traits::InvalidComponentCharReplacement();
1038 }
1039 }
1040 return result;
1041 }
1042
1043 BasicPathString WithoutTrailingSlash() const {
1044 BasicPathString result = *this;
1045 while (result.HasTrailingSlash()) {
1046 if (result.Length() == 1) {
1047 return result;
1048 }
1049 result = BasicPathString(result.AsNative().substr(0, result.AsNative().length() - 1));
1050 }
1051 return result;
1052 }
1053
1054 BasicPathString WithTrailingSlash() const {
1055 BasicPathString result = *this;
1056 if (!result.empty() && !result.HasTrailingSlash()) {
1057 result.path += GetDefaultPathSeparator();
1058 }
1059 return result;
1060 }
1061
1062 void SplitPath(BasicPathString * prefix, BasicPathString * drive, BasicPathString * dir, BasicPathString * fbase, BasicPathString * fext) const {
1063 path_traits::SplitPath(path, prefix ? &prefix->path : nullptr, drive ? &drive->path : nullptr, dir ? &dir->path : nullptr, fbase ? &fbase->path : nullptr, fext ? &fext->path : nullptr);
1064 }
1065
1066 // \\?\ or \\?\\UNC or empty
1067 BasicPathString GetPrefix() const {
1068 BasicPathString prefix;
1069 SplitPath(&prefix, nullptr, nullptr, nullptr, nullptr);
1070 return prefix;
1071 }
1072
1073 // Drive letter + colon, e.g. "C:" or \\server\share
1074 BasicPathString GetDrive() const {
1075 BasicPathString drive;
1076 SplitPath(nullptr, &drive, nullptr, nullptr, nullptr);
1077 return drive;
1078 }
1079
1080 // Directory, e.g. "\OpenMPT\"
1081 BasicPathString GetDirectory() const {
1082 BasicPathString dir;
1083 SplitPath(nullptr, nullptr, &dir, nullptr, nullptr);
1084 return dir;
1085 }
1086
1087 // Drive + Dir, e.g. "C:\OpenMPT\"
1088 BasicPathString GetDirectoryWithDrive() const {
1089 BasicPathString drive, dir;
1090 SplitPath(nullptr, &drive, &dir, nullptr, nullptr);
1091 return drive + dir;
1092 }
1093
1094 // File name without extension, e.g. "OpenMPT"
1095 BasicPathString GetFilenameBase() const {
1096 BasicPathString fname;
1097 SplitPath(nullptr, nullptr, nullptr, &fname, nullptr);
1098 return fname;
1099 }
1100
1101 // Extension including dot, e.g. ".exe"
1102 BasicPathString GetFilenameExtension() const {
1103 BasicPathString ext;
1104 SplitPath(nullptr, nullptr, nullptr, nullptr, &ext);
1105 return ext;
1106 }
1107
1108 // File name + extension, e.g. "OpenMPT.exe"
1109 BasicPathString GetFilename() const {
1110 BasicPathString name, ext;
1111 SplitPath(nullptr, nullptr, nullptr, &name, &ext);
1112 return name + ext;
1113 }
1114
1115 // Return the same path string with a different (or appended) extension (including "."), e.g. "foo.bar",".txt" -> "foo.txt" or "C:\OpenMPT\foo",".txt" -> "C:\OpenMPT\foo.txt"
1116 BasicPathString ReplaceExtension(const BasicPathString & newExt) const {
1117 return GetDirectoryWithDrive() + GetFilenameBase() + newExt;
1118 }
1119
1120 // Convert a path to its simplified form, i.e. remove ".\" and "..\" entries, similar to std::fs::path::lexically_normal
1121 BasicPathString Simplify() const {
1122 return BasicPathString::FromNative(path_traits::Simplify(path));
1123 }
1124
1125 bool IsAbsolute() const {
1126 return path_traits::IsAbsolute(path);
1127 }
1128
1129 bool is_absolute() const {
1130 return path_traits::IsAbsolute(path);
1131 }
1132
1133 bool is_relative() const {
1134 return !path_traits::IsAbsolute(path);
1135 }
1136 };
1137
1138
1139
1140 } // namespace MPT_INLINE_NS
1141 } // namespace mpt
1142
1143
1144
1145 #endif // MPT_PATH_BASIC_PATH_HPP