From e50e941581f48e4157cc4e6cd89f442b8025ca29 Mon Sep 17 00:00:00 2001
From: stedif <stefano.difrischia@inaf.it>
Date: Wed, 13 Apr 2022 15:22:11 +0200
Subject: [PATCH] L2SS-711: align boolean function with Tango value function

---
 src/PqxxExtension.hpp | 33 +++++++++++++++++++++++++++++++++
 1 file changed, 33 insertions(+)

diff --git a/src/PqxxExtension.hpp b/src/PqxxExtension.hpp
index bf2caeb..75268f2 100644
--- a/src/PqxxExtension.hpp
+++ b/src/PqxxExtension.hpp
@@ -323,6 +323,17 @@ public:
         // testing only. Copy the str into a std::string so we can work
         // with it more easily.
         std::string in(str + 1, str + (strlen(str) - 1));
+
+        // count commas and add one to deduce elements in the string,
+        // note we reduce string size to remove the brace at each end
+        auto items = std::count(in.begin(), in.end(), ',');
+        value.clear();
+
+        // preallocate all the items in the vector, we can then
+        // simply set each in turn
+        value.resize(items + 1);
+
+        auto element = 0;
         std::string::size_type comma = 0;
 
         // loop and copy out each value from between the separators
@@ -349,7 +360,29 @@ public:
             return {};
 
         // simply use the pqxx utilities for this, rather than reinvent the wheel
+        if(value.dim_y < 2)
+        {
         return "{" + separated_list(",", value.begin(), value.end()) + "}";
+        }
+
+        // In case of image, unwrap the vector. 
+
+        assert(value.dim_x != 0);
+        assert(value.dim_x * value.dim_y <= value.size());
+
+        std::stringstream result;
+        result << "{";
+        for(std::size_t i = 0; i != value.dim_y; ++i)
+        {
+            if (i > 0)
+            {
+                result << ",";
+            }
+
+            result << "{" << separated_list(",", std::next(value.begin(), i * value.dim_x), std::next(value.begin(), (i+1) * value.dim_x)) << "}";
+        }
+        result << "}";
+        return result.str();
     }
 };
 
-- 
GitLab