PR libstdc++/85818 ensure path::preferred_separator is defined
[official-gcc.git] / libstdc++-v3 / src / filesystem / path.cc
blob899d94e0067cd6e2488a56581a7d41708d8a5235
1 // Class experimental::filesystem::path -*- C++ -*-
3 // Copyright (C) 2014-2018 Free Software Foundation, Inc.
4 //
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)
9 // any later version.
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
27 #endif
29 #include <experimental/filesystem>
31 namespace fs = std::experimental::filesystem;
32 using fs::path;
34 fs::filesystem_error::~filesystem_error() = default;
36 constexpr path::value_type path::preferred_separator [[gnu::used]];
38 path&
39 path::remove_filename()
41 if (_M_type == _Type::_Multi)
43 if (!_M_cmpts.empty())
45 auto cmpt = std::prev(_M_cmpts.end());
46 _M_pathname.erase(cmpt->_M_pos);
47 _M_cmpts.erase(cmpt);
48 _M_trim();
51 else
52 clear();
53 return *this;
56 path&
57 path::replace_filename(const path& replacement)
59 remove_filename();
60 operator/=(replacement);
61 return *this;
64 path&
65 path::replace_extension(const path& replacement)
67 auto ext = _M_find_extension();
68 if (ext.first && ext.second != string_type::npos)
70 if (ext.first == &_M_pathname)
71 _M_pathname.erase(ext.second);
72 else
74 const auto& back = _M_cmpts.back();
75 if (ext.first != &back._M_pathname)
76 _GLIBCXX_THROW_OR_ABORT(
77 std::logic_error("path::replace_extension failed"));
78 _M_pathname.erase(back._M_pos + ext.second);
81 if (!replacement.empty() && replacement.native()[0] != '.')
82 _M_pathname += '.';
83 _M_pathname += replacement.native();
84 _M_split_cmpts();
85 return *this;
88 namespace
90 template<typename Iter1, typename Iter2>
91 int do_compare(Iter1 begin1, Iter1 end1, Iter2 begin2, Iter2 end2)
93 int cmpt = 1;
94 while (begin1 != end1 && begin2 != end2)
96 if (begin1->native() < begin2->native())
97 return -cmpt;
98 if (begin1->native() > begin2->native())
99 return +cmpt;
100 ++begin1;
101 ++begin2;
102 ++cmpt;
104 if (begin1 == end1)
106 if (begin2 == end2)
107 return 0;
108 return -cmpt;
110 return +cmpt;
115 path::compare(const path& p) const noexcept
117 struct CmptRef
119 const path* ptr;
120 const string_type& native() const noexcept { return ptr->native(); }
123 if (_M_type == _Type::_Multi && p._M_type == _Type::_Multi)
124 return do_compare(_M_cmpts.begin(), _M_cmpts.end(),
125 p._M_cmpts.begin(), p._M_cmpts.end());
126 else if (_M_type == _Type::_Multi)
128 CmptRef c[1] = { { &p } };
129 return do_compare(_M_cmpts.begin(), _M_cmpts.end(), c, c+1);
131 else if (p._M_type == _Type::_Multi)
133 CmptRef c[1] = { { this } };
134 return do_compare(c, c+1, p._M_cmpts.begin(), p._M_cmpts.end());
136 else
137 return _M_pathname.compare(p._M_pathname);
140 path
141 path::root_name() const
143 path __ret;
144 if (_M_type == _Type::_Root_name)
145 __ret = *this;
146 else if (_M_cmpts.size() && _M_cmpts.begin()->_M_type == _Type::_Root_name)
147 __ret = *_M_cmpts.begin();
148 return __ret;
151 path
152 path::root_directory() const
154 path __ret;
155 if (_M_type == _Type::_Root_dir)
156 __ret = *this;
157 else if (!_M_cmpts.empty())
159 auto __it = _M_cmpts.begin();
160 if (__it->_M_type == _Type::_Root_name)
161 ++__it;
162 if (__it != _M_cmpts.end() && __it->_M_type == _Type::_Root_dir)
163 __ret = *__it;
165 return __ret;
169 path
170 path::root_path() const
172 path __ret;
173 if (_M_type == _Type::_Root_name || _M_type == _Type::_Root_dir)
174 __ret = *this;
175 else if (!_M_cmpts.empty())
177 auto __it = _M_cmpts.begin();
178 if (__it->_M_type == _Type::_Root_name)
180 __ret = *__it++;
181 if (__it != _M_cmpts.end() && __it->_M_type == _Type::_Root_dir)
183 __ret._M_pathname += preferred_separator;
184 __ret._M_split_cmpts();
187 else if (__it->_M_type == _Type::_Root_dir)
188 __ret = *__it;
190 return __ret;
193 path
194 path::relative_path() const
196 path __ret;
197 if (_M_type == _Type::_Filename)
198 __ret = *this;
199 else if (!_M_cmpts.empty())
201 auto __it = _M_cmpts.begin();
202 if (__it->_M_type == _Type::_Root_name)
203 ++__it;
204 if (__it != _M_cmpts.end() && __it->_M_type == _Type::_Root_dir)
205 ++__it;
206 if (__it != _M_cmpts.end())
207 __ret.assign(_M_pathname.substr(__it->_M_pos));
209 return __ret;
212 path
213 path::parent_path() const
215 path __ret;
216 if (_M_cmpts.size() < 2)
217 return __ret;
218 for (auto __it = _M_cmpts.begin(), __end = std::prev(_M_cmpts.end());
219 __it != __end; ++__it)
221 __ret /= *__it;
223 return __ret;
226 bool
227 path::has_root_name() const
229 if (_M_type == _Type::_Root_name)
230 return true;
231 if (!_M_cmpts.empty() && _M_cmpts.begin()->_M_type == _Type::_Root_name)
232 return true;
233 return false;
236 bool
237 path::has_root_directory() const
239 if (_M_type == _Type::_Root_dir)
240 return true;
241 if (!_M_cmpts.empty())
243 auto __it = _M_cmpts.begin();
244 if (__it->_M_type == _Type::_Root_name)
245 ++__it;
246 if (__it != _M_cmpts.end() && __it->_M_type == _Type::_Root_dir)
247 return true;
249 return false;
252 bool
253 path::has_root_path() const
255 if (_M_type == _Type::_Root_name || _M_type == _Type::_Root_dir)
256 return true;
257 if (!_M_cmpts.empty())
259 auto __type = _M_cmpts.front()._M_type;
260 if (__type == _Type::_Root_name || __type == _Type::_Root_dir)
261 return true;
263 return false;
266 bool
267 path::has_relative_path() const
269 if (_M_type == _Type::_Filename)
270 return true;
271 if (!_M_cmpts.empty())
273 auto __it = _M_cmpts.begin();
274 if (__it->_M_type == _Type::_Root_name)
275 ++__it;
276 if (__it != _M_cmpts.end() && __it->_M_type == _Type::_Root_dir)
277 ++__it;
278 if (__it != _M_cmpts.end())
279 return true;
281 return false;
285 bool
286 path::has_parent_path() const
288 return _M_cmpts.size() > 1;
291 bool
292 path::has_filename() const
294 return !empty();
297 std::pair<const path::string_type*, std::size_t>
298 path::_M_find_extension() const
300 const std::string* s = nullptr;
302 if (_M_type != _Type::_Multi)
303 s = &_M_pathname;
304 else if (!_M_cmpts.empty())
306 const auto& c = _M_cmpts.back();
307 if (c._M_type == _Type::_Filename)
308 s = &c._M_pathname;
311 if (s)
313 if (auto sz = s->size())
315 if (sz <= 2 && (*s)[0] == '.')
317 if (sz == 1 || (*s)[1] == '.') // filename is "." or ".."
318 return { s, string_type::npos };
319 else
320 return { s, 0 }; // filename is like ".?"
322 return { s, s->rfind('.') };
325 return {};
328 void
329 path::_M_split_cmpts()
331 _M_type = _Type::_Multi;
332 _M_cmpts.clear();
334 if (_M_pathname.empty())
335 return;
337 size_t pos = 0;
338 const size_t len = _M_pathname.size();
340 // look for root name or root directory
341 if (_S_is_dir_sep(_M_pathname[0]))
343 // look for root name, such as "//" or "//foo"
344 if (len > 1 && _M_pathname[1] == _M_pathname[0])
346 if (len == 2)
348 // entire path is just "//"
349 _M_type = _Type::_Root_name;
350 return;
353 if (!_S_is_dir_sep(_M_pathname[2]))
355 // got root name, find its end
356 pos = 3;
357 while (pos < len && !_S_is_dir_sep(_M_pathname[pos]))
358 ++pos;
359 _M_add_root_name(pos);
360 if (pos < len) // also got root directory
361 _M_add_root_dir(pos);
363 else
365 // got something like "///foo" which is just a root directory
366 // composed of multiple redundant directory separators
367 _M_add_root_dir(0);
370 else // got root directory
371 _M_add_root_dir(0);
372 ++pos;
374 #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
375 else if (len > 1 && _M_pathname[1] == L':')
377 // got disk designator
378 _M_add_root_name(2);
379 if (len > 2 && _S_is_dir_sep(_M_pathname[2]))
380 _M_add_root_dir(2);
381 pos = 2;
383 #endif
385 size_t back = pos;
386 while (pos < len)
388 if (_S_is_dir_sep(_M_pathname[pos]))
390 if (back != pos)
391 _M_add_filename(back, pos - back);
392 back = ++pos;
394 else
395 ++pos;
398 if (back != pos)
399 _M_add_filename(back, pos - back);
400 else if (_S_is_dir_sep(_M_pathname.back()))
402 // [path.itr]/8
403 // "Dot, if one or more trailing non-root slash characters are present."
404 if (_M_cmpts.back()._M_type == _Type::_Filename)
406 const auto& last = _M_cmpts.back();
407 pos = last._M_pos + last._M_pathname.size();
408 _M_cmpts.emplace_back(string_type(1, '.'), _Type::_Filename, pos);
412 _M_trim();
415 void
416 path::_M_add_root_name(size_t n)
418 _M_cmpts.emplace_back(_M_pathname.substr(0, n), _Type::_Root_name, 0);
421 void
422 path::_M_add_root_dir(size_t pos)
424 _M_cmpts.emplace_back(_M_pathname.substr(pos, 1), _Type::_Root_dir, pos);
427 void
428 path::_M_add_filename(size_t pos, size_t n)
430 _M_cmpts.emplace_back(_M_pathname.substr(pos, n), _Type::_Filename, pos);
433 void
434 path::_M_trim()
436 if (_M_cmpts.size() == 1)
438 _M_type = _M_cmpts.front()._M_type;
439 _M_cmpts.clear();
443 path::string_type
444 path::_S_convert_loc(const char* __first, const char* __last,
445 const std::locale& __loc)
447 #if _GLIBCXX_USE_WCHAR_T
448 auto& __cvt = std::use_facet<codecvt<wchar_t, char, mbstate_t>>(__loc);
449 basic_string<wchar_t> __ws;
450 if (!__str_codecvt_in(__first, __last, __ws, __cvt))
451 _GLIBCXX_THROW_OR_ABORT(filesystem_error(
452 "Cannot convert character sequence",
453 std::make_error_code(errc::illegal_byte_sequence)));
454 #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
455 return __ws;
456 #else
457 return _Cvt<wchar_t>::_S_convert(__ws.data(), __ws.data() + __ws.size());
458 #endif
459 #else
460 return {__first, __last};
461 #endif
464 std::size_t
465 fs::hash_value(const path& p) noexcept
467 // [path.non-member]
468 // "If for two paths, p1 == p2 then hash_value(p1) == hash_value(p2)."
469 // Equality works as if by traversing the range [begin(), end()), meaning
470 // e.g. path("a//b") == path("a/b"), so we cannot simply hash _M_pathname
471 // but need to iterate over individual elements. Use the hash_combine from
472 // http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n3876.pdf
473 size_t seed = 0;
474 for (const auto& x : p)
476 seed ^= std::hash<path::string_type>()(x.native()) + 0x9e3779b9
477 + (seed<<6) + (seed>>2);
479 return seed;
482 namespace std
484 _GLIBCXX_BEGIN_NAMESPACE_VERSION
485 namespace filesystem
487 extern string
488 fs_err_concat(const string& __what, const string& __path1,
489 const string& __path2);
490 } // namespace filesystem
492 namespace experimental::filesystem::v1 {
493 _GLIBCXX_BEGIN_NAMESPACE_CXX11
495 std::string filesystem_error::_M_gen_what()
497 using std::filesystem::fs_err_concat;
498 return fs_err_concat(system_error::what(), _M_path1.native(),
499 _M_path2.native());
502 _GLIBCXX_END_NAMESPACE_CXX11
503 } // namespace experimental::filesystem::v1
505 _GLIBCXX_END_NAMESPACE_VERSION
506 } // namespace std