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