Wildmeshing Toolkit
resolve_path.cpp
Go to the documentation of this file.
1 #include "resolve_path.hpp"
2 
3 #include <filesystem>
4 #include <wmtk/utils/Logger.hpp>
5 
6 namespace fs = std::filesystem;
7 
8 namespace wmtk::components::utils {
9 
10 fs::path resolve_path(const fs::path& root, const fs::path& path)
11 {
12  if (path.is_absolute()) {
13  return path;
14  }
15 
16  const fs::path root_abs = fs::absolute(root);
17 
18  const fs::path root_dir =
19  fs::exists(root_abs) && !fs::is_directory(root_abs) ? root_abs.parent_path() : root_abs;
20 
21 
22  const fs::path resolved_path = fs::weakly_canonical(root_dir / path);
23 
24  return resolved_path;
25 }
26 
27 fs::path resolve_paths(const fs::path& root, const std::initializer_list<fs::path>& paths)
28 {
29  fs::path p_ret = root;
30 
31  for (const fs::path& p : paths) {
32  p_ret = resolve_path(p_ret, p);
33  }
34 
35  return p_ret;
36 }
37 
38 std::filesystem::path resolve_path_if_not_empty(
39  const std::filesystem::path& root,
40  const std::filesystem::path& path)
41 {
42  if (path.empty()) {
43  return path;
44  }
45 
46  return resolve_path(path, root);
47 }
48 
49 } // namespace wmtk::components::utils
fs::path resolve_path(const fs::path &root, const fs::path &path)
fs::path resolve_paths(const fs::path &root, const std::initializer_list< fs::path > &paths)
std::filesystem::path resolve_path_if_not_empty(const std::filesystem::path &root, const std::filesystem::path &path)
Wraps resolve_path() but returns path directly if it is empty.