Line data Source code
1 : // filesystem path.hpp ---------------------------------------------------------------//
2 :
3 : // Copyright Beman Dawes 2002-2005, 2009
4 : // Copyright Vladimir Prus 2002
5 :
6 : // Distributed under the Boost Software License, Version 1.0.
7 : // See http://www.boost.org/LICENSE_1_0.txt
8 :
9 : // Library home page: http://www.boost.org/libs/filesystem
10 :
11 : // path::stem(), extension(), and replace_extension() are based on
12 : // basename(), extension(), and change_extension() from the original
13 : // filesystem/convenience.hpp header by Vladimir Prus.
14 :
15 : #ifndef BOOST_FILESYSTEM_PATH_HPP
16 : #define BOOST_FILESYSTEM_PATH_HPP
17 :
18 : #include <boost/config.hpp>
19 :
20 : # if defined( BOOST_NO_STD_WSTRING )
21 : # error Configuration not supported: Boost.Filesystem V3 and later requires std::wstring support
22 : # endif
23 :
24 : #include <boost/assert.hpp>
25 : #include <boost/filesystem/config.hpp>
26 : #include <boost/filesystem/path_traits.hpp> // includes <cwchar>
27 : #include <boost/system/error_code.hpp>
28 : #include <boost/system/system_error.hpp>
29 : #include <boost/iterator/iterator_facade.hpp>
30 : #include <boost/core/enable_if.hpp>
31 : #include <boost/io/detail/quoted_manip.hpp>
32 : #include <boost/functional/hash_fwd.hpp>
33 : #include <boost/type_traits/is_integral.hpp>
34 : #include <string>
35 : #include <iterator>
36 : #include <cstring>
37 : #include <iosfwd>
38 : #include <stdexcept>
39 : #include <cassert>
40 : #include <locale>
41 : #include <algorithm>
42 :
43 : #include <boost/config/abi_prefix.hpp> // must be the last #include
44 :
45 : namespace boost
46 : {
47 : namespace filesystem
48 : {
49 : namespace path_detail // intentionally don't use filesystem::detail to not bring internal Boost.Filesystem functions into ADL via path_constants
50 : {
51 :
52 : template< typename Char, Char Separator, Char PreferredSeparator, Char Dot >
53 : struct path_constants
54 : {
55 : typedef path_constants< Char, Separator, PreferredSeparator, Dot > path_constants_base;
56 : typedef Char value_type;
57 : static BOOST_CONSTEXPR_OR_CONST value_type separator = Separator;
58 : static BOOST_CONSTEXPR_OR_CONST value_type preferred_separator = PreferredSeparator;
59 : static BOOST_CONSTEXPR_OR_CONST value_type dot = Dot;
60 : };
61 :
62 : template< typename Char, Char Separator, Char PreferredSeparator, Char Dot >
63 : BOOST_CONSTEXPR_OR_CONST typename path_constants< Char, Separator, PreferredSeparator, Dot >::value_type
64 : path_constants< Char, Separator, PreferredSeparator, Dot >::separator;
65 : template< typename Char, Char Separator, Char PreferredSeparator, Char Dot >
66 : BOOST_CONSTEXPR_OR_CONST typename path_constants< Char, Separator, PreferredSeparator, Dot >::value_type
67 : path_constants< Char, Separator, PreferredSeparator, Dot >::preferred_separator;
68 : template< typename Char, Char Separator, Char PreferredSeparator, Char Dot >
69 : BOOST_CONSTEXPR_OR_CONST typename path_constants< Char, Separator, PreferredSeparator, Dot >::value_type
70 : path_constants< Char, Separator, PreferredSeparator, Dot >::dot;
71 :
72 : } // namespace path_detail
73 :
74 : //------------------------------------------------------------------------------------//
75 : // //
76 : // class path //
77 : // //
78 : //------------------------------------------------------------------------------------//
79 :
80 1756 : class path :
81 : public filesystem::path_detail::path_constants<
82 : #ifdef BOOST_WINDOWS_API
83 : wchar_t, L'/', L'\\', L'.'
84 : #else
85 : char, '/', '/', '.'
86 : #endif
87 : >
88 : {
89 : public:
90 :
91 : // value_type is the character type used by the operating system API to
92 : // represent paths.
93 :
94 : typedef path_constants_base::value_type value_type;
95 : typedef std::basic_string<value_type> string_type;
96 : typedef std::codecvt<wchar_t, char,
97 : std::mbstate_t> codecvt_type;
98 :
99 :
100 : // ----- character encoding conversions -----
101 :
102 : // Following the principle of least astonishment, path input arguments
103 : // passed to or obtained from the operating system via objects of
104 : // class path behave as if they were directly passed to or
105 : // obtained from the O/S API, unless conversion is explicitly requested.
106 : //
107 : // POSIX specfies that path strings are passed unchanged to and from the
108 : // API. Note that this is different from the POSIX command line utilities,
109 : // which convert according to a locale.
110 : //
111 : // Thus for POSIX, char strings do not undergo conversion. wchar_t strings
112 : // are converted to/from char using the path locale or, if a conversion
113 : // argument is given, using a conversion object modeled on
114 : // std::wstring_convert.
115 : //
116 : // The path locale, which is global to the thread, can be changed by the
117 : // imbue() function. It is initialized to an implementation defined locale.
118 : //
119 : // For Windows, wchar_t strings do not undergo conversion. char strings
120 : // are converted using the "ANSI" or "OEM" code pages, as determined by
121 : // the AreFileApisANSI() function, or, if a conversion argument is given,
122 : // using a conversion object modeled on std::wstring_convert.
123 : //
124 : // See m_pathname comments for further important rationale.
125 :
126 : // TODO: rules needed for operating systems that use / or .
127 : // differently, or format directory paths differently from file paths.
128 : //
129 : // **********************************************************************************
130 : //
131 : // More work needed: How to handle an operating system that may have
132 : // slash characters or dot characters in valid filenames, either because
133 : // it doesn't follow the POSIX standard, or because it allows MBCS
134 : // filename encodings that may contain slash or dot characters. For
135 : // example, ISO/IEC 2022 (JIS) encoding which allows switching to
136 : // JIS x0208-1983 encoding. A valid filename in this set of encodings is
137 : // 0x1B 0x24 0x42 [switch to X0208-1983] 0x24 0x2F [U+304F Kiragana letter KU]
138 : // ^^^^
139 : // Note that 0x2F is the ASCII slash character
140 : //
141 : // **********************************************************************************
142 :
143 : // Supported source arguments: half-open iterator range, container, c-array,
144 : // and single pointer to null terminated string.
145 :
146 : // All source arguments except pointers to null terminated byte strings support
147 : // multi-byte character strings which may have embedded nulls. Embedded null
148 : // support is required for some Asian languages on Windows.
149 :
150 : // "const codecvt_type& cvt=codecvt()" default arguments are not used because this
151 : // limits the impact of locale("") initialization failures on POSIX systems to programs
152 : // that actually depend on locale(""). It further ensures that exceptions thrown
153 : // as a result of such failues occur after main() has started, so can be caught.
154 :
155 : // ----- constructors -----
156 :
157 0 : path() BOOST_NOEXCEPT {}
158 0 : path(const path& p) : m_pathname(p.m_pathname) {}
159 :
160 : template <class Source>
161 : path(Source const& source,
162 : typename boost::enable_if<path_traits::is_pathable<
163 : typename boost::decay<Source>::type> >::type* =0)
164 : {
165 : path_traits::dispatch(source, m_pathname);
166 : }
167 :
168 281 : path(const value_type* s) : m_pathname(s) {}
169 : path(value_type* s) : m_pathname(s) {}
170 0 : path(const string_type& s) : m_pathname(s) {}
171 637 : path(string_type& s) : m_pathname(s) {}
172 :
173 : // As of October 2015 the interaction between noexcept and =default is so troublesome
174 : // for VC++, GCC, and probably other compilers, that =default is not used with noexcept
175 : // functions. GCC is not even consistent for the same release on different platforms.
176 :
177 : # if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
178 0 : path(path&& p) BOOST_NOEXCEPT : m_pathname(std::move(p.m_pathname)) {}
179 0 : path& operator=(path&& p) BOOST_NOEXCEPT
180 0 : { m_pathname = std::move(p.m_pathname); return *this; }
181 : # endif
182 :
183 : template <class Source>
184 : path(Source const& source, const codecvt_type& cvt)
185 : {
186 : path_traits::dispatch(source, m_pathname, cvt);
187 : }
188 :
189 : template <class InputIterator>
190 : path(InputIterator begin, InputIterator end)
191 : {
192 : if (begin != end)
193 : {
194 : // convert requires contiguous string, so copy
195 : std::basic_string<typename std::iterator_traits<InputIterator>::value_type>
196 : seq(begin, end);
197 : path_traits::convert(seq.c_str(), seq.c_str()+seq.size(), m_pathname);
198 : }
199 : }
200 :
201 : template <class InputIterator>
202 : path(InputIterator begin, InputIterator end, const codecvt_type& cvt)
203 : {
204 : if (begin != end)
205 : {
206 : // convert requires contiguous string, so copy
207 : std::basic_string<typename std::iterator_traits<InputIterator>::value_type>
208 : seq(begin, end);
209 : path_traits::convert(seq.c_str(), seq.c_str()+seq.size(), m_pathname, cvt);
210 : }
211 : }
212 :
213 : // ----- assignments -----
214 :
215 0 : path& operator=(const path& p)
216 : {
217 0 : m_pathname = p.m_pathname;
218 0 : return *this;
219 : }
220 :
221 : template <class Source>
222 : typename boost::enable_if<path_traits::is_pathable<
223 : typename boost::decay<Source>::type>, path&>::type
224 : operator=(Source const& source)
225 : {
226 : m_pathname.clear();
227 : path_traits::dispatch(source, m_pathname);
228 : return *this;
229 : }
230 :
231 : // value_type overloads
232 :
233 : path& operator=(const value_type* ptr) // required in case ptr overlaps *this
234 : {m_pathname = ptr; return *this;}
235 : path& operator=(value_type* ptr) // required in case ptr overlaps *this
236 : {m_pathname = ptr; return *this;}
237 : path& operator=(const string_type& s) {m_pathname = s; return *this;}
238 : path& operator=(string_type& s) {m_pathname = s; return *this;}
239 :
240 : path& assign(const value_type* ptr, const codecvt_type&) // required in case ptr overlaps *this
241 : {m_pathname = ptr; return *this;}
242 : template <class Source>
243 : path& assign(Source const& source, const codecvt_type& cvt)
244 : {
245 : m_pathname.clear();
246 : path_traits::dispatch(source, m_pathname, cvt);
247 : return *this;
248 : }
249 :
250 : template <class InputIterator>
251 : path& assign(InputIterator begin, InputIterator end)
252 : {
253 : m_pathname.clear();
254 : if (begin != end)
255 : {
256 : std::basic_string<typename std::iterator_traits<InputIterator>::value_type>
257 : seq(begin, end);
258 : path_traits::convert(seq.c_str(), seq.c_str()+seq.size(), m_pathname);
259 : }
260 : return *this;
261 : }
262 :
263 : template <class InputIterator>
264 : path& assign(InputIterator begin, InputIterator end, const codecvt_type& cvt)
265 : {
266 : m_pathname.clear();
267 : if (begin != end)
268 : {
269 : std::basic_string<typename std::iterator_traits<InputIterator>::value_type>
270 : seq(begin, end);
271 : path_traits::convert(seq.c_str(), seq.c_str()+seq.size(), m_pathname, cvt);
272 : }
273 : return *this;
274 : }
275 :
276 : // ----- concatenation -----
277 :
278 : template <class Source>
279 : typename boost::enable_if<path_traits::is_pathable<
280 : typename boost::decay<Source>::type>, path&>::type
281 : operator+=(Source const& source)
282 : {
283 : return concat(source);
284 : }
285 :
286 : // value_type overloads. Same rationale as for constructors above
287 : path& operator+=(const path& p) { m_pathname += p.m_pathname; return *this; }
288 : path& operator+=(const value_type* ptr) { m_pathname += ptr; return *this; }
289 : path& operator+=(value_type* ptr) { m_pathname += ptr; return *this; }
290 : path& operator+=(const string_type& s) { m_pathname += s; return *this; }
291 : path& operator+=(string_type& s) { m_pathname += s; return *this; }
292 : path& operator+=(value_type c) { m_pathname += c; return *this; }
293 :
294 : template <class CharT>
295 : typename boost::enable_if<boost::is_integral<CharT>, path&>::type
296 : operator+=(CharT c)
297 : {
298 : CharT tmp[2];
299 : tmp[0] = c;
300 : tmp[1] = 0;
301 : return concat(tmp);
302 : }
303 :
304 : template <class Source>
305 : path& concat(Source const& source)
306 : {
307 : path_traits::dispatch(source, m_pathname);
308 : return *this;
309 : }
310 :
311 : template <class Source>
312 : path& concat(Source const& source, const codecvt_type& cvt)
313 : {
314 : path_traits::dispatch(source, m_pathname, cvt);
315 : return *this;
316 : }
317 :
318 : template <class InputIterator>
319 : path& concat(InputIterator begin, InputIterator end)
320 : {
321 : if (begin == end)
322 : return *this;
323 : std::basic_string<typename std::iterator_traits<InputIterator>::value_type>
324 : seq(begin, end);
325 : path_traits::convert(seq.c_str(), seq.c_str()+seq.size(), m_pathname);
326 : return *this;
327 : }
328 :
329 : template <class InputIterator>
330 : path& concat(InputIterator begin, InputIterator end, const codecvt_type& cvt)
331 : {
332 : if (begin == end)
333 : return *this;
334 : std::basic_string<typename std::iterator_traits<InputIterator>::value_type>
335 : seq(begin, end);
336 : path_traits::convert(seq.c_str(), seq.c_str()+seq.size(), m_pathname, cvt);
337 : return *this;
338 : }
339 :
340 : // ----- appends -----
341 :
342 : // if a separator is added, it is the preferred separator for the platform;
343 : // slash for POSIX, backslash for Windows
344 :
345 : BOOST_FILESYSTEM_DECL path& operator/=(const path& p);
346 :
347 : template <class Source>
348 : typename boost::enable_if<path_traits::is_pathable<
349 : typename boost::decay<Source>::type>, path&>::type
350 : operator/=(Source const& source)
351 : {
352 : return append(source);
353 : }
354 :
355 : BOOST_FILESYSTEM_DECL path& operator/=(const value_type* ptr);
356 : path& operator/=(value_type* ptr)
357 : {
358 : return this->operator/=(const_cast<const value_type*>(ptr));
359 : }
360 : path& operator/=(const string_type& s) { return this->operator/=(path(s)); }
361 : path& operator/=(string_type& s) { return this->operator/=(path(s)); }
362 :
363 : path& append(const value_type* ptr) // required in case ptr overlaps *this
364 : {
365 : this->operator/=(ptr);
366 : return *this;
367 : }
368 :
369 : path& append(const value_type* ptr, const codecvt_type&) // required in case ptr overlaps *this
370 : {
371 : this->operator/=(ptr);
372 : return *this;
373 : }
374 :
375 : template <class Source>
376 : path& append(Source const& source);
377 :
378 : template <class Source>
379 : path& append(Source const& source, const codecvt_type& cvt);
380 :
381 : template <class InputIterator>
382 : path& append(InputIterator begin, InputIterator end);
383 :
384 : template <class InputIterator>
385 : path& append(InputIterator begin, InputIterator end, const codecvt_type& cvt);
386 :
387 : // ----- modifiers -----
388 :
389 0 : void clear() BOOST_NOEXCEPT { m_pathname.clear(); }
390 : # ifdef BOOST_POSIX_API
391 0 : path& make_preferred() { return *this; } // POSIX no effect
392 : # else // BOOST_WINDOWS_API
393 : BOOST_FILESYSTEM_DECL path& make_preferred(); // change slashes to backslashes
394 : # endif
395 : BOOST_FILESYSTEM_DECL path& remove_filename();
396 : BOOST_FILESYSTEM_DECL path& remove_trailing_separator();
397 : BOOST_FILESYSTEM_DECL path& replace_extension(const path& new_extension = path());
398 0 : void swap(path& rhs) BOOST_NOEXCEPT { m_pathname.swap(rhs.m_pathname); }
399 :
400 : // ----- observers -----
401 :
402 : // For operating systems that format file paths differently than directory
403 : // paths, return values from observers are formatted as file names unless there
404 : // is a trailing separator, in which case returns are formatted as directory
405 : // paths. POSIX and Windows make no such distinction.
406 :
407 : // Implementations are permitted to return const values or const references.
408 :
409 : // The string or path returned by an observer are specified as being formatted
410 : // as "native" or "generic".
411 : //
412 : // For POSIX, these are all the same format; slashes and backslashes are as input and
413 : // are not modified.
414 : //
415 : // For Windows, native: as input; slashes and backslashes are not modified;
416 : // this is the format of the internally stored string.
417 : // generic: backslashes are converted to slashes
418 :
419 : // ----- native format observers -----
420 :
421 : const string_type& native() const BOOST_NOEXCEPT { return m_pathname; }
422 0 : const value_type* c_str() const BOOST_NOEXCEPT { return m_pathname.c_str(); }
423 : string_type::size_type size() const BOOST_NOEXCEPT { return m_pathname.size(); }
424 :
425 : template <class String>
426 : String string() const;
427 :
428 : template <class String>
429 : String string(const codecvt_type& cvt) const;
430 :
431 : # ifdef BOOST_WINDOWS_API
432 : const std::string string() const
433 : {
434 : std::string tmp;
435 : if (!m_pathname.empty())
436 : path_traits::convert(m_pathname.c_str(), m_pathname.c_str()+m_pathname.size(),
437 : tmp);
438 : return tmp;
439 : }
440 : const std::string string(const codecvt_type& cvt) const
441 : {
442 : std::string tmp;
443 : if (!m_pathname.empty())
444 : path_traits::convert(m_pathname.c_str(), m_pathname.c_str()+m_pathname.size(),
445 : tmp, cvt);
446 : return tmp;
447 : }
448 :
449 : // string_type is std::wstring, so there is no conversion
450 : const std::wstring& wstring() const { return m_pathname; }
451 : const std::wstring& wstring(const codecvt_type&) const { return m_pathname; }
452 : # else // BOOST_POSIX_API
453 : // string_type is std::string, so there is no conversion
454 0 : const std::string& string() const { return m_pathname; }
455 : const std::string& string(const codecvt_type&) const { return m_pathname; }
456 :
457 : const std::wstring wstring() const
458 : {
459 : std::wstring tmp;
460 : if (!m_pathname.empty())
461 : path_traits::convert(m_pathname.c_str(), m_pathname.c_str()+m_pathname.size(),
462 : tmp);
463 : return tmp;
464 : }
465 : const std::wstring wstring(const codecvt_type& cvt) const
466 : {
467 : std::wstring tmp;
468 : if (!m_pathname.empty())
469 : path_traits::convert(m_pathname.c_str(), m_pathname.c_str()+m_pathname.size(),
470 : tmp, cvt);
471 : return tmp;
472 : }
473 : # endif
474 :
475 : // ----- generic format observers -----
476 :
477 : // Experimental generic function returning generic formatted path (i.e. separators
478 : // are forward slashes). Motivation: simpler than a family of generic_*string
479 : // functions.
480 : # ifdef BOOST_WINDOWS_API
481 : BOOST_FILESYSTEM_DECL path generic_path() const;
482 : # else
483 : path generic_path() const { return path(*this); }
484 : # endif
485 :
486 : template <class String>
487 : String generic_string() const;
488 :
489 : template <class String>
490 : String generic_string(const codecvt_type& cvt) const;
491 :
492 : # ifdef BOOST_WINDOWS_API
493 : const std::string generic_string() const { return generic_path().string(); }
494 : const std::string generic_string(const codecvt_type& cvt) const { return generic_path().string(cvt); }
495 : const std::wstring generic_wstring() const { return generic_path().wstring(); }
496 : const std::wstring generic_wstring(const codecvt_type&) const { return generic_wstring(); }
497 : # else // BOOST_POSIX_API
498 : // On POSIX-like systems, the generic format is the same as the native format
499 0 : const std::string& generic_string() const { return m_pathname; }
500 : const std::string& generic_string(const codecvt_type&) const { return m_pathname; }
501 : const std::wstring generic_wstring() const { return this->wstring(); }
502 : const std::wstring generic_wstring(const codecvt_type& cvt) const { return this->wstring(cvt); }
503 : # endif
504 :
505 : // ----- compare -----
506 :
507 : BOOST_FILESYSTEM_DECL int compare(const path& p) const BOOST_NOEXCEPT; // generic, lexicographical
508 : int compare(const std::string& s) const { return compare(path(s)); }
509 0 : int compare(const value_type* s) const { return compare(path(s)); }
510 :
511 : // ----- decomposition -----
512 :
513 : BOOST_FILESYSTEM_DECL path root_path() const;
514 : BOOST_FILESYSTEM_DECL path root_name() const; // returns 0 or 1 element path
515 : // even on POSIX, root_name() is non-empty() for network paths
516 : BOOST_FILESYSTEM_DECL path root_directory() const; // returns 0 or 1 element path
517 : BOOST_FILESYSTEM_DECL path relative_path() const;
518 : BOOST_FILESYSTEM_DECL path parent_path() const;
519 : BOOST_FILESYSTEM_DECL path filename() const; // returns 0 or 1 element path
520 : BOOST_FILESYSTEM_DECL path stem() const; // returns 0 or 1 element path
521 : BOOST_FILESYSTEM_DECL path extension() const; // returns 0 or 1 element path
522 :
523 : // ----- query -----
524 :
525 0 : bool empty() const BOOST_NOEXCEPT { return m_pathname.empty(); }
526 : bool filename_is_dot() const;
527 : bool filename_is_dot_dot() const;
528 0 : bool has_root_path() const { return has_root_directory() || has_root_name(); }
529 0 : bool has_root_name() const { return !root_name().empty(); }
530 0 : bool has_root_directory() const { return !root_directory().empty(); }
531 : bool has_relative_path() const { return !relative_path().empty(); }
532 0 : bool has_parent_path() const { return !parent_path().empty(); }
533 : bool has_filename() const { return !m_pathname.empty(); }
534 : bool has_stem() const { return !stem().empty(); }
535 : bool has_extension() const { return !extension().empty(); }
536 : bool is_relative() const { return !is_absolute(); }
537 0 : bool is_absolute() const
538 : {
539 : // Windows CE has no root name (aka drive letters)
540 : # if defined(BOOST_WINDOWS_API) && !defined(UNDER_CE)
541 : return has_root_name() && has_root_directory();
542 : # else
543 0 : return has_root_directory();
544 : # endif
545 : }
546 :
547 : // ----- lexical operations -----
548 :
549 : BOOST_FILESYSTEM_DECL path lexically_normal() const;
550 : BOOST_FILESYSTEM_DECL path lexically_relative(const path& base) const;
551 : path lexically_proximate(const path& base) const
552 : {
553 : path tmp(lexically_relative(base));
554 : return tmp.empty() ? *this : tmp;
555 : }
556 :
557 : // ----- iterators -----
558 :
559 : class iterator;
560 : typedef iterator const_iterator;
561 : class reverse_iterator;
562 : typedef reverse_iterator const_reverse_iterator;
563 :
564 : BOOST_FILESYSTEM_DECL iterator begin() const;
565 : BOOST_FILESYSTEM_DECL iterator end() const;
566 : reverse_iterator rbegin() const;
567 : reverse_iterator rend() const;
568 :
569 : // ----- static member functions -----
570 :
571 : static BOOST_FILESYSTEM_DECL std::locale imbue(const std::locale& loc);
572 : static BOOST_FILESYSTEM_DECL const codecvt_type& codecvt();
573 :
574 : // ----- deprecated functions -----
575 :
576 : # if defined(BOOST_FILESYSTEM_DEPRECATED) && defined(BOOST_FILESYSTEM_NO_DEPRECATED)
577 : # error both BOOST_FILESYSTEM_DEPRECATED and BOOST_FILESYSTEM_NO_DEPRECATED are defined
578 : # endif
579 :
580 : # if !defined(BOOST_FILESYSTEM_NO_DEPRECATED)
581 : // recently deprecated functions supplied by default
582 0 : path& normalize() {
583 0 : path tmp(lexically_normal());
584 0 : m_pathname.swap(tmp.m_pathname);
585 0 : return *this;
586 : }
587 : path& remove_leaf() { return remove_filename(); }
588 : path leaf() const { return filename(); }
589 0 : path branch_path() const { return parent_path(); }
590 : path generic() const { return generic_path(); }
591 : bool has_leaf() const { return !m_pathname.empty(); }
592 : bool has_branch_path() const { return !parent_path().empty(); }
593 : bool is_complete() const { return is_absolute(); }
594 : # endif
595 :
596 : # if defined(BOOST_FILESYSTEM_DEPRECATED)
597 : // deprecated functions with enough signature or semantic changes that they are
598 : // not supplied by default
599 : const std::string file_string() const { return string(); }
600 : const std::string directory_string() const { return string(); }
601 : const std::string native_file_string() const { return string(); }
602 : const std::string native_directory_string() const { return string(); }
603 : const string_type external_file_string() const { return native(); }
604 : const string_type external_directory_string() const { return native(); }
605 :
606 : // older functions no longer supported
607 : //typedef bool (*name_check)(const std::string & name);
608 : //basic_path(const string_type& str, name_check) { operator/=(str); }
609 : //basic_path(const typename string_type::value_type* s, name_check)
610 : // { operator/=(s);}
611 : //static bool default_name_check_writable() { return false; }
612 : //static void default_name_check(name_check) {}
613 : //static name_check default_name_check() { return 0; }
614 : //basic_path& canonize();
615 : # endif
616 :
617 : //--------------------------------------------------------------------------------------//
618 : // class path private members //
619 : //--------------------------------------------------------------------------------------//
620 :
621 : private:
622 :
623 : # if defined(_MSC_VER)
624 : # pragma warning(push) // Save warning settings
625 : # pragma warning(disable : 4251) // disable warning: class 'std::basic_string<_Elem,_Traits,_Ax>'
626 : # endif // needs to have dll-interface...
627 : /*
628 : m_pathname has the type, encoding, and format required by the native
629 : operating system. Thus for POSIX and Windows there is no conversion for
630 : passing m_pathname.c_str() to the O/S API or when obtaining a path from the
631 : O/S API. POSIX encoding is unspecified other than for dot and slash
632 : characters; POSIX just treats paths as a sequence of bytes. Windows
633 : encoding is UCS-2 or UTF-16 depending on the version.
634 : */
635 : string_type m_pathname; // Windows: as input; backslashes NOT converted to slashes,
636 : // slashes NOT converted to backslashes
637 : # if defined(_MSC_VER)
638 : # pragma warning(pop) // restore warning settings.
639 : # endif
640 :
641 : // Returns: If separator is to be appended, m_pathname.size() before append. Otherwise 0.
642 : // Note: An append is never performed if size()==0, so a returned 0 is unambiguous.
643 : BOOST_FILESYSTEM_DECL string_type::size_type m_append_separator_if_needed();
644 :
645 : BOOST_FILESYSTEM_DECL void m_erase_redundant_separator(string_type::size_type sep_pos);
646 : BOOST_FILESYSTEM_DECL string_type::size_type m_parent_path_end() const;
647 :
648 : // Was qualified; como433beta8 reports:
649 : // warning #427-D: qualified name is not allowed in member declaration
650 : friend class iterator;
651 : friend bool operator<(const path& lhs, const path& rhs);
652 :
653 : // see path::iterator::increment/decrement comment below
654 : static BOOST_FILESYSTEM_DECL void m_path_iterator_increment(path::iterator& it);
655 : static BOOST_FILESYSTEM_DECL void m_path_iterator_decrement(path::iterator& it);
656 :
657 : }; // class path
658 :
659 : namespace detail
660 : {
661 : BOOST_FILESYSTEM_DECL
662 : int lex_compare(path::iterator first1, path::iterator last1,
663 : path::iterator first2, path::iterator last2);
664 : BOOST_FILESYSTEM_DECL
665 : const path& dot_path();
666 : BOOST_FILESYSTEM_DECL
667 : const path& dot_dot_path();
668 : }
669 :
670 : # ifndef BOOST_FILESYSTEM_NO_DEPRECATED
671 : typedef path wpath;
672 : # endif
673 :
674 : //------------------------------------------------------------------------------------//
675 : // class path::iterator //
676 : //------------------------------------------------------------------------------------//
677 :
678 0 : class path::iterator
679 : : public boost::iterator_facade<
680 : path::iterator,
681 : path const,
682 : boost::bidirectional_traversal_tag >
683 : {
684 : private:
685 : friend class boost::iterator_core_access;
686 : friend class boost::filesystem::path;
687 : friend class boost::filesystem::path::reverse_iterator;
688 : friend void m_path_iterator_increment(path::iterator & it);
689 : friend void m_path_iterator_decrement(path::iterator & it);
690 :
691 0 : const path& dereference() const { return m_element; }
692 :
693 0 : bool equal(const iterator & rhs) const
694 : {
695 0 : return m_path_ptr == rhs.m_path_ptr && m_pos == rhs.m_pos;
696 : }
697 :
698 : // iterator_facade derived classes don't seem to like implementations in
699 : // separate translation unit dll's, so forward to class path static members
700 0 : void increment() { m_path_iterator_increment(*this); }
701 : void decrement() { m_path_iterator_decrement(*this); }
702 :
703 : path m_element; // current element
704 : const path* m_path_ptr; // path being iterated over
705 : string_type::size_type m_pos; // position of m_element in
706 : // m_path_ptr->m_pathname.
707 : // if m_element is implicit dot, m_pos is the
708 : // position of the last separator in the path.
709 : // end() iterator is indicated by
710 : // m_pos == m_path_ptr->m_pathname.size()
711 : }; // path::iterator
712 :
713 : //------------------------------------------------------------------------------------//
714 : // class path::reverse_iterator //
715 : //------------------------------------------------------------------------------------//
716 :
717 : class path::reverse_iterator
718 : : public boost::iterator_facade<
719 : path::reverse_iterator,
720 : path const,
721 : boost::bidirectional_traversal_tag >
722 : {
723 : public:
724 : explicit reverse_iterator(iterator itr) : m_itr(itr)
725 : {
726 : if (itr != itr.m_path_ptr->begin())
727 : m_element = *--itr;
728 : }
729 :
730 : private:
731 : friend class boost::iterator_core_access;
732 : friend class boost::filesystem::path;
733 :
734 : const path& dereference() const { return m_element; }
735 : bool equal(const reverse_iterator& rhs) const { return m_itr == rhs.m_itr; }
736 : void increment()
737 : {
738 : --m_itr;
739 : if (m_itr != m_itr.m_path_ptr->begin())
740 : {
741 : iterator tmp = m_itr;
742 : m_element = *--tmp;
743 : }
744 : }
745 : void decrement()
746 : {
747 : m_element = *m_itr;
748 : ++m_itr;
749 : }
750 :
751 : iterator m_itr;
752 : path m_element;
753 :
754 : }; // path::reverse_iterator
755 :
756 : //------------------------------------------------------------------------------------//
757 : // //
758 : // non-member functions //
759 : // //
760 : //------------------------------------------------------------------------------------//
761 :
762 : // std::lexicographical_compare would infinitely recurse because path iterators
763 : // yield paths, so provide a path aware version
764 : inline bool lexicographical_compare(path::iterator first1, path::iterator last1,
765 : path::iterator first2, path::iterator last2)
766 : { return detail::lex_compare(first1, last1, first2, last2) < 0; }
767 :
768 0 : inline bool operator==(const path& lhs, const path& rhs) {return lhs.compare(rhs) == 0;}
769 : inline bool operator==(const path& lhs, const path::string_type& rhs) {return lhs.compare(rhs) == 0;}
770 : inline bool operator==(const path::string_type& lhs, const path& rhs) {return rhs.compare(lhs) == 0;}
771 : inline bool operator==(const path& lhs, const path::value_type* rhs) {return lhs.compare(rhs) == 0;}
772 : inline bool operator==(const path::value_type* lhs, const path& rhs) {return rhs.compare(lhs) == 0;}
773 :
774 0 : inline bool operator!=(const path& lhs, const path& rhs) {return lhs.compare(rhs) != 0;}
775 : inline bool operator!=(const path& lhs, const path::string_type& rhs) {return lhs.compare(rhs) != 0;}
776 : inline bool operator!=(const path::string_type& lhs, const path& rhs) {return rhs.compare(lhs) != 0;}
777 0 : inline bool operator!=(const path& lhs, const path::value_type* rhs) {return lhs.compare(rhs) != 0;}
778 : inline bool operator!=(const path::value_type* lhs, const path& rhs) {return rhs.compare(lhs) != 0;}
779 :
780 : // TODO: why do == and != have additional overloads, but the others don't?
781 :
782 0 : inline bool operator<(const path& lhs, const path& rhs) {return lhs.compare(rhs) < 0;}
783 : inline bool operator<=(const path& lhs, const path& rhs) {return !(rhs < lhs);}
784 : inline bool operator> (const path& lhs, const path& rhs) {return rhs < lhs;}
785 : inline bool operator>=(const path& lhs, const path& rhs) {return !(lhs < rhs);}
786 :
787 : inline std::size_t hash_value(const path& x) BOOST_NOEXCEPT
788 : {
789 : # ifdef BOOST_WINDOWS_API
790 : std::size_t seed = 0;
791 : for(const path::value_type* it = x.c_str(); *it; ++it)
792 : hash_combine(seed, *it == L'/' ? L'\\' : *it);
793 : return seed;
794 : # else // BOOST_POSIX_API
795 : return hash_range(x.native().begin(), x.native().end());
796 : # endif
797 : }
798 :
799 0 : inline void swap(path& lhs, path& rhs) BOOST_NOEXCEPT { lhs.swap(rhs); }
800 :
801 0 : inline path operator/(const path& lhs, const path& rhs)
802 : {
803 0 : path p = lhs;
804 0 : p /= rhs;
805 0 : return p;
806 : }
807 : # if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
808 0 : inline path operator/(path&& lhs, const path& rhs)
809 : {
810 0 : lhs /= rhs;
811 0 : return std::move(lhs);
812 : }
813 : # endif
814 :
815 : // inserters and extractors
816 : // use boost::io::quoted() to handle spaces in paths
817 : // use '&' as escape character to ease use for Windows paths
818 :
819 : template <class Char, class Traits>
820 : inline std::basic_ostream<Char, Traits>&
821 0 : operator<<(std::basic_ostream<Char, Traits>& os, const path& p)
822 : {
823 : return os
824 0 : << boost::io::quoted(p.template string<std::basic_string<Char> >(), static_cast<Char>('&'));
825 : }
826 :
827 : template <class Char, class Traits>
828 : inline std::basic_istream<Char, Traits>&
829 : operator>>(std::basic_istream<Char, Traits>& is, path& p)
830 : {
831 : std::basic_string<Char> str;
832 : is >> boost::io::quoted(str, static_cast<Char>('&'));
833 : p = str;
834 : return is;
835 : }
836 :
837 : // name_checks
838 :
839 : // These functions are holdovers from version 1. It isn't clear they have much
840 : // usefulness, or how to generalize them for later versions.
841 :
842 : BOOST_FILESYSTEM_DECL bool portable_posix_name(const std::string & name);
843 : BOOST_FILESYSTEM_DECL bool windows_name(const std::string & name);
844 : BOOST_FILESYSTEM_DECL bool portable_name(const std::string & name);
845 : BOOST_FILESYSTEM_DECL bool portable_directory_name(const std::string & name);
846 : BOOST_FILESYSTEM_DECL bool portable_file_name(const std::string & name);
847 : BOOST_FILESYSTEM_DECL bool native(const std::string & name);
848 :
849 : namespace detail
850 : {
851 : // For POSIX, is_directory_separator() and is_element_separator() are identical since
852 : // a forward slash is the only valid directory separator and also the only valid
853 : // element separator. For Windows, forward slash and back slash are the possible
854 : // directory separators, but colon (example: "c:foo") is also an element separator.
855 :
856 : inline bool is_directory_separator(path::value_type c) BOOST_NOEXCEPT
857 : {
858 : return c == path::separator
859 : # ifdef BOOST_WINDOWS_API
860 : || c == path::preferred_separator
861 : # endif
862 : ;
863 : }
864 : inline bool is_element_separator(path::value_type c) BOOST_NOEXCEPT
865 : {
866 : return c == path::separator
867 : # ifdef BOOST_WINDOWS_API
868 : || c == path::preferred_separator || c == L':'
869 : # endif
870 : ;
871 : }
872 : } // namespace detail
873 :
874 : //------------------------------------------------------------------------------------//
875 : // class path miscellaneous function implementations //
876 : //------------------------------------------------------------------------------------//
877 :
878 : inline path::reverse_iterator path::rbegin() const { return reverse_iterator(end()); }
879 : inline path::reverse_iterator path::rend() const { return reverse_iterator(begin()); }
880 :
881 : inline bool path::filename_is_dot() const
882 : {
883 : // implicit dot is tricky, so actually call filename(); see path::filename() example
884 : // in reference.html
885 : path p(filename());
886 : return p.size() == 1 && *p.c_str() == dot;
887 : }
888 :
889 : inline bool path::filename_is_dot_dot() const
890 : {
891 : return size() >= 2 && m_pathname[size()-1] == dot && m_pathname[size()-2] == dot
892 : && (m_pathname.size() == 2 || detail::is_element_separator(m_pathname[size()-3]));
893 : // use detail::is_element_separator() rather than detail::is_directory_separator
894 : // to deal with "c:.." edge case on Windows when ':' acts as a separator
895 : }
896 :
897 : //--------------------------------------------------------------------------------------//
898 : // class path member template implementation //
899 : //--------------------------------------------------------------------------------------//
900 :
901 : template <class InputIterator>
902 : path& path::append(InputIterator begin, InputIterator end)
903 : {
904 : if (begin == end)
905 : return *this;
906 : string_type::size_type sep_pos(m_append_separator_if_needed());
907 : std::basic_string<typename std::iterator_traits<InputIterator>::value_type>
908 : seq(begin, end);
909 : path_traits::convert(seq.c_str(), seq.c_str()+seq.size(), m_pathname);
910 : if (sep_pos)
911 : m_erase_redundant_separator(sep_pos);
912 : return *this;
913 : }
914 :
915 : template <class InputIterator>
916 : path& path::append(InputIterator begin, InputIterator end, const codecvt_type& cvt)
917 : {
918 : if (begin == end)
919 : return *this;
920 : string_type::size_type sep_pos(m_append_separator_if_needed());
921 : std::basic_string<typename std::iterator_traits<InputIterator>::value_type>
922 : seq(begin, end);
923 : path_traits::convert(seq.c_str(), seq.c_str()+seq.size(), m_pathname, cvt);
924 : if (sep_pos)
925 : m_erase_redundant_separator(sep_pos);
926 : return *this;
927 : }
928 :
929 : template <class Source>
930 : path& path::append(Source const& source)
931 : {
932 : if (path_traits::empty(source))
933 : return *this;
934 : string_type::size_type sep_pos(m_append_separator_if_needed());
935 : path_traits::dispatch(source, m_pathname);
936 : if (sep_pos)
937 : m_erase_redundant_separator(sep_pos);
938 : return *this;
939 : }
940 :
941 : template <class Source>
942 : path& path::append(Source const& source, const codecvt_type& cvt)
943 : {
944 : if (path_traits::empty(source))
945 : return *this;
946 : string_type::size_type sep_pos(m_append_separator_if_needed());
947 : path_traits::dispatch(source, m_pathname, cvt);
948 : if (sep_pos)
949 : m_erase_redundant_separator(sep_pos);
950 : return *this;
951 : }
952 :
953 : //--------------------------------------------------------------------------------------//
954 : // class path member template specializations //
955 : //--------------------------------------------------------------------------------------//
956 :
957 : template <> inline
958 0 : std::string path::string<std::string>() const
959 0 : { return string(); }
960 :
961 : template <> inline
962 : std::wstring path::string<std::wstring>() const
963 : { return wstring(); }
964 :
965 : template <> inline
966 : std::string path::string<std::string>(const codecvt_type& cvt) const
967 : { return string(cvt); }
968 :
969 : template <> inline
970 : std::wstring path::string<std::wstring>(const codecvt_type& cvt) const
971 : { return wstring(cvt); }
972 :
973 : template <> inline
974 : std::string path::generic_string<std::string>() const
975 : { return generic_string(); }
976 :
977 : template <> inline
978 : std::wstring path::generic_string<std::wstring>() const
979 : { return generic_wstring(); }
980 :
981 : template <> inline
982 : std::string path::generic_string<std::string>(const codecvt_type& cvt) const
983 : { return generic_string(cvt); }
984 :
985 : template <> inline
986 : std::wstring path::generic_string<std::wstring>(const codecvt_type& cvt) const
987 : { return generic_wstring(cvt); }
988 :
989 : //--------------------------------------------------------------------------------------//
990 : // path_traits convert function implementations //
991 : // requiring path::codecvt() be visable //
992 : //--------------------------------------------------------------------------------------//
993 :
994 : namespace path_traits
995 : { // without codecvt
996 :
997 : inline
998 : void convert(const char* from,
999 : const char* from_end, // 0 for null terminated MBCS
1000 : std::wstring & to)
1001 : {
1002 : convert(from, from_end, to, path::codecvt());
1003 : }
1004 :
1005 : inline
1006 : void convert(const wchar_t* from,
1007 : const wchar_t* from_end, // 0 for null terminated MBCS
1008 : std::string & to)
1009 : {
1010 : convert(from, from_end, to, path::codecvt());
1011 : }
1012 :
1013 : inline
1014 : void convert(const char* from,
1015 : std::wstring & to)
1016 : {
1017 : BOOST_ASSERT(!!from);
1018 : convert(from, 0, to, path::codecvt());
1019 : }
1020 :
1021 : inline
1022 : void convert(const wchar_t* from,
1023 : std::string & to)
1024 : {
1025 : BOOST_ASSERT(!!from);
1026 : convert(from, 0, to, path::codecvt());
1027 : }
1028 : } // namespace path_traits
1029 : } // namespace filesystem
1030 : } // namespace boost
1031 :
1032 : //----------------------------------------------------------------------------//
1033 :
1034 : #include <boost/config/abi_suffix.hpp> // pops abi_prefix.hpp pragmas
1035 :
1036 : #endif // BOOST_FILESYSTEM_PATH_HPP
|