summaryrefslogtreecommitdiff
path: root/libbridge/src/main/cpp/helpers.cpp
diff options
context:
space:
mode:
authorCzarek Nakamoto <cyjan@mrcyjanek.net>2024-01-02 00:26:21 +0100
committerCzarek Nakamoto <cyjan@mrcyjanek.net>2024-01-02 00:26:21 +0100
commit8af549fea9dbcb945c3098d850aa1fa15a6735d5 (patch)
treee3c59456a443f604745bfd50c7c793871593d9b1 /libbridge/src/main/cpp/helpers.cpp
parenta1e087b61726e0a82f4f1250f15ff82f7edf56f8 (diff)
More functions ported over to monero_c.
Some leftovers are still present - but shouldn't contain anything that is required by anonero
Diffstat (limited to 'libbridge/src/main/cpp/helpers.cpp')
-rw-r--r--libbridge/src/main/cpp/helpers.cpp64
1 files changed, 61 insertions, 3 deletions
diff --git a/libbridge/src/main/cpp/helpers.cpp b/libbridge/src/main/cpp/helpers.cpp
index c19c39d..ffb96f2 100644
--- a/libbridge/src/main/cpp/helpers.cpp
+++ b/libbridge/src/main/cpp/helpers.cpp
@@ -14,6 +14,7 @@ const char* vectorToString(const std::vector<std::string>& vec, const std::strin
std::string result;
for (const auto& str : vec) {
result += str;
+ result += separator;
}
const char* cstr = result.c_str();
return cstr;
@@ -27,7 +28,7 @@ const char* vectorToString(const std::vector<uint32_t>& vec, const std::string s
size += snprintf(nullptr, 0, "%u", vec[i]);
// Add comma and space for all elements except the last one
if (i < vec.size() - 1) {
- size += 2; // comma and space
+ size += separator.size(); // comma and space
}
}
@@ -46,8 +47,43 @@ const char* vectorToString(const std::vector<uint32_t>& vec, const std::string s
current += written;
// Add comma and space for all elements except the last one
if (i < vec.size() - 1) {
- strcpy(current, ", ");
- current += 2;
+ strcpy(current, separator.c_str());
+ current += separator.size();
+ }
+ }
+
+ return result;
+}
+
+const char* vectorToString(const std::vector<uint64_t>& vec, const std::string separator) {
+ // Calculate the size needed for the result string
+ size_t size = 0;
+ for (size_t i = 0; i < vec.size(); ++i) {
+ // Calculate the number of digits in each element
+ size += snprintf(nullptr, 0, "%lu", vec[i]);
+ // Add comma and space for all elements except the last one
+ if (i < vec.size() - 1) {
+ size += separator.size(); // comma and space
+ }
+ }
+
+ // Allocate memory for the result string
+ char* result = static_cast<char*>(malloc(size + 1));
+ if (result == nullptr) {
+ // Handle memory allocation failure
+ return nullptr;
+ }
+
+ // Fill in the result string
+ char* current = result;
+ for (size_t i = 0; i < vec.size(); ++i) {
+ // Convert each element to string and copy to the result string
+ int written = snprintf(current, size + 1, "%lu", vec[i]);
+ current += written;
+ // Add comma and space for all elements except the last one
+ if (i < vec.size() - 1) {
+ strcpy(current, separator.c_str());
+ current += separator.size();
}
}
@@ -84,3 +120,25 @@ const char* vectorToString(const std::vector<std::set<uint32_t>>& vec, const std
memcpy(buffer, str.c_str(), size + 1);
return buffer;
}
+
+// Function to convert std::set<uint32_t> to a string
+const char* vectorToString(const std::set<uint32_t>& intSet, const std::string separator) {
+ // Check if the set is empty
+ if (intSet.empty()) {
+ return "";
+ }
+
+ // Use a stringstream to concatenate elements with commas
+ std::ostringstream oss;
+ auto it = intSet.begin();
+ oss << *it;
+ for (++it; it != intSet.end(); ++it) {
+ oss << ", " << *it;
+ }
+
+ std::string str = oss.str();
+ const std::string::size_type size = str.size();
+ char *buffer = new char[size + 1]; //we need extra char for NUL
+ memcpy(buffer, str.c_str(), size + 1);
+ return buffer;
+}