1 // Class experimental::filesystem::path -*- C++ -*-
3 // Copyright (C) 2014-2018 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 namespace fs
= std::experimental::filesystem
;
34 fs::filesystem_error::~filesystem_error() = default;
36 constexpr path::value_type
path::preferred_separator
[[gnu::used
]];
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
);
57 path::replace_filename(const path
& replacement
)
60 operator/=(replacement
);
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
);
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] != '.')
83 _M_pathname
+= replacement
.native();
90 template<typename Iter1
, typename Iter2
>
91 int do_compare(Iter1 begin1
, Iter1 end1
, Iter2 begin2
, Iter2 end2
)
94 while (begin1
!= end1
&& begin2
!= end2
)
96 if (begin1
->native() < begin2
->native())
98 if (begin1
->native() > begin2
->native())
115 path::compare(const path
& p
) const noexcept
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());
137 return _M_pathname
.compare(p
._M_pathname
);
141 path::root_name() const
144 if (_M_type
== _Type::_Root_name
)
146 else if (_M_cmpts
.size() && _M_cmpts
.begin()->_M_type
== _Type::_Root_name
)
147 __ret
= *_M_cmpts
.begin();
152 path::root_directory() const
155 if (_M_type
== _Type::_Root_dir
)
157 else if (!_M_cmpts
.empty())
159 auto __it
= _M_cmpts
.begin();
160 if (__it
->_M_type
== _Type::_Root_name
)
162 if (__it
!= _M_cmpts
.end() && __it
->_M_type
== _Type::_Root_dir
)
170 path::root_path() const
173 if (_M_type
== _Type::_Root_name
|| _M_type
== _Type::_Root_dir
)
175 else if (!_M_cmpts
.empty())
177 auto __it
= _M_cmpts
.begin();
178 if (__it
->_M_type
== _Type::_Root_name
)
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
)
194 path::relative_path() const
197 if (_M_type
== _Type::_Filename
)
199 else if (!_M_cmpts
.empty())
201 auto __it
= _M_cmpts
.begin();
202 if (__it
->_M_type
== _Type::_Root_name
)
204 if (__it
!= _M_cmpts
.end() && __it
->_M_type
== _Type::_Root_dir
)
206 if (__it
!= _M_cmpts
.end())
207 __ret
.assign(_M_pathname
.substr(__it
->_M_pos
));
213 path::parent_path() const
216 if (_M_cmpts
.size() < 2)
218 for (auto __it
= _M_cmpts
.begin(), __end
= std::prev(_M_cmpts
.end());
219 __it
!= __end
; ++__it
)
227 path::has_root_name() const
229 if (_M_type
== _Type::_Root_name
)
231 if (!_M_cmpts
.empty() && _M_cmpts
.begin()->_M_type
== _Type::_Root_name
)
237 path::has_root_directory() const
239 if (_M_type
== _Type::_Root_dir
)
241 if (!_M_cmpts
.empty())
243 auto __it
= _M_cmpts
.begin();
244 if (__it
->_M_type
== _Type::_Root_name
)
246 if (__it
!= _M_cmpts
.end() && __it
->_M_type
== _Type::_Root_dir
)
253 path::has_root_path() const
255 if (_M_type
== _Type::_Root_name
|| _M_type
== _Type::_Root_dir
)
257 if (!_M_cmpts
.empty())
259 auto __type
= _M_cmpts
.front()._M_type
;
260 if (__type
== _Type::_Root_name
|| __type
== _Type::_Root_dir
)
267 path::has_relative_path() const
269 if (_M_type
== _Type::_Filename
)
271 if (!_M_cmpts
.empty())
273 auto __it
= _M_cmpts
.begin();
274 if (__it
->_M_type
== _Type::_Root_name
)
276 if (__it
!= _M_cmpts
.end() && __it
->_M_type
== _Type::_Root_dir
)
278 if (__it
!= _M_cmpts
.end())
286 path::has_parent_path() const
288 return _M_cmpts
.size() > 1;
292 path::has_filename() const
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
)
304 else if (!_M_cmpts
.empty())
306 const auto& c
= _M_cmpts
.back();
307 if (c
._M_type
== _Type::_Filename
)
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
};
320 return { s
, 0 }; // filename is like ".?"
322 return { s
, s
->rfind('.') };
329 path::_M_split_cmpts()
331 _M_type
= _Type::_Multi
;
334 if (_M_pathname
.empty())
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])
348 // entire path is just "//"
349 _M_type
= _Type::_Root_name
;
353 if (!_S_is_dir_sep(_M_pathname
[2]))
355 // got root name, find its end
357 while (pos
< len
&& !_S_is_dir_sep(_M_pathname
[pos
]))
359 _M_add_root_name(pos
);
360 if (pos
< len
) // also got root directory
361 _M_add_root_dir(pos
);
365 // got something like "///foo" which is just a root directory
366 // composed of multiple redundant directory separators
370 else // got root directory
374 #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
375 else if (len
> 1 && _M_pathname
[1] == L
':')
377 // got disk designator
379 if (len
> 2 && _S_is_dir_sep(_M_pathname
[2]))
388 if (_S_is_dir_sep(_M_pathname
[pos
]))
391 _M_add_filename(back
, pos
- back
);
399 _M_add_filename(back
, pos
- back
);
400 else if (_S_is_dir_sep(_M_pathname
.back()))
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
);
416 path::_M_add_root_name(size_t n
)
418 _M_cmpts
.emplace_back(_M_pathname
.substr(0, n
), _Type::_Root_name
, 0);
422 path::_M_add_root_dir(size_t pos
)
424 _M_cmpts
.emplace_back(_M_pathname
.substr(pos
, 1), _Type::_Root_dir
, pos
);
428 path::_M_add_filename(size_t pos
, size_t n
)
430 _M_cmpts
.emplace_back(_M_pathname
.substr(pos
, n
), _Type::_Filename
, pos
);
436 if (_M_cmpts
.size() == 1)
438 _M_type
= _M_cmpts
.front()._M_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
457 return _Cvt
<wchar_t>::_S_convert(__ws
.data(), __ws
.data() + __ws
.size());
460 return {__first
, __last
};
465 fs::hash_value(const path
& p
) noexcept
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
474 for (const auto& x
: p
)
476 seed
^= std::hash
<path::string_type
>()(x
.native()) + 0x9e3779b9
477 + (seed
<<6) + (seed
>>2);
484 _GLIBCXX_BEGIN_NAMESPACE_VERSION
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(),
502 _GLIBCXX_END_NAMESPACE_CXX11
503 } // namespace experimental::filesystem::v1
505 _GLIBCXX_END_NAMESPACE_VERSION