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@16
|
124 // [defaults] "const codecvt_type& cvt=codecvt()" default arguments are not used
|
Chris@16
|
125 // because some compilers, such as Microsoft prior to VC++ 10, do not handle defaults
|
Chris@16
|
126 // correctly in templates.
|
Chris@16
|
127
|
Chris@16
|
128 // ----- constructors -----
|
Chris@16
|
129
|
Chris@16
|
130 path(){}
|
Chris@16
|
131
|
Chris@16
|
132 path(const path& p) : m_pathname(p.m_pathname) {}
|
Chris@16
|
133
|
Chris@16
|
134 template <class Source>
|
Chris@16
|
135 path(Source const& source,
|
Chris@16
|
136 typename boost::enable_if<path_traits::is_pathable<
|
Chris@16
|
137 typename boost::decay<Source>::type> >::type* =0)
|
Chris@16
|
138 {
|
Chris@16
|
139 path_traits::dispatch(source, m_pathname, codecvt());
|
Chris@16
|
140 }
|
Chris@16
|
141
|
Chris@16
|
142 // Overloads for the operating system API's native character type. Rationale:
|
Chris@16
|
143 // - Avoids use of codecvt() for native value_type strings. This limits the
|
Chris@16
|
144 // impact of locale("") initialization failures on POSIX systems to programs
|
Chris@16
|
145 // that actually depend on locale(""). It further ensures that exceptions thrown
|
Chris@16
|
146 // as a result of such failues occur after main() has started, so can be caught.
|
Chris@16
|
147 // This is a partial resolution of tickets 4688, 5100, and 5289.
|
Chris@16
|
148 // - A slight optimization for a common use case, particularly on POSIX since
|
Chris@16
|
149 // value_type is char and that is the most common useage.
|
Chris@16
|
150 path(const value_type* s) : m_pathname(s) {}
|
Chris@16
|
151 path(const std::basic_string<value_type>& s) : m_pathname(s) {}
|
Chris@16
|
152
|
Chris@16
|
153 template <class Source>
|
Chris@16
|
154 path(Source const& source, const codecvt_type& cvt)
|
Chris@16
|
155 // see [defaults] note above explaining why codecvt() default arguments are not used
|
Chris@16
|
156 {
|
Chris@16
|
157 path_traits::dispatch(source, m_pathname, cvt);
|
Chris@16
|
158 }
|
Chris@16
|
159
|
Chris@16
|
160 template <class InputIterator>
|
Chris@16
|
161 path(InputIterator begin, InputIterator end)
|
Chris@16
|
162 {
|
Chris@16
|
163 if (begin != end)
|
Chris@16
|
164 {
|
Chris@16
|
165 std::basic_string<typename std::iterator_traits<InputIterator>::value_type>
|
Chris@16
|
166 s(begin, end);
|
Chris@16
|
167 path_traits::convert(s.c_str(), s.c_str()+s.size(), m_pathname, codecvt());
|
Chris@16
|
168 }
|
Chris@16
|
169 }
|
Chris@16
|
170
|
Chris@16
|
171 template <class InputIterator>
|
Chris@16
|
172 path(InputIterator begin, InputIterator end, const codecvt_type& cvt)
|
Chris@16
|
173 {
|
Chris@16
|
174 if (begin != end)
|
Chris@16
|
175 {
|
Chris@16
|
176 std::basic_string<typename std::iterator_traits<InputIterator>::value_type>
|
Chris@16
|
177 s(begin, end);
|
Chris@16
|
178 path_traits::convert(s.c_str(), s.c_str()+s.size(), m_pathname, cvt);
|
Chris@16
|
179 }
|
Chris@16
|
180 }
|
Chris@16
|
181
|
Chris@16
|
182 // ----- assignments -----
|
Chris@16
|
183
|
Chris@16
|
184 path& operator=(const path& p)
|
Chris@16
|
185 {
|
Chris@16
|
186 m_pathname = p.m_pathname;
|
Chris@16
|
187 return *this;
|
Chris@16
|
188 }
|
Chris@16
|
189
|
Chris@16
|
190 path& operator=(const value_type* ptr) // required in case ptr overlaps *this
|
Chris@16
|
191 {
|
Chris@16
|
192 m_pathname = ptr;
|
Chris@16
|
193 return *this;
|
Chris@16
|
194 }
|
Chris@16
|
195
|
Chris@16
|
196 template <class Source>
|
Chris@16
|
197 typename boost::enable_if<path_traits::is_pathable<
|
Chris@16
|
198 typename boost::decay<Source>::type>, path&>::type
|
Chris@16
|
199 operator=(Source const& source)
|
Chris@16
|
200 {
|
Chris@16
|
201 m_pathname.clear();
|
Chris@16
|
202 path_traits::dispatch(source, m_pathname, codecvt());
|
Chris@16
|
203 return *this;
|
Chris@16
|
204 }
|
Chris@16
|
205
|
Chris@16
|
206 path& assign(const value_type* ptr, const codecvt_type&) // required in case ptr overlaps *this
|
Chris@16
|
207 {
|
Chris@16
|
208 m_pathname = ptr;
|
Chris@16
|
209 return *this;
|
Chris@16
|
210 }
|
Chris@16
|
211
|
Chris@16
|
212 template <class Source>
|
Chris@16
|
213 path& assign(Source const& source, const codecvt_type& cvt)
|
Chris@16
|
214 {
|
Chris@16
|
215 m_pathname.clear();
|
Chris@16
|
216 path_traits::dispatch(source, m_pathname, cvt);
|
Chris@16
|
217 return *this;
|
Chris@16
|
218 }
|
Chris@16
|
219
|
Chris@16
|
220 template <class InputIterator>
|
Chris@16
|
221 path& assign(InputIterator begin, InputIterator end)
|
Chris@16
|
222 {
|
Chris@16
|
223 return assign(begin, end, codecvt());
|
Chris@16
|
224 }
|
Chris@16
|
225
|
Chris@16
|
226 template <class InputIterator>
|
Chris@16
|
227 path& assign(InputIterator begin, InputIterator end, const codecvt_type& cvt)
|
Chris@16
|
228 {
|
Chris@16
|
229 m_pathname.clear();
|
Chris@16
|
230 if (begin != end)
|
Chris@16
|
231 {
|
Chris@16
|
232 std::basic_string<typename std::iterator_traits<InputIterator>::value_type>
|
Chris@16
|
233 s(begin, end);
|
Chris@16
|
234 path_traits::convert(s.c_str(), s.c_str()+s.size(), m_pathname, cvt);
|
Chris@16
|
235 }
|
Chris@16
|
236 return *this;
|
Chris@16
|
237 }
|
Chris@16
|
238
|
Chris@16
|
239 // ----- concatenation -----
|
Chris@16
|
240
|
Chris@16
|
241 path& operator+=(const path& p) {m_pathname += p.m_pathname; return *this;}
|
Chris@16
|
242 path& operator+=(const string_type& s) {m_pathname += s; return *this;}
|
Chris@16
|
243 path& operator+=(const value_type* ptr) {m_pathname += ptr; return *this;}
|
Chris@16
|
244 path& operator+=(value_type c) {m_pathname += c; return *this;}
|
Chris@16
|
245
|
Chris@16
|
246 template <class Source>
|
Chris@16
|
247 typename boost::enable_if<path_traits::is_pathable<
|
Chris@16
|
248 typename boost::decay<Source>::type>, path&>::type
|
Chris@16
|
249 operator+=(Source const& source)
|
Chris@16
|
250 {
|
Chris@16
|
251 return concat(source, codecvt());
|
Chris@16
|
252 }
|
Chris@16
|
253
|
Chris@16
|
254 template <class CharT>
|
Chris@16
|
255 typename boost::enable_if<is_integral<CharT>, path&>::type
|
Chris@16
|
256 operator+=(CharT c)
|
Chris@16
|
257 {
|
Chris@16
|
258 CharT tmp[2];
|
Chris@16
|
259 tmp[0] = c;
|
Chris@16
|
260 tmp[1] = 0;
|
Chris@16
|
261 return concat(tmp, codecvt());
|
Chris@16
|
262 }
|
Chris@16
|
263
|
Chris@16
|
264 template <class Source>
|
Chris@16
|
265 path& concat(Source const& source, const codecvt_type& cvt)
|
Chris@16
|
266 {
|
Chris@16
|
267 path_traits::dispatch(source, m_pathname, cvt);
|
Chris@16
|
268 return *this;
|
Chris@16
|
269 }
|
Chris@16
|
270
|
Chris@16
|
271 template <class InputIterator>
|
Chris@16
|
272 path& concat(InputIterator begin, InputIterator end)
|
Chris@16
|
273 {
|
Chris@16
|
274 return concat(begin, end, codecvt());
|
Chris@16
|
275 }
|
Chris@16
|
276
|
Chris@16
|
277 template <class InputIterator>
|
Chris@16
|
278 path& concat(InputIterator begin, InputIterator end, const codecvt_type& cvt)
|
Chris@16
|
279 {
|
Chris@16
|
280 if (begin == end)
|
Chris@16
|
281 return *this;
|
Chris@16
|
282 std::basic_string<typename std::iterator_traits<InputIterator>::value_type>
|
Chris@16
|
283 s(begin, end);
|
Chris@16
|
284 path_traits::convert(s.c_str(), s.c_str()+s.size(), m_pathname, cvt);
|
Chris@16
|
285 return *this;
|
Chris@16
|
286 }
|
Chris@16
|
287
|
Chris@16
|
288 // ----- appends -----
|
Chris@16
|
289
|
Chris@16
|
290 // if a separator is added, it is the preferred separator for the platform;
|
Chris@16
|
291 // slash for POSIX, backslash for Windows
|
Chris@16
|
292
|
Chris@16
|
293 path& operator/=(const path& p);
|
Chris@16
|
294
|
Chris@16
|
295 path& operator/=(const value_type* ptr);
|
Chris@16
|
296
|
Chris@16
|
297 template <class Source>
|
Chris@16
|
298 typename boost::enable_if<path_traits::is_pathable<
|
Chris@16
|
299 typename boost::decay<Source>::type>, path&>::type
|
Chris@16
|
300 operator/=(Source const& source)
|
Chris@16
|
301 {
|
Chris@16
|
302 return append(source, codecvt());
|
Chris@16
|
303 }
|
Chris@16
|
304
|
Chris@16
|
305 path& append(const value_type* ptr, const codecvt_type&) // required in case ptr overlaps *this
|
Chris@16
|
306 {
|
Chris@16
|
307 this->operator/=(ptr);
|
Chris@16
|
308 return *this;
|
Chris@16
|
309 }
|
Chris@16
|
310
|
Chris@16
|
311 template <class Source>
|
Chris@16
|
312 path& append(Source const& source, const codecvt_type& cvt);
|
Chris@16
|
313
|
Chris@16
|
314 template <class InputIterator>
|
Chris@16
|
315 path& append(InputIterator begin, InputIterator end)
|
Chris@16
|
316 {
|
Chris@16
|
317 return append(begin, end, codecvt());
|
Chris@16
|
318 }
|
Chris@16
|
319
|
Chris@16
|
320 template <class InputIterator>
|
Chris@16
|
321 path& append(InputIterator begin, InputIterator end, const codecvt_type& cvt);
|
Chris@16
|
322
|
Chris@16
|
323 // ----- modifiers -----
|
Chris@16
|
324
|
Chris@16
|
325 void clear() { m_pathname.clear(); }
|
Chris@16
|
326 path& make_preferred()
|
Chris@16
|
327 # ifdef BOOST_POSIX_API
|
Chris@16
|
328 { return *this; } // POSIX no effect
|
Chris@16
|
329 # else // BOOST_WINDOWS_API
|
Chris@16
|
330 ; // change slashes to backslashes
|
Chris@16
|
331 # endif
|
Chris@16
|
332 path& remove_filename();
|
Chris@16
|
333 path& replace_extension(const path& new_extension = path());
|
Chris@16
|
334 void swap(path& rhs) { m_pathname.swap(rhs.m_pathname); }
|
Chris@16
|
335
|
Chris@16
|
336 // ----- observers -----
|
Chris@16
|
337
|
Chris@16
|
338 // For operating systems that format file paths differently than directory
|
Chris@16
|
339 // paths, return values from observers are formatted as file names unless there
|
Chris@16
|
340 // is a trailing separator, in which case returns are formatted as directory
|
Chris@16
|
341 // paths. POSIX and Windows make no such distinction.
|
Chris@16
|
342
|
Chris@16
|
343 // Implementations are permitted to return const values or const references.
|
Chris@16
|
344
|
Chris@16
|
345 // The string or path returned by an observer are specified as being formatted
|
Chris@16
|
346 // as "native" or "generic".
|
Chris@16
|
347 //
|
Chris@16
|
348 // For POSIX, these are all the same format; slashes and backslashes are as input and
|
Chris@16
|
349 // are not modified.
|
Chris@16
|
350 //
|
Chris@16
|
351 // For Windows, native: as input; slashes and backslashes are not modified;
|
Chris@16
|
352 // this is the format of the internally stored string.
|
Chris@16
|
353 // generic: backslashes are converted to slashes
|
Chris@16
|
354
|
Chris@16
|
355 // ----- native format observers -----
|
Chris@16
|
356
|
Chris@16
|
357 const string_type& native() const { return m_pathname; } // Throws: nothing
|
Chris@16
|
358 const value_type* c_str() const { return m_pathname.c_str(); } // Throws: nothing
|
Chris@16
|
359
|
Chris@16
|
360 template <class String>
|
Chris@16
|
361 String string() const;
|
Chris@16
|
362
|
Chris@16
|
363 template <class String>
|
Chris@16
|
364 String string(const codecvt_type& cvt) const;
|
Chris@16
|
365
|
Chris@16
|
366 # ifdef BOOST_WINDOWS_API
|
Chris@16
|
367 const std::string string() const { return string(codecvt()); }
|
Chris@16
|
368 const std::string string(const codecvt_type& cvt) const
|
Chris@16
|
369 {
|
Chris@16
|
370 std::string tmp;
|
Chris@16
|
371 if (!m_pathname.empty())
|
Chris@16
|
372 path_traits::convert(&*m_pathname.begin(), &*m_pathname.begin()+m_pathname.size(),
|
Chris@16
|
373 tmp, cvt);
|
Chris@16
|
374 return tmp;
|
Chris@16
|
375 }
|
Chris@16
|
376
|
Chris@16
|
377 // string_type is std::wstring, so there is no conversion
|
Chris@16
|
378 const std::wstring& wstring() const { return m_pathname; }
|
Chris@16
|
379 const std::wstring& wstring(const codecvt_type&) const { return m_pathname; }
|
Chris@16
|
380
|
Chris@16
|
381 # else // BOOST_POSIX_API
|
Chris@16
|
382 // string_type is std::string, so there is no conversion
|
Chris@16
|
383 const std::string& string() const { return m_pathname; }
|
Chris@16
|
384 const std::string& string(const codecvt_type&) const { return m_pathname; }
|
Chris@16
|
385
|
Chris@16
|
386 const std::wstring wstring() const { return wstring(codecvt()); }
|
Chris@16
|
387 const std::wstring wstring(const codecvt_type& cvt) const
|
Chris@16
|
388 {
|
Chris@16
|
389 std::wstring tmp;
|
Chris@16
|
390 if (!m_pathname.empty())
|
Chris@16
|
391 path_traits::convert(&*m_pathname.begin(), &*m_pathname.begin()+m_pathname.size(),
|
Chris@16
|
392 tmp, cvt);
|
Chris@16
|
393 return tmp;
|
Chris@16
|
394 }
|
Chris@16
|
395
|
Chris@16
|
396 # endif
|
Chris@16
|
397
|
Chris@16
|
398 // ----- generic format observers -----
|
Chris@16
|
399
|
Chris@16
|
400 template <class String>
|
Chris@16
|
401 String generic_string() const;
|
Chris@16
|
402
|
Chris@16
|
403 template <class String>
|
Chris@16
|
404 String generic_string(const codecvt_type& cvt) const;
|
Chris@16
|
405
|
Chris@16
|
406 # ifdef BOOST_WINDOWS_API
|
Chris@16
|
407 const std::string generic_string() const { return generic_string(codecvt()); }
|
Chris@16
|
408 const std::string generic_string(const codecvt_type& cvt) const;
|
Chris@16
|
409 const std::wstring generic_wstring() const;
|
Chris@16
|
410 const std::wstring generic_wstring(const codecvt_type&) const { return generic_wstring(); };
|
Chris@16
|
411
|
Chris@16
|
412 # else // BOOST_POSIX_API
|
Chris@16
|
413 // On POSIX-like systems, the generic format is the same as the native format
|
Chris@16
|
414 const std::string& generic_string() const { return m_pathname; }
|
Chris@16
|
415 const std::string& generic_string(const codecvt_type&) const { return m_pathname; }
|
Chris@16
|
416 const std::wstring generic_wstring() const { return wstring(codecvt()); }
|
Chris@16
|
417 const std::wstring generic_wstring(const codecvt_type& cvt) const { return wstring(cvt); }
|
Chris@16
|
418
|
Chris@16
|
419 # endif
|
Chris@16
|
420
|
Chris@16
|
421 // ----- compare -----
|
Chris@16
|
422
|
Chris@16
|
423 int compare(const path& p) const BOOST_NOEXCEPT; // generic, lexicographical
|
Chris@16
|
424 int compare(const std::string& s) const { return compare(path(s)); }
|
Chris@16
|
425 int compare(const value_type* s) const { return compare(path(s)); }
|
Chris@16
|
426
|
Chris@16
|
427 // ----- decomposition -----
|
Chris@16
|
428
|
Chris@16
|
429 path root_path() const;
|
Chris@16
|
430 path root_name() const; // returns 0 or 1 element path
|
Chris@16
|
431 // even on POSIX, root_name() is non-empty() for network paths
|
Chris@16
|
432 path root_directory() const; // returns 0 or 1 element path
|
Chris@16
|
433 path relative_path() const;
|
Chris@16
|
434 path parent_path() const;
|
Chris@16
|
435 path filename() const; // returns 0 or 1 element path
|
Chris@16
|
436 path stem() const; // returns 0 or 1 element path
|
Chris@16
|
437 path extension() const; // returns 0 or 1 element path
|
Chris@16
|
438
|
Chris@16
|
439 // ----- query -----
|
Chris@16
|
440
|
Chris@16
|
441 bool empty() const { return m_pathname.empty(); } // name consistent with std containers
|
Chris@16
|
442 bool has_root_path() const { return has_root_directory() || has_root_name(); }
|
Chris@16
|
443 bool has_root_name() const { return !root_name().empty(); }
|
Chris@16
|
444 bool has_root_directory() const { return !root_directory().empty(); }
|
Chris@16
|
445 bool has_relative_path() const { return !relative_path().empty(); }
|
Chris@16
|
446 bool has_parent_path() const { return !parent_path().empty(); }
|
Chris@16
|
447 bool has_filename() const { return !m_pathname.empty(); }
|
Chris@16
|
448 bool has_stem() const { return !stem().empty(); }
|
Chris@16
|
449 bool has_extension() const { return !extension().empty(); }
|
Chris@16
|
450 bool is_absolute() const
|
Chris@16
|
451 {
|
Chris@16
|
452 # ifdef BOOST_WINDOWS_API
|
Chris@16
|
453 return has_root_name() && has_root_directory();
|
Chris@16
|
454 # else
|
Chris@16
|
455 return has_root_directory();
|
Chris@16
|
456 # endif
|
Chris@16
|
457 }
|
Chris@16
|
458 bool is_relative() const { return !is_absolute(); }
|
Chris@16
|
459
|
Chris@16
|
460 // ----- iterators -----
|
Chris@16
|
461
|
Chris@16
|
462 class iterator;
|
Chris@16
|
463 typedef iterator const_iterator;
|
Chris@16
|
464
|
Chris@16
|
465 iterator begin() const;
|
Chris@16
|
466 iterator end() const;
|
Chris@16
|
467
|
Chris@16
|
468 // ----- static member functions -----
|
Chris@16
|
469
|
Chris@16
|
470 static std::locale imbue(const std::locale& loc);
|
Chris@16
|
471 static const codecvt_type& codecvt();
|
Chris@16
|
472
|
Chris@16
|
473 // ----- deprecated functions -----
|
Chris@16
|
474
|
Chris@16
|
475 # if defined(BOOST_FILESYSTEM_DEPRECATED) && defined(BOOST_FILESYSTEM_NO_DEPRECATED)
|
Chris@16
|
476 # error both BOOST_FILESYSTEM_DEPRECATED and BOOST_FILESYSTEM_NO_DEPRECATED are defined
|
Chris@16
|
477 # endif
|
Chris@16
|
478
|
Chris@16
|
479 # if !defined(BOOST_FILESYSTEM_NO_DEPRECATED)
|
Chris@16
|
480 // recently deprecated functions supplied by default
|
Chris@16
|
481 path& normalize() { return m_normalize(); }
|
Chris@16
|
482 path& remove_leaf() { return remove_filename(); }
|
Chris@16
|
483 path leaf() const { return filename(); }
|
Chris@16
|
484 path branch_path() const { return parent_path(); }
|
Chris@16
|
485 bool has_leaf() const { return !m_pathname.empty(); }
|
Chris@16
|
486 bool has_branch_path() const { return !parent_path().empty(); }
|
Chris@16
|
487 bool is_complete() const { return is_absolute(); }
|
Chris@16
|
488 # endif
|
Chris@16
|
489
|
Chris@16
|
490 # if defined(BOOST_FILESYSTEM_DEPRECATED)
|
Chris@16
|
491 // deprecated functions with enough signature or semantic changes that they are
|
Chris@16
|
492 // not supplied by default
|
Chris@16
|
493 const std::string file_string() const { return string(); }
|
Chris@16
|
494 const std::string directory_string() const { return string(); }
|
Chris@16
|
495 const std::string native_file_string() const { return string(); }
|
Chris@16
|
496 const std::string native_directory_string() const { return string(); }
|
Chris@16
|
497 const string_type external_file_string() const { return native(); }
|
Chris@16
|
498 const string_type external_directory_string() const { return native(); }
|
Chris@16
|
499
|
Chris@16
|
500 // older functions no longer supported
|
Chris@16
|
501 //typedef bool (*name_check)(const std::string & name);
|
Chris@16
|
502 //basic_path(const string_type& str, name_check) { operator/=(str); }
|
Chris@16
|
503 //basic_path(const typename string_type::value_type* s, name_check)
|
Chris@16
|
504 // { operator/=(s);}
|
Chris@16
|
505 //static bool default_name_check_writable() { return false; }
|
Chris@16
|
506 //static void default_name_check(name_check) {}
|
Chris@16
|
507 //static name_check default_name_check() { return 0; }
|
Chris@16
|
508 //basic_path& canonize();
|
Chris@16
|
509 # endif
|
Chris@16
|
510
|
Chris@16
|
511 //--------------------------------------------------------------------------------------//
|
Chris@16
|
512 // class path private members //
|
Chris@16
|
513 //--------------------------------------------------------------------------------------//
|
Chris@16
|
514
|
Chris@16
|
515 private:
|
Chris@16
|
516 # if defined(_MSC_VER)
|
Chris@16
|
517 # pragma warning(push) // Save warning settings
|
Chris@16
|
518 # pragma warning(disable : 4251) // disable warning: class 'std::basic_string<_Elem,_Traits,_Ax>'
|
Chris@16
|
519 # endif // needs to have dll-interface...
|
Chris@16
|
520 /*
|
Chris@16
|
521 m_pathname has the type, encoding, and format required by the native
|
Chris@16
|
522 operating system. Thus for POSIX and Windows there is no conversion for
|
Chris@16
|
523 passing m_pathname.c_str() to the O/S API or when obtaining a path from the
|
Chris@16
|
524 O/S API. POSIX encoding is unspecified other than for dot and slash
|
Chris@16
|
525 characters; POSIX just treats paths as a sequence of bytes. Windows
|
Chris@16
|
526 encoding is UCS-2 or UTF-16 depending on the version.
|
Chris@16
|
527 */
|
Chris@16
|
528 string_type m_pathname; // Windows: as input; backslashes NOT converted to slashes,
|
Chris@16
|
529 // slashes NOT converted to backslashes
|
Chris@16
|
530 # if defined(_MSC_VER)
|
Chris@16
|
531 # pragma warning(pop) // restore warning settings.
|
Chris@16
|
532 # endif
|
Chris@16
|
533
|
Chris@16
|
534 string_type::size_type m_append_separator_if_needed();
|
Chris@16
|
535 // Returns: If separator is to be appended, m_pathname.size() before append. Otherwise 0.
|
Chris@16
|
536 // Note: An append is never performed if size()==0, so a returned 0 is unambiguous.
|
Chris@16
|
537
|
Chris@16
|
538 void m_erase_redundant_separator(string_type::size_type sep_pos);
|
Chris@16
|
539 string_type::size_type m_parent_path_end() const;
|
Chris@16
|
540
|
Chris@16
|
541 path& m_normalize();
|
Chris@16
|
542
|
Chris@16
|
543 // Was qualified; como433beta8 reports:
|
Chris@16
|
544 // warning #427-D: qualified name is not allowed in member declaration
|
Chris@16
|
545 friend class iterator;
|
Chris@16
|
546 friend bool operator<(const path& lhs, const path& rhs);
|
Chris@16
|
547
|
Chris@16
|
548 // see path::iterator::increment/decrement comment below
|
Chris@16
|
549 static void m_path_iterator_increment(path::iterator & it);
|
Chris@16
|
550 static void m_path_iterator_decrement(path::iterator & it);
|
Chris@16
|
551
|
Chris@16
|
552 }; // class path
|
Chris@16
|
553
|
Chris@16
|
554 namespace detail
|
Chris@16
|
555 {
|
Chris@16
|
556 BOOST_FILESYSTEM_DECL
|
Chris@16
|
557 int lex_compare(path::iterator first1, path::iterator last1,
|
Chris@16
|
558 path::iterator first2, path::iterator last2);
|
Chris@16
|
559 }
|
Chris@16
|
560
|
Chris@16
|
561 # ifndef BOOST_FILESYSTEM_NO_DEPRECATED
|
Chris@16
|
562 typedef path wpath;
|
Chris@16
|
563 # endif
|
Chris@16
|
564
|
Chris@16
|
565 //------------------------------------------------------------------------------------//
|
Chris@16
|
566 // class path::iterator //
|
Chris@16
|
567 //------------------------------------------------------------------------------------//
|
Chris@16
|
568
|
Chris@16
|
569 class path::iterator
|
Chris@16
|
570 : public boost::iterator_facade<
|
Chris@16
|
571 path::iterator,
|
Chris@16
|
572 path const,
|
Chris@16
|
573 boost::bidirectional_traversal_tag >
|
Chris@16
|
574 {
|
Chris@16
|
575 private:
|
Chris@16
|
576 friend class boost::iterator_core_access;
|
Chris@16
|
577 friend class boost::filesystem::path;
|
Chris@16
|
578 friend void m_path_iterator_increment(path::iterator & it);
|
Chris@16
|
579 friend void m_path_iterator_decrement(path::iterator & it);
|
Chris@16
|
580
|
Chris@16
|
581 const path& dereference() const { return m_element; }
|
Chris@16
|
582
|
Chris@16
|
583 bool equal(const iterator & rhs) const
|
Chris@16
|
584 {
|
Chris@16
|
585 return m_path_ptr == rhs.m_path_ptr && m_pos == rhs.m_pos;
|
Chris@16
|
586 }
|
Chris@16
|
587
|
Chris@16
|
588 // iterator_facade derived classes don't seem to like implementations in
|
Chris@16
|
589 // separate translation unit dll's, so forward to class path static members
|
Chris@16
|
590 void increment() { m_path_iterator_increment(*this); }
|
Chris@16
|
591 void decrement() { m_path_iterator_decrement(*this); }
|
Chris@16
|
592
|
Chris@16
|
593 path m_element; // current element
|
Chris@16
|
594 const path* m_path_ptr; // path being iterated over
|
Chris@16
|
595 string_type::size_type m_pos; // position of m_element in
|
Chris@16
|
596 // m_path_ptr->m_pathname.
|
Chris@16
|
597 // if m_element is implicit dot, m_pos is the
|
Chris@16
|
598 // position of the last separator in the path.
|
Chris@16
|
599 // end() iterator is indicated by
|
Chris@16
|
600 // m_pos == m_path_ptr->m_pathname.size()
|
Chris@16
|
601 }; // path::iterator
|
Chris@16
|
602
|
Chris@16
|
603 //------------------------------------------------------------------------------------//
|
Chris@16
|
604 // //
|
Chris@16
|
605 // non-member functions //
|
Chris@16
|
606 // //
|
Chris@16
|
607 //------------------------------------------------------------------------------------//
|
Chris@16
|
608
|
Chris@16
|
609 // std::lexicographical_compare would infinately recurse because path iterators
|
Chris@16
|
610 // yield paths, so provide a path aware version
|
Chris@16
|
611 inline bool lexicographical_compare(path::iterator first1, path::iterator last1,
|
Chris@16
|
612 path::iterator first2, path::iterator last2)
|
Chris@16
|
613 { return detail::lex_compare(first1, last1, first2, last2) < 0; }
|
Chris@16
|
614
|
Chris@16
|
615 inline bool operator==(const path& lhs, const path& rhs) {return lhs.compare(rhs) == 0;}
|
Chris@16
|
616 inline bool operator==(const path& lhs, const path::string_type& rhs) {return lhs.compare(rhs) == 0;}
|
Chris@16
|
617 inline bool operator==(const path::string_type& lhs, const path& rhs) {return rhs.compare(lhs) == 0;}
|
Chris@16
|
618 inline bool operator==(const path& lhs, const path::value_type* rhs) {return lhs.compare(rhs) == 0;}
|
Chris@16
|
619 inline bool operator==(const path::value_type* lhs, const path& rhs) {return rhs.compare(lhs) == 0;}
|
Chris@16
|
620
|
Chris@16
|
621 inline bool operator!=(const path& lhs, const path& rhs) {return lhs.compare(rhs) != 0;}
|
Chris@16
|
622 inline bool operator!=(const path& lhs, const path::string_type& rhs) {return lhs.compare(rhs) != 0;}
|
Chris@16
|
623 inline bool operator!=(const path::string_type& lhs, const path& rhs) {return rhs.compare(lhs) != 0;}
|
Chris@16
|
624 inline bool operator!=(const path& lhs, const path::value_type* rhs) {return lhs.compare(rhs) != 0;}
|
Chris@16
|
625 inline bool operator!=(const path::value_type* lhs, const path& rhs) {return rhs.compare(lhs) != 0;}
|
Chris@16
|
626
|
Chris@16
|
627 // TODO: why do == and != have additional overloads, but the others don't?
|
Chris@16
|
628
|
Chris@16
|
629 inline bool operator<(const path& lhs, const path& rhs) {return lhs.compare(rhs) < 0;}
|
Chris@16
|
630 inline bool operator<=(const path& lhs, const path& rhs) {return !(rhs < lhs);}
|
Chris@16
|
631 inline bool operator> (const path& lhs, const path& rhs) {return rhs < lhs;}
|
Chris@16
|
632 inline bool operator>=(const path& lhs, const path& rhs) {return !(lhs < rhs);}
|
Chris@16
|
633
|
Chris@16
|
634 inline std::size_t hash_value(const path& x)
|
Chris@16
|
635 {
|
Chris@16
|
636 # ifdef BOOST_WINDOWS_API
|
Chris@16
|
637 std::size_t seed = 0;
|
Chris@16
|
638 for(const path::value_type* it = x.c_str(); *it; ++it)
|
Chris@16
|
639 hash_combine(seed, *it == '/' ? L'\\' : *it);
|
Chris@16
|
640 return seed;
|
Chris@16
|
641 # else // BOOST_POSIX_API
|
Chris@16
|
642 return hash_range(x.native().begin(), x.native().end());
|
Chris@16
|
643 # endif
|
Chris@16
|
644 }
|
Chris@16
|
645
|
Chris@16
|
646 inline void swap(path& lhs, path& rhs) { lhs.swap(rhs); }
|
Chris@16
|
647
|
Chris@16
|
648 inline path operator/(const path& lhs, const path& rhs) { return path(lhs) /= rhs; }
|
Chris@16
|
649
|
Chris@16
|
650 // inserters and extractors
|
Chris@16
|
651 // use boost::io::quoted() to handle spaces in paths
|
Chris@16
|
652 // use '&' as escape character to ease use for Windows paths
|
Chris@16
|
653
|
Chris@16
|
654 template <class Char, class Traits>
|
Chris@16
|
655 inline std::basic_ostream<Char, Traits>&
|
Chris@16
|
656 operator<<(std::basic_ostream<Char, Traits>& os, const path& p)
|
Chris@16
|
657 {
|
Chris@16
|
658 return os
|
Chris@16
|
659 << boost::io::quoted(p.template string<std::basic_string<Char> >(), static_cast<Char>('&'));
|
Chris@16
|
660 }
|
Chris@16
|
661
|
Chris@16
|
662 template <class Char, class Traits>
|
Chris@16
|
663 inline std::basic_istream<Char, Traits>&
|
Chris@16
|
664 operator>>(std::basic_istream<Char, Traits>& is, path& p)
|
Chris@16
|
665 {
|
Chris@16
|
666 std::basic_string<Char> str;
|
Chris@16
|
667 is >> boost::io::quoted(str, static_cast<Char>('&'));
|
Chris@16
|
668 p = str;
|
Chris@16
|
669 return is;
|
Chris@16
|
670 }
|
Chris@16
|
671
|
Chris@16
|
672 // name_checks
|
Chris@16
|
673
|
Chris@16
|
674 // These functions are holdovers from version 1. It isn't clear they have much
|
Chris@16
|
675 // usefulness, or how to generalize them for later versions.
|
Chris@16
|
676
|
Chris@16
|
677 BOOST_FILESYSTEM_DECL bool portable_posix_name(const std::string & name);
|
Chris@16
|
678 BOOST_FILESYSTEM_DECL bool windows_name(const std::string & name);
|
Chris@16
|
679 BOOST_FILESYSTEM_DECL bool portable_name(const std::string & name);
|
Chris@16
|
680 BOOST_FILESYSTEM_DECL bool portable_directory_name(const std::string & name);
|
Chris@16
|
681 BOOST_FILESYSTEM_DECL bool portable_file_name(const std::string & name);
|
Chris@16
|
682 BOOST_FILESYSTEM_DECL bool native(const std::string & name);
|
Chris@16
|
683
|
Chris@16
|
684 //--------------------------------------------------------------------------------------//
|
Chris@16
|
685 // class path member template implementation //
|
Chris@16
|
686 //--------------------------------------------------------------------------------------//
|
Chris@16
|
687
|
Chris@16
|
688 template <class InputIterator>
|
Chris@16
|
689 path& path::append(InputIterator begin, InputIterator end, const codecvt_type& cvt)
|
Chris@16
|
690 {
|
Chris@16
|
691 if (begin == end)
|
Chris@16
|
692 return *this;
|
Chris@16
|
693 string_type::size_type sep_pos(m_append_separator_if_needed());
|
Chris@16
|
694 std::basic_string<typename std::iterator_traits<InputIterator>::value_type>
|
Chris@16
|
695 s(begin, end);
|
Chris@16
|
696 path_traits::convert(s.c_str(), s.c_str()+s.size(), m_pathname, cvt);
|
Chris@16
|
697 if (sep_pos)
|
Chris@16
|
698 m_erase_redundant_separator(sep_pos);
|
Chris@16
|
699 return *this;
|
Chris@16
|
700 }
|
Chris@16
|
701
|
Chris@16
|
702 template <class Source>
|
Chris@16
|
703 path& path::append(Source const& source, const codecvt_type& cvt)
|
Chris@16
|
704 {
|
Chris@16
|
705 if (path_traits::empty(source))
|
Chris@16
|
706 return *this;
|
Chris@16
|
707 string_type::size_type sep_pos(m_append_separator_if_needed());
|
Chris@16
|
708 path_traits::dispatch(source, m_pathname, cvt);
|
Chris@16
|
709 if (sep_pos)
|
Chris@16
|
710 m_erase_redundant_separator(sep_pos);
|
Chris@16
|
711 return *this;
|
Chris@16
|
712 }
|
Chris@16
|
713
|
Chris@16
|
714 //--------------------------------------------------------------------------------------//
|
Chris@16
|
715 // class path member template specializations //
|
Chris@16
|
716 //--------------------------------------------------------------------------------------//
|
Chris@16
|
717
|
Chris@16
|
718 template <> inline
|
Chris@16
|
719 std::string path::string<std::string>() const
|
Chris@16
|
720 { return string(); }
|
Chris@16
|
721
|
Chris@16
|
722 template <> inline
|
Chris@16
|
723 std::wstring path::string<std::wstring>() const
|
Chris@16
|
724 { return wstring(); }
|
Chris@16
|
725
|
Chris@16
|
726 template <> inline
|
Chris@16
|
727 std::string path::string<std::string>(const codecvt_type& cvt) const
|
Chris@16
|
728 { return string(cvt); }
|
Chris@16
|
729
|
Chris@16
|
730 template <> inline
|
Chris@16
|
731 std::wstring path::string<std::wstring>(const codecvt_type& cvt) const
|
Chris@16
|
732 { return wstring(cvt); }
|
Chris@16
|
733
|
Chris@16
|
734 template <> inline
|
Chris@16
|
735 std::string path::generic_string<std::string>() const
|
Chris@16
|
736 { return generic_string(); }
|
Chris@16
|
737
|
Chris@16
|
738 template <> inline
|
Chris@16
|
739 std::wstring path::generic_string<std::wstring>() const
|
Chris@16
|
740 { return generic_wstring(); }
|
Chris@16
|
741
|
Chris@16
|
742 template <> inline
|
Chris@16
|
743 std::string path::generic_string<std::string>(const codecvt_type& cvt) const
|
Chris@16
|
744 { return generic_string(cvt); }
|
Chris@16
|
745
|
Chris@16
|
746 template <> inline
|
Chris@16
|
747 std::wstring path::generic_string<std::wstring>(const codecvt_type& cvt) const
|
Chris@16
|
748 { return generic_wstring(cvt); }
|
Chris@16
|
749
|
Chris@16
|
750
|
Chris@16
|
751 } // namespace filesystem
|
Chris@16
|
752 } // namespace boost
|
Chris@16
|
753
|
Chris@16
|
754 //----------------------------------------------------------------------------//
|
Chris@16
|
755
|
Chris@16
|
756 #include <boost/config/abi_suffix.hpp> // pops abi_prefix.hpp pragmas
|
Chris@16
|
757
|
Chris@16
|
758 #endif // BOOST_FILESYSTEM_PATH_HPP
|