Add length limit for user-defined values

Previously, the only limit for most user-defined values like id or name
was the one enforced by dbus, which was fairly big. In order to save
space used by persistent data, new meson options were added, with length
limit for those fields.

This directly impacts following dbus interfaces:
- TriggerManager.AddTrigger:
  - Id
  - Name
  - Reports
  - Thresholds (only name of discrete threshold)
- Trigger.Name
- Trigger.Reports
- Trigger.Thresholds (only name of discrete threshold)
- ReportManager.AddReport(FutureVersion):
  - Id
  - Name
  - MetricParameters (metricId)
- Report.Name
- Report.ReadingParameters (metricId)

For Id fields we support 'prefixes', which also are limited, but those
limit are separate. So if limit for prefix is set to 5 and limit for
id/name is set to 5, following Ids are fine:
- 12345/12345
- 12345
and following are not:
- 123456/1234
- 1/123456

Testing done:
- UTs are passing.
- new limits are reflected when calling mentioned dbus interfaces.

Signed-off-by: Szymon Dompke <szymon.dompke@intel.com>
Change-Id: I29291a1cc56a344d92fb65491c9186fdb90a8529
diff --git a/src/utils/make_id_name.cpp b/src/utils/make_id_name.cpp
new file mode 100644
index 0000000..0df6c17
--- /dev/null
+++ b/src/utils/make_id_name.cpp
@@ -0,0 +1,122 @@
+#include "utils/make_id_name.hpp"
+
+#include "utils/dbus_path_utils.hpp"
+
+#include <sdbusplus/exception.hpp>
+
+#include <algorithm>
+#include <system_error>
+
+namespace utils
+{
+namespace details
+{
+
+size_t countDigits(size_t value)
+{
+    size_t result = 1;
+    while (value >= 10)
+    {
+        ++result;
+        value /= 10;
+    }
+    return result;
+}
+
+std::string generateId(std::string_view idIn, std::string_view nameIn,
+                       std::string_view defaultName,
+                       const std::vector<std::string>& conflictIds)
+{
+    verifyIdCharacters(idIn);
+    verifyIdPrefixes(idIn);
+
+    if (!idIn.empty() && !idIn.ends_with('/'))
+    {
+        if (std::find(conflictIds.begin(), conflictIds.end(), idIn) !=
+            conflictIds.end())
+        {
+            throw sdbusplus::exception::SdBusError(
+                static_cast<int>(std::errc::file_exists), "Duplicated id");
+        }
+        return std::string(idIn);
+    }
+
+    const std::string prefixes(idIn);
+
+    std::string strippedId(nameIn);
+    if (strippedId.find_first_of(utils::constants::allowedCharactersInPath) ==
+        std::string::npos)
+    {
+        strippedId = defaultName;
+    }
+    strippedId.erase(
+        std::remove_if(
+            strippedId.begin(), strippedId.end(),
+            [](char c) {
+                return c == '/' ||
+                       utils::constants::allowedCharactersInPath.find(c) ==
+                           std::string_view::npos;
+            }),
+        strippedId.end());
+
+    size_t idx = 0;
+    std::string tmpId =
+        prefixes + strippedId.substr(0, constants::maxIdNameLength);
+
+    while (std::find(conflictIds.begin(), conflictIds.end(), tmpId) !=
+           conflictIds.end())
+    {
+        size_t digitsInIdx = countDigits(idx);
+
+        if (digitsInIdx > constants::maxIdNameLength)
+        {
+            throw sdbusplus::exception::SdBusError(
+                static_cast<int>(std::errc::file_exists),
+                "Unique indices are depleted");
+        }
+
+        tmpId = prefixes +
+                strippedId.substr(0, constants::maxIdNameLength - digitsInIdx) +
+                std::to_string(idx);
+        ++idx;
+    }
+
+    return tmpId;
+}
+
+} // namespace details
+
+std::pair<std::string, std::string>
+    makeIdName(std::string_view id, std::string_view name,
+               std::string_view defaultName,
+               const std::vector<std::string>& conflictIds)
+{
+    if (name.length() > constants::maxIdNameLength)
+    {
+        throw sdbusplus::exception::SdBusError(
+            static_cast<int>(std::errc::invalid_argument), "Name too long");
+    }
+
+    if (name.empty() && !id.ends_with('/'))
+    {
+        name = id;
+
+        if (auto pos = name.find_last_of("/"); pos != std::string::npos)
+        {
+            name = name.substr(pos + 1);
+        }
+
+        name = name.substr(0, constants::maxIdNameLength);
+    }
+
+    if (name.empty())
+    {
+        name = defaultName;
+    }
+
+    return std::make_pair(
+        details::generateId(id, name, defaultName, conflictIds),
+        std::string{name});
+}
+
+} // namespace utils