Add a level of indirection when naming the DBUS objects

The current design ignores the itemX_label sysfs attribute, if the
environment variable is populated with the MODE_itemX="label", then
fetch the label from itemX_label file.The actual label for the dbus
object is fetched from the environment variable LABEL_item<label>.

Resolves openbmc/openbmc#1633

Change-Id: I0d4baaa91073dd5db75ac62277d78ad9b3065e64
Signed-off-by: Tom Joseph <tomjoseph@in.ibm.com>
diff --git a/env.cpp b/env.cpp
index d77200e..5745e16 100644
--- a/env.cpp
+++ b/env.cpp
@@ -14,8 +14,10 @@
  * limitations under the License.
  */
 
-#include <string>
 #include <cstdlib>
+#include <fstream>
+#include <string>
+#include "hwmon.hpp"
 #include "sensorset.hpp"
 
 std::string getEnv(
@@ -37,4 +39,45 @@
     return value;
 }
 
+std::string getIndirectLabelEnv(
+    const char* prefix, std::string path, const SensorSet::key_type& sensor)
+{
+    std::string key;
+    std::string value;
+
+    path.append(sensor.first);
+    path.append(sensor.second);
+    path.append(1, '_');
+    path.append(hwmon::entry::label);
+
+    std::ifstream handle(path.c_str());
+    if (handle.fail())
+    {
+        return value;
+    }
+
+    std::string content(
+        (std::istreambuf_iterator<char>(handle)),
+        (std::istreambuf_iterator<char>()));
+
+    if (content.empty())
+    {
+        return value;
+    }
+
+    content.pop_back();
+
+    key.assign(prefix);
+    key.append(1, '_');
+    key.append(sensor.first);
+    key.append(content);
+    auto env = getenv(key.c_str());
+    if (env)
+    {
+        value.assign(env);
+    }
+
+    return value;
+}
+
 // vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4