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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
|
/*
Facebook plugin for Miranda Instant Messenger
_____________________________________________
Copyright © 2009-11 Michal Zelinka, 2011-13 Robert Pösel
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "common.h"
std::string utils::url::encode(const std::string &s)
{
return (char*)ptrA(mir_urlEncode(s.c_str()));
}
std::string utils::url::decode(std::string data)
{
// TODO: Better and universal method?
utils::text::replace_all(&data, "%2F", "/");
utils::text::replace_all(&data, "%3F", "?");
utils::text::replace_all(&data, "%3D", "=");
utils::text::replace_all(&data, "%26", "&");
return data;
}
std::string utils::time::unix_timestamp()
{
time_t in = ::time(NULL);
return utils::conversion::to_string((void*)&in, UTILS_CONV_TIME_T);
}
std::string utils::time::mili_timestamp()
{
SYSTEMTIME st;
std::string timestamp = utils::time::unix_timestamp();
GetSystemTime(&st);
timestamp.append(utils::conversion::to_string((void*)&st.wMilliseconds, UTILS_CONV_UNSIGNED_NUMBER));
return timestamp;
}
DWORD utils::time::fix_timestamp(unsigned __int64 mili_timestamp)
{
// If it is really mili_timestamp
if (mili_timestamp > 100000000000) {
return (DWORD) (mili_timestamp / 1000);
}
return (DWORD) mili_timestamp;
}
DWORD utils::conversion::to_timestamp(std::string data)
{
DWORD timestamp = NULL;
if (!utils::conversion::from_string<DWORD>(timestamp, data, std::dec)) {
timestamp = static_cast<DWORD>(::time(NULL));
}
return timestamp;
}
struct tm *utils::conversion::fbtime_to_timeinfo(unsigned __int64 timestamp) {
time_t time = utils::time::fix_timestamp(timestamp);
return localtime(&time);
}
std::string utils::conversion::to_string(void* data, WORD type)
{
std::stringstream out;
switch (type)
{
case UTILS_CONV_BOOLEAN:
out << (data ? "true" : "false");
case UTILS_CONV_TIME_T:
out << (*(time_t*)data);
break;
case UTILS_CONV_SIGNED_NUMBER:
out << (*(signed int*)data);
break;
case UTILS_CONV_UNSIGNED_NUMBER:
out << (*(unsigned int*)data);
break;
}
return out.str();
}
void utils::text::replace_first(std::string* data, std::string from, std::string to)
{
std::string::size_type position = data->find(from);
if (position != std::string::npos)
{
data->replace(position, from.size(), to);
}
}
void utils::text::replace_all(std::string* data, std::string from, std::string to)
{
std::string::size_type position = 0;
while ((position = data->find(from, position)) != std::string::npos)
{
data->replace(position, from.size(), to);
position++;
}
}
unsigned int utils::text::count_all(std::string* data, std::string term)
{
unsigned int count = 0;
std::string::size_type position = 0;
while ((position = data->find(term, position)) != std::string::npos)
{
count++;
position++;
}
return count;
}
void utils::text::append_ordinal(unsigned long value, std::string* data)
{
if (value >= 128 && value <= 2047)
{ // U+0080 .. U+07FF
*data += (char)(192 + (value / 64));
*data += (char)(128 + (value % 64));
}
else if (value >= 2048 && value <= 65535)
{ // U+0800 .. U+FFFF
*data += (char)(224 + (value / 4096));
*data += (char)(128 + ((value / 64) % 64));
*data += (char)(128 + (value % 64));
}
else if (value <= 127)
{ // U+0000 .. U+007F
*data += (char)value;
}
}
std::string utils::text::special_expressions_decode(std::string data)
{
utils::text::replace_all(&data, "&", "&");
utils::text::replace_all(&data, """, "\"");
utils::text::replace_all(&data, "<", "<");
utils::text::replace_all(&data, ">", ">");
utils::text::replace_all(&data, "♥", "\xE2\x99\xA5"); // direct byte replacement
// utils::text::replace_all(&data, "♥", "\\u2665"); // indirect slashu replacement
utils::text::replace_all(&data, "\\/", "/");
utils::text::replace_all(&data, "\\\\", "\\");
// TODO: Add more to comply general usage
// http://www.utexas.edu/learn/html/spchar.html
// http://www.webmonkey.com/reference/Special_Characters
// http://www.degraeve.com/reference/specialcharacters.php
// http://www.chami.com/tips/internet/050798i.html
// http://www.w3schools.com/tags/ref_entities.asp
// http://www.natural-innovations.com/wa/doc-charset.html
// http://webdesign.about.com/library/bl_htmlcodes.htm
std::string new_string = "";
for (std::string::size_type i = 0; i < data.length(); i++)
{
if (data.at(i) == '&' && (i+1) < data.length() && data.at(i+1) == '#')
{
unsigned int udn;
std::string::size_type comma = data.find(";", i);
if (comma != std::string::npos) {
bool hexa = false;
if ((i+2) < data.length() && data.at(i+2) == 'x') {
hexa = true;
i += 3;
} else {
i += 2;
}
udn = strtol(data.substr(i, comma-i).c_str(), NULL, hexa ? 16 : 10);
i = comma;
}
utils::text::append_ordinal(udn, &new_string);
continue;
}
if (data.at(i) == -19 && (i+2) < data.length()) {
std::string chs = data.substr(i, i+2).c_str();
unsigned char a[4] = {0};
memcpy(&a[1], chs.c_str(), 3);
new_string += "\\u";
utils::text::append_ordinal((unsigned long)a, &new_string);
/*u = reinterpret_cast<unsigned char&>(data.at(i+1));
utils::text::append_ordinal(u, &new_string);
u = reinterpret_cast<unsigned char&>(data.at(i+2));
utils::text::append_ordinal(u, &new_string);*/
i += 2;
continue;
}
new_string += data.at(i);
}
return new_string;
}
std::string utils::text::edit_html(std::string data)
{
std::string::size_type end = 0;
std::string::size_type start = 0;
std::string new_string = "";
while (end != std::string::npos)
{
end = data.find("<span class=\\\"text_exposed_hide", start);
if (end != std::string::npos)
{
new_string += data.substr(start, end - start);
start = data.find("<\\/span", end);
} else {
new_string += data.substr(start, data.length() - start);
}
}
start = end = 0;
data = new_string;
new_string = "";
while (end != std::string::npos)
{
end = data.find("<span class=\\\"uiTooltipText", start);
if (end != std::string::npos)
{
new_string += data.substr(start, end - start);
start = data.find("<\\/span", end);
} else {
new_string += data.substr(start, data.length() - start);
}
}
// Remove "Translate" link
start = end = 0;
data = new_string;
new_string = "";
while (end != std::string::npos)
{
end = data.find("translate_story_link\\\">", start);
if (end != std::string::npos)
{
new_string += data.substr(start, end - start);
start = data.find("<\\/div", end);
} else {
new_string += data.substr(start, data.length() - start);
}
}
// Append newline after attachement title
start = new_string.find("class=\\\"uiAttachmentTitle", 0);
if (start != std::string::npos)
{
data = new_string.substr(0, start);
data = utils::text::trim(data);
start = new_string.find(">", start);
if (start != std::string::npos)
new_string.insert(start+1, "\n\n");
start = new_string.find("<\\/div>", start);
if (start != std::string::npos)
new_string.insert(start, "\n");
}
// Append newline between attachement link and description
start = new_string.find("uiAttachmentDesc", 0);
if (start != std::string::npos)
{
start = new_string.find(">", start);
if (start != std::string::npos)
new_string.insert(start+1, "\n");
start = new_string.find("<\\/div>", start);
if (start != std::string::npos)
new_string.insert(start, "\n");
}
utils::text::replace_all(&new_string, "<br \\/>", "\n");
utils::text::replace_all(&new_string, "\n\n\n", "\n\n");
//utils::text::replace_all(&new_string, "\\t", "");
//utils::text::replace_all(&new_string, "\\n", "");
return new_string;
}
std::string utils::text::remove_html(std::string data)
{
std::string new_string = "";
for (std::string::size_type i = 0; i < data.length(); i++)
{
if (data.at(i) == '<' && data.at(i+1) != ' ')
{
i = data.find(">", i);
if (i == std::string::npos)
break;
continue;
}
new_string += data.at(i);
}
return new_string;
}
std::string utils::text::slashu_to_utf8(std::string data)
{
std::string new_string = "";
for (std::string::size_type i = 0; i < data.length(); i++)
{
if (data.at(i) == '\\' && (i+1) < data.length() && data.at(i+1) == 'u')
{
unsigned int udn = strtol(data.substr(i + 2, 4).c_str(), NULL, 16);
append_ordinal(udn, &new_string);
i += 5;
continue;
}
new_string += data.at(i);
}
return new_string;
}
std::string utils::text::trim(std::string data, bool rtrim)
{
std::string spaces = " \t\r\n"; // TODO: include "nbsp"?
std::string::size_type begin = rtrim ? 0 : data.find_first_not_of(spaces);
std::string::size_type end = data.find_last_not_of(spaces) + 1;
return (end != std::string::npos) ? data.substr(begin, end - begin) : "";
}
void utils::text::explode(std::string str, std::string separator, std::vector<std::string>* results)
{
std::string::size_type pos;
pos = str.find_first_of(separator);
while (pos != std::string::npos) {
if (pos > 0) {
results->push_back(str.substr(0,pos));
}
str = str.substr(pos+1);
pos = str.find_first_of(separator);
}
if (str.length() > 0) {
results->push_back(str);
}
}
std::string utils::text::source_get_value(std::string* data, unsigned int argument_count, ...)
{
va_list arg;
std::string ret;
std::string::size_type start = 0, end = 0;
va_start(arg, argument_count);
for (unsigned int i = argument_count; i > 0; i--)
{
if (i == 1)
{
end = data->find(va_arg(arg, char*), start);
if (start == std::string::npos || end == std::string::npos)
break;
ret = data->substr(start, end - start);
} else {
std::string term = va_arg(arg, char*);
start = data->find(term, start);
if (start == std::string::npos)
break;
start += term.length();
}
}
va_end(arg);
return ret;
}
std::string utils::text::source_get_value2(std::string* data, const char *term, const char *endings, bool wholeString)
{
std::string::size_type start = 0, end = 0;
std::string ret;
start = data->find(term);
if (start != std::string::npos) {
start += strlen(term);
end = data->find_first_of(endings, start);
if (end != std::string::npos) {
ret = data->substr(start, end - start);
} else if (wholeString) {
ret = data->substr(start);
}
}
return ret;
}
int utils::number::random()
{
srand(::time(NULL));
return rand();
}
int utils::debug::log(std::string file_name, std::string text)
{
char szFile[MAX_PATH];
GetModuleFileNameA(g_hInstance, szFile, SIZEOF(szFile));
std::string path = szFile;
path = path.substr(0, path.rfind("\\"));
path = path.substr(0, path.rfind("\\") + 1);
path = path + file_name.c_str() + ".txt";
SYSTEMTIME time;
GetLocalTime(&time);
std::ofstream out(path.c_str(), std::ios_base::out | std::ios_base::app | std::ios_base::ate);
out << "[" << (time.wHour < 10 ? "0" : "") << time.wHour << ":" << (time.wMinute < 10 ? "0" : "") << time.wMinute << ":" << (time.wSecond < 10 ? "0" : "") << time.wSecond << "] " << text << std::endl;
out.close();
return EXIT_SUCCESS;
}
void __fastcall utils::mem::detract(char** str)
{
utils::mem::detract((void**)str);
}
void __fastcall utils::mem::detract(void** p)
{
utils::mem::detract((void*)(*p));
}
void __fastcall utils::mem::detract(void* p)
{
mir_free(p);
}
void* __fastcall utils::mem::allocate(size_t size)
{
return mir_calloc(size);
}
struct
{
char *ext;
int fmt;
}
static formats[] = {
{ ".png", PA_FORMAT_PNG },
{ ".jpg", PA_FORMAT_JPEG },
{ ".jpeg", PA_FORMAT_JPEG },
{ ".ico", PA_FORMAT_ICON },
{ ".bmp", PA_FORMAT_BMP },
{ ".gif", PA_FORMAT_GIF },
};
int ext_to_format(const std::string &ext)
{
for(size_t i=0; i<SIZEOF(formats); i++)
{
if(ext == formats[i].ext)
return formats[i].fmt;
}
return PA_FORMAT_UNKNOWN;
}
|