]>
git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/src/filesystem/path.cc
1 // Class filesystem::path -*- C++ -*-
3 // Copyright (C) 2014-2016 Free Software Foundation, Inc.
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 3, or (at your option)
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 // <http://www.gnu.org/licenses/>.
25 #ifndef _GLIBCXX_USE_CXX11_ABI
26 # define _GLIBCXX_USE_CXX11_ABI 1
29 #include <experimental/filesystem>
31 using std::experimental::filesystem::path
;
33 std::experimental::filesystem::filesystem_error::~filesystem_error() = default;
35 constexpr path::value_type
path::preferred_separator
;
38 path::remove_filename()
40 if (_M_type
== _Type::_Multi
)
42 if (!_M_cmpts
.empty())
44 auto cmpt
= std::prev(_M_cmpts
.end());
45 _M_pathname
.erase(cmpt
->_M_pos
);
56 path::replace_filename(const path
& replacement
)
59 operator/=(replacement
);
64 path::replace_extension(const path
& replacement
)
66 auto ext
= _M_find_extension();
67 if (ext
.first
&& ext
.second
!= string_type::npos
)
69 if (ext
.first
== &_M_pathname
)
70 _M_pathname
.erase(ext
.second
);
73 const auto& back
= _M_cmpts
.back();
74 if (ext
.first
!= &back
._M_pathname
)
75 _GLIBCXX_THROW_OR_ABORT(
76 std::logic_error("path::replace_extension failed"));
77 _M_pathname
.erase(back
._M_pos
+ ext
.second
);
80 if (!replacement
.empty() && replacement
.native()[0] != '.')
82 _M_pathname
+= replacement
.native();
89 template<typename Iter1
, typename Iter2
>
90 int do_compare(Iter1 begin1
, Iter1 end1
, Iter2 begin2
, Iter2 end2
)
93 while (begin1
!= end1
&& begin2
!= end2
)
95 if (begin1
->native() < begin2
->native())
97 if (begin1
->native() > begin2
->native())
114 path::compare(const path
& p
) const noexcept
119 const string_type
& native() const noexcept
{ return ptr
->native(); }
122 if (_M_type
== _Type::_Multi
&& p
._M_type
== _Type::_Multi
)
123 return do_compare(_M_cmpts
.begin(), _M_cmpts
.end(),
124 p
._M_cmpts
.begin(), p
._M_cmpts
.end());
125 else if (_M_type
== _Type::_Multi
)
127 CmptRef c
[1] = { { &p
} };
128 return do_compare(_M_cmpts
.begin(), _M_cmpts
.end(), c
, c
+1);
130 else if (p
._M_type
== _Type::_Multi
)
132 CmptRef c
[1] = { { this } };
133 return do_compare(c
, c
+1, p
._M_cmpts
.begin(), p
._M_cmpts
.end());
136 return _M_pathname
.compare(p
._M_pathname
);
140 path::root_name() const
143 if (_M_type
== _Type::_Root_name
)
145 else if (_M_cmpts
.size() && _M_cmpts
.begin()->_M_type
== _Type::_Root_name
)
146 __ret
= *_M_cmpts
.begin();
151 path::root_directory() const
154 if (_M_type
== _Type::_Root_dir
)
156 else if (!_M_cmpts
.empty())
158 auto __it
= _M_cmpts
.begin();
159 if (__it
->_M_type
== _Type::_Root_name
)
161 if (__it
!= _M_cmpts
.end() && __it
->_M_type
== _Type::_Root_dir
)
169 path::root_path() const
172 if (_M_type
== _Type::_Root_name
|| _M_type
== _Type::_Root_dir
)
174 else if (!_M_cmpts
.empty())
176 auto __it
= _M_cmpts
.begin();
177 if (__it
->_M_type
== _Type::_Root_name
)
180 if (__it
!= _M_cmpts
.end() && __it
->_M_type
== _Type::_Root_dir
)
182 __ret
._M_pathname
+= preferred_separator
;
183 __ret
._M_split_cmpts();
186 else if (__it
->_M_type
== _Type::_Root_dir
)
193 path::relative_path() const
196 if (_M_type
== _Type::_Filename
)
198 else if (!_M_cmpts
.empty())
200 auto __it
= _M_cmpts
.begin();
201 if (__it
->_M_type
== _Type::_Root_name
)
203 if (__it
!= _M_cmpts
.end() && __it
->_M_type
== _Type::_Root_dir
)
205 if (__it
!= _M_cmpts
.end())
206 __ret
.assign(_M_pathname
.substr(__it
->_M_pos
));
212 path::parent_path() const
215 if (_M_cmpts
.size() < 2)
217 for (auto __it
= _M_cmpts
.begin(), __end
= std::prev(_M_cmpts
.end());
218 __it
!= __end
; ++__it
)
226 path::has_root_name() const
228 if (_M_type
== _Type::_Root_name
)
230 if (!_M_cmpts
.empty() && _M_cmpts
.begin()->_M_type
== _Type::_Root_name
)
236 path::has_root_directory() const
238 if (_M_type
== _Type::_Root_dir
)
240 if (!_M_cmpts
.empty())
242 auto __it
= _M_cmpts
.begin();
243 if (__it
->_M_type
== _Type::_Root_name
)
245 if (__it
!= _M_cmpts
.end() && __it
->_M_type
== _Type::_Root_dir
)
252 path::has_root_path() const
254 if (_M_type
== _Type::_Root_name
|| _M_type
== _Type::_Root_dir
)
256 if (!_M_cmpts
.empty())
258 auto __type
= _M_cmpts
.front()._M_type
;
259 if (__type
== _Type::_Root_name
|| __type
== _Type::_Root_dir
)
266 path::has_relative_path() const
268 if (_M_type
== _Type::_Filename
)
270 if (!_M_cmpts
.empty())
272 auto __it
= _M_cmpts
.begin();
273 if (__it
->_M_type
== _Type::_Root_name
)
275 if (__it
!= _M_cmpts
.end() && __it
->_M_type
== _Type::_Root_dir
)
277 if (__it
!= _M_cmpts
.end())
285 path::has_parent_path() const
287 return _M_cmpts
.size() > 1;
291 path::has_filename() const
296 std::pair
<const path::string_type
*, std::size_t>
297 path::_M_find_extension() const
299 const std::string
* s
= nullptr;
301 if (_M_type
!= _Type::_Multi
)
303 else if (!_M_cmpts
.empty())
305 const auto& c
= _M_cmpts
.back();
306 if (c
._M_type
== _Type::_Filename
)
312 if (auto sz
= s
->size())
314 if (sz
<= 2 && (*s
)[0] == '.')
316 if (sz
== 1 || (*s
)[1] == '.') // filename is "." or ".."
317 return { s
, string_type::npos
};
319 return { s
, 0 }; // filename is like ".?"
321 return { s
, s
->rfind('.') };
328 path::_M_split_cmpts()
330 _M_type
= _Type::_Multi
;
333 if (_M_pathname
.empty())
337 const size_t len
= _M_pathname
.size();
339 // look for root name or root directory
340 if (_S_is_dir_sep(_M_pathname
[0]))
342 // look for root name, such as "//" or "//foo"
343 if (len
> 1 && _M_pathname
[1] == _M_pathname
[0])
347 // entire path is just "//"
348 _M_type
= _Type::_Root_name
;
352 if (!_S_is_dir_sep(_M_pathname
[2]))
354 // got root name, find its end
356 while (pos
< len
&& !_S_is_dir_sep(_M_pathname
[pos
]))
358 _M_add_root_name(pos
);
359 if (pos
< len
) // also got root directory
360 _M_add_root_dir(pos
);
364 // got something like "///foo" which is just a root directory
365 // composed of multiple redundant directory separators
369 else // got root directory
373 #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
374 else if (len
> 1 && _M_pathname
[1] == L
':')
376 // got disk designator
378 if (len
> 2 && _S_is_dir_sep(_M_pathname
[2]))
387 if (_S_is_dir_sep(_M_pathname
[pos
]))
390 _M_add_filename(back
, pos
- back
);
398 _M_add_filename(back
, pos
- back
);
399 else if (_S_is_dir_sep(_M_pathname
.back()))
402 // "Dot, if one or more trailing non-root slash characters are present."
403 if (_M_cmpts
.back()._M_type
== _Type::_Filename
)
405 const auto& last
= _M_cmpts
.back();
406 pos
= last
._M_pos
+ last
._M_pathname
.size();
407 _M_cmpts
.emplace_back(string_type(1, '.'), _Type::_Filename
, pos
);
415 path::_M_add_root_name(size_t n
)
417 _M_cmpts
.emplace_back(_M_pathname
.substr(0, n
), _Type::_Root_name
, 0);
421 path::_M_add_root_dir(size_t pos
)
423 _M_cmpts
.emplace_back(_M_pathname
.substr(pos
, 1), _Type::_Root_dir
, pos
);
427 path::_M_add_filename(size_t pos
, size_t n
)
429 _M_cmpts
.emplace_back(_M_pathname
.substr(pos
, n
), _Type::_Filename
, pos
);
435 if (_M_cmpts
.size() == 1)
437 _M_type
= _M_cmpts
.front()._M_type
;
443 path::_S_convert_loc(const char* __first
, const char* __last
,
444 const std::locale
& __loc
)
446 #if _GLIBCXX_USE_WCHAR_T
447 auto& __cvt
= std::use_facet
<codecvt
<wchar_t, char, mbstate_t>>(__loc
);
448 basic_string
<wchar_t> __ws
;
449 if (!__str_codecvt_in(__first
, __last
, __ws
, __cvt
))
450 _GLIBCXX_THROW_OR_ABORT(filesystem_error(
451 "Cannot convert character sequence",
452 std::make_error_code(errc::illegal_byte_sequence
)));
453 #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
456 return _Cvt
<wchar_t>::_S_convert(__ws
.data(), __ws
.data() + __ws
.size());
459 return {__first
, __last
};
464 std::experimental::filesystem::hash_value(const path
& p
) noexcept
467 // "If for two paths, p1 == p2 then hash_value(p1) == hash_value(p2)."
468 // Equality works as if by traversing the range [begin(), end()), meaning
469 // e.g. path("a//b") == path("a/b"), so we cannot simply hash _M_pathname
470 // but need to iterate over individual elements. Use the hash_combine from
471 // http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n3876.pdf
473 for (const auto& x
: p
)
475 seed
^= std::hash
<path::string_type
>()(x
.native()) + 0x9e3779b9
476 + (seed
<<6) + (seed
>>2);