1 // path implementation -----------------------------------------------------//
3 // © Copyright Beman Dawes 2002-2003
4 // Use, modification, and distribution is subject to the Boost Software
5 // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
6 // http://www.boost.org/LICENSE_1_0.txt)
8 // See library home page at http://www.boost.org/libs/filesystem
11 //****************************************************************************//
13 // WARNING: This code was hacked time and time again as different library
14 // designs were tried. Thus portions may be residue from the earlier
15 // experiments, and be totally stupid or wrong in light of the final library
16 // specifications. The code needs to be reviewed line-by-line to elmininate
19 //****************************************************************************//
21 // define BOOST_FILESYSTEM_SOURCE so that <boost/filesystem/config.hpp> knows
22 // the library is being built (possibly exporting rather than importing code)
23 #define BOOST_FILESYSTEM_SOURCE
25 #include <boost/filesystem/path.hpp>
26 #include <boost/filesystem/exception.hpp>
28 // BOOST_POSIX or BOOST_WINDOWS specify which API to use.
29 # if !defined( BOOST_WINDOWS ) && !defined( BOOST_POSIX )
30 # if defined(_WIN32) || defined(__WIN32__) || defined(WIN32) || defined(__CYGWIN__)
31 # define BOOST_WINDOWS
38 namespace fs = boost::filesystem;
40 #include <boost/config.hpp>
41 #ifdef BOOST_NO_STDC_NAMESPACE
42 namespace std { using ::strlen; }
45 #include <boost/throw_exception.hpp>
46 #include <cstring> // SGI MIPSpro compilers need this
48 #include <algorithm> // for lexicographical_compare
51 #include <boost/config/abi_prefix.hpp> // must be the last header
53 // helpers -----------------------------------------------------------------//
57 // POSIX & Windows cases: "", "/", "/foo", "foo", "foo/bar"
58 // Windows only cases: "c:", "c:/", "c:foo", "c:/foo",
59 // "prn:", "//share", "//share/", "//share/foo"
61 std::string::size_type leaf_pos( const std::string & str,
62 std::string::size_type end_pos ) // end_pos is past-the-end position
63 // return 0 if str itself is leaf (or empty)
65 if ( end_pos && str[end_pos-1] == '/' ) return end_pos-1;
67 std::string::size_type pos( str.find_last_of( '/', end_pos-1 ) );
69 if ( pos == std::string::npos ) pos = str.find_last_of( ':', end_pos-2 );
72 return ( pos == std::string::npos // path itself must be a leaf (or empty)
74 || (pos == 1 && str[0] == '/') // or share
76 ) ? 0 // so leaf is entire string
77 : pos + 1; // or starts after delimiter
80 void first_name( const std::string & src, std::string & target )
82 target = ""; // VC++ 6.0 doesn't have string::clear()
83 std::string::const_iterator itr( src.begin() );
87 if ( src.size() >= 2 && src[0] == '/' && src[1] == '/' )
88 { target = "//"; itr += 2; }
91 while ( itr != src.end()
95 && *itr != '/' ) { target += *itr++; }
97 if ( itr == src.end() ) return;
108 if ( itr == src.begin() ) { target += '/'; }
112 const char invalid_chars[] =
113 "\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A\x0B\x0C\x0D\x0E\x0F"
114 "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1A\x1B\x1C\x1D\x1E\x1F"
116 // note that the terminating '\0' is part of the string - thus the size below
117 // is sizeof(invalid_chars) rather than sizeof(invalid_chars)-1. I
118 const std::string windows_invalid_chars( invalid_chars, sizeof(invalid_chars) );
120 const std::string valid_posix(
121 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789._-" );
123 fs::path::name_check default_check = fs::portable_name;
124 bool safe_to_write_check = true; // write-once-before-read allowed
126 } // unnamed namespace
128 //----------------------------------------------------------------------------//
134 // name_check functions ----------------------------------------------//
136 # ifdef BOOST_WINDOWS
137 BOOST_FILESYSTEM_DECL bool native( const std::string & name )
139 return windows_name( name );
142 BOOST_FILESYSTEM_DECL bool native( const std::string & )
148 BOOST_FILESYSTEM_DECL bool no_check( const std::string & ) { return true; }
150 BOOST_FILESYSTEM_DECL bool portable_posix_name( const std::string & name )
152 return name.size() != 0
153 && name.find_first_not_of( valid_posix ) == std::string::npos;
156 BOOST_FILESYSTEM_DECL bool windows_name( const std::string & name )
158 return name.size() != 0
159 && name.find_first_of( windows_invalid_chars ) == std::string::npos
160 && *(name.end()-1) != ' '
161 && (*(name.end()-1) != '.'
162 || name.length() == 1 || name == "..");
165 BOOST_FILESYSTEM_DECL bool portable_name( const std::string & name )
171 || (windows_name( name )
172 && portable_posix_name( name )
173 && name[0] != '.' && name[0] != '-');
176 BOOST_FILESYSTEM_DECL bool portable_directory_name( const std::string & name )
181 || (portable_name( name )
182 && name.find('.') == std::string::npos);
185 BOOST_FILESYSTEM_DECL bool portable_file_name( const std::string & name )
187 std::string::size_type pos;
191 || (portable_name( name )
192 && ( (pos = name.find( '.' )) == std::string::npos
193 || (name.find( '.', pos+1 )== std::string::npos
194 && (pos + 5) > name.length() )))
199 // path implementation -----------------------------------------------------//
201 path::path( const std::string & src )
203 m_path_append( src, default_name_check() );
206 path::path( const char * src )
209 m_path_append( src, default_name_check() );
212 path::path( const std::string & src, name_check checker )
214 m_path_append( src, checker );
217 path::path( const char * src, name_check checker )
220 m_path_append( src, checker );
223 path & path::operator /=( const path & rhs )
225 m_path_append( rhs.m_path, no_check );
229 void path::m_path_append( const std::string & src, name_check checker )
231 // convert backslash to forward slash if checker==native
232 // allow system-specific-root if checker==no_check || checker==native
235 assert( src.size() == std::strlen( src.c_str() ) ); // no embedded 0
237 if ( src.size() == 0 ) return;
239 std::string::const_iterator itr( src.begin() );
242 # ifdef BOOST_WINDOWS
243 if ( (checker == no_check || checker == native) && src.size() >= 2 )
246 if ( src[1] == ':' || src[src.size()-1] == ':' )
248 for ( ; *itr != ':'; ++itr ) m_path += *itr;
254 else if ( (*itr == '/' || (*itr == '\\' && checker == native))
255 && (*(itr+1) == '/' || (*(itr+1) == '\\' && checker == native)) )
259 itr != src.end() && *itr != '/' && *itr != '\\';
260 ++itr ) m_path += *itr;
265 // root directory [ "/" ]
266 if ( itr != src.end() && (*itr == '/'
267 # ifdef BOOST_WINDOWS
268 || (*itr == '\\' && checker == native)
273 if ( m_path.size() == 0
274 # ifdef BOOST_WINDOWS
275 || m_path[m_path.size()-1] == ':' // drive or device
280 && m_path.find( '/', 2 ) == std::string::npos
286 // element { "/" element } [ "/" ]
287 while ( itr != src.end() )
289 if ( m_path == "." ) m_path = "";
291 // directory-placeholder
292 if ( *itr == '.' && ((itr+1) == src.end() || *(itr+1) == '/'
293 # ifdef BOOST_WINDOWS
298 if ( empty() ) m_path += '.';
302 // parent-directory or name
305 // append '/' if needed
307 # ifdef BOOST_WINDOWS
308 && *(m_path.end()-1) != ':'
310 && *(m_path.end()-1) != '/' )
315 && (itr+1) != src.end() && *(itr+1) == '.'
316 && ((itr+2) == src.end() || *(itr+2) == '/'
317 # ifdef BOOST_WINDOWS
325 } // parent-directory
333 while ( ++itr != src.end() && *itr != '/'
334 # ifdef BOOST_WINDOWS
335 && (*itr != '\\' || checker != native)
339 if ( !checker( name ) )
341 boost::throw_exception( filesystem_error(
342 "boost::filesystem::path",
343 "invalid name \"" + name + "\" in path: \"" + src + "\"" ) );
348 } // parent-directory or name
351 if ( itr != src.end() )
354 # ifdef BOOST_WINDOWS
355 || (*itr == '\\' && checker == native)
359 boost::throw_exception( filesystem_error(
360 "boost::filesystem::path",
361 "invalid path syntax: \"" + src + "\"" ) );
364 } // while more elements
366 // special case: remove one or more leading "/.."
368 std::string::size_type pos = 0, sz = m_path.size();
370 # ifdef BOOST_WINDOWS
371 if ( sz > 2 && m_path[pos] != '/' && m_path[pos+1] == ':' ) // drive
372 { pos += 2; sz -= 2; }
375 while ( sz >= 3 && m_path[pos] == '/'
376 && m_path[pos+1] == '.' && m_path[pos+2] == '.'
377 && (sz == 3 || m_path[pos+3] == '/') )
379 m_path.erase( pos+1, 3 ); // "..[/]"
380 sz -= 3; // on last, 3 should be 2; that doesn't matter
384 // path conversion functions ------------------------------------------------//
386 std::string path::native_file_string() const
388 # ifdef BOOST_WINDOWS
389 std::string s( m_path );
390 for ( std::string::iterator itr( s.begin() );
391 itr != s.end(); ++itr )
392 if ( *itr == '/' ) *itr = '\\';
399 std::string path::native_directory_string() const
400 { return native_file_string(); }
402 // path modification functions -----------------------------------------------//
404 path & path::normalize()
406 if ( m_path.empty() ) return *this;
407 std::string::size_type end, beg(0), start(0);
409 # ifdef BOOST_WINDOWS
410 if ( m_path.size() > 2
411 && m_path[0] != '/' && m_path[1] == ':' ) start = 2; // drive
414 while ( (beg=m_path.find( "/..", beg )) != std::string::npos )
417 if ( (beg == 1 && m_path[0] == '.')
418 || (beg == 2 && m_path[0] == '.' && m_path[1] == '.')
419 || (beg > 2 && m_path[beg-3] == '/'
420 && m_path[beg-2] == '.' && m_path[beg-1] == '.') )
425 if ( end < m_path.size() )
427 if ( m_path[end] == '/' ) ++end;
428 else { beg = end; continue; } // name starts with ..
431 // end is one past end of substr to be erased; now set beg
432 while ( beg > start && m_path[--beg] != '/' ) {}
433 if ( m_path[beg] == '/') ++beg;
434 m_path.erase( beg, end-beg );
438 if ( m_path.empty() ) m_path = ".";
440 { // remove trailing '/' if not root directory
441 std::string::size_type sz = m_path.size();
443 # ifdef BOOST_WINDOWS
444 if ( start ) sz -= 2; // drive
447 if ( sz > 1 && m_path[m_path.size()-1] == '/' )
448 { m_path.erase( m_path.size()-1 ); }
453 // path decomposition functions ---------------------------------------------//
455 path::iterator path::begin() const
458 itr.m_path_ptr = this;
459 first_name( m_path, itr.m_name );
464 void path::m_replace_leaf( const char * new_leaf )
466 m_path.erase( leaf_pos( m_path, m_path.size() ) );
470 std::string path::leaf() const
472 return m_path.substr( leaf_pos( m_path, m_path.size() ) );
477 inline bool is_absolute_root( const std::string & s,
478 std::string::size_type len )
481 len && s[len-1] == '/'
485 # ifdef BOOST_WINDOWS
487 && ( s[len-2] == ':' // drive or device
488 || ( s[0] == '/' // share
490 && s.find( '/', 2 ) == len-1
499 path path::branch_path() const
501 std::string::size_type end_pos( leaf_pos( m_path, m_path.size() ) );
503 // skip a '/' unless it is a root directory
504 if ( end_pos && m_path[end_pos-1] == '/'
505 && !detail::is_absolute_root( m_path, end_pos ) ) --end_pos;
506 return path( m_path.substr( 0, end_pos ), no_check );
509 path path::relative_path() const
511 std::string::size_type pos( 0 );
512 if ( m_path.size() && m_path[0] == '/' )
514 # ifdef BOOST_WINDOWS
515 if ( m_path.size()>1 && m_path[1] == '/' ) // share
517 if ( (pos = m_path.find( '/', 2 )) != std::string::npos ) ++pos;
521 else if ( (pos = m_path.find( ':' )) == std::string::npos ) pos = 0;
524 if ( ++pos < m_path.size() && m_path[pos] == '/' ) ++pos;
527 return path( m_path.substr( pos ), no_check );
530 std::string path::root_name() const
532 # ifdef BOOST_WINDOWS
533 std::string::size_type pos( m_path.find( ':' ) );
534 if ( pos != std::string::npos ) return m_path.substr( 0, pos+1 );
535 if ( m_path.size() > 2 && m_path[0] == '/' && m_path[1] == '/' )
537 pos = m_path.find( '/', 2 );
538 return m_path.substr( 0, pos );
541 return std::string();
544 std::string path::root_directory() const
547 ( m_path.size() && m_path[0] == '/' ) // covers both "/" and "//share"
548 # ifdef BOOST_WINDOWS
549 || ( m_path.size() > 2
551 && m_path[2] == '/' ) // "c:/"
556 path path::root_path() const
559 # ifdef BOOST_WINDOWS
560 root_name(), no_check ) /= root_directory();
566 // path query functions -----------------------------------------------------//
568 bool path::is_complete() const
570 # ifdef BOOST_WINDOWS
571 return m_path.size() > 2
572 && ( (m_path[1] == ':' && m_path[2] == '/') // "c:/"
573 || (m_path[0] == '/' && m_path[1] == '/') // "//share"
574 || m_path[m_path.size()-1] == ':' );
576 return m_path.size() && m_path[0] == '/';
580 bool path::has_root_path() const
582 return ( m_path.size()
583 && m_path[0] == '/' ) // covers both "/" and "//share"
584 # ifdef BOOST_WINDOWS
585 || ( m_path.size() > 1 && m_path[1] == ':' ) // "c:" and "c:/"
586 || ( m_path.size() > 3
587 && m_path[m_path.size()-1] == ':' ) // "device:"
592 bool path::has_root_name() const
594 # ifdef BOOST_WINDOWS
595 return m_path.size() > 1
596 && ( m_path[1] == ':' // "c:"
597 || m_path[m_path.size()-1] == ':' // "prn:"
598 || (m_path[0] == '/' && m_path[1] == '/') // "//share"
605 bool path::has_root_directory() const
607 return ( m_path.size()
608 && m_path[0] == '/' ) // covers both "/" and "//share"
609 # ifdef BOOST_WINDOWS
610 || ( m_path.size() > 2
611 && m_path[1] == ':' && m_path[2] == '/' ) // "c:/"
616 bool path::has_relative_path() const { return !relative_path().empty(); }
617 bool path::has_branch_path() const { return !branch_path().empty(); }
619 // default name_check mechanism ----------------------------------------//
621 bool path::default_name_check_writable()
623 return safe_to_write_check;
626 void path::default_name_check( name_check new_check )
629 if ( !safe_to_write_check )
630 boost::throw_exception(
631 filesystem_error( "boost::filesystem::default_name_check",
632 "default name check already set" ));
633 default_check = new_check;
634 safe_to_write_check = false;
637 path::name_check path::default_name_check()
639 safe_to_write_check = false;
640 return default_check;
643 // path operator< ------------------------------------------------------//
644 bool path::operator<( const path & that ) const
646 return std::lexicographical_compare(
647 begin(), end(), that.begin(), that.end() );
651 // path::iterator implementation --------------------------------------------//
653 BOOST_FILESYSTEM_DECL void path::iterator::increment()
655 assert( m_pos < m_path_ptr->m_path.size() ); // detect increment past end
656 m_pos += m_name.size();
657 if ( m_pos == m_path_ptr->m_path.size() )
659 m_name = ""; // not strictly required, but might aid debugging
662 if ( m_path_ptr->m_path[m_pos] == '/' )
664 # ifdef BOOST_WINDOWS
665 if ( m_name[m_name.size()-1] == ':' // drive or device
666 || (m_name[0] == '/' && m_name[1] == '/') ) // share
674 std::string::size_type end_pos( m_path_ptr->m_path.find( '/', m_pos ) );
675 if ( end_pos == std::string::npos )
676 end_pos = m_path_ptr->m_path.size();
677 m_name = m_path_ptr->m_path.substr( m_pos, end_pos - m_pos );
680 BOOST_FILESYSTEM_DECL void path::iterator::decrement()
682 assert( m_pos ); // detect decrement of begin
683 std::string::size_type end_pos( m_pos );
685 // skip a '/' unless it is a root directory
686 if ( m_path_ptr->m_path[end_pos-1] == '/'
687 && !detail::is_absolute_root( m_path_ptr->m_path, end_pos ) ) --end_pos;
688 m_pos = leaf_pos( m_path_ptr->m_path, end_pos );
689 m_name = m_path_ptr->m_path.substr( m_pos, end_pos - m_pos );
691 } // namespace filesystem