39#if defined(_WIN32) || defined(_WIN64)
47 std::size_t
size() const noexcept {
return paths_.size(); }
50 bool empty() const noexcept {
return paths_.empty(); }
53 const std::filesystem::path&
operator[](std::size_t i)
const {
59 void add(
const std::filesystem::path& dir) {
60 if (dir.empty())
return;
61 for (
const auto& existing : paths_) {
62 if (existing == dir)
return;
64 paths_.push_back(dir);
71 if (dir.empty())
return;
72 for (
auto it = paths_.begin(); it != paths_.end(); ++it) {
78 paths_.insert(paths_.begin(), dir);
82 void clear() noexcept { paths_.clear(); }
86 if (i < paths_.size()) {
87 paths_.erase(paths_.begin() +
static_cast<std::ptrdiff_t
>(i));
92 const std::vector<std::filesystem::path>&
paths() const noexcept {
100 std::optional<std::filesystem::path>
find(
101 const std::filesystem::path& filename)
const {
102 if (filename.empty())
return std::nullopt;
104 for (
const auto& dir : paths_) {
105 auto candidate = dir / filename;
106 if (std::filesystem::exists(candidate, ec)) {
117 const std::filesystem::path& filename)
const {
118 std::vector<std::filesystem::path> hits;
119 if (filename.empty())
return hits;
121 for (
const auto& dir : paths_) {
122 auto candidate = dir / filename;
123 if (std::filesystem::exists(candidate, ec)) {
124 hits.push_back(candidate);
136 for (
const auto& p : paths_) {
137 if (!first) out.push_back(sep);
151 std::size_t start = 0;
152 while (start <= serialized.size()) {
153 std::size_t end = serialized.find(sep, start);
154 if (end == std::string_view::npos) end = serialized.size();
156 auto piece = serialized.substr(start, end - start);
157 add(std::filesystem::path(std::string(piece)));
159 if (end == serialized.size())
break;
165 return paths_ ==
other.paths_;
168 return !(*
this ==
other);
172 std::vector<std::filesystem::path> paths_;
void clear() noexcept
Remove all directories.
Definition file_search_path.hpp:82
FileSearchPath(std::string_view serialized)
Construct from a PATH-style serialized string.
Definition file_search_path.hpp:33
bool operator==(const FileSearchPath &other) const noexcept
Definition file_search_path.hpp:164
void remove(std::size_t i)
Remove the directory at index i. Out-of-range index is a no-op.
Definition file_search_path.hpp:85
void add_at_front(const std::filesystem::path &dir)
Insert dir at the front so it is searched before everything else.
Definition file_search_path.hpp:70
void from_string(std::string_view serialized)
Replace the contents of the search path with the entries parsed from serialized.
Definition file_search_path.hpp:148
bool operator!=(const FileSearchPath &other) const noexcept
Definition file_search_path.hpp:167
std::vector< std::filesystem::path > find_all(const std::filesystem::path &filename) const
Search the path for filename and return ALL matches across every directory, in search order.
Definition file_search_path.hpp:116
bool empty() const noexcept
True when the search path contains no directories.
Definition file_search_path.hpp:50
static constexpr char separator() noexcept
The platform-native PATH separator: : on POSIX, ; on Windows.
Definition file_search_path.hpp:38
void add(const std::filesystem::path &dir)
Append dir to the end of the search list.
Definition file_search_path.hpp:59
std::size_t size() const noexcept
Number of directories in the search path.
Definition file_search_path.hpp:47
std::string to_string() const
Serialize the search path to a PATH-style string using separator().
Definition file_search_path.hpp:132
std::optional< std::filesystem::path > find(const std::filesystem::path &filename) const
Search the path for filename and return the first match, or nullopt if no directory contains it.
Definition file_search_path.hpp:100
const std::filesystem::path & operator[](std::size_t i) const
Access the path at index i (no bounds check beyond size()).
Definition file_search_path.hpp:53
FileSearchPath()=default
Construct an empty search path.
const std::vector< std::filesystem::path > & paths() const noexcept
Read-only view of the directories in search order.
Definition file_search_path.hpp:92
Definition generated_event_source.hpp:33
@ other
Definition udev_monitor.hpp:26