-
Notifications
You must be signed in to change notification settings - Fork 73
Expand file tree
/
Copy pathutils.cpp
More file actions
265 lines (233 loc) · 6.68 KB
/
Copy pathutils.cpp
File metadata and controls
265 lines (233 loc) · 6.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: Copyright 2017 Intel Corporation
#include "utils.hpp"
#include <boost/lexical_cast.hpp>
#include <phosphor-logging/lg2.hpp>
#include <sdbusplus/bus/match.hpp>
#include <algorithm>
#include <cctype>
#include <filesystem>
#include <flat_map>
#include <map>
#include <ranges>
#include <regex>
#include <string_view>
#include <system_error>
#include <utility>
namespace fs = std::filesystem;
bool findFiles(const fs::path& dirPath, const std::string& matchString,
std::vector<fs::path>& foundPaths)
{
if (!fs::exists(dirPath))
{
return false;
}
std::regex search(matchString);
std::smatch match;
for (const auto& p : fs::directory_iterator(dirPath))
{
std::string path = p.path().string();
if (std::regex_search(path, match, search))
{
foundPaths.emplace_back(p.path());
}
}
return true;
}
bool findFiles(const std::vector<fs::path>& dirPaths,
const std::string& matchString,
std::vector<fs::path>& foundPaths)
{
std::map<fs::path, fs::path> paths;
std::regex search(matchString);
std::smatch match;
for (const auto& dirPath : dirPaths)
{
if (!fs::exists(dirPath))
{
continue;
}
for (const auto& p : fs::recursive_directory_iterator(dirPath))
{
std::error_code ec;
if (p.is_directory(ec))
{
continue;
}
std::string path = p.path().string();
if (std::regex_search(path, match, search))
{
paths[p.path().filename()] = p.path();
}
}
}
for (const auto& [key, value] : paths)
{
foundPaths.emplace_back(value);
}
return !foundPaths.empty();
}
bool getI2cDevicePaths(const fs::path& dirPath,
std::flat_map<size_t, fs::path>& busPaths)
{
if (!fs::exists(dirPath))
{
return false;
}
// Regex for matching the path
std::regex searchPath(std::string(R"(i2c-\d+$)"));
// Regex for matching the bus numbers
std::regex searchBus(std::string(R"(\w[^-]*$)"));
std::smatch matchPath;
std::smatch matchBus;
for (const auto& p : fs::directory_iterator(dirPath))
{
std::string path = p.path().string();
if (std::regex_search(path, matchPath, searchPath))
{
if (std::regex_search(path, matchBus, searchBus))
{
size_t bus = stoul(*matchBus.begin());
busPaths.insert(std::pair<size_t, fs::path>(bus, p.path()));
}
}
}
return true;
}
/// \brief JSON/DBus matching Callable for std::variant (visitor)
///
/// Default match JSON/DBus match implementation
/// \tparam T The concrete DBus value type from DBusValueVariant
template <typename T>
struct MatchProbe
{
/// \param probe the probe statement to match against
/// \param value the property value being matched to a probe
/// \return true if the dbusValue matched the probe otherwise false
static bool match(const nlohmann::json& probe, const T& value)
{
return probe == value;
}
};
/// \brief JSON/DBus matching Callable for std::variant (visitor)
///
/// std::string specialization of MatchProbe enabling regex matching
template <>
struct MatchProbe<std::string>
{
/// \param probe the probe statement to match against
/// \param value the string value being matched to a probe
/// \return true if the dbusValue matched the probe otherwise false
static bool match(const nlohmann::json& probe, const std::string& value)
{
if (probe.is_string())
{
try
{
std::regex search(probe);
std::smatch regMatch;
return std::regex_search(value, regMatch, search);
}
catch (const std::regex_error&)
{
lg2::error(
"Syntax error in regular expression: {PROBE} will never match",
"PROBE", probe);
}
}
// Skip calling nlohmann here, since it will never match a non-string
// to a std::string
return false;
}
};
/// \brief Forwarding JSON/DBus matching Callable for std::variant (visitor)
///
/// Forward calls to the correct template instantiation of MatchProbe
struct MatchProbeForwarder
{
explicit MatchProbeForwarder(const nlohmann::json& probe) : probeRef(probe)
{}
const nlohmann::json& probeRef;
template <typename T>
bool operator()(const T& dbusValue) const
{
return MatchProbe<T>::match(probeRef, dbusValue);
}
};
bool matchProbe(const nlohmann::json& probe, const DBusValueVariant& dbusValue)
{
return std::visit(MatchProbeForwarder(probe), dbusValue);
}
std::vector<std::string> split(std::string_view str, char delim)
{
std::vector<std::string> out;
size_t start = 0;
while (start <= str.size())
{
size_t end = str.find(delim, start);
if (end == std::string_view::npos)
{
out.emplace_back(str.substr(start));
break;
}
out.emplace_back(str.substr(start, end - start));
start = end + 1;
}
return out;
}
void iReplaceAll(std::string& str, std::string_view search,
std::string_view replace)
{
if (search.empty() || search == replace)
{
return;
}
while (true)
{
std::ranges::subrange<std::string::iterator> match =
iFindFirst(str, search);
if (!match)
{
break;
}
str.replace(match.begin(), match.end(), replace.begin(), replace.end());
}
}
void replaceAll(std::string& str, std::string_view search,
std::string_view replace)
{
if (search.empty())
{
return;
}
size_t pos = 0;
while ((pos = str.find(search, pos)) != std::string::npos)
{
str.replace(pos, search.size(), replace);
pos += replace.size();
}
}
std::string toLowerCopy(std::string_view str)
{
std::string result(str);
std::transform(result.begin(), result.end(), result.begin(),
[](unsigned char c) { return asciiToLower(c); });
return result;
}
int parseMuxDeviceRootBus(std::string_view muxDeviceName)
{
const auto findBus = muxDeviceName.find('-');
if (findBus == std::string_view::npos)
{
return -1;
}
int val = 0;
bool fullMatch = false;
const std::from_chars_result res =
fromCharsWrapper(muxDeviceName.substr(0, findBus), val, fullMatch);
if (res.ec != std::errc{} || !fullMatch)
{
return -1;
}
return val;
}