-
Notifications
You must be signed in to change notification settings - Fork 2
/
util.cpp
640 lines (521 loc) · 11 KB
/
util.cpp
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
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
/* $Id: util.cpp 169 2009-10-23 20:17:53Z sausage $
* EOSERV is released under the zlib license.
* See LICENSE.txt for more info.
*/
#include "util.hpp"
#include <algorithm>
#include <ctime>
#include <limits>
#if defined(WIN32) || defined(WIN64)
#include <windows.h>
#endif // defined(WIN32) || defined(WIN64)
namespace util
{
variant::variant()
{
this->SetInt(0);
}
variant::variant(int i)
{
this->SetInt(i);
}
variant::variant(double d)
{
this->SetFloat(d);
}
variant::variant(const std::string &s)
{
this->SetString(s);
}
variant::variant(const char *p)
{
std::string s(p);
this->SetString(s);
}
variant::variant(bool b)
{
this->SetBool(b);
}
int variant::int_length(int x)
{
int count = 1;
int val = 10;
while (x >= val)
{
val *= 10;
++count;
}
return count;
}
int variant::GetInt()
{
if (this->cache_val[type_int])
{
return this->val_int;
}
this->cache_val[type_int] = true;
switch (this->type)
{
case type_float:
this->val_int = static_cast<int>(this->val_float);
break;
case type_string:
this->val_int = tdparse(this->val_string);
break;
case type_bool:
this->val_int = this->val_bool ? 0 : -1;
break;
default: ; // Shut the compiler up
}
return this->val_int;
}
double variant::GetFloat()
{
if (this->cache_val[type_float])
{
return this->val_float;
}
this->cache_val[type_float] = true;
switch (this->type)
{
case type_int:
this->val_float = static_cast<double>(this->val_int);
break;
case type_string:
this->val_float = tdparse(this->val_string);
break;
case type_bool:
this->val_float = this->val_bool ? 0.0 : 1.0;
break;
default: ; // Shut the compiler up
}
return this->val_float;
}
std::string variant::GetString()
{
if (this->cache_val[type_string])
{
return this->val_string;
}
this->cache_val[type_string] = true;
char buf[1024];
switch (this->type)
{
case type_int:
snprintf(buf, 1024, "%i", this->val_int);
this->val_string = buf;
break;
case type_float:
snprintf(buf, 1024, "%lf", this->val_float);
this->val_string = buf;
break;
case type_bool:
this->val_string = this->val_bool ? "yes" : "no";
break;
default: ; // Shut the compiler up
}
return this->val_string;
}
bool variant::GetBool()
{
if (this->cache_val[type_bool])
{
return this->val_bool;
}
this->cache_val[type_bool] = true;
int intval = 0;
std::string s = this->val_string;
switch (this->type)
{
case type_int:
this->val_bool = static_cast<bool>(this->val_int);
break;
case type_float:
this->val_bool = std::abs(this->val_float) != 0.0 && this->val_float == this->val_float;
break;
case type_string:
std::sscanf(this->val_string.c_str(), "%d", &intval);
util::lowercase(s);
this->val_bool = (s == "yes" || s == "true" || s == "enabled" || intval != 0);
break;
default: ; // Shut the compiler up
}
return this->val_bool;
}
void variant::SetType(variant::var_type type)
{
this->type = type;
for (std::size_t i = 0; i < sizeof(this->cache_val); ++i)
{
this->cache_val[i] = (static_cast<variant::var_type>(i) == type);
}
}
variant &variant::SetInt(int i)
{
this->val_int = i;
this->SetType(type_int);
return *this;
}
variant &variant::SetFloat(double d)
{
this->val_float = d;
this->SetType(type_float);
return *this;
}
variant &variant::SetString(const std::string &s)
{
this->val_string = s;
this->SetType(type_string);
return *this;
}
variant &variant::SetBool(bool b)
{
this->val_bool = b;
this->SetType(type_bool);
return *this;
}
variant &variant::operator =(int i)
{
return this->SetInt(i);
}
variant &variant::operator =(double d)
{
return this->SetFloat(d);
}
variant &variant::operator =(const std::string &s)
{
return this->SetString(s);
}
variant &variant::operator =(const char *p)
{
std::string s(p);
return this->SetString(s);
}
variant &variant::operator =(bool b)
{
return this->SetBool(b);
}
variant::operator int()
{
return this->GetInt();
}
variant::operator double()
{
return this->GetFloat();
}
variant::operator std::string()
{
return this->GetString();
}
variant::operator bool()
{
return this->GetBool();
}
std::string ltrim(const std::string &str)
{
std::size_t si = str.find_first_not_of(" \t\n\r");
if (si == std::string::npos)
{
si = 0;
}
else
{
--si;
}
++si;
return str.substr(si);
}
std::string rtrim(const std::string &str)
{
std::size_t ei = str.find_last_not_of(" \t\n\r");
if (ei == std::string::npos)
{
ei = str.length()-1;
}
++ei;
return str.substr(0, ei);
}
std::string trim(const std::string &str)
{
std::size_t si, ei;
bool notfound = false;
si = str.find_first_not_of(" \t\n\r");
if (si == std::string::npos)
{
si = 0;
notfound = true;
}
ei = str.find_last_not_of(" \t\n\r");
if (ei == std::string::npos)
{
if (notfound)
{
return "";
}
ei = str.length()-1;
}
++ei;
return str.substr(si, ei);
}
std::vector<std::string> explode(char delimiter, std::string str)
{
std::size_t lastpos = 0;
std::size_t pos = 0;
std::vector<std::string> pieces;
for (pos = str.find_first_of(delimiter); pos != std::string::npos; )
{
pieces.push_back(str.substr(lastpos, pos - lastpos));
lastpos = pos+1;
pos = str.find_first_of(delimiter, pos+1);
}
pieces.push_back(str.substr(lastpos));
return pieces;
}
std::vector<std::string> explode(std::string delimiter, std::string str)
{
std::size_t lastpos = 0;
std::size_t pos = 0;
std::vector<std::string> pieces;
if (delimiter.length() == 0)
{
return pieces;
}
if (delimiter.length() == 1)
{
return explode(delimiter[0], str);
}
for (pos = str.find(delimiter); pos != std::string::npos; )
{
pieces.push_back(str.substr(lastpos, pos - lastpos));
lastpos = pos + delimiter.length();
pos = str.find(delimiter, pos + delimiter.length());
}
pieces.push_back(str.substr(lastpos));
return pieces;
}
double tdparse(std::string timestr)
{
static char period_names[] = {'s', 'm', '%', 'k', 'h', 'd' };
static double period_mul[] = {1.0, 60.0, 100.0, 1000.0, 3600.0, 86400.0};
double ret = 0.0;
double val = 0.0;
bool decimal = false;
double decimalmulti = 0.1;
bool negate = false;
for (std::size_t i = 0; i < timestr.length(); ++i)
{
char c = timestr[i];
bool found = false;
if (c == '-')
{
negate = true;
continue;
}
if (c >= 'A' && c <= 'Z')
{
c -= 'A' - 'a';
}
for (std::size_t ii = 0; ii < sizeof(period_names)/sizeof(char); ++ii)
{
if (c == period_names[ii])
{
if (c == 'm' && timestr[i+1] == 's')
{
ret += val / 1000.0;
++i;
}
else if (c == '%')
{
ret += val / period_mul[ii];
}
else
{
ret += val * period_mul[ii];
}
found = true;
val = 0.0;
decimal = false;
decimalmulti = 0.1;
break;
}
}
if (!found)
{
if (c >= '0' && c <= '9')
{
if (!decimal)
{
val *= 10.0;
val += c - '0';
}
else
{
val += (c - '0') * decimalmulti;
decimalmulti /= 10.0;
}
}
else if (c == '.')
{
decimal = true;
decimalmulti = 0.1;
}
}
}
return (ret + val) * (negate ? -1.0 : 1.0);
}
int to_int(const std::string &subject)
{
return static_cast<int>(util::variant(subject));
}
double to_float(const std::string &subject)
{
return static_cast<double>(util::variant(subject));
}
std::string to_string(int subject)
{
return static_cast<std::string>(util::variant(subject));
}
std::string to_string(double subject)
{
return static_cast<std::string>(util::variant(subject));
}
void lowercase(std::string &subject)
{
std::transform(subject.begin(), subject.end(), subject.begin(), static_cast<int(*)(int)>(std::tolower));
}
void uppercase(std::string &subject)
{
std::transform(subject.begin(), subject.end(), subject.begin(), static_cast<int(*)(int)>(std::toupper));
}
void ucfirst(std::string &subject)
{
if (subject[0] > 'a' && subject[0] < 'z')
{
subject[0] += 'A' - 'a';
}
}
static void rand_init()
{
static bool init = false;
if (!init)
{
init = true;
std::srand(std::time(0));
}
}
static unsigned long long_rand()
{
#if RAND_MAX < 65535
return (std::rand() & 0xFF) << 24 | (std::rand() & 0xFF) << 16 | (std::rand() & 0xFF) << 8 | (std::rand() & 0xFF);
#else
#if RAND_MAX < 4294967295
return (std::rand() & 0xFFFF) << 16 | (std::rand() & 0xFFFF);
#else
return std::rand();
#endif
#endif
}
int rand(int min, int max)
{
rand_init();
rand(double(min), double(max));
return static_cast<int>(double(long_rand()) / (double(std::numeric_limits<unsigned long>::max()) + 1.0) * double(max - min + 1) + double(min));
}
double rand(double min, double max)
{
rand_init();
return double(long_rand()) / double(std::numeric_limits<unsigned long>::max()) * (max - min) + min;
}
double round(double subject)
{
return std::floor(subject + 0.5);
}
std::string timeago(double time, double current_time)
{
static bool init = false;
static std::vector<std::pair<int, std::string> > times;
if (!init)
{
init = true;
times.resize(5);
times.push_back(std::make_pair(1, "second"));
times.push_back(std::make_pair(60, "minute"));
times.push_back(std::make_pair(60*60, "hour"));
times.push_back(std::make_pair(24*60*60, "day"));
times.push_back(std::make_pair(7*24*60*60, "week"));
}
std::string ago;
double diff = current_time - time;
ago = ((diff >= 0) ? " ago" : " from now");
diff = std::abs(diff);
for (int i = times.size()-1; i >= 0; --i)
{
int x = int(diff / times[i].first);
diff -= x * times[i].first;
if (x > 0)
{
return util::to_string(x) + " " + times[i].second + ((x == 1) ? "" : "s") + ago;
}
}
return "now";
}
void sleep(double seconds)
{
#if defined(WIN32) || defined(WIN64)
Sleep(int(seconds * 1000.0));
#else // defined(WIN32) || defined(WIN64)
long sec = seconds;
long nsec = (seconds - double(sec)) * 1000000000.0;
timespec ts = {sec, nsec};
nanosleep(&ts, 0);
#endif // defined(WIN32) || defined(WIN64)
}
std::string DecodeEMFString(std::string chars)
{
reverse(chars.begin(), chars.end());
bool flippy = (chars.length() % 2) == 1;
for (std::size_t i = 0; i < chars.length(); ++i)
{
unsigned char c = chars[i];
if (flippy)
{
if (c >= 0x22 && c <= 0x4F)
c = (unsigned char)(0x71 - c);
else if (c >= 0x50 && c <= 0x7E)
c = (unsigned char)(0xCD - c);
}
else
{
if (c >= 0x22 && c <= 0x7E)
c = (unsigned char)(0x9F - c);
}
chars[i] = c;
flippy = !flippy;
}
return chars;
}
std::string EncodeEMFString(std::string chars)
{
bool flippy = (chars.length() % 2) == 1;
for (std::size_t i = 0; i < chars.length(); ++i)
{
unsigned char c = chars[i];
if (flippy)
{
if (c >= 0x22 && c <= 0x4F)
c = (unsigned char)(0x71 - c);
else if (c >= 0x50 && c <= 0x7E)
c = (unsigned char)(0xCD - c);
}
else
{
if (c >= 0x22 && c <= 0x7E)
c = (unsigned char)(0x9F - c);
}
chars[i] = c;
flippy = !flippy;
}
reverse(chars.begin(), chars.end());
return chars;
}
}