annotate DEPENDENCIES/generic/include/boost/filesystem/path.hpp @ 125:34e428693f5d vext

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