Sequoia
Loading...
Searching...
No Matches
FileSystem.hpp
Go to the documentation of this file.
1
2// Copyright Oliver J. Rosten 2021. //
3// Distributed under the GNU GENERAL PUBLIC LICENSE, Version 3.0. //
4// (See accompanying file LICENSE.md or copy at //
5// https://www.gnu.org/licenses/gpl-3.0.en.html) //
7
8#pragma once
9
14#include <filesystem>
15#include <functional>
16
17namespace sequoia
18{
20 {
21 public:
22 normal_path() = default;
23
24 normal_path(const std::filesystem::path& path)
25 : m_Path{path.lexically_normal()}
26 {}
27
28 [[nodiscard]]
29 operator const std::filesystem::path& () const
30 {
31 return m_Path;
32 }
33
34 [[nodiscard]]
35 const std::filesystem::path& path() const noexcept
36 {
37 return m_Path;
38 }
39
40 [[nodiscard]]
41 friend auto operator<=>(const normal_path&, const normal_path&) = default;
42 private:
43 std::filesystem::path m_Path;
44 };
45
46 [[nodiscard]]
47 inline std::filesystem::path back(const std::filesystem::path& p)
48 {
49 if(p.empty())
50 throw std::runtime_error{"Cannot extract final element from an empty path"};
51
52 return *--p.end();
53 }
54
59 template<class Path, class Pattern, class Proj=std::identity>
60 requires std::is_same_v<std::remove_cvref_t<Path>, std::filesystem::path> && std::predicate<std::ranges::equal_to, std::invoke_result_t<Proj, std::filesystem::path> , Pattern>
61 [[nodiscard]]
62 std::conditional_t<std::is_const_v<std::remove_reference_t<Path>>, std::filesystem::path::const_iterator, std::filesystem::path::iterator>
63 rfind(Path&& p, Pattern pattern, Proj proj = {})
64 {
65 auto i{p.end()};
66 while(i != p.begin())
67 {
68 --i;
69 if(std::ranges::equal_to{}(proj(*i), pattern)) return i;
70 }
71
72 return p.end();
73 }
74}
std::conditional_t< std::is_const_v< std::remove_reference_t< Path > >, std::filesystem::path::const_iterator, std::filesystem::path::iterator > rfind(Path &&p, Pattern pattern, Proj proj={})
Definition: FileSystem.hpp:63
Definition: FileSystem.hpp:20