Wildmeshing Toolkit
Loading...
Searching...
No Matches
PathResolver.cpp
Go to the documentation of this file.
1#include "PathResolver.hpp"
2#include "json_utils.hpp"
3
4
5namespace fs = std::filesystem;
7
8std::pair<std::filesystem::path, bool> PathResolver::try_resolving_path(
9 const std::filesystem::path& potential_base,
10 const std::filesystem::path& path)
11{
12 if (path.is_absolute()) {
13 return {path, true};
14 }
15
16 const fs::path root_abs = fs::absolute(potential_base);
17
18 if (!fs::is_directory(root_abs)) {
19 return {path, false};
20 }
21
22
23 const fs::path resolved_path = fs::weakly_canonical(potential_base / path);
24
25 return {resolved_path, fs::exists(resolved_path)};
26}
27
29{
30 Impl() = default;
31 Impl(Impl&&) = default;
32 Impl(const Impl&) = default;
33 Impl& operator=(Impl&&) = default;
34 Impl& operator=(const Impl&) = default;
35
36 void add_path(const std::filesystem::path& path) { m_paths.emplace_back(path); }
37
38 std::pair<std::filesystem::path, bool> resolve(const std::filesystem::path& path) const
39 {
40 for (const auto& p : m_paths) {
41 auto res = try_resolving_path(p, path);
42 const auto& [new_path, succeeded] = res;
43 if (succeeded) {
44 return res;
45 }
46 }
47 return {path, false};
48 }
49
50
51 std::vector<std::filesystem::path> m_paths;
52};
53
54
55PathResolver::PathResolver()
56 : PathResolver(".")
57{}
58
59PathResolver::PathResolver(const std::filesystem::path& path)
60 : m_impl(std::make_unique<Impl>())
61{
62 m_impl->add_path(path);
63}
64PathResolver::PathResolver(PathResolver&& o) = default;
65PathResolver::PathResolver(const PathResolver& o)
66 : m_impl(std::make_unique<Impl>(*o.m_impl))
67{}
68PathResolver& PathResolver::operator=(PathResolver&&) = default;
69PathResolver& PathResolver::operator=(const PathResolver& o)
70{
71 *m_impl = *o.m_impl;
72 return *this;
73}
74PathResolver::~PathResolver() = default;
75
76
77void PathResolver::clear_paths()
78{
79 m_impl->m_paths.clear();
80}
81void PathResolver::add_path(const std::filesystem::path& path)
82{
83 m_impl->add_path(path);
84}
85
86std::pair<std::filesystem::path, bool> PathResolver::resolve(
87 const std::filesystem::path& path) const
88{
89 return m_impl->resolve(path);
90}
91
92std::vector<std::filesystem::path> PathResolver::get_paths() const
93{
94 return m_impl->m_paths;
95}
96
98{
99 nlohmann_json_j = nlohmann_json_t.m_impl->m_paths;
100}
102{
103 nlohmann_json_t.m_impl->m_paths.clear();
104 if (nlohmann_json_j.is_string()) {
105 nlohmann_json_t.add_path(nlohmann_json_j.get<std::filesystem::path>());
106 } else if (nlohmann_json_j.is_array()) {
107 for (const auto& p : nlohmann_json_j) {
108 nlohmann_json_t.add_path(nlohmann_json_j.get<std::filesystem::path>());
109 }
110 }
111}
112
113} // namespace wmtk::components::utils
static std::pair< std::filesystem::path, bool > try_resolving_path(const std::filesystem::path &potential_base, const std::filesystem::path &path)
#define WMTK_NLOHMANN_JSON_FRIEND_TO_JSON_PROTOTYPE(Type)
#define WMTK_NLOHMANN_JSON_FRIEND_FROM_JSON_PROTOTYPE(Type)
std::vector< std::filesystem::path > m_paths
Impl & operator=(const Impl &)=default
void add_path(const std::filesystem::path &path)
std::pair< std::filesystem::path, bool > resolve(const std::filesystem::path &path) const