7namespace pixelbullet::filesystem
9[[nodiscard]]
inline std::filesystem::path normalized_path(
const std::filesystem::path& path)
11 std::filesystem::path normalized = path.lexically_normal();
12 while (!normalized.empty() && !normalized.has_filename())
14 const std::filesystem::path parent = normalized.parent_path();
15 if (parent.empty() || parent == normalized)
24[[nodiscard]]
inline bool paths_equal(
const std::filesystem::path& lhs,
const std::filesystem::path& rhs)
26 return normalized_path(lhs) == normalized_path(rhs);
29[[nodiscard]]
inline bool path_is_within(
const std::filesystem::path& path,
const std::filesystem::path& directory)
31 if (path.empty() || directory.empty())
36 const std::filesystem::path normalized_target = normalized_path(path);
37 const std::filesystem::path normalized_directory = normalized_path(directory);
38 auto target_it = normalized_target.begin();
39 for (
auto directory_it = normalized_directory.begin(); directory_it != normalized_directory.end(); ++directory_it, ++target_it)
41 if (target_it == normalized_target.end() || *target_it != *directory_it)
50inline void normalize_sort_unique_paths(std::vector<std::filesystem::path>& paths)
52 for (std::filesystem::path& path : paths)
54 path = normalized_path(path);
57 std::sort(paths.begin(), paths.end());
58 paths.erase(std::unique(paths.begin(), paths.end()), paths.end());