Get ignored interfaces from environment

Add several functions to get a comma separated string from environment,
and parse the string into a set.

Tested: Verify the unit tests passes.

Signed-off-by: Lei YU <yulei.sh@bytedance.com>
Change-Id: I0dd7270d9882cf6fa3ce73d8eac516ae42c61fc0
diff --git a/util.cpp b/util.cpp
index 67a9345..ee76cc3 100644
--- a/util.cpp
+++ b/util.cpp
@@ -9,6 +9,7 @@
 #include <sys/wait.h>
 
 #include <algorithm>
+#include <cctype>
 #include <cstdlib>
 #include <cstring>
 #include <filesystem>
@@ -17,6 +18,7 @@
 #include <nlohmann/json.hpp>
 #include <phosphor-logging/elog-errors.hpp>
 #include <phosphor-logging/log.hpp>
+#include <sstream>
 #include <stdexcept>
 #include <stdplus/raw.hpp>
 #include <string>
@@ -91,6 +93,49 @@
     }
 }
 
+/** @brief Get ignored interfaces from environment */
+std::string getIgnoredInterfacesEnv()
+{
+    auto r = std::getenv("IGNORED_INTERFACES");
+    if (r == nullptr)
+    {
+        return {};
+    }
+    return r;
+}
+
+/** @brief Parse the comma separated interface names */
+std::set<std::string> parseInterfaces(const std::string& interfaces)
+{
+    std::set<std::string> result;
+    std::stringstream ss(interfaces);
+    while (ss.good())
+    {
+        std::string str;
+        std::getline(ss, str, ',');
+        // Trim str
+        if (!str.empty())
+        {
+            str.erase(
+                std::remove_if(str.begin(), str.end(),
+                               [](unsigned char c) { return std::isspace(c); }),
+                str.end());
+        }
+        if (!str.empty())
+        {
+            result.insert(str);
+        }
+    }
+    return result;
+}
+
+/** @brief Get the ignored interfaces */
+const std::set<std::string>& getIgnoredInterfaces()
+{
+    static auto ignoredInterfaces = parseInterfaces(getIgnoredInterfacesEnv());
+    return ignoredInterfaces;
+}
+
 } // namespace internal
 
 uint8_t toCidr(int addressFamily, const std::string& subnetMask)