Wildmeshing Toolkit
PathResolver.cpp
Go to the documentation of this file.
1 #include "PathResolver.hpp"
2 #include "json_utils.hpp"
3 
4 
5 namespace fs = std::filesystem;
6 namespace wmtk::components::utils {
7 
8 std::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 
56  : PathResolver(".")
57 {}
58 
59 PathResolver::PathResolver(const std::filesystem::path& path)
60  : m_impl(std::make_unique<Impl>())
61 {
62  m_impl->add_path(path);
63 }
66  : m_impl(std::make_unique<Impl>(*o.m_impl))
67 {}
70 {
71  *m_impl = *o.m_impl;
72  return *this;
73 }
74 PathResolver::~PathResolver() = default;
75 
76 
78 {
79  m_impl->m_paths.clear();
80 }
81 void PathResolver::add_path(const std::filesystem::path& path)
82 {
83  m_impl->add_path(path);
84 }
85 
86 std::pair<std::filesystem::path, bool> PathResolver::resolve(
87  const std::filesystem::path& path) const
88 {
89  return m_impl->resolve(path);
90 }
91 
92 std::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
void add_path(const std::filesystem::path &path)
static std::pair< std::filesystem::path, bool > try_resolving_path(const std::filesystem::path &potential_base, const std::filesystem::path &path)
Definition: PathResolver.cpp:8
std::vector< std::filesystem::path > get_paths() const
std::pair< std::filesystem::path, bool > resolve(const std::filesystem::path &path) const
PathResolver & operator=(PathResolver &&)
Definition: autodiff.h:995
WMTK_NLOHMANN_JSON_FRIEND_TO_JSON_PROTOTYPE(PathResolver)
WMTK_NLOHMANN_JSON_FRIEND_FROM_JSON_PROTOTYPE(PathResolver)
std::vector< Simplex > make_unique(const Mesh &m, const std::vector< Simplex > &s)
Definition: make_unique.cpp:8
std::pair< std::filesystem::path, bool > resolve(const std::filesystem::path &path) const
std::vector< std::filesystem::path > m_paths
Impl & operator=(const Impl &)=default
void add_path(const std::filesystem::path &path)