From 2943965b225ba824014f144022e5f0352d1e6df8 Mon Sep 17 00:00:00 2001
From: Fabian Peller <fabian.peller-konrad@kit.edu>
Date: Fri, 17 Mar 2023 09:01:14 +0000
Subject: [PATCH] add camel2snake conversion

---
 .../codegenerator/codewriter/cpp/Writer.cpp   | 43 ++++++++++++++++++-
 1 file changed, 41 insertions(+), 2 deletions(-)

diff --git a/source/RobotAPI/libraries/aron/codegeneration/codegenerator/codewriter/cpp/Writer.cpp b/source/RobotAPI/libraries/aron/codegeneration/codegenerator/codewriter/cpp/Writer.cpp
index a62bb8b68..01933af56 100644
--- a/source/RobotAPI/libraries/aron/codegeneration/codegenerator/codewriter/cpp/Writer.cpp
+++ b/source/RobotAPI/libraries/aron/codegeneration/codegenerator/codewriter/cpp/Writer.cpp
@@ -32,6 +32,45 @@
 
 namespace armarx::aron::codegenerator::cpp
 {
+
+    // Function to convert camel case
+    // string to snake case string
+    std::string camelToSnake(const std::string& str)
+    {
+
+        // Empty String
+        std::string result = "";
+
+        // Append first character(in lower case)
+        // to result string
+        char c = std::tolower(str[0]);
+        result+=(char(c));
+
+        // Traverse the string from
+        // ist index to last index
+        for (unsigned int i = 1; i < str.length(); i++) {
+
+            char ch = str[i];
+
+            // Check if the character is upper case
+            // then append '_' and such character
+            // (in lower case) to result string
+            if (std::isupper(ch)) {
+                result = result + '_';
+                result+=char(std::tolower(ch));
+            }
+
+            // If the character is lower case then
+            // add such character into result string
+            else {
+                result = result + ch;
+            }
+        }
+
+        // return the result
+        return result;
+    }
+
     Writer::Writer(const std::string& producerName, const std::vector<std::string>& additionalIncludesFromXMLFile) :
         CodeWriter(producerName, additionalIncludesFromXMLFile)
     {
@@ -361,7 +400,7 @@ namespace armarx::aron::codegenerator::cpp
 
         std::vector<std::string> namespaces = info.getNamespaces();
         std::string rawObjectName = info.getNameWithoutNamespace();
-        namespaces.push_back(simox::alg::to_lower(rawObjectName) + "_details");
+        namespaces.push_back(simox::alg::to_lower(camelToSnake(rawObjectName)) + "_details");
 
         std::string enumdoc = "The enum definition of the enum of the auogenerated class '" + gen.getFullClassCppTypename() + "'.";
         CppEnumPtr e = std::make_shared<CppEnum>(namespaces, "Enum");
@@ -476,7 +515,7 @@ namespace armarx::aron::codegenerator::cpp
 
     void Writer::setupMemberFields(CppClassPtr& c, const std::map<std::string, std::string>& doc_members, const generator::IntEnumClass& o) const
     {
-        auto publicFields = o.getPublicVariableDeclarations(c->getName());
+        auto publicFields = o.getPublicVariableDeclarations(camelToSnake(c->getName()));
         for (const auto& f : publicFields)
         {
             if (auto it = doc_members.find(f->getName()); it != doc_members.end())
-- 
GitLab