Use std::filesystem::path::relative_path()
Use that function instead of making a relative path manually.
Change-Id: I64908353295a10af09baa91bab5d1c3a0152e994
Signed-off-by: Matt Spinler <spinler@us.ibm.com>
diff --git a/sysfs.cpp b/sysfs.cpp
index 09fc26b..7fdadd0 100644
--- a/sysfs.cpp
+++ b/sysfs.cpp
@@ -162,18 +162,11 @@
return emptyString;
}
-std::string findHwmonFromOFPath(std::string ofNode)
+std::string findHwmonFromOFPath(const std::string& ofNode)
{
static constexpr auto hwmonRoot = "/sys/class/hwmon";
- // Can't append an absolute path
- if (!ofNode.empty() && (ofNode.front() == '/'))
- {
- ofNode = ofNode.substr(1);
- }
-
- fs::path fullOfPath{ofRoot};
- fullOfPath /= ofNode;
+ auto fullOfPath = fs::path(ofRoot) / fs::path(ofNode).relative_path();
for (const auto& hwmonInst : fs::directory_iterator(hwmonRoot))
{
@@ -209,16 +202,10 @@
return emptyString;
}
-std::string findHwmonFromDevPath(std::string devPath)
+std::string findHwmonFromDevPath(const std::string& devPath)
{
- // Can't append an absolute path
- if (!devPath.empty() && devPath.front() == '/')
- {
- devPath = devPath.substr(1);
- }
-
fs::path p{"/sys"};
- p /= devPath;
+ p /= fs::path(devPath).relative_path();
p /= "hwmon";
try
diff --git a/sysfs.hpp b/sysfs.hpp
index e93db65..c1799f6 100644
--- a/sysfs.hpp
+++ b/sysfs.hpp
@@ -50,7 +50,7 @@
* @returns[in] - The hwmon instance path or an empty
* string if no match is found.
*/
-std::string findHwmonFromOFPath(std::string ofNode);
+std::string findHwmonFromOFPath(const std::string& ofNode);
/** @brief Find hwmon instances from a device path
*
@@ -63,7 +63,7 @@
* @return - The hwmon instance path or an empty
* string if no match is found.
*/
-std::string findHwmonFromDevPath(std::string devPath);
+std::string findHwmonFromDevPath(const std::string& devPath);
/** @brief Return the path to use for a call out.
*