1// filesystem path.hpp ---------------------------------------------------------------//
2
3// Copyright Vladimir Prus 2002
4// Copyright Beman Dawes 2002-2005, 2009
5// Copyright Andrey Semashev 2021-2024
6
7// Distributed under the Boost Software License, Version 1.0.
8// See http://www.boost.org/LICENSE_1_0.txt
9
10// Library home page: http://www.boost.org/libs/filesystem
11
12// path::stem(), extension(), and replace_extension() are based on
13// basename(), extension(), and change_extension() from the original
14// filesystem/convenience.hpp header by Vladimir Prus.
15
16#ifndef BOOST_FILESYSTEM_PATH_HPP
17#define BOOST_FILESYSTEM_PATH_HPP
18
19#include <boost/filesystem/config.hpp>
20#include <cstddef>
21#include <iosfwd>
22#include <locale>
23#include <string>
24#include <iterator>
25#include <type_traits>
26#if !defined(BOOST_NO_CXX17_HDR_STRING_VIEW)
27#include <string_view>
28#endif
29#include <boost/assert.hpp>
30#include <boost/iterator/iterator_facade.hpp>
31#include <boost/iterator/iterator_categories.hpp>
32#include <boost/io/quoted.hpp>
33#include <boost/functional/hash_fwd.hpp>
34#include <boost/filesystem/detail/path_traits.hpp>
35#include <boost/filesystem/detail/type_traits/negation.hpp>
36#include <boost/filesystem/detail/type_traits/conjunction.hpp>
37#include <boost/filesystem/detail/type_traits/disjunction.hpp>
38
39#include <boost/filesystem/detail/header.hpp> // must be the last #include
40
41namespace boost {
42namespace filesystem {
43
44class path;
45
46namespace path_detail { // intentionally don't use filesystem::detail to not bring internal Boost.Filesystem functions into ADL via path_constants
47
48template< typename Char, Char Separator, Char PreferredSeparator, Char Dot >
49struct path_constants
50{
51 typedef path_constants< Char, Separator, PreferredSeparator, Dot > path_constants_base;
52 typedef Char value_type;
53 static BOOST_CONSTEXPR_OR_CONST value_type separator = Separator;
54 static BOOST_CONSTEXPR_OR_CONST value_type preferred_separator = PreferredSeparator;
55 static BOOST_CONSTEXPR_OR_CONST value_type dot = Dot;
56};
57
58#if defined(BOOST_NO_CXX17_INLINE_VARIABLES)
59template< typename Char, Char Separator, Char PreferredSeparator, Char Dot >
60BOOST_CONSTEXPR_OR_CONST typename path_constants< Char, Separator, PreferredSeparator, Dot >::value_type
61path_constants< Char, Separator, PreferredSeparator, Dot >::separator;
62template< typename Char, Char Separator, Char PreferredSeparator, Char Dot >
63BOOST_CONSTEXPR_OR_CONST typename path_constants< Char, Separator, PreferredSeparator, Dot >::value_type
64path_constants< Char, Separator, PreferredSeparator, Dot >::preferred_separator;
65template< typename Char, Char Separator, Char PreferredSeparator, Char Dot >
66BOOST_CONSTEXPR_OR_CONST typename path_constants< Char, Separator, PreferredSeparator, Dot >::value_type
67path_constants< Char, Separator, PreferredSeparator, Dot >::dot;
68#endif
69
70class path_iterator;
71class path_reverse_iterator;
72
73} // namespace path_detail
74
75namespace detail {
76
77struct path_algorithms
78{
79 // A struct that denotes a contiguous range of characters in a string. A lightweight alternative to string_view.
80 struct substring
81 {
82 std::size_t pos;
83 std::size_t size;
84 };
85
86 typedef path_traits::path_native_char_type value_type;
87 typedef std::basic_string< value_type > string_type;
88
89 static bool has_filename_v3(path const& p);
90 static bool has_filename_v4(path const& p);
91 BOOST_FILESYSTEM_DECL static path filename_v3(path const& p);
92 static path filename_v4(path const& p);
93
94 BOOST_FILESYSTEM_DECL static path stem_v3(path const& p);
95 BOOST_FILESYSTEM_DECL static path stem_v4(path const& p);
96 BOOST_FILESYSTEM_DECL static path extension_v3(path const& p);
97 static path extension_v4(path const& p);
98
99 BOOST_FILESYSTEM_DECL static void remove_filename_v3(path& p);
100 BOOST_FILESYSTEM_DECL static void remove_filename_v4(path& p);
101
102 BOOST_FILESYSTEM_DECL static void replace_extension_v3(path& p, path const& new_extension);
103 BOOST_FILESYSTEM_DECL static void replace_extension_v4(path& p, path const& new_extension);
104
105 BOOST_FILESYSTEM_DECL static path lexically_normal_v3(path const& p);
106 BOOST_FILESYSTEM_DECL static path lexically_normal_v4(path const& p);
107
108 BOOST_FILESYSTEM_DECL static path generic_path_v3(path const& p);
109 BOOST_FILESYSTEM_DECL static path generic_path_v4(path const& p);
110
111#if defined(BOOST_WINDOWS_API)
112 BOOST_FILESYSTEM_DECL static void make_preferred_v3(path& p);
113 BOOST_FILESYSTEM_DECL static void make_preferred_v4(path& p);
114#endif
115
116 BOOST_FILESYSTEM_DECL static int compare_v3(path const& left, path const& right);
117 BOOST_FILESYSTEM_DECL static int compare_v4(path const& left, path const& right);
118
119 BOOST_FILESYSTEM_DECL static void append_v3(path& p, const value_type* b, const value_type* e);
120 BOOST_FILESYSTEM_DECL static void append_v4(path& p, const value_type* b, const value_type* e);
121 static void append_v4(path& left, path const& right);
122
123 // Returns: If separator is to be appended, m_pathname.size() before append. Otherwise 0.
124 // Note: An append is never performed if size()==0, so a returned 0 is unambiguous.
125 BOOST_FILESYSTEM_DECL static string_type::size_type append_separator_if_needed(path& p);
126 BOOST_FILESYSTEM_DECL static void erase_redundant_separator(path& p, string_type::size_type sep_pos);
127
128 BOOST_FILESYSTEM_DECL static string_type::size_type find_root_name_size(path const& p);
129 BOOST_FILESYSTEM_DECL static string_type::size_type find_root_path_size(path const& p);
130 BOOST_FILESYSTEM_DECL static substring find_root_directory(path const& p);
131 BOOST_FILESYSTEM_DECL static substring find_relative_path(path const& p);
132 BOOST_FILESYSTEM_DECL static string_type::size_type find_parent_path_size(path const& p);
133 BOOST_FILESYSTEM_DECL static string_type::size_type find_filename_v4_size(path const& p);
134 BOOST_FILESYSTEM_DECL static string_type::size_type find_extension_v4_size(path const& p);
135
136 BOOST_FILESYSTEM_DECL static int lex_compare_v3
137 (
138 path_detail::path_iterator first1, path_detail::path_iterator const& last1,
139 path_detail::path_iterator first2, path_detail::path_iterator const& last2
140 );
141 BOOST_FILESYSTEM_DECL static int lex_compare_v4
142 (
143 path_detail::path_iterator first1, path_detail::path_iterator const& last1,
144 path_detail::path_iterator first2, path_detail::path_iterator const& last2
145 );
146
147 BOOST_FILESYSTEM_DECL static void increment_v3(path_detail::path_iterator& it);
148 BOOST_FILESYSTEM_DECL static void increment_v4(path_detail::path_iterator& it);
149 BOOST_FILESYSTEM_DECL static void decrement_v3(path_detail::path_iterator& it);
150 BOOST_FILESYSTEM_DECL static void decrement_v4(path_detail::path_iterator& it);
151};
152
153} // namespace detail
154
155//------------------------------------------------------------------------------------//
156// //
157// class path //
158// //
159//------------------------------------------------------------------------------------//
160
161class path :
162 public filesystem::path_detail::path_constants<
163#ifdef BOOST_WINDOWS_API
164 detail::path_traits::path_native_char_type, L'/', L'\\', L'.'
165#else
166 detail::path_traits::path_native_char_type, '/', '/', '.'
167#endif
168 >
169{
170 friend class path_detail::path_iterator;
171 friend class path_detail::path_reverse_iterator;
172 friend struct detail::path_algorithms;
173
174public:
175 // value_type is the character type used by the operating system API to
176 // represent paths.
177
178 typedef detail::path_algorithms::value_type value_type;
179 typedef detail::path_algorithms::string_type string_type;
180 typedef detail::path_traits::codecvt_type codecvt_type;
181
182 // ----- character encoding conversions -----
183
184 // Following the principle of least astonishment, path input arguments
185 // passed to or obtained from the operating system via objects of
186 // class path behave as if they were directly passed to or
187 // obtained from the O/S API, unless conversion is explicitly requested.
188 //
189 // POSIX specfies that path strings are passed unchanged to and from the
190 // API. Note that this is different from the POSIX command line utilities,
191 // which convert according to a locale.
192 //
193 // Thus for POSIX, char strings do not undergo conversion. wchar_t strings
194 // are converted to/from char using the path locale or, if a conversion
195 // argument is given, using a conversion object modeled on
196 // std::wstring_convert.
197 //
198 // The path locale, which is global to the thread, can be changed by the
199 // imbue() function. It is initialized to an implementation defined locale.
200 //
201 // For Windows, wchar_t strings do not undergo conversion. char strings
202 // are converted using the "ANSI" or "OEM" code pages, as determined by
203 // the AreFileApisANSI() function, or, if a conversion argument is given,
204 // using a conversion object modeled on std::wstring_convert.
205 //
206 // See m_pathname comments for further important rationale.
207
208 // TODO: rules needed for operating systems that use / or .
209 // differently, or format directory paths differently from file paths.
210 //
211 // **********************************************************************************
212 //
213 // More work needed: How to handle an operating system that may have
214 // slash characters or dot characters in valid filenames, either because
215 // it doesn't follow the POSIX standard, or because it allows MBCS
216 // filename encodings that may contain slash or dot characters. For
217 // example, ISO/IEC 2022 (JIS) encoding which allows switching to
218 // JIS x0208-1983 encoding. A valid filename in this set of encodings is
219 // 0x1B 0x24 0x42 [switch to X0208-1983] 0x24 0x2F [U+304F Kiragana letter KU]
220 // ^^^^
221 // Note that 0x2F is the ASCII slash character
222 //
223 // **********************************************************************************
224
225 // Supported source arguments: half-open iterator range, container, c-array,
226 // and single pointer to null terminated string.
227
228 // All source arguments except pointers to null terminated byte strings support
229 // multi-byte character strings which may have embedded nulls. Embedded null
230 // support is required for some Asian languages on Windows.
231
232 // "const codecvt_type& cvt=codecvt()" default arguments are not used because this
233 // limits the impact of locale("") initialization failures on POSIX systems to programs
234 // that actually depend on locale(""). It further ensures that exceptions thrown
235 // as a result of such failues occur after main() has started, so can be caught.
236
237private:
238 //! Assignment operation
239 class assign_op
240 {
241 private:
242 path& m_self;
243
244 public:
245 typedef void result_type;
246
247 explicit assign_op(path& self) noexcept : m_self(self) {}
248
249 result_type operator() (const value_type* source, const value_type* source_end, const codecvt_type* = nullptr) const
250 {
251 m_self.m_pathname.assign(first: source, last: source_end);
252 }
253
254 template< typename OtherChar >
255 result_type operator() (const OtherChar* source, const OtherChar* source_end, const codecvt_type* cvt = nullptr) const
256 {
257 m_self.m_pathname.clear();
258 detail::path_traits::convert(source, source_end, m_self.m_pathname, cvt);
259 }
260 };
261
262 //! Concatenation operation
263 class concat_op
264 {
265 private:
266 path& m_self;
267
268 public:
269 typedef void result_type;
270
271 explicit concat_op(path& self) noexcept : m_self(self) {}
272
273 result_type operator() (const value_type* source, const value_type* source_end, const codecvt_type* = nullptr) const
274 {
275 m_self.m_pathname.append(first: source, last: source_end);
276 }
277
278 template< typename OtherChar >
279 result_type operator() (const OtherChar* source, const OtherChar* source_end, const codecvt_type* cvt = nullptr) const
280 {
281 detail::path_traits::convert(source, source_end, m_self.m_pathname, cvt);
282 }
283 };
284
285 //! Path appending operation
286 class append_op
287 {
288 private:
289 path& m_self;
290
291 public:
292 typedef void result_type;
293
294 explicit append_op(path& self) noexcept : m_self(self) {}
295
296 BOOST_FORCEINLINE result_type operator() (const value_type* source, const value_type* source_end, const codecvt_type* = nullptr) const
297 {
298 m_self.append(begin: source, end: source_end);
299 }
300
301 template< typename OtherChar >
302 BOOST_FORCEINLINE result_type operator() (const OtherChar* source, const OtherChar* source_end, const codecvt_type* cvt = nullptr) const
303 {
304 string_type src;
305 detail::path_traits::convert(source, source_end, src, cvt);
306 m_self.append(begin: src.data(), end: src.data() + src.size());
307 }
308 };
309
310 //! Path comparison operation
311 class compare_op
312 {
313 private:
314 path const& m_self;
315
316 public:
317 typedef int result_type;
318
319 explicit compare_op(path const& self) noexcept : m_self(self) {}
320
321 result_type operator() (const value_type* source, const value_type* source_end, const codecvt_type* = nullptr) const;
322
323 template< typename OtherChar >
324 result_type operator() (const OtherChar* source, const OtherChar* source_end, const codecvt_type* cvt = nullptr) const;
325 };
326
327public:
328 typedef path_detail::path_iterator iterator;
329 typedef iterator const_iterator;
330 typedef path_detail::path_reverse_iterator reverse_iterator;
331 typedef reverse_iterator const_reverse_iterator;
332
333public:
334 // ----- constructors -----
335
336 path() noexcept {}
337 path(path const& p) : m_pathname(p.m_pathname) {}
338 path(path const& p, codecvt_type const&) : m_pathname(p.m_pathname) {}
339
340 path(const value_type* s) : m_pathname(s) {}
341 path(const value_type* s, codecvt_type const&) : m_pathname(s) {}
342 path(string_type const& s) : m_pathname(s) {}
343 path(string_type const& s, codecvt_type const&) : m_pathname(s) {}
344#if !defined(BOOST_NO_CXX17_HDR_STRING_VIEW)
345 path(std::basic_string_view< value_type > const& s) : m_pathname(s) {}
346 path(std::basic_string_view< value_type > const& s, codecvt_type const&) : m_pathname(s) {}
347#endif
348
349 template<
350 typename Source,
351 typename = typename std::enable_if<
352 detail::conjunction<
353 detail::path_traits::is_path_source< typename std::remove_cv< Source >::type >,
354 detail::negation< detail::path_traits::is_native_path_source< typename std::remove_cv< Source >::type > >
355 >::value
356 >::type
357 >
358 path(Source const& source)
359 {
360 assign(source);
361 }
362
363 template<
364 typename Source,
365 typename = typename std::enable_if<
366 detail::conjunction<
367 detail::path_traits::is_path_source< typename std::remove_cv< Source >::type >,
368 detail::negation< detail::path_traits::is_native_path_source< typename std::remove_cv< Source >::type > >
369 >::value
370 >::type
371 >
372 explicit path(Source const& source, codecvt_type const& cvt)
373 {
374 assign(source, cvt);
375 }
376
377 path(path&& p) noexcept : m_pathname(static_cast< string_type&& >(p.m_pathname))
378 {
379 }
380 path(path&& p, codecvt_type const&) noexcept : m_pathname(static_cast< string_type&& >(p.m_pathname))
381 {
382 }
383 path& operator=(path&& p) noexcept
384 {
385 m_pathname = static_cast< string_type&& >(p.m_pathname);
386 return *this;
387 }
388 path& assign(path&& p) noexcept
389 {
390 m_pathname = static_cast< string_type&& >(p.m_pathname);
391 return *this;
392 }
393 path& assign(path&& p, codecvt_type const&) noexcept
394 {
395 m_pathname = static_cast< string_type&& >(p.m_pathname);
396 return *this;
397 }
398
399 path(string_type&& s) noexcept : m_pathname(static_cast< string_type&& >(s))
400 {
401 }
402 path(string_type&& s, codecvt_type const&) noexcept : m_pathname(static_cast< string_type&& >(s))
403 {
404 }
405 path& operator=(string_type&& p) noexcept
406 {
407 m_pathname = static_cast< string_type&& >(p);
408 return *this;
409 }
410 path& assign(string_type&& p) noexcept
411 {
412 m_pathname = static_cast< string_type&& >(p);
413 return *this;
414 }
415 path& assign(string_type&& p, codecvt_type const&) noexcept
416 {
417 m_pathname = static_cast< string_type&& >(p);
418 return *this;
419 }
420
421 path(const value_type* begin, const value_type* end) : m_pathname(begin, end) {}
422 path(const value_type* begin, const value_type* end, codecvt_type const&) : m_pathname(begin, end) {}
423
424 template<
425 typename InputIterator,
426 typename = typename std::enable_if<
427 detail::conjunction<
428 detail::path_traits::is_path_source_iterator< InputIterator >,
429 detail::negation< detail::path_traits::is_native_char_ptr< InputIterator > >
430 >::value
431 >::type
432 >
433 path(InputIterator begin, InputIterator end)
434 {
435 if (begin != end)
436 {
437 typedef std::basic_string< typename std::iterator_traits< InputIterator >::value_type > source_t;
438 source_t source(begin, end);
439 assign(static_cast< source_t&& >(source));
440 }
441 }
442
443 template<
444 typename InputIterator,
445 typename = typename std::enable_if<
446 detail::conjunction<
447 detail::path_traits::is_path_source_iterator< InputIterator >,
448 detail::negation< detail::path_traits::is_native_char_ptr< InputIterator > >
449 >::value
450 >::type
451 >
452 path(InputIterator begin, InputIterator end, codecvt_type const& cvt)
453 {
454 if (begin != end)
455 {
456 typedef std::basic_string< typename std::iterator_traits< InputIterator >::value_type > source_t;
457 source_t source(begin, end);
458 assign(static_cast< source_t&& >(source), cvt);
459 }
460 }
461
462 path(std::nullptr_t) = delete;
463 path& operator= (std::nullptr_t) = delete;
464
465public:
466 // ----- assignments -----
467
468 // We need to explicitly define copy assignment as otherwise it will be implicitly defined as deleted because there is move assignment
469 path& operator=(path const& p);
470
471 template< typename Source >
472 typename std::enable_if<
473 detail::disjunction<
474 detail::path_traits::is_path_source< typename std::remove_cv< Source >::type >,
475 detail::path_traits::is_convertible_to_path_source< typename std::remove_cv< Source >::type >
476 >::value,
477 path&
478 >::type operator=(Source const& source)
479 {
480 return assign(source);
481 }
482
483 path& assign(path const& p)
484 {
485 m_pathname = p.m_pathname;
486 return *this;
487 }
488
489 template< typename Source >
490 typename std::enable_if<
491 detail::path_traits::is_path_source< typename std::remove_cv< Source >::type >::value,
492 path&
493 >::type assign(Source const& source)
494 {
495 detail::path_traits::dispatch(source, assign_op(*this));
496 return *this;
497 }
498
499 template< typename Source >
500 typename std::enable_if<
501 detail::conjunction<
502 detail::path_traits::is_convertible_to_path_source< typename std::remove_cv< Source >::type >,
503 detail::negation< detail::path_traits::is_path_source< typename std::remove_cv< Source >::type > >
504 >::value,
505 path&
506 >::type assign(Source const& source)
507 {
508 detail::path_traits::dispatch_convertible(source, assign_op(*this));
509 return *this;
510 }
511
512 path& assign(path const& p, codecvt_type const&)
513 {
514 m_pathname = p.m_pathname;
515 return *this;
516 }
517
518 template< typename Source >
519 typename std::enable_if<
520 detail::path_traits::is_path_source< typename std::remove_cv< Source >::type >::value,
521 path&
522 >::type assign(Source const& source, codecvt_type const& cvt)
523 {
524 detail::path_traits::dispatch(source, assign_op(*this), &cvt);
525 return *this;
526 }
527
528 template< typename Source >
529 typename std::enable_if<
530 detail::conjunction<
531 detail::path_traits::is_convertible_to_path_source< typename std::remove_cv< Source >::type >,
532 detail::negation< detail::path_traits::is_path_source< typename std::remove_cv< Source >::type > >
533 >::value,
534 path&
535 >::type assign(Source const& source, codecvt_type const& cvt)
536 {
537 detail::path_traits::dispatch_convertible(source, assign_op(*this), &cvt);
538 return *this;
539 }
540
541 path& assign(const value_type* begin, const value_type* end)
542 {
543 m_pathname.assign(first: begin, last: end);
544 return *this;
545 }
546
547 template< typename InputIterator >
548 typename std::enable_if<
549 detail::conjunction<
550 detail::path_traits::is_path_source_iterator< InputIterator >,
551 detail::negation< detail::path_traits::is_native_char_ptr< InputIterator > >
552 >::value,
553 path&
554 >::type assign(InputIterator begin, InputIterator end)
555 {
556 m_pathname.clear();
557 if (begin != end)
558 {
559 typedef std::basic_string< typename std::iterator_traits< InputIterator >::value_type > source_t;
560 source_t source(begin, end);
561 assign(static_cast< source_t&& >(source));
562 }
563 return *this;
564 }
565
566 path& assign(const value_type* begin, const value_type* end, codecvt_type const&)
567 {
568 m_pathname.assign(first: begin, last: end);
569 return *this;
570 }
571
572 template< typename InputIterator >
573 typename std::enable_if<
574 detail::conjunction<
575 detail::path_traits::is_path_source_iterator< InputIterator >,
576 detail::negation< detail::path_traits::is_native_char_ptr< InputIterator > >
577 >::value,
578 path&
579 >::type assign(InputIterator begin, InputIterator end, codecvt_type const& cvt)
580 {
581 m_pathname.clear();
582 if (begin != end)
583 {
584 typedef std::basic_string< typename std::iterator_traits< InputIterator >::value_type > source_t;
585 source_t source(begin, end);
586 assign(static_cast< source_t&& >(source), cvt);
587 }
588 return *this;
589 }
590
591 // ----- concatenation -----
592
593 path& operator+=(path const& p);
594
595 template< typename Source >
596 typename std::enable_if<
597 detail::path_traits::is_convertible_to_path_source< typename std::remove_cv< Source >::type >::value,
598 path&
599 >::type operator+=(Source const& source)
600 {
601 return concat(source);
602 }
603
604 path& operator+=(value_type c)
605 {
606 m_pathname.push_back(c: c);
607 return *this;
608 }
609
610 template< typename CharT >
611 typename std::enable_if<
612 detail::path_traits::is_path_char_type< CharT >::value,
613 path&
614 >::type operator+=(CharT c)
615 {
616 CharT tmp[2];
617 tmp[0] = c;
618 tmp[1] = static_cast< CharT >(0);
619 concat_op(*this)(tmp, tmp + 1);
620 return *this;
621 }
622
623 path& concat(path const& p)
624 {
625 m_pathname.append(str: p.m_pathname);
626 return *this;
627 }
628
629 template< typename Source >
630 typename std::enable_if<
631 detail::path_traits::is_path_source< typename std::remove_cv< Source >::type >::value,
632 path&
633 >::type concat(Source const& source)
634 {
635 detail::path_traits::dispatch(source, concat_op(*this));
636 return *this;
637 }
638
639 template< typename Source >
640 typename std::enable_if<
641 detail::conjunction<
642 detail::path_traits::is_convertible_to_path_source< typename std::remove_cv< Source >::type >,
643 detail::negation< detail::path_traits::is_path_source< typename std::remove_cv< Source >::type > >
644 >::value,
645 path&
646 >::type concat(Source const& source)
647 {
648 detail::path_traits::dispatch_convertible(source, concat_op(*this));
649 return *this;
650 }
651
652 path& concat(path const& p, codecvt_type const&)
653 {
654 m_pathname.append(str: p.m_pathname);
655 return *this;
656 }
657
658 template< typename Source >
659 typename std::enable_if<
660 detail::path_traits::is_path_source< typename std::remove_cv< Source >::type >::value,
661 path&
662 >::type concat(Source const& source, codecvt_type const& cvt)
663 {
664 detail::path_traits::dispatch(source, concat_op(*this), &cvt);
665 return *this;
666 }
667
668 template< typename Source >
669 typename std::enable_if<
670 detail::conjunction<
671 detail::path_traits::is_convertible_to_path_source< typename std::remove_cv< Source >::type >,
672 detail::negation< detail::path_traits::is_path_source< typename std::remove_cv< Source >::type > >
673 >::value,
674 path&
675 >::type concat(Source const& source, codecvt_type const& cvt)
676 {
677 detail::path_traits::dispatch_convertible(source, concat_op(*this), &cvt);
678 return *this;
679 }
680
681 path& concat(const value_type* begin, const value_type* end)
682 {
683 m_pathname.append(first: begin, last: end);
684 return *this;
685 }
686
687 template< typename InputIterator >
688 typename std::enable_if<
689 detail::conjunction<
690 detail::path_traits::is_path_source_iterator< InputIterator >,
691 detail::negation< detail::path_traits::is_native_char_ptr< InputIterator > >
692 >::value,
693 path&
694 >::type concat(InputIterator begin, InputIterator end)
695 {
696 if (begin != end)
697 {
698 std::basic_string< typename std::iterator_traits< InputIterator >::value_type > source(begin, end);
699 detail::path_traits::dispatch(source, concat_op(*this));
700 }
701 return *this;
702 }
703
704 path& concat(const value_type* begin, const value_type* end, codecvt_type const&)
705 {
706 m_pathname.append(first: begin, last: end);
707 return *this;
708 }
709
710 template< typename InputIterator >
711 typename std::enable_if<
712 detail::conjunction<
713 detail::path_traits::is_path_source_iterator< InputIterator >,
714 detail::negation< detail::path_traits::is_native_char_ptr< InputIterator > >
715 >::value,
716 path&
717 >::type concat(InputIterator begin, InputIterator end, codecvt_type const& cvt)
718 {
719 if (begin != end)
720 {
721 std::basic_string< typename std::iterator_traits< InputIterator >::value_type > source(begin, end);
722 detail::path_traits::dispatch(source, concat_op(*this), &cvt);
723 }
724 return *this;
725 }
726
727 // ----- appends -----
728
729 // if a separator is added, it is the preferred separator for the platform;
730 // slash for POSIX, backslash for Windows
731
732 path& operator/=(path const& p);
733
734 template< typename Source >
735 BOOST_FORCEINLINE typename std::enable_if<
736 detail::path_traits::is_convertible_to_path_source< typename std::remove_cv< Source >::type >::value,
737 path&
738 >::type operator/=(Source const& source)
739 {
740 return append(source);
741 }
742
743 path& append(path const& p);
744
745 template< typename Source >
746 BOOST_FORCEINLINE typename std::enable_if<
747 detail::path_traits::is_path_source< typename std::remove_cv< Source >::type >::value,
748 path&
749 >::type append(Source const& source)
750 {
751 detail::path_traits::dispatch(source, append_op(*this));
752 return *this;
753 }
754
755 template< typename Source >
756 BOOST_FORCEINLINE typename std::enable_if<
757 detail::conjunction<
758 detail::path_traits::is_convertible_to_path_source< typename std::remove_cv< Source >::type >,
759 detail::negation< detail::path_traits::is_path_source< typename std::remove_cv< Source >::type > >
760 >::value,
761 path&
762 >::type append(Source const& source)
763 {
764 detail::path_traits::dispatch_convertible(source, append_op(*this));
765 return *this;
766 }
767
768 path& append(path const& p, codecvt_type const&);
769
770 template< typename Source >
771 BOOST_FORCEINLINE typename std::enable_if<
772 detail::path_traits::is_path_source< typename std::remove_cv< Source >::type >::value,
773 path&
774 >::type append(Source const& source, codecvt_type const& cvt)
775 {
776 detail::path_traits::dispatch(source, append_op(*this), &cvt);
777 return *this;
778 }
779
780 template< typename Source >
781 BOOST_FORCEINLINE typename std::enable_if<
782 detail::conjunction<
783 detail::path_traits::is_convertible_to_path_source< typename std::remove_cv< Source >::type >,
784 detail::negation< detail::path_traits::is_path_source< typename std::remove_cv< Source >::type > >
785 >::value,
786 path&
787 >::type append(Source const& source, codecvt_type const& cvt)
788 {
789 detail::path_traits::dispatch_convertible(source, append_op(*this), &cvt);
790 return *this;
791 }
792
793 path& append(const value_type* begin, const value_type* end);
794
795 template< typename InputIterator >
796 BOOST_FORCEINLINE typename std::enable_if<
797 detail::conjunction<
798 detail::path_traits::is_path_source_iterator< InputIterator >,
799 detail::negation< detail::path_traits::is_native_char_ptr< InputIterator > >
800 >::value,
801 path&
802 >::type append(InputIterator begin, InputIterator end)
803 {
804 std::basic_string< typename std::iterator_traits< InputIterator >::value_type > source(begin, end);
805 detail::path_traits::dispatch(source, append_op(*this));
806 return *this;
807 }
808
809 path& append(const value_type* begin, const value_type* end, codecvt_type const&);
810
811 template< typename InputIterator >
812 BOOST_FORCEINLINE typename std::enable_if<
813 detail::conjunction<
814 detail::path_traits::is_path_source_iterator< InputIterator >,
815 detail::negation< detail::path_traits::is_native_char_ptr< InputIterator > >
816 >::value,
817 path&
818 >::type append(InputIterator begin, InputIterator end, const codecvt_type& cvt)
819 {
820 std::basic_string< typename std::iterator_traits< InputIterator >::value_type > source(begin, end);
821 detail::path_traits::dispatch(source, append_op(*this), &cvt);
822 return *this;
823 }
824
825 // ----- modifiers -----
826
827 void clear() noexcept { m_pathname.clear(); }
828 path& make_preferred();
829 path& remove_filename();
830 BOOST_FILESYSTEM_DECL path& remove_filename_and_trailing_separators();
831 BOOST_FILESYSTEM_DECL path& remove_trailing_separator();
832 BOOST_FILESYSTEM_DECL path& replace_filename(path const& replacement);
833 path& replace_extension(path const& new_extension = path());
834
835 void swap(path& rhs) noexcept { m_pathname.swap(s&: rhs.m_pathname); }
836
837 // ----- observers -----
838
839 // For operating systems that format file paths differently than directory
840 // paths, return values from observers are formatted as file names unless there
841 // is a trailing separator, in which case returns are formatted as directory
842 // paths. POSIX and Windows make no such distinction.
843
844 // Implementations are permitted to return const values or const references.
845
846 // The string or path returned by an observer are specified as being formatted
847 // as "native" or "generic".
848 //
849 // For POSIX, these are all the same format; slashes and backslashes are as input and
850 // are not modified.
851 //
852 // For Windows, native: as input; slashes and backslashes are not modified;
853 // this is the format of the internally stored string.
854 // generic: backslashes are converted to slashes
855
856 // ----- native format observers -----
857
858 string_type const& native() const noexcept { return m_pathname; }
859 const value_type* c_str() const noexcept { return m_pathname.c_str(); }
860 string_type::size_type size() const noexcept { return m_pathname.size(); }
861
862 template< typename String >
863 String string() const;
864
865 template< typename String >
866 String string(codecvt_type const& cvt) const;
867
868#ifdef BOOST_WINDOWS_API
869 std::string string() const
870 {
871 std::string tmp;
872 if (!m_pathname.empty())
873 detail::path_traits::convert(m_pathname.data(), m_pathname.data() + m_pathname.size(), tmp);
874 return tmp;
875 }
876 std::string string(codecvt_type const& cvt) const
877 {
878 std::string tmp;
879 if (!m_pathname.empty())
880 detail::path_traits::convert(m_pathname.data(), m_pathname.data() + m_pathname.size(), tmp, &cvt);
881 return tmp;
882 }
883
884 // string_type is std::wstring, so there is no conversion
885 std::wstring const& wstring() const { return m_pathname; }
886 std::wstring const& wstring(codecvt_type const&) const { return m_pathname; }
887#else // BOOST_POSIX_API
888 // string_type is std::string, so there is no conversion
889 std::string const& string() const { return m_pathname; }
890 std::string const& string(codecvt_type const&) const { return m_pathname; }
891
892 std::wstring wstring() const
893 {
894 std::wstring tmp;
895 if (!m_pathname.empty())
896 detail::path_traits::convert(from: m_pathname.data(), from_end: m_pathname.data() + m_pathname.size(), to&: tmp);
897 return tmp;
898 }
899 std::wstring wstring(codecvt_type const& cvt) const
900 {
901 std::wstring tmp;
902 if (!m_pathname.empty())
903 detail::path_traits::convert(from: m_pathname.data(), from_end: m_pathname.data() + m_pathname.size(), to&: tmp, cvt: &cvt);
904 return tmp;
905 }
906#endif
907
908 // ----- generic format observers -----
909
910 // Experimental generic function returning generic formatted path (i.e. separators
911 // are forward slashes). Motivation: simpler than a family of generic_*string
912 // functions.
913 path generic_path() const;
914
915 template< typename String >
916 String generic_string() const;
917
918 template< typename String >
919 String generic_string(codecvt_type const& cvt) const;
920
921 std::string generic_string() const { return generic_path().string(); }
922 std::string generic_string(codecvt_type const& cvt) const { return generic_path().string(cvt); }
923 std::wstring generic_wstring() const { return generic_path().wstring(); }
924 std::wstring generic_wstring(codecvt_type const& cvt) const { return generic_path().wstring(cvt); }
925
926 // ----- compare -----
927
928 int compare(path const& p) const; // generic, lexicographical
929
930 template< typename Source >
931 BOOST_FORCEINLINE typename std::enable_if<
932 detail::path_traits::is_path_source< typename std::remove_cv< Source >::type >::value,
933 int
934 >::type compare(Source const& source) const
935 {
936 return detail::path_traits::dispatch(source, compare_op(*this));
937 }
938
939 template< typename Source >
940 BOOST_FORCEINLINE typename std::enable_if<
941 detail::conjunction<
942 detail::path_traits::is_convertible_to_path_source< typename std::remove_cv< Source >::type >,
943 detail::negation< detail::path_traits::is_path_source< typename std::remove_cv< Source >::type > >
944 >::value,
945 int
946 >::type compare(Source const& source) const
947 {
948 return detail::path_traits::dispatch_convertible(source, compare_op(*this));
949 }
950
951 template< typename Source >
952 BOOST_FORCEINLINE typename std::enable_if<
953 detail::path_traits::is_path_source< typename std::remove_cv< Source >::type >::value,
954 int
955 >::type compare(Source const& source, codecvt_type const& cvt) const
956 {
957 return detail::path_traits::dispatch(source, compare_op(*this), &cvt);
958 }
959
960 template< typename Source >
961 BOOST_FORCEINLINE typename std::enable_if<
962 detail::conjunction<
963 detail::path_traits::is_convertible_to_path_source< typename std::remove_cv< Source >::type >,
964 detail::negation< detail::path_traits::is_path_source< typename std::remove_cv< Source >::type > >
965 >::value,
966 int
967 >::type compare(Source const& source, codecvt_type const& cvt) const
968 {
969 return detail::path_traits::dispatch_convertible(source, compare_op(*this), &cvt);
970 }
971
972 // ----- decomposition -----
973
974 path root_path() const { return path(m_pathname.c_str(), m_pathname.c_str() + detail::path_algorithms::find_root_path_size(p: *this)); }
975 // returns 0 or 1 element path even on POSIX, root_name() is non-empty() for network paths
976 path root_name() const { return path(m_pathname.c_str(), m_pathname.c_str() + detail::path_algorithms::find_root_name_size(p: *this)); }
977
978 // returns 0 or 1 element path
979 path root_directory() const
980 {
981 detail::path_algorithms::substring root_dir = detail::path_algorithms::find_root_directory(p: *this);
982 const value_type* p = m_pathname.c_str() + root_dir.pos;
983 return path(p, p + root_dir.size);
984 }
985
986 path relative_path() const
987 {
988 detail::path_algorithms::substring rel_path = detail::path_algorithms::find_relative_path(p: *this);
989 const value_type* p = m_pathname.c_str() + rel_path.pos;
990 return path(p, p + rel_path.size);
991 }
992
993 path parent_path() const { return path(m_pathname.c_str(), m_pathname.c_str() + detail::path_algorithms::find_parent_path_size(p: *this)); }
994
995 path filename() const; // returns 0 or 1 element path
996 path stem() const; // returns 0 or 1 element path
997 path extension() const; // returns 0 or 1 element path
998
999 // ----- query -----
1000
1001 bool empty() const noexcept { return m_pathname.empty(); }
1002 bool filename_is_dot() const;
1003 bool filename_is_dot_dot() const;
1004 bool has_root_path() const { return detail::path_algorithms::find_root_path_size(p: *this) > 0; }
1005 bool has_root_name() const { return detail::path_algorithms::find_root_name_size(p: *this) > 0; }
1006 bool has_root_directory() const { return detail::path_algorithms::find_root_directory(p: *this).size > 0; }
1007 bool has_relative_path() const { return detail::path_algorithms::find_relative_path(p: *this).size > 0; }
1008 bool has_parent_path() const { return detail::path_algorithms::find_parent_path_size(p: *this) > 0; }
1009 bool has_filename() const;
1010 bool has_stem() const { return !stem().empty(); }
1011 bool has_extension() const { return !extension().empty(); }
1012 bool is_relative() const { return !is_absolute(); }
1013 bool is_absolute() const
1014 {
1015#if defined(BOOST_WINDOWS_API)
1016 return has_root_name() && has_root_directory();
1017#else
1018 return has_root_directory();
1019#endif
1020 }
1021
1022 // ----- lexical operations -----
1023
1024 path lexically_normal() const;
1025 BOOST_FILESYSTEM_DECL path lexically_relative(path const& base) const;
1026 path lexically_proximate(path const& base) const;
1027
1028 // ----- iterators -----
1029
1030 BOOST_FILESYSTEM_DECL iterator begin() const;
1031 BOOST_FILESYSTEM_DECL iterator end() const;
1032 reverse_iterator rbegin() const;
1033 reverse_iterator rend() const;
1034
1035 // ----- static member functions -----
1036
1037 static BOOST_FILESYSTEM_DECL std::locale imbue(std::locale const& loc);
1038 static BOOST_FILESYSTEM_DECL codecvt_type const& codecvt();
1039
1040 //--------------------------------------------------------------------------------------//
1041 // class path private members //
1042 //--------------------------------------------------------------------------------------//
1043private:
1044 /*
1045 * m_pathname has the type, encoding, and format required by the native
1046 * operating system. Thus for POSIX and Windows there is no conversion for
1047 * passing m_pathname.c_str() to the O/S API or when obtaining a path from the
1048 * O/S API. POSIX encoding is unspecified other than for dot and slash
1049 * characters; POSIX just treats paths as a sequence of bytes. Windows
1050 * encoding is UCS-2 or UTF-16 depending on the version.
1051 */
1052 string_type m_pathname; // Windows: as input; backslashes NOT converted to slashes,
1053 // slashes NOT converted to backslashes
1054};
1055
1056namespace detail {
1057BOOST_FILESYSTEM_DECL path const& dot_path();
1058BOOST_FILESYSTEM_DECL path const& dot_dot_path();
1059} // namespace detail
1060
1061namespace path_detail {
1062
1063//------------------------------------------------------------------------------------//
1064// class path::iterator //
1065//------------------------------------------------------------------------------------//
1066
1067class path_iterator :
1068 public boost::iterator_facade<
1069 path_iterator,
1070 const path,
1071 boost::bidirectional_traversal_tag
1072 >
1073{
1074private:
1075 friend class boost::iterator_core_access;
1076 friend class boost::filesystem::path;
1077 friend class path_reverse_iterator;
1078 friend struct boost::filesystem::detail::path_algorithms;
1079
1080 path const& dereference() const { return m_element; }
1081
1082 bool equal(path_iterator const& rhs) const noexcept
1083 {
1084 return m_path_ptr == rhs.m_path_ptr && m_pos == rhs.m_pos;
1085 }
1086
1087 void increment();
1088 void decrement();
1089
1090private:
1091 // current element
1092 path m_element;
1093 // path being iterated over
1094 const path* m_path_ptr;
1095 // position of m_element in m_path_ptr->m_pathname.
1096 // if m_element is implicit dot, m_pos is the
1097 // position of the last separator in the path.
1098 // end() iterator is indicated by
1099 // m_pos == m_path_ptr->m_pathname.size()
1100 path::string_type::size_type m_pos;
1101};
1102
1103//------------------------------------------------------------------------------------//
1104// class path::reverse_iterator //
1105//------------------------------------------------------------------------------------//
1106
1107class path_reverse_iterator :
1108 public boost::iterator_facade<
1109 path_reverse_iterator,
1110 const path,
1111 boost::bidirectional_traversal_tag
1112 >
1113{
1114public:
1115 explicit path_reverse_iterator(path_iterator itr) :
1116 m_itr(itr)
1117 {
1118 if (itr != itr.m_path_ptr->begin())
1119 m_element = *--itr;
1120 }
1121
1122private:
1123 friend class boost::iterator_core_access;
1124 friend class boost::filesystem::path;
1125
1126 path const& dereference() const { return m_element; }
1127 bool equal(path_reverse_iterator const& rhs) const noexcept { return m_itr == rhs.m_itr; }
1128
1129 void increment()
1130 {
1131 --m_itr;
1132 if (m_itr != m_itr.m_path_ptr->begin())
1133 {
1134 path_iterator tmp = m_itr;
1135 m_element = *--tmp;
1136 }
1137 }
1138
1139 void decrement()
1140 {
1141 m_element = *m_itr;
1142 ++m_itr;
1143 }
1144
1145private:
1146 path_iterator m_itr;
1147 path m_element;
1148};
1149
1150// std::lexicographical_compare would infinitely recurse because path iterators
1151// yield paths, so provide a path aware version
1152bool lexicographical_compare(path_iterator first1, path_iterator const& last1, path_iterator first2, path_iterator const& last2);
1153
1154} // namespace path_detail
1155
1156using path_detail::lexicographical_compare;
1157
1158//------------------------------------------------------------------------------------//
1159// //
1160// non-member functions //
1161// //
1162//------------------------------------------------------------------------------------//
1163
1164BOOST_FORCEINLINE bool operator==(path const& lhs, path const& rhs)
1165{
1166 return lhs.compare(p: rhs) == 0;
1167}
1168
1169template< typename Path, typename Source >
1170BOOST_FORCEINLINE typename std::enable_if<
1171 detail::conjunction<
1172 std::is_same< Path, path >,
1173 detail::path_traits::is_convertible_to_path_source< typename std::remove_cv< Source >::type >
1174 >::value,
1175 bool
1176>::type operator==(Path const& lhs, Source const& rhs)
1177{
1178 return lhs.compare(rhs) == 0;
1179}
1180
1181template< typename Source, typename Path >
1182BOOST_FORCEINLINE typename std::enable_if<
1183 detail::conjunction<
1184 std::is_same< Path, path >,
1185 detail::path_traits::is_convertible_to_path_source< typename std::remove_cv< Source >::type >
1186 >::value,
1187 bool
1188>::type operator==(Source const& lhs, Path const& rhs)
1189{
1190 return rhs.compare(lhs) == 0;
1191}
1192
1193BOOST_FORCEINLINE bool operator!=(path const& lhs, path const& rhs)
1194{
1195 return lhs.compare(p: rhs) != 0;
1196}
1197
1198template< typename Path, typename Source >
1199BOOST_FORCEINLINE typename std::enable_if<
1200 detail::conjunction<
1201 std::is_same< Path, path >,
1202 detail::path_traits::is_convertible_to_path_source< typename std::remove_cv< Source >::type >
1203 >::value,
1204 bool
1205>::type operator!=(Path const& lhs, Source const& rhs)
1206{
1207 return lhs.compare(rhs) != 0;
1208}
1209
1210template< typename Source, typename Path >
1211BOOST_FORCEINLINE typename std::enable_if<
1212 detail::conjunction<
1213 std::is_same< Path, path >,
1214 detail::path_traits::is_convertible_to_path_source< typename std::remove_cv< Source >::type >
1215 >::value,
1216 bool
1217>::type operator!=(Source const& lhs, Path const& rhs)
1218{
1219 return rhs.compare(lhs) != 0;
1220}
1221
1222BOOST_FORCEINLINE bool operator<(path const& lhs, path const& rhs)
1223{
1224 return lhs.compare(p: rhs) < 0;
1225}
1226
1227template< typename Path, typename Source >
1228BOOST_FORCEINLINE typename std::enable_if<
1229 detail::conjunction<
1230 std::is_same< Path, path >,
1231 detail::path_traits::is_convertible_to_path_source< typename std::remove_cv< Source >::type >
1232 >::value,
1233 bool
1234>::type operator<(Path const& lhs, Source const& rhs)
1235{
1236 return lhs.compare(rhs) < 0;
1237}
1238
1239template< typename Source, typename Path >
1240BOOST_FORCEINLINE typename std::enable_if<
1241 detail::conjunction<
1242 std::is_same< Path, path >,
1243 detail::path_traits::is_convertible_to_path_source< typename std::remove_cv< Source >::type >
1244 >::value,
1245 bool
1246>::type operator<(Source const& lhs, Path const& rhs)
1247{
1248 return rhs.compare(lhs) > 0;
1249}
1250
1251BOOST_FORCEINLINE bool operator<=(path const& lhs, path const& rhs)
1252{
1253 return lhs.compare(p: rhs) <= 0;
1254}
1255
1256template< typename Path, typename Source >
1257BOOST_FORCEINLINE typename std::enable_if<
1258 detail::conjunction<
1259 std::is_same< Path, path >,
1260 detail::path_traits::is_convertible_to_path_source< typename std::remove_cv< Source >::type >
1261 >::value,
1262 bool
1263>::type operator<=(Path const& lhs, Source const& rhs)
1264{
1265 return lhs.compare(rhs) <= 0;
1266}
1267
1268template< typename Source, typename Path >
1269BOOST_FORCEINLINE typename std::enable_if<
1270 detail::conjunction<
1271 std::is_same< Path, path >,
1272 detail::path_traits::is_convertible_to_path_source< typename std::remove_cv< Source >::type >
1273 >::value,
1274 bool
1275>::type operator<=(Source const& lhs, Path const& rhs)
1276{
1277 return rhs.compare(lhs) >= 0;
1278}
1279
1280BOOST_FORCEINLINE bool operator>(path const& lhs, path const& rhs)
1281{
1282 return lhs.compare(p: rhs) > 0;
1283}
1284
1285template< typename Path, typename Source >
1286BOOST_FORCEINLINE typename std::enable_if<
1287 detail::conjunction<
1288 std::is_same< Path, path >,
1289 detail::path_traits::is_convertible_to_path_source< typename std::remove_cv< Source >::type >
1290 >::value,
1291 bool
1292>::type operator>(Path const& lhs, Source const& rhs)
1293{
1294 return lhs.compare(rhs) > 0;
1295}
1296
1297template< typename Source, typename Path >
1298BOOST_FORCEINLINE typename std::enable_if<
1299 detail::conjunction<
1300 std::is_same< Path, path >,
1301 detail::path_traits::is_convertible_to_path_source< typename std::remove_cv< Source >::type >
1302 >::value,
1303 bool
1304>::type operator>(Source const& lhs, Path const& rhs)
1305{
1306 return rhs.compare(lhs) < 0;
1307}
1308
1309BOOST_FORCEINLINE bool operator>=(path const& lhs, path const& rhs)
1310{
1311 return lhs.compare(p: rhs) >= 0;
1312}
1313
1314template< typename Path, typename Source >
1315BOOST_FORCEINLINE typename std::enable_if<
1316 detail::conjunction<
1317 std::is_same< Path, path >,
1318 detail::path_traits::is_convertible_to_path_source< typename std::remove_cv< Source >::type >
1319 >::value,
1320 bool
1321>::type operator>=(Path const& lhs, Source const& rhs)
1322{
1323 return lhs.compare(rhs) >= 0;
1324}
1325
1326template< typename Source, typename Path >
1327BOOST_FORCEINLINE typename std::enable_if<
1328 detail::conjunction<
1329 std::is_same< Path, path >,
1330 detail::path_traits::is_convertible_to_path_source< typename std::remove_cv< Source >::type >
1331 >::value,
1332 bool
1333>::type operator>=(Source const& lhs, Path const& rhs)
1334{
1335 return rhs.compare(lhs) <= 0;
1336}
1337
1338
1339// Note: Declared as a template to delay binding to Boost.ContainerHash functions and make the dependency optional
1340template< typename Path >
1341inline typename std::enable_if<
1342 std::is_same< Path, path >::value,
1343 std::size_t
1344>::type hash_value(Path const& p) noexcept
1345{
1346#ifdef BOOST_WINDOWS_API
1347 std::size_t seed = 0u;
1348 for (typename Path::value_type const* it = p.c_str(); *it; ++it)
1349 hash_combine(seed, *it == L'/' ? L'\\' : *it);
1350 return seed;
1351#else // BOOST_POSIX_API
1352 return hash_range(p.native().begin(), p.native().end());
1353#endif
1354}
1355
1356inline void swap(path& lhs, path& rhs) noexcept
1357{
1358 lhs.swap(rhs);
1359}
1360
1361BOOST_FORCEINLINE path operator/(path lhs, path const& rhs)
1362{
1363 lhs.append(p: rhs);
1364 return lhs;
1365}
1366
1367template< typename Source >
1368BOOST_FORCEINLINE typename std::enable_if<
1369 detail::path_traits::is_convertible_to_path_source< typename std::remove_cv< Source >::type >::value,
1370 path
1371>::type operator/(path lhs, Source const& rhs)
1372{
1373 lhs.append(rhs);
1374 return lhs;
1375}
1376
1377// inserters and extractors
1378// use boost::io::quoted() to handle spaces in paths
1379// use '&' as escape character to ease use for Windows paths
1380
1381template< typename Char, typename Traits >
1382inline std::basic_ostream< Char, Traits >&
1383operator<<(std::basic_ostream< Char, Traits >& os, path const& p)
1384{
1385 return os << boost::io::quoted(p.template string< std::basic_string< Char > >(), static_cast< Char >('&'));
1386}
1387
1388template< typename Char, typename Traits >
1389inline std::basic_istream< Char, Traits >&
1390operator>>(std::basic_istream< Char, Traits >& is, path& p)
1391{
1392 std::basic_string< Char > str;
1393 is >> boost::io::quoted(str, static_cast< Char >('&'));
1394 p = str;
1395 return is;
1396}
1397
1398// name_checks
1399
1400// These functions are holdovers from version 1. It isn't clear they have much
1401// usefulness, or how to generalize them for later versions.
1402
1403BOOST_FILESYSTEM_DECL bool portable_posix_name(std::string const& name);
1404BOOST_FILESYSTEM_DECL bool windows_name(std::string const& name);
1405BOOST_FILESYSTEM_DECL bool portable_name(std::string const& name);
1406BOOST_FILESYSTEM_DECL bool portable_directory_name(std::string const& name);
1407BOOST_FILESYSTEM_DECL bool portable_file_name(std::string const& name);
1408BOOST_FILESYSTEM_DECL bool native(std::string const& name);
1409
1410namespace detail {
1411
1412// For POSIX, is_directory_separator() and is_element_separator() are identical since
1413// a forward slash is the only valid directory separator and also the only valid
1414// element separator. For Windows, forward slash and back slash are the possible
1415// directory separators, but colon (example: "c:foo") is also an element separator.
1416inline bool is_directory_separator(path::value_type c) noexcept
1417{
1418 return c == path::separator
1419#ifdef BOOST_WINDOWS_API
1420 || c == path::preferred_separator
1421#endif
1422 ;
1423}
1424
1425inline bool is_element_separator(path::value_type c) noexcept
1426{
1427 return c == path::separator
1428#ifdef BOOST_WINDOWS_API
1429 || c == path::preferred_separator || c == L':'
1430#endif
1431 ;
1432}
1433
1434} // namespace detail
1435
1436//------------------------------------------------------------------------------------//
1437// class path miscellaneous function implementations //
1438//------------------------------------------------------------------------------------//
1439
1440namespace detail {
1441
1442inline bool path_algorithms::has_filename_v3(path const& p)
1443{
1444 return !p.m_pathname.empty();
1445}
1446
1447inline bool path_algorithms::has_filename_v4(path const& p)
1448{
1449 return path_algorithms::find_filename_v4_size(p) > 0;
1450}
1451
1452inline path path_algorithms::filename_v4(path const& p)
1453{
1454 string_type::size_type filename_size = path_algorithms::find_filename_v4_size(p);
1455 string_type::size_type pos = p.m_pathname.size() - filename_size;
1456 const value_type* ptr = p.m_pathname.c_str() + pos;
1457 return path(ptr, ptr + filename_size);
1458}
1459
1460inline path path_algorithms::extension_v4(path const& p)
1461{
1462 string_type::size_type extension_size = path_algorithms::find_extension_v4_size(p);
1463 string_type::size_type pos = p.m_pathname.size() - extension_size;
1464 const value_type* ptr = p.m_pathname.c_str() + pos;
1465 return path(ptr, ptr + extension_size);
1466}
1467
1468inline void path_algorithms::append_v4(path& left, path const& right)
1469{
1470 path_algorithms::append_v4(p&: left, b: right.m_pathname.c_str(), e: right.m_pathname.c_str() + right.m_pathname.size());
1471}
1472
1473} // namespace detail
1474
1475// Note: Because of the range constructor in C++23 std::string_view that involves a check for contiguous_range concept,
1476// any non-template function call that requires a check whether the source argument (which may be fs::path)
1477// is convertible to std::string_view must be made after fs::path::iterator is defined. This includes overload
1478// resolution and SFINAE checks. Otherwise, the concept check result formally changes between fs::path::iterator
1479// is not defined and defined, which causes compilation errors with gcc 11 and later.
1480// https://gcc.gnu.org/bugzilla/show_bug.cgi?id=106808
1481
1482BOOST_FORCEINLINE path::compare_op::result_type path::compare_op::operator() (const value_type* source, const value_type* source_end, const codecvt_type*) const
1483{
1484 path src;
1485 src.m_pathname.assign(first: source, last: source_end);
1486 return m_self.compare(p: src);
1487}
1488
1489template< typename OtherChar >
1490BOOST_FORCEINLINE path::compare_op::result_type path::compare_op::operator() (const OtherChar* source, const OtherChar* source_end, const codecvt_type* cvt) const
1491{
1492 path src;
1493 detail::path_traits::convert(source, source_end, src.m_pathname, cvt);
1494 return m_self.compare(p: src);
1495}
1496
1497inline path& path::operator=(path const& p)
1498{
1499 return assign(p);
1500}
1501
1502inline path& path::operator+=(path const& p)
1503{
1504 return concat(p);
1505}
1506
1507BOOST_FORCEINLINE path& path::operator/=(path const& p)
1508{
1509 return append(p);
1510}
1511
1512inline path path::lexically_proximate(path const& base) const
1513{
1514 path tmp(lexically_relative(base));
1515 return tmp.empty() ? *this : tmp;
1516}
1517
1518inline path::reverse_iterator path::rbegin() const
1519{
1520 return reverse_iterator(end());
1521}
1522
1523inline path::reverse_iterator path::rend() const
1524{
1525 return reverse_iterator(begin());
1526}
1527
1528inline bool path::filename_is_dot() const
1529{
1530 // implicit dot is tricky, so actually call filename(); see path::filename() example
1531 // in reference.html
1532 path p(filename());
1533 return p.size() == 1 && *p.c_str() == dot;
1534}
1535
1536inline bool path::filename_is_dot_dot() const
1537{
1538 return size() >= 2 && m_pathname[size() - 1] == dot && m_pathname[size() - 2] == dot && (m_pathname.size() == 2 || detail::is_element_separator(c: m_pathname[size() - 3]));
1539 // use detail::is_element_separator() rather than detail::is_directory_separator
1540 // to deal with "c:.." edge case on Windows when ':' acts as a separator
1541}
1542
1543// The following functions are defined differently, depending on Boost.Filesystem version in use.
1544// To avoid ODR violation, these functions are not defined when the library itself is built.
1545// This makes sure they are not compiled when the library is built, and the only version there is
1546// is the one in user's code. Users are supposed to consistently use the same Boost.Filesystem version
1547// in all their translation units.
1548#if !defined(BOOST_FILESYSTEM_SOURCE)
1549
1550BOOST_FORCEINLINE path& path::append(path const& p)
1551{
1552 BOOST_FILESYSTEM_VERSIONED_SYM(detail::path_algorithms::append)(p&: *this, b: p.m_pathname.data(), e: p.m_pathname.data() + p.m_pathname.size());
1553 return *this;
1554}
1555
1556BOOST_FORCEINLINE path& path::append(path const& p, codecvt_type const&)
1557{
1558 BOOST_FILESYSTEM_VERSIONED_SYM(detail::path_algorithms::append)(p&: *this, b: p.m_pathname.data(), e: p.m_pathname.data() + p.m_pathname.size());
1559 return *this;
1560}
1561
1562BOOST_FORCEINLINE path& path::append(const value_type* begin, const value_type* end)
1563{
1564 BOOST_FILESYSTEM_VERSIONED_SYM(detail::path_algorithms::append)(p&: *this, b: begin, e: end);
1565 return *this;
1566}
1567
1568BOOST_FORCEINLINE path& path::append(const value_type* begin, const value_type* end, codecvt_type const&)
1569{
1570 BOOST_FILESYSTEM_VERSIONED_SYM(detail::path_algorithms::append)(p&: *this, b: begin, e: end);
1571 return *this;
1572}
1573
1574BOOST_FORCEINLINE path& path::remove_filename()
1575{
1576 BOOST_FILESYSTEM_VERSIONED_SYM(detail::path_algorithms::remove_filename)(p&: *this);
1577 return *this;
1578}
1579
1580BOOST_FORCEINLINE path& path::replace_extension(path const& new_extension)
1581{
1582 BOOST_FILESYSTEM_VERSIONED_SYM(detail::path_algorithms::replace_extension)(p&: *this, new_extension);
1583 return *this;
1584}
1585
1586BOOST_FORCEINLINE int path::compare(path const& p) const
1587{
1588 return BOOST_FILESYSTEM_VERSIONED_SYM(detail::path_algorithms::compare)(left: *this, right: p);
1589}
1590
1591BOOST_FORCEINLINE path path::filename() const
1592{
1593 return BOOST_FILESYSTEM_VERSIONED_SYM(detail::path_algorithms::filename)(p: *this);
1594}
1595
1596BOOST_FORCEINLINE path path::stem() const
1597{
1598 return BOOST_FILESYSTEM_VERSIONED_SYM(detail::path_algorithms::stem)(p: *this);
1599}
1600
1601BOOST_FORCEINLINE path path::extension() const
1602{
1603 return BOOST_FILESYSTEM_VERSIONED_SYM(detail::path_algorithms::extension)(p: *this);
1604}
1605
1606BOOST_FORCEINLINE bool path::has_filename() const
1607{
1608 return BOOST_FILESYSTEM_VERSIONED_SYM(detail::path_algorithms::has_filename)(p: *this);
1609}
1610
1611BOOST_FORCEINLINE path path::lexically_normal() const
1612{
1613 return BOOST_FILESYSTEM_VERSIONED_SYM(detail::path_algorithms::lexically_normal)(p: *this);
1614}
1615
1616BOOST_FORCEINLINE path path::generic_path() const
1617{
1618 return BOOST_FILESYSTEM_VERSIONED_SYM(detail::path_algorithms::generic_path)(p: *this);
1619}
1620
1621BOOST_FORCEINLINE path& path::make_preferred()
1622{
1623 // No effect on POSIX
1624#if defined(BOOST_WINDOWS_API)
1625 BOOST_FILESYSTEM_VERSIONED_SYM(detail::path_algorithms::make_preferred)(*this);
1626#endif
1627 return *this;
1628}
1629
1630namespace path_detail {
1631
1632BOOST_FORCEINLINE void path_iterator::increment()
1633{
1634 BOOST_FILESYSTEM_VERSIONED_SYM(detail::path_algorithms::increment)(it&: *this);
1635}
1636
1637BOOST_FORCEINLINE void path_iterator::decrement()
1638{
1639 BOOST_FILESYSTEM_VERSIONED_SYM(detail::path_algorithms::decrement)(it&: *this);
1640}
1641
1642BOOST_FORCEINLINE bool lexicographical_compare(path_iterator first1, path_iterator const& last1, path_iterator first2, path_iterator const& last2)
1643{
1644 return BOOST_FILESYSTEM_VERSIONED_SYM(detail::path_algorithms::lex_compare)(first1, last1, first2, last2) < 0;
1645}
1646
1647} // namespace path_detail
1648
1649#endif // !defined(BOOST_FILESYSTEM_SOURCE)
1650
1651//--------------------------------------------------------------------------------------//
1652// class path member template specializations //
1653//--------------------------------------------------------------------------------------//
1654
1655template< >
1656inline std::string path::string< std::string >() const
1657{
1658 return string();
1659}
1660
1661template< >
1662inline std::wstring path::string< std::wstring >() const
1663{
1664 return wstring();
1665}
1666
1667template< >
1668inline std::string path::string< std::string >(codecvt_type const& cvt) const
1669{
1670 return string(cvt);
1671}
1672
1673template< >
1674inline std::wstring path::string< std::wstring >(codecvt_type const& cvt) const
1675{
1676 return wstring(cvt);
1677}
1678
1679template< >
1680inline std::string path::generic_string< std::string >() const
1681{
1682 return generic_string();
1683}
1684
1685template< >
1686inline std::wstring path::generic_string< std::wstring >() const
1687{
1688 return generic_wstring();
1689}
1690
1691template< >
1692inline std::string path::generic_string< std::string >(codecvt_type const& cvt) const
1693{
1694 return generic_string(cvt);
1695}
1696
1697template< >
1698inline std::wstring path::generic_string< std::wstring >(codecvt_type const& cvt) const
1699{
1700 return generic_wstring(cvt);
1701}
1702
1703} // namespace filesystem
1704} // namespace boost
1705
1706//----------------------------------------------------------------------------//
1707
1708#include <boost/filesystem/detail/footer.hpp>
1709
1710#endif // BOOST_FILESYSTEM_PATH_HPP
1711

source code of boost/libs/filesystem/include/boost/filesystem/path.hpp