| 1 | // portability.cpp ---------------------------------------------------------// |
|---|
| 2 | |
|---|
| 3 | // Copyright © 2002-2005 Beman Dawes |
|---|
| 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 |
|---|
| 6 | // at http://www.boost.org/LICENSE_1_0.txt) |
|---|
| 7 | |
|---|
| 8 | // See library home page at http://www.boost.org/libs/filesystem |
|---|
| 9 | |
|---|
| 10 | //----------------------------------------------------------------------------// |
|---|
| 11 | |
|---|
| 12 | // define BOOST_FILESYSTEM_SOURCE so that <boost/filesystem/config.hpp> knows |
|---|
| 13 | // the library is being built (possibly exporting rather than importing code) |
|---|
| 14 | #define BOOST_FILESYSTEM_SOURCE |
|---|
| 15 | |
|---|
| 16 | #include <boost/filesystem/config.hpp> |
|---|
| 17 | #include <boost/filesystem/path.hpp> |
|---|
| 18 | |
|---|
| 19 | namespace fs = boost::filesystem; |
|---|
| 20 | |
|---|
| 21 | #include <cstring> // SGI MIPSpro compilers need this |
|---|
| 22 | |
|---|
| 23 | # ifdef BOOST_NO_STDC_NAMESPACE |
|---|
| 24 | namespace std { using ::strerror; } |
|---|
| 25 | # endif |
|---|
| 26 | |
|---|
| 27 | //----------------------------------------------------------------------------// |
|---|
| 28 | |
|---|
| 29 | namespace |
|---|
| 30 | { |
|---|
| 31 | const char invalid_chars[] = |
|---|
| 32 | "\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A\x0B\x0C\x0D\x0E\x0F" |
|---|
| 33 | "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1A\x1B\x1C\x1D\x1E\x1F" |
|---|
| 34 | "<>:\"/\\|"; |
|---|
| 35 | // note that the terminating '\0' is part of the string - thus the size below |
|---|
| 36 | // is sizeof(invalid_chars) rather than sizeof(invalid_chars)-1. I |
|---|
| 37 | const std::string windows_invalid_chars( invalid_chars, sizeof(invalid_chars) ); |
|---|
| 38 | |
|---|
| 39 | const std::string valid_posix( |
|---|
| 40 | "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789._-" ); |
|---|
| 41 | |
|---|
| 42 | } // unnamed namespace |
|---|
| 43 | |
|---|
| 44 | namespace boost |
|---|
| 45 | { |
|---|
| 46 | namespace filesystem |
|---|
| 47 | { |
|---|
| 48 | |
|---|
| 49 | // name_check functions ----------------------------------------------// |
|---|
| 50 | |
|---|
| 51 | # ifdef BOOST_WINDOWS |
|---|
| 52 | BOOST_FILESYSTEM_DECL bool native( const std::string & name ) |
|---|
| 53 | { |
|---|
| 54 | return windows_name( name ); |
|---|
| 55 | } |
|---|
| 56 | # else |
|---|
| 57 | BOOST_FILESYSTEM_DECL bool native( const std::string & name ) |
|---|
| 58 | { |
|---|
| 59 | return name.find('/') == std::string::npos; |
|---|
| 60 | } |
|---|
| 61 | # endif |
|---|
| 62 | |
|---|
| 63 | BOOST_FILESYSTEM_DECL bool portable_posix_name( const std::string & name ) |
|---|
| 64 | { |
|---|
| 65 | return name.size() != 0 |
|---|
| 66 | && name.find_first_not_of( valid_posix ) == std::string::npos; |
|---|
| 67 | } |
|---|
| 68 | |
|---|
| 69 | BOOST_FILESYSTEM_DECL bool windows_name( const std::string & name ) |
|---|
| 70 | { |
|---|
| 71 | return name.size() != 0 |
|---|
| 72 | && name.find_first_of( windows_invalid_chars ) == std::string::npos |
|---|
| 73 | && *(name.end()-1) != ' ' |
|---|
| 74 | && (*(name.end()-1) != '.' |
|---|
| 75 | || name.length() == 1 || name == ".."); |
|---|
| 76 | } |
|---|
| 77 | |
|---|
| 78 | BOOST_FILESYSTEM_DECL bool portable_name( const std::string & name ) |
|---|
| 79 | { |
|---|
| 80 | return |
|---|
| 81 | name.size() == 0 |
|---|
| 82 | || name == "." |
|---|
| 83 | || name == ".." |
|---|
| 84 | || (windows_name( name ) |
|---|
| 85 | && portable_posix_name( name ) |
|---|
| 86 | && name[0] != '.' && name[0] != '-'); |
|---|
| 87 | } |
|---|
| 88 | |
|---|
| 89 | BOOST_FILESYSTEM_DECL bool portable_directory_name( const std::string & name ) |
|---|
| 90 | { |
|---|
| 91 | return |
|---|
| 92 | name == "." |
|---|
| 93 | || name == ".." |
|---|
| 94 | || (portable_name( name ) |
|---|
| 95 | && name.find('.') == std::string::npos); |
|---|
| 96 | } |
|---|
| 97 | |
|---|
| 98 | BOOST_FILESYSTEM_DECL bool portable_file_name( const std::string & name ) |
|---|
| 99 | { |
|---|
| 100 | std::string::size_type pos; |
|---|
| 101 | return |
|---|
| 102 | name == "." |
|---|
| 103 | || name == ".." |
|---|
| 104 | || (portable_name( name ) |
|---|
| 105 | && ( (pos = name.find( '.' )) == std::string::npos |
|---|
| 106 | || (name.find( '.', pos+1 )== std::string::npos |
|---|
| 107 | && (pos + 5) > name.length() ))) |
|---|
| 108 | ; |
|---|
| 109 | } |
|---|
| 110 | |
|---|
| 111 | } // namespace filesystem |
|---|
| 112 | } // namespace boost |
|---|