Minor path/string op tweaks

Store the hwmon instance and hwmon classpath root
separately.

Use string::assign(char) for where appropriate.

Change-Id: I4ba756a262b9efee7a31610bf01c014974d27af3
Signed-off-by: Brad Bishop <bradleyb@fuzziesquirrel.com>
diff --git a/mainloop.cpp b/mainloop.cpp
index 1bef288..31b02ac 100644
--- a/mainloop.cpp
+++ b/mainloop.cpp
@@ -15,7 +15,6 @@
  */
 #include <iostream>
 #include <memory>
-#include <cstring>
 #include <cstdlib>
 #include <chrono>
 #include <algorithm>
@@ -23,7 +22,6 @@
 #include "hwmon.hpp"
 #include "sysfs.hpp"
 #include "mainloop.hpp"
-#include "util.hpp"
 #include "env.hpp"
 #include "thresholds.hpp"
 
@@ -151,15 +149,27 @@
     : _bus(std::move(bus)),
       _manager(sdbusplus::server::manager::manager(_bus, root)),
       _shutdown(false),
-      _path(path),
+      _hwmonRoot(),
+      _instance(),
       _prefix(prefix),
       _root(root),
       state()
 {
-    if (_path.back() == '/')
+    std::string p = path;
+    while (!p.empty() && p.back() == '/')
     {
-        _path.pop_back();
+        p.pop_back();
     }
+
+    auto n = p.rfind('/');
+    if (n != std::string::npos)
+    {
+        _instance.assign(p.substr(n + 1));
+        _hwmonRoot.assign(p.substr(0, n));
+    }
+
+    assert(!_instance.empty());
+    assert(!_hwmonRoot.empty());
 }
 
 void MainLoop::shutdown() noexcept
@@ -170,7 +180,8 @@
 void MainLoop::run()
 {
     // Check sysfs for available sensors.
-    auto sensors = std::make_unique<SensorSet>(_path);
+    std::string hwmonPath = _hwmonRoot + '/' + _instance;
+    auto sensors = std::make_unique<SensorSet>(hwmonPath);
 
     for (auto& i : *sensors)
     {
@@ -190,13 +201,13 @@
         }
 
         std::string objectPath{_root};
-        objectPath.append("/");
+        objectPath.append(1, '/');
         objectPath.append(getNamespace(attrs));
-        objectPath.append("/");
+        objectPath.append(1, '/');
         objectPath.append(label);
 
         ObjectInfo info(&_bus, std::move(objectPath), Object());
-        auto valueInterface = addValue(i.first, _path, info);
+        auto valueInterface = addValue(i.first, hwmonPath, info);
         auto sensorValue = valueInterface->value();
         addThreshold<WarningObject>(i.first, sensorValue, info);
         addThreshold<CriticalObject>(i.first, sensorValue, info);
@@ -214,9 +225,9 @@
     }
 
     {
-        auto copy = std::unique_ptr<char, phosphor::utility::Free<char>>(strdup(
-                        _path.c_str()));
-        auto busname = std::string(_prefix) + '.' + basename(copy.get());
+        std::string busname{_prefix};
+        busname.append(1, '.');
+        busname.append(_instance);
         _bus.request_name(busname.c_str());
     }
 
@@ -234,7 +245,7 @@
             {
                 // Read value from sensor.
                 int value = 0;
-                read_sysfs(make_sysfs_path(_path,
+                read_sysfs(make_sysfs_path(hwmonPath,
                                            i.first.first, i.first.second,
                                            hwmon::entry::input),
                            value);
diff --git a/mainloop.hpp b/mainloop.hpp
index c5038f7..f48e590 100644
--- a/mainloop.hpp
+++ b/mainloop.hpp
@@ -61,8 +61,10 @@
         sdbusplus::server::manager::manager _manager;
         /** @brief Shutdown requested. */
         volatile bool _shutdown;
-        /** @brief Path to hwmon sysfs instance. */
-        std::string _path;
+        /** @brief hwmon sysfs class path. */
+        std::string _hwmonRoot;
+        /** @brief hwmon sysfs instance. */
+        std::string _instance;
         /** @brief DBus busname prefix. */
         const char* _prefix;
         /** @brief DBus sensors namespace root. */