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
|
/*
* Copyright (C) 2005-2009 MaNGOS <http://getmangos.com/>
*
* Copyright (C) 2008-2009 Trinity <http://www.trinitycore.org/>
*
* 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, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#ifndef _UTIL_H
#define _UTIL_H
#include "Common.h"
#include <string>
#include <vector>
typedef std::vector<std::string> Tokens;
Tokens StrSplit(const std::string &src, const std::string &sep);
void stripLineInvisibleChars(std::string &src);
std::string secsToTimeString(uint32 timeInSecs, bool shortText = false, bool hoursOnly = false);
uint32 TimeStringToSecs(const std::string& timestring);
std::string TimeToTimestampStr(time_t t);
inline uint32 secsToTimeBitFields(time_t secs)
{
tm* lt = localtime(&secs);
return (lt->tm_year - 100) << 24 | lt->tm_mon << 20 | (lt->tm_mday - 1) << 14 | lt->tm_wday << 11 | lt->tm_hour << 6 | lt->tm_min;
}
/* Return a random number in the range min..max; (max-min) must be smaller than 32768. */
TRINITY_DLL_SPEC int32 irand(int32 min, int32 max);
/* Return a random number in the range min..max (inclusive). For reliable results, the difference
* between max and min should be less than RAND32_MAX. */
TRINITY_DLL_SPEC uint32 urand(uint32 min, uint32 max);
/* Return a random number in the range 0 .. RAND32_MAX. */
TRINITY_DLL_SPEC int32 rand32();
/* Return a random double from 0.0 to 1.0 (exclusive). Floats support only 7 valid decimal digits.
* A double supports up to 15 valid decimal digits and is used internally (RAND32_MAX has 10 digits).
* With an FPU, there is usually no difference in performance between float and double. */
TRINITY_DLL_SPEC double rand_norm(void);
/* Return a random double from 0.0 to 99.9999999999999. Floats support only 7 valid decimal digits.
* A double supports up to 15 valid decimal digits and is used internaly (RAND32_MAX has 10 digits).
* With an FPU, there is usually no difference in performance between float and double. */
TRINITY_DLL_SPEC double rand_chance(void);
/* Return true if a random roll fits in the specified chance (range 0-100). */
inline bool roll_chance_f(float chance)
{
return chance > rand_chance();
}
/* Return true if a random roll fits in the specified chance (range 0-100). */
inline bool roll_chance_i(int chance)
{
return chance > irand(0, 99);
}
inline void ApplyModUInt32Var(uint32& var, int32 val, bool apply)
{
int32 cur = var;
cur += (apply ? val : -val);
if(cur < 0)
cur = 0;
var = cur;
}
inline void ApplyModFloatVar(float& var, float val, bool apply)
{
var += (apply ? val : -val);
if(var < 0)
var = 0;
}
inline void ApplyPercentModFloatVar(float& var, float val, bool apply)
{
if (val == -100.0f) // prevent set var to zero
val = -99.99f;
var *= (apply?(100.0f+val)/100.0f : 100.0f / (100.0f+val));
}
bool Utf8toWStr(const std::string& utf8str, std::wstring& wstr);
// in wsize==max size of buffer, out wsize==real string size
bool Utf8toWStr(char const* utf8str, size_t csize, wchar_t* wstr, size_t& wsize);
inline bool Utf8toWStr(const std::string& utf8str, wchar_t* wstr, size_t& wsize)
{
return Utf8toWStr(utf8str.c_str(), utf8str.size(), wstr, wsize);
}
bool WStrToUtf8(std::wstring wstr, std::string& utf8str);
// size==real string size
bool WStrToUtf8(wchar_t* wstr, size_t size, std::string& utf8str);
size_t utf8length(std::string& utf8str); // set string to "" if invalid utf8 sequence
void utf8truncate(std::string& utf8str,size_t len);
inline bool isBasicLatinCharacter(wchar_t wchar)
{
if(wchar >= L'a' && wchar <= L'z') // LATIN SMALL LETTER A - LATIN SMALL LETTER Z
return true;
if(wchar >= L'A' && wchar <= L'Z') // LATIN CAPITAL LETTER A - LATIN CAPITAL LETTER Z
return true;
return false;
}
inline bool isExtendedLatinCharacter(wchar_t wchar)
{
if(isBasicLatinCharacter(wchar))
return true;
if(wchar >= 0x00C0 && wchar <= 0x00D6) // LATIN CAPITAL LETTER A WITH GRAVE - LATIN CAPITAL LETTER O WITH DIAERESIS
return true;
if(wchar >= 0x00D8 && wchar <= 0x00DF) // LATIN CAPITAL LETTER O WITH STROKE - LATIN CAPITAL LETTER THORN
return true;
if(wchar == 0x00DF) // LATIN SMALL LETTER SHARP S
return true;
if(wchar >= 0x00E0 && wchar <= 0x00F6) // LATIN SMALL LETTER A WITH GRAVE - LATIN SMALL LETTER O WITH DIAERESIS
return true;
if(wchar >= 0x00F8 && wchar <= 0x00FE) // LATIN SMALL LETTER O WITH STROKE - LATIN SMALL LETTER THORN
return true;
if(wchar >= 0x0100 && wchar <= 0x012F) // LATIN CAPITAL LETTER A WITH MACRON - LATIN SMALL LETTER I WITH OGONEK
return true;
if(wchar == 0x1E9E) // LATIN CAPITAL LETTER SHARP S
return true;
return false;
}
inline bool isCyrillicCharacter(wchar_t wchar)
{
if(wchar >= 0x0410 && wchar <= 0x044F) // CYRILLIC CAPITAL LETTER A - CYRILLIC SMALL LETTER YA
return true;
if(wchar == 0x0401 || wchar == 0x0451) // CYRILLIC CAPITAL LETTER IO, CYRILLIC SMALL LETTER IO
return true;
return false;
}
inline bool isEastAsianCharacter(wchar_t wchar)
{
if(wchar >= 0x1100 && wchar <= 0x11F9) // Hangul Jamo
return true;
if(wchar >= 0x3041 && wchar <= 0x30FF) // Hiragana + Katakana
return true;
if(wchar >= 0x3131 && wchar <= 0x318E) // Hangul Compatibility Jamo
return true;
if(wchar >= 0x31F0 && wchar <= 0x31FF) // Katakana Phonetic Ext.
return true;
if(wchar >= 0x3400 && wchar <= 0x4DB5) // CJK Ideographs Ext. A
return true;
if(wchar >= 0x4E00 && wchar <= 0x9FC3) // Unified CJK Ideographs
return true;
if(wchar >= 0xAC00 && wchar <= 0xD7A3) // Hangul Syllables
return true;
if(wchar >= 0xFF01 && wchar <= 0xFFEE) // Halfwidth forms
return true;
return false;
}
inline bool isNumeric(wchar_t wchar)
{
return (wchar >= L'0' && wchar <=L'9');
}
inline bool isNumeric(char c)
{
return (c >= '0' && c <='9');
}
inline bool isNumericOrSpace(wchar_t wchar)
{
return isNumeric(wchar) || wchar == L' ';
}
inline bool isBasicLatinString(std::wstring wstr, bool numericOrSpace)
{
for (size_t i = 0; i < wstr.size(); ++i)
if(!isBasicLatinCharacter(wstr[i]) && (!numericOrSpace || !isNumericOrSpace(wstr[i])))
return false;
return true;
}
inline bool isExtendedLatinString(std::wstring wstr, bool numericOrSpace)
{
for (size_t i = 0; i < wstr.size(); ++i)
if(!isExtendedLatinCharacter(wstr[i]) && (!numericOrSpace || !isNumericOrSpace(wstr[i])))
return false;
return true;
}
inline bool isCyrillicString(std::wstring wstr, bool numericOrSpace)
{
for (size_t i = 0; i < wstr.size(); ++i)
if(!isCyrillicCharacter(wstr[i]) && (!numericOrSpace || !isNumericOrSpace(wstr[i])))
return false;
return true;
}
inline bool isEastAsianString(std::wstring wstr, bool numericOrSpace)
{
for (size_t i = 0; i < wstr.size(); ++i)
if(!isEastAsianCharacter(wstr[i]) && (!numericOrSpace || !isNumericOrSpace(wstr[i])))
return false;
return true;
}
inline wchar_t wcharToUpper(wchar_t wchar)
{
if(wchar >= L'a' && wchar <= L'z') // LATIN SMALL LETTER A - LATIN SMALL LETTER Z
return wchar_t(uint16(wchar)-0x0020);
if(wchar == 0x00DF) // LATIN SMALL LETTER SHARP S
return wchar_t(0x1E9E);
if(wchar >= 0x00E0 && wchar <= 0x00F6) // LATIN SMALL LETTER A WITH GRAVE - LATIN SMALL LETTER O WITH DIAERESIS
return wchar_t(uint16(wchar)-0x0020);
if(wchar >= 0x00F8 && wchar <= 0x00FE) // LATIN SMALL LETTER O WITH STROKE - LATIN SMALL LETTER THORN
return wchar_t(uint16(wchar)-0x0020);
if(wchar >= 0x0101 && wchar <= 0x012F) // LATIN SMALL LETTER A WITH MACRON - LATIN SMALL LETTER I WITH OGONEK (only %2=1)
{
if(wchar % 2 == 1)
return wchar_t(uint16(wchar)-0x0001);
}
if(wchar >= 0x0430 && wchar <= 0x044F) // CYRILLIC SMALL LETTER A - CYRILLIC SMALL LETTER YA
return wchar_t(uint16(wchar)-0x0020);
if(wchar == 0x0451) // CYRILLIC SMALL LETTER IO
return wchar_t(0x0401);
return wchar;
}
inline wchar_t wcharToUpperOnlyLatin(wchar_t wchar)
{
return isBasicLatinCharacter(wchar) ? wcharToUpper(wchar) : wchar;
}
inline wchar_t wcharToLower(wchar_t wchar)
{
if(wchar >= L'A' && wchar <= L'Z') // LATIN CAPITAL LETTER A - LATIN CAPITAL LETTER Z
return wchar_t(uint16(wchar)+0x0020);
if(wchar >= 0x00C0 && wchar <= 0x00D6) // LATIN CAPITAL LETTER A WITH GRAVE - LATIN CAPITAL LETTER O WITH DIAERESIS
return wchar_t(uint16(wchar)+0x0020);
if(wchar >= 0x00D8 && wchar <= 0x00DE) // LATIN CAPITAL LETTER O WITH STROKE - LATIN CAPITAL LETTER THORN
return wchar_t(uint16(wchar)+0x0020);
if(wchar >= 0x0100 && wchar <= 0x012E) // LATIN CAPITAL LETTER A WITH MACRON - LATIN CAPITAL LETTER I WITH OGONEK (only %2=0)
{
if(wchar % 2 == 0)
return wchar_t(uint16(wchar)+0x0001);
}
if(wchar == 0x1E9E) // LATIN CAPITAL LETTER SHARP S
return wchar_t(0x00DF);
if(wchar == 0x0401) // CYRILLIC CAPITAL LETTER IO
return wchar_t(0x0451);
if(wchar >= 0x0410 && wchar <= 0x042F) // CYRILLIC CAPITAL LETTER A - CYRILLIC CAPITAL LETTER YA
return wchar_t(uint16(wchar)+0x0020);
return wchar;
}
inline void wstrToUpper(std::wstring& str)
{
std::transform( str.begin(), str.end(), str.begin(), wcharToUpper );
}
inline void wstrToLower(std::wstring& str)
{
std::transform( str.begin(), str.end(), str.begin(), wcharToLower );
}
std::wstring GetMainPartOfName(std::wstring wname, uint32 declension);
bool utf8ToConsole(const std::string& utf8str, std::string& conStr);
bool consoleToUtf8(const std::string& conStr,std::string& utf8str);
bool Utf8FitTo(const std::string& str, std::wstring search);
void utf8printf(FILE *out, const char *str, ...);
void vutf8printf(FILE *out, const char *str, va_list* ap);
bool IsIPAddress(char const* ipaddress);
uint32 CreatePIDFile(const std::string& filename);
void hexEncodeByteArray(uint8* bytes, uint32 arrayLen, std::string& result);
#endif
//handler for operations on large flags
#ifndef _FLAG96
#define _FLAG96
#ifndef PAIR64_HIPART
#define PAIR64_HIPART(x) (uint32)((uint64(x) >> 32) & UI64LIT(0x00000000FFFFFFFF))
#define PAIR64_LOPART(x) (uint32)(uint64(x) & UI64LIT(0x00000000FFFFFFFF))
#endif
class flag96
{
private:
uint32 part[3];
public:
flag96(uint32 p1=0,uint32 p2=0,uint32 p3=0)
{
part[0]=p1;
part[1]=p2;
part[2]=p3;
}
flag96(uint64 p1, uint32 p2)
{
part[0]=PAIR64_LOPART(p1);
part[1]=PAIR64_HIPART(p1);
part[2]=p2;
}
inline bool IsEqual(uint32 p1=0, uint32 p2=0, uint32 p3=0) const
{
return (
part[0]==p1 &&
part[1]==p2 &&
part[2]==p3);
};
inline bool HasFlag(uint32 p1=0, uint32 p2=0, uint32 p3=0) const
{
return (
part[0]&p1 ||
part[1]&p2 ||
part[2]&p3);
};
inline void Set(uint32 p1=0, uint32 p2=0, uint32 p3=0)
{
part[0]=p1;
part[1]=p2;
part[2]=p3;
};
template<class type>
inline bool operator < (type & right)
{
for (uint8 i=3; i > 0; --i)
{
if (part[i-1]<right.part[i-1])
return 1;
else if (part[i-1]>right.part[i-1])
return 0;
}
return 0;
};
template<class type>
inline bool operator < (type & right) const
{
for (uint8 i = 3; i > 0; --i)
{
if (part[i-1]<right.part[i-1])
return 1;
else if (part[i-1]>right.part[i-1])
return 0;
}
return 0;
};
template<class type>
inline bool operator != (type & right)
{
if (part[0]!=right.part[0]
|| part[1]!=right.part[1]
|| part[2]!=right.part[2])
return true;
return false;
}
template<class type>
inline bool operator != (type & right) const
{
if (part[0]!=right.part[0]
|| part[1]!=right.part[1]
|| part[2]!=right.part[2])
return true;
return false;
};
template<class type>
inline bool operator == (type & right)
{
if (part[0]!=right.part[0]
|| part[1]!=right.part[1]
|| part[2]!=right.part[2])
return false;
return true;
};
template<class type>
inline bool operator == (type & right) const
{
if (part[0]!=right.part[0]
|| part[1]!=right.part[1]
|| part[2]!=right.part[2])
return false;
return true;
};
template<class type>
inline void operator = (type & right)
{
part[0]=right.part[0];
part[1]=right.part[1];
part[2]=right.part[2];
};
template<class type>
inline flag96 operator & (type & right)
{
flag96 ret(part[0] & right.part[0],part[1] & right.part[1],part[2] & right.part[2]);
return
ret;
};
template<class type>
inline flag96 operator & (type & right) const
{
flag96 ret(part[0] & right.part[0],part[1] & right.part[1],part[2] & right.part[2]);
return
ret;
};
template<class type>
inline void operator &= (type & right)
{
*this=*this & right;
};
template<class type>
inline flag96 operator | (type & right)
{
flag96 ret(part[0] | right.part[0],part[1] | right.part[1],part[2] | right.part[2]);
return
ret;
};
template<class type>
inline flag96 operator | (type & right) const
{
flag96 ret(part[0] | right.part[0],part[1] | right.part[1],part[2] | right.part[2]);
return
ret;
};
template<class type>
inline void operator |= (type & right)
{
*this=*this | right;
};
inline void operator ~ ()
{
part[2]=~part[2];
part[1]=~part[1];
part[0]=~part[0];
};
template<class type>
inline flag96 operator ^ (type & right)
{
flag96 ret(part[0] ^ right.part[0],part[1] ^ right.part[1],part[2] ^ right.part[2]);
return
ret;
};
template<class type>
inline flag96 operator ^ (type & right) const
{
flag96 ret(part[0] ^ right.part[0],part[1] ^ right.part[1],part[2] ^ right.part[2]);
return
ret;
};
template<class type>
inline void operator ^= (type & right)
{
*this=*this^right;
};
inline operator bool() const
{
return(
part[0] != 0 ||
part[1] != 0 ||
part[2] != 0);
};
inline operator bool()
{
return(
part[0] != 0 ||
part[1] != 0 ||
part[2] != 0);
};
inline bool operator ! () const
{
return(
part[0] == 0 &&
part[1] == 0 &&
part[2] == 0);
};
inline bool operator ! ()
{
return(
part[0] == 0 &&
part[1] == 0 &&
part[2] == 0);
};
inline uint32 & operator[](uint8 el)
{
return (part[el]);
};
inline const uint32 & operator[](uint8 el) const
{
return (part[el]);
};
};
#endif
|