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