EM: On duplicates use device index

For some platforms the PSU have the same address
but a different bus. To fix this, change the duplicate
names over to using index.

Tested: EM had no duplicate PSU numbers

Change-Id: Ibf9c7fe1b75a6420c858de194346433781d8f389
Signed-off-by: James Feist <james.feist@linux.intel.com>
diff --git a/src/Utils.cpp b/src/Utils.cpp
index 81fa3d0..c92aabc 100644
--- a/src/Utils.cpp
+++ b/src/Utils.cpp
@@ -150,31 +150,39 @@
 // finds the template character (currently set to $) and replaces the value with
 // the field found in a dbus object i.e. $ADDRESS would get populated with the
 // ADDRESS field from a object on dbus
-void templateCharReplace(
+std::optional<std::string> templateCharReplace(
     nlohmann::json::iterator& keyPair,
     const boost::container::flat_map<std::string, BasicVariantType>&
         foundDevice,
-    const size_t foundDeviceIdx)
+    const size_t foundDeviceIdx, const std::optional<std::string>& replaceStr)
 {
+    std::optional<std::string> ret = std::nullopt;
+
     if (keyPair.value().type() == nlohmann::json::value_t::object ||
         keyPair.value().type() == nlohmann::json::value_t::array)
     {
         for (auto nextLayer = keyPair.value().begin();
              nextLayer != keyPair.value().end(); nextLayer++)
         {
-            templateCharReplace(nextLayer, foundDevice, foundDeviceIdx);
+            templateCharReplace(nextLayer, foundDevice, foundDeviceIdx,
+                                replaceStr);
         }
-        return;
+        return ret;
     }
 
     std::string* strPtr = keyPair.value().get_ptr<std::string*>();
     if (strPtr == nullptr)
     {
-        return;
+        return ret;
     }
 
     boost::replace_all(*strPtr, std::string(templateChar) + "index",
                        std::to_string(foundDeviceIdx));
+    if (replaceStr)
+    {
+        boost::replace_all(*strPtr, *replaceStr,
+                           std::to_string(foundDeviceIdx));
+    }
 
     for (auto& foundDevicePair : foundDevice)
     {
@@ -193,7 +201,7 @@
             {
                 std::visit([&](auto&& val) { keyPair.value() = val; },
                            foundDevicePair.second);
-                return;
+                return ret;
             }
             else if (nextItemIdx > strPtr->size() ||
                      std::find(mathChars.begin(), mathChars.end(),
@@ -315,8 +323,21 @@
                 }
                 isOperator = !isOperator;
             }
+
             std::string result = prefix + std::to_string(number);
 
+            std::string replaced(find.begin(), find.end());
+            for (auto it2 = split.begin(); it2 != split.end(); it2++)
+            {
+                replaced += " ";
+                replaced += *it2;
+                if (it2 == it)
+                {
+                    break;
+                }
+            }
+            ret = replaced;
+
             if (it != split.end())
             {
                 for (; it != split.end(); it++)
@@ -335,7 +356,7 @@
     strPtr = keyPair.value().get_ptr<std::string*>();
     if (strPtr == nullptr)
     {
-        return;
+        return ret;
     }
 
     // convert hex numbers to ints
@@ -369,4 +390,5 @@
         {
         }
     }
+    return ret;
 }