-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUtils.cpp
More file actions
302 lines (264 loc) · 11.3 KB
/
Utils.cpp
File metadata and controls
302 lines (264 loc) · 11.3 KB
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
#include "KindroidBot.h"
std::string wstringToString(const std::wstring& wstr) {
if (wstr.empty()) return std::string();
int size = WideCharToMultiByte(CP_UTF8, 0, wstr.c_str(), -1, NULL, 0, NULL, NULL);
std::string str(size - 1, 0);
WideCharToMultiByte(CP_UTF8, 0, wstr.c_str(), -1, &str[0], size, NULL, NULL);
return str;
}
std::wstring stringToWstring(const std::string& str) {
if (str.empty()) return std::wstring();
int size = MultiByteToWideChar(CP_UTF8, 0, str.c_str(), -1, NULL, 0);
std::wstring wstr(size - 1, 0);
MultiByteToWideChar(CP_UTF8, 0, str.c_str(), -1, &wstr[0], size);
return wstr;
}
std::string censorString(const std::string& str) {
if (str.length() <= 8) {
return std::string(str.length(), '*');
}
return str.substr(0, 4) + std::string(str.length() - 8, '*') + str.substr(str.length() - 4);
}
std::string getCurrentTimestamp() {
time_t now = time(nullptr);
struct tm* timeinfo = localtime(&now);
char buffer[32];
strftime(buffer, sizeof(buffer), "%H:%M:%S", timeinfo);
return std::string(buffer);
}
static const char base64_chars[] =
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz"
"0123456789+/";
std::string base64Encode(const std::string& input) {
std::string ret;
int i = 0;
int j = 0;
unsigned char char_array_3[3];
unsigned char char_array_4[4];
size_t in_len = input.size();
const unsigned char* bytes_to_encode = (const unsigned char*)input.c_str();
while (in_len--) {
char_array_3[i++] = *(bytes_to_encode++);
if (i == 3) {
char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
char_array_4[3] = char_array_3[2] & 0x3f;
for(i = 0; i < 4; i++)
ret += base64_chars[char_array_4[i]];
i = 0;
}
}
if (i) {
for(j = i; j < 3; j++)
char_array_3[j] = '\0';
char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
for (j = 0; j < i + 1; j++)
ret += base64_chars[char_array_4[j]];
while(i++ < 3)
ret += '=';
}
return ret;
}
// Simple JSON implementation
std::string SimpleJSON::escape(const std::string& str) {
std::string result;
for (size_t i = 0; i < str.length(); i++) {
unsigned char c = (unsigned char)str[i];
switch (c) {
case '"': result += "\\\""; break;
case '\\': result += "\\\\"; break;
case '\b': result += "\\b"; break;
case '\f': result += "\\f"; break;
case '\n': result += "\\n"; break;
case '\r': result += "\\r"; break;
case '\t': result += "\\t"; break;
default:
// Pass through all bytes >= 0x80 (UTF-8 continuation bytes and multibyte chars)
// Only escape control characters < 32
if (c < 32) {
char buf[8];
sprintf(buf, "\\u%04x", c);
result += buf;
} else {
result += (char)c;
}
}
}
return result;
}
std::string SimpleJSON::unescape(const std::string& str) {
std::string result;
for (size_t i = 0; i < str.length(); i++) {
if (str[i] == '\\' && i + 1 < str.length()) {
switch (str[i + 1]) {
case '"': result += '"'; i++; break;
case '\\': result += '\\'; i++; break;
case 'b': result += '\b'; i++; break;
case 'f': result += '\f'; i++; break;
case 'n': result += '\n'; i++; break;
case 'r': result += '\r'; i++; break;
case 't': result += '\t'; i++; break;
case '/': result += '/'; i++; break;
case 'u': {
// Unicode escape: \uXXXX
if (i + 5 < str.length()) {
std::string hex = str.substr(i + 2, 4);
unsigned int codepoint = std::stoul(hex, nullptr, 16);
// Check for surrogate pair (emoji)
if (codepoint >= 0xD800 && codepoint <= 0xDBFF && i + 11 < str.length()
&& str[i + 6] == '\\' && str[i + 7] == 'u') {
// High surrogate followed by low surrogate
std::string hex2 = str.substr(i + 8, 4);
unsigned int low = std::stoul(hex2, nullptr, 16);
if (low >= 0xDC00 && low <= 0xDFFF) {
// Combine surrogates into full codepoint
unsigned int fullCodepoint = 0x10000 + ((codepoint & 0x3FF) << 10) + (low & 0x3FF);
// Convert to UTF-8
if (fullCodepoint <= 0x7F) {
result += (char)fullCodepoint;
} else if (fullCodepoint <= 0x7FF) {
result += (char)(0xC0 | (fullCodepoint >> 6));
result += (char)(0x80 | (fullCodepoint & 0x3F));
} else if (fullCodepoint <= 0xFFFF) {
result += (char)(0xE0 | (fullCodepoint >> 12));
result += (char)(0x80 | ((fullCodepoint >> 6) & 0x3F));
result += (char)(0x80 | (fullCodepoint & 0x3F));
} else {
result += (char)(0xF0 | (fullCodepoint >> 18));
result += (char)(0x80 | ((fullCodepoint >> 12) & 0x3F));
result += (char)(0x80 | ((fullCodepoint >> 6) & 0x3F));
result += (char)(0x80 | (fullCodepoint & 0x3F));
}
i += 11; // Skip both \uXXXX sequences
break;
}
}
// Single codepoint (not surrogate pair) - convert to UTF-8
if (codepoint <= 0x7F) {
result += (char)codepoint;
} else if (codepoint <= 0x7FF) {
result += (char)(0xC0 | (codepoint >> 6));
result += (char)(0x80 | (codepoint & 0x3F));
} else {
result += (char)(0xE0 | (codepoint >> 12));
result += (char)(0x80 | ((codepoint >> 6) & 0x3F));
result += (char)(0x80 | (codepoint & 0x3F));
}
i += 5; // Skip \uXXXX
} else {
result += str[i];
}
break;
}
default: result += str[i];
}
} else {
result += str[i];
}
}
return result;
}
std::string SimpleJSON::buildObject(const std::map<std::string, std::string>& obj) {
std::string result = "{";
bool first = true;
for (const auto& pair : obj) {
if (!first) result += ",";
result += "\"" + pair.first + "\":\"" + escape(pair.second) + "\"";
first = false;
}
result += "}";
return result;
}
std::map<std::string, std::string> SimpleJSON::parseObject(const std::string& json) {
std::map<std::string, std::string> result;
size_t pos = json.find('{');
if (pos == std::string::npos) return result;
size_t end = json.rfind('}');
if (end == std::string::npos) return result;
std::string content = json.substr(pos + 1, end - pos - 1);
// Simple parser - finds "key":"value" pairs
size_t i = 0;
while (i < content.length()) {
// Skip whitespace
while (i < content.length() && (content[i] == ' ' || content[i] == '\n' || content[i] == '\r' || content[i] == '\t')) i++;
if (i >= content.length()) break;
// Find key
if (content[i] == '"') {
i++;
size_t keyStart = i;
while (i < content.length() && content[i] != '"') i++;
std::string key = content.substr(keyStart, i - keyStart);
i++; // skip closing "
// Skip to :
while (i < content.length() && content[i] != ':') i++;
i++; // skip :
// Skip whitespace
while (i < content.length() && (content[i] == ' ' || content[i] == '\n' || content[i] == '\r' || content[i] == '\t')) i++;
// Get value
if (i < content.length() && content[i] == '"') {
i++;
size_t valueStart = i;
while (i < content.length() && content[i] != '"') {
if (content[i] == '\\') i++; // skip escaped char
i++;
}
std::string value = unescape(content.substr(valueStart, i - valueStart));
result[key] = value;
i++; // skip closing "
}
// Skip to comma or end
while (i < content.length() && content[i] != ',' && content[i] != '}') i++;
if (i < content.length() && content[i] == ',') i++;
} else {
i++;
}
}
return result;
}
std::string SimpleJSON::getString(const std::map<std::string, std::string>& obj, const std::string& key) {
auto it = obj.find(key);
return (it != obj.end()) ? it->second : "";
}
std::vector<std::map<std::string, std::string>> SimpleJSON::parseArray(const std::string& json) {
std::vector<std::map<std::string, std::string>> result;
size_t pos = json.find('[');
if (pos == std::string::npos) return result;
size_t end = json.rfind(']');
if (end == std::string::npos) return result;
std::string content = json.substr(pos + 1, end - pos - 1);
// Find objects in array
int depth = 0;
size_t objStart = 0;
bool inString = false;
for (size_t i = 0; i < content.length(); i++) {
if (content[i] == '"' && (i == 0 || content[i-1] != '\\')) {
inString = !inString;
}
if (!inString) {
if (content[i] == '{') {
if (depth == 0) objStart = i;
depth++;
} else if (content[i] == '}') {
depth--;
if (depth == 0) {
std::string obj = content.substr(objStart, i - objStart + 1);
result.push_back(parseObject(obj));
}
}
}
}
return result;
}
std::string SimpleJSON::buildConversationArray(const std::vector<std::map<std::string, std::string>>& conversation) {
std::string result = "[";
for (size_t i = 0; i < conversation.size(); i++) {
if (i > 0) result += ",";
result += buildObject(conversation[i]);
}
result += "]";
return result;
}