summaryrefslogtreecommitdiff
path: root/monero_libwallet2_api_c/src/main/cpp/helpers.cpp
blob: 324c12dd90761f0982d6c9c2198c0f17aa7ade1a (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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
#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>
#include <thread>
#include <iostream>
#include <stdexcept>

#ifdef __ANDROID__
#include <android/log.h>

#define LOG_TAG "moneroc"
#define BUFFER_SIZE 1024*32

static int stdoutToLogcat(const char *buf, int size) {
    __android_log_write(ANDROID_LOG_INFO, LOG_TAG, buf);
    return size;
}

static int stderrToLogcat(const char *buf, int size) {
    __android_log_write(ANDROID_LOG_ERROR, LOG_TAG, buf);
    return size;
}

void redirectStdoutThread(int pipe_stdout[2]) {
    char bufferStdout[BUFFER_SIZE];
    while (true) {
        int read_size = read(pipe_stdout[0], bufferStdout, sizeof(bufferStdout) - 1);
        if (read_size > 0) {
            bufferStdout[read_size] = '\0';
            stdoutToLogcat(bufferStdout, read_size);
        }
    }
}

void redirectStderrThread(int pipe_stderr[2]) {
    char bufferStderr[BUFFER_SIZE];
    while (true) {
        int read_size = read(pipe_stderr[0], bufferStderr, sizeof(bufferStderr) - 1);
        if (read_size > 0) {
            bufferStderr[read_size] = '\0';
            stderrToLogcat(bufferStderr, read_size);
        }
    }
}

void setupAndroidLogging() {
    static int pfdStdout[2];
    static int pfdStderr[2];

    pipe(pfdStdout);
    pipe(pfdStderr);

    dup2(pfdStdout[1], STDOUT_FILENO);
    dup2(pfdStderr[1], STDERR_FILENO);

    std::thread stdoutThread(redirectStdoutThread, pfdStdout);
    std::thread stderrThread(redirectStderrThread, pfdStderr);

    stdoutThread.detach();
    stderrThread.detach();
}

#endif // __ANDROID__

__attribute__((constructor))
void library_init() {
#ifdef __ANDROID__
    setupAndroidLogging();  // This will now run automatically when the library is loaded
#endif
}

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

    // Concatenate all strings in the vector
    std::string result;
    for (size_t i = 0; i < vec.size() - 1; ++i) {
        result += vec[i];
        result += separator;
    }
    result += vec.back();  // Append the last string without the separator

    std::string str = result;
    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;
}

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, "%llu", 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, "%llu", 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<std::string> splitStringVector(const std::string& str, const std::string& delim) {
    std::vector<std::string> tokens;
    if (str.empty()) return tokens;
    size_t pos = 0;
    std::string content = str;  // Copy of str so we can safely erase content
    while ((pos = content.find(delim)) != std::string::npos) {
        tokens.push_back(content.substr(0, pos));
        content.erase(0, pos + delim.length());
    }
    tokens.push_back(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;
}