summaryrefslogtreecommitdiff
path: root/monero_libwallet2_api_c/src/main/cpp/helpers.cpp
blob: d77995ba3c94d02720404d606d93dda32bd00fb8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#include <inttypes.h>
#include <unistd.h>
#include <stdint.h>
#include <stdbool.h>
#include <stddef.h>
#include <vector>
#include <string>
#include "helpers.hpp"
#include <set>
#include <sstream>
#include <cstring>

const char* vectorToString(const std::vector<std::string>& vec, const std::string separator) {
    // Concatenate all strings in the vector
    std::string result;
    for (const auto& str : vec) {
        result += str;
        result += separator;
    }
    const char* cstr = result.c_str();
    return cstr;
}

const char* vectorToString(const std::vector<uint32_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, "%u", 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, "%u", 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();
        }
    }

    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();
        }
    }

    return result;
}

const char* vectorToString(const std::vector<std::set<uint32_t>>& vec, const std::string separator) {
    // Check if the vector is empty
    if (vec.empty()) {
        return "";
    }

    // Use a stringstream to concatenate sets with commas and individual elements with spaces
    std::ostringstream oss;
    oss << "{";
    for (auto it = vec.begin(); it != vec.end(); ++it) {
        if (it != vec.begin()) {
            oss << separator;
        }

        oss << "{";
        for (auto setIt = it->begin(); setIt != it->end(); ++setIt) {
            if (setIt != it->begin()) {
                oss << separator;
            }
            oss << *setIt;
        }
        oss << "}";
    }
    oss << "}";
    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;
}

// 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;
}

std::set<std::string> splitString(const std::string& str, const std::string& delim) {
    std::set<std::string> tokens;
    if (str.empty()) return tokens;
    size_t pos = 0;
    std::string token;
    std::string content = str;  // Copy of str so we can safely erase content
    while ((pos = content.find(delim)) != std::string::npos) {
        token = content.substr(0, pos);
        tokens.insert(token);
        content.erase(0, pos + delim.length());
    }
    tokens.insert(content);  // Inserting the last token
    return tokens;
}

std::vector<uint64_t> splitStringUint(const std::string& str, const std::string& delim) {
    std::vector<uint64_t> tokens;
    if (str.empty()) return tokens;
    size_t pos = 0;
    std::string token;
    std::string content = str;  // Copy of str so we can safely erase content
    while ((pos = content.find(delim)) != std::string::npos) {
        token = content.substr(0, pos);
        tokens.push_back(std::stoull(token));  // Convert string to uint64_t and push to vector
        content.erase(0, pos + delim.length());
    }
    tokens.push_back(std::stoull(content));  // Inserting the last token
    return tokens;
}