OregonCore  revision fb2a440-git
Your Favourite TBC server
Util.h
Go to the documentation of this file.
1 /*
2  * This file is part of the OregonCore Project. See AUTHORS file for Copyright information
3  *
4  * This program is free software; you can redistribute it and/or modify it
5  * under the terms of the GNU General Public License as published by the
6  * Free Software Foundation; either version 2 of the License, or (at your
7  * option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12  * more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #ifndef _UTIL_H
19 #define _UTIL_H
20 
21 #include "Common.h"
22 
23 #include <string>
24 #include <vector>
25 
26 typedef std::vector<std::string> Tokens;
27 
28 Tokens StrSplit(const std::string& src, const std::string& sep);
29 
30 void stripLineInvisibleChars(std::string& src);
31 
32 std::string secsToTimeString(uint32 timeInSecs, bool shortText = false, bool hoursOnly = false);
33 uint32 TimeStringToSecs(const std::string& timestring);
34 std::string TimeToTimestampStr(time_t t);
35 
36 inline uint32 secsToTimeBitFields(time_t secs)
37 {
38  tm* lt = localtime(&secs);
39  return uint32((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);
40 }
41 
42 /* Return a random number in the range min..max. */
43 int32 irand(int32 min, int32 max);
44 
45 /* Return a random number in the range min..max (inclusive). */
46 uint32 urand(uint32 min, uint32 max);
47 
48 /* Return a random number in the range 0 .. UINT32_MAX. */
49 uint32 rand32();
50 
51 /* Return a random number in the range min..max */
52 float frand(float min, float max);
53 
54 /* Return a random double from 0.0 to 1.0 (exclusive). */
55 double rand_norm();
56 
57 /* Return a random double from 0.0 to 100.0 (exclusive). */
58 double rand_chance();
59 
60 // Return true if a random roll fits in the specified chance (range 0-100).
61 inline bool roll_chance_f(float chance)
62 {
63  return chance > rand_chance();
64 }
65 
66 // Return true if a random roll fits in the specified chance (range 0-100).
67 inline bool roll_chance_i(int chance)
68 {
69  return chance > irand(0, 99);
70 }
71 
72 inline void ApplyModUInt32Var(uint32& var, int32 val, bool apply)
73 {
74  int32 cur = var;
75  cur += (apply ? val : -val);
76  if (cur < 0)
77  cur = 0;
78  var = cur;
79 }
80 
81 inline void ApplyModFloatVar(float& var, float val, bool apply)
82 {
83  var += (apply ? val : -val);
84  if (var < 0)
85  var = 0;
86 }
87 
88 inline void ApplyPercentModFloatVar(float& var, float val, bool apply)
89 {
90  if (val == -100.0f) // prevent set var to zero
91  val = -99.99f;
92  var *= (apply ? (100.0f + val) / 100.0f : 100.0f / (100.0f + val));
93 }
94 
95 // Percentage calculation
96 template <class T, class U>
97 inline T CalculatePct(T base, U pct)
98 {
99  return T(base * static_cast<float>(pct) / 100.0f);
100 }
101 
102 template <class T, class U>
103 inline T AddPct(T &base, U pct)
104 {
105  return base += CalculatePct(base, pct);
106 }
107 
108 template <class T, class U>
109 inline T ApplyPct(T &base, U pct)
110 {
111  return base = CalculatePct(base, pct);
112 }
113 
114 template <class T>
115 inline T RoundToInterval(T& num, T floor, T ceil)
116 {
117  return num = std::min(std::max(num, floor), ceil);
118 }
119 
120 // UTF8 handling
121 bool Utf8toWStr(const std::string& utf8str, std::wstring& wstr);
122 // in wsize==max size of buffer, out wsize==real string size
123 bool Utf8toWStr(char const* utf8str, size_t csize, wchar_t* wstr, size_t& wsize);
124 inline bool Utf8toWStr(const std::string& utf8str, wchar_t* wstr, size_t& wsize)
125 {
126  return Utf8toWStr(utf8str.c_str(), utf8str.size(), wstr, wsize);
127 }
128 
129 bool WStrToUtf8(std::wstring wstr, std::string& utf8str);
130 // size==real string size
131 bool WStrToUtf8(wchar_t* wstr, size_t size, std::string& utf8str);
132 
133 size_t utf8length(std::string& utf8str); // set string to "" if invalid utf8 sequence
134 void utf8truncate(std::string& utf8str, size_t len);
135 
136 inline bool isBasicLatinCharacter(wchar_t wchar)
137 {
138  if (wchar >= L'a' && wchar <= L'z') // LATIN SMALL LETTER A - LATIN SMALL LETTER Z
139  return true;
140  if (wchar >= L'A' && wchar <= L'Z') // LATIN CAPITAL LETTER A - LATIN CAPITAL LETTER Z
141  return true;
142  return false;
143 }
144 
145 inline bool isExtendedLatinCharacter(wchar_t wchar)
146 {
147  if (isBasicLatinCharacter(wchar))
148  return true;
149  if (wchar >= 0x00C0 && wchar <= 0x00D6) // LATIN CAPITAL LETTER A WITH GRAVE - LATIN CAPITAL LETTER O WITH DIAERESIS
150  return true;
151  if (wchar >= 0x00D8 && wchar <= 0x00DE) // LATIN CAPITAL LETTER O WITH STROKE - LATIN CAPITAL LETTER THORN
152  return true;
153  if (wchar == 0x00DF) // LATIN SMALL LETTER SHARP S
154  return true;
155  if (wchar >= 0x00E0 && wchar <= 0x00F6) // LATIN SMALL LETTER A WITH GRAVE - LATIN SMALL LETTER O WITH DIAERESIS
156  return true;
157  if (wchar >= 0x00F8 && wchar <= 0x00FE) // LATIN SMALL LETTER O WITH STROKE - LATIN SMALL LETTER THORN
158  return true;
159  if (wchar >= 0x0100 && wchar <= 0x012F) // LATIN CAPITAL LETTER A WITH MACRON - LATIN SMALL LETTER I WITH OGONEK
160  return true;
161  if (wchar == 0x1E9E) // LATIN CAPITAL LETTER SHARP S
162  return true;
163  return false;
164 }
165 
166 inline bool isCyrillicCharacter(wchar_t wchar)
167 {
168  if (wchar >= 0x0410 && wchar <= 0x044F) // CYRILLIC CAPITAL LETTER A - CYRILLIC SMALL LETTER YA
169  return true;
170  if (wchar == 0x0401 || wchar == 0x0451) // CYRILLIC CAPITAL LETTER IO, CYRILLIC SMALL LETTER IO
171  return true;
172  return false;
173 }
174 
175 inline bool isEastAsianCharacter(wchar_t wchar)
176 {
177  if (wchar >= 0x1100 && wchar <= 0x11F9) // Hangul Jamo
178  return true;
179  if (wchar >= 0x3041 && wchar <= 0x30FF) // Hiragana + Katakana
180  return true;
181  if (wchar >= 0x3131 && wchar <= 0x318E) // Hangul Compatibility Jamo
182  return true;
183  if (wchar >= 0x31F0 && wchar <= 0x31FF) // Katakana Phonetic Ext.
184  return true;
185  if (wchar >= 0x3400 && wchar <= 0x4DB5) // CJK Ideographs Ext. A
186  return true;
187  if (wchar >= 0x4E00 && wchar <= 0x9FC3) // Unified CJK Ideographs
188  return true;
189  if (wchar >= 0xAC00 && wchar <= 0xD7A3) // Hangul Syllables
190  return true;
191  if (wchar >= 0xFF01 && wchar <= 0xFFEE) // Halfwidth forms
192  return true;
193  return false;
194 }
195 
196 inline bool isNumeric(wchar_t wchar)
197 {
198  return (wchar >= L'0' && wchar <= L'9');
199 }
200 
201 inline bool isNumeric(char c)
202 {
203  return (c >= '0' && c <= '9');
204 }
205 
206 inline bool isNumericOrSpace(wchar_t wchar)
207 {
208  return isNumeric(wchar) || wchar == L' ';
209 }
210 
211 inline bool isNumeric(char const* str)
212 {
213  for (char const* c = str; *c; ++c)
214  if (!isNumeric(*c))
215  return false;
216 
217  return true;
218 }
219 
220 inline bool isBasicLatinString(std::wstring wstr, bool numericOrSpace)
221 {
222  for (size_t i = 0; i < wstr.size(); ++i)
223  if (!isBasicLatinCharacter(wstr[i]) && (!numericOrSpace || !isNumericOrSpace(wstr[i])))
224  return false;
225  return true;
226 }
227 
228 inline bool isExtendedLatinString(std::wstring wstr, bool numericOrSpace)
229 {
230  for (size_t i = 0; i < wstr.size(); ++i)
231  if (!isExtendedLatinCharacter(wstr[i]) && (!numericOrSpace || !isNumericOrSpace(wstr[i])))
232  return false;
233  return true;
234 }
235 
236 inline bool isCyrillicString(std::wstring wstr, bool numericOrSpace)
237 {
238  for (size_t i = 0; i < wstr.size(); ++i)
239  if (!isCyrillicCharacter(wstr[i]) && (!numericOrSpace || !isNumericOrSpace(wstr[i])))
240  return false;
241  return true;
242 }
243 
244 inline bool isEastAsianString(std::wstring wstr, bool numericOrSpace)
245 {
246  for (size_t i = 0; i < wstr.size(); ++i)
247  if (!isEastAsianCharacter(wstr[i]) && (!numericOrSpace || !isNumericOrSpace(wstr[i])))
248  return false;
249  return true;
250 }
251 
252 inline wchar_t wcharToUpper(wchar_t wchar)
253 {
254  if (wchar >= L'a' && wchar <= L'z') // LATIN SMALL LETTER A - LATIN SMALL LETTER Z
255  return wchar_t(uint16(wchar) - 0x0020);
256  if (wchar == 0x00DF) // LATIN SMALL LETTER SHARP S
257  return wchar_t(0x1E9E);
258  if (wchar >= 0x00E0 && wchar <= 0x00F6) // LATIN SMALL LETTER A WITH GRAVE - LATIN SMALL LETTER O WITH DIAERESIS
259  return wchar_t(uint16(wchar) - 0x0020);
260  if (wchar >= 0x00F8 && wchar <= 0x00FE) // LATIN SMALL LETTER O WITH STROKE - LATIN SMALL LETTER THORN
261  return wchar_t(uint16(wchar) - 0x0020);
262  if (wchar >= 0x0101 && wchar <= 0x012F) // LATIN SMALL LETTER A WITH MACRON - LATIN SMALL LETTER I WITH OGONEK (only %2=1)
263  {
264  if (wchar % 2 == 1)
265  return wchar_t(uint16(wchar) - 0x0001);
266  }
267  if (wchar >= 0x0430 && wchar <= 0x044F) // CYRILLIC SMALL LETTER A - CYRILLIC SMALL LETTER YA
268  return wchar_t(uint16(wchar) - 0x0020);
269  if (wchar == 0x0451) // CYRILLIC SMALL LETTER IO
270  return wchar_t(0x0401);
271 
272  return wchar;
273 }
274 
275 inline wchar_t wcharToUpperOnlyLatin(wchar_t wchar)
276 {
277  return isBasicLatinCharacter(wchar) ? wcharToUpper(wchar) : wchar;
278 }
279 
280 inline wchar_t wcharToLower(wchar_t wchar)
281 {
282  if (wchar >= L'A' && wchar <= L'Z') // LATIN CAPITAL LETTER A - LATIN CAPITAL LETTER Z
283  return wchar_t(uint16(wchar) + 0x0020);
284  if (wchar >= 0x00C0 && wchar <= 0x00D6) // LATIN CAPITAL LETTER A WITH GRAVE - LATIN CAPITAL LETTER O WITH DIAERESIS
285  return wchar_t(uint16(wchar) + 0x0020);
286  if (wchar >= 0x00D8 && wchar <= 0x00DE) // LATIN CAPITAL LETTER O WITH STROKE - LATIN CAPITAL LETTER THORN
287  return wchar_t(uint16(wchar) + 0x0020);
288  if (wchar >= 0x0100 && wchar <= 0x012E) // LATIN CAPITAL LETTER A WITH MACRON - LATIN CAPITAL LETTER I WITH OGONEK (only %2=0)
289  {
290  if (wchar % 2 == 0)
291  return wchar_t(uint16(wchar) + 0x0001);
292  }
293  if (wchar == 0x1E9E) // LATIN CAPITAL LETTER SHARP S
294  return wchar_t(0x00DF);
295  if (wchar == 0x0401) // CYRILLIC CAPITAL LETTER IO
296  return wchar_t(0x0451);
297  if (wchar >= 0x0410 && wchar <= 0x042F) // CYRILLIC CAPITAL LETTER A - CYRILLIC CAPITAL LETTER YA
298  return wchar_t(uint16(wchar) + 0x0020);
299 
300  return wchar;
301 }
302 
303 inline void wstrToUpper(std::wstring& str)
304 {
305  std::transform( str.begin(), str.end(), str.begin(), wcharToUpper );
306 }
307 
308 inline void wstrToLower(std::wstring& str)
309 {
310  std::transform( str.begin(), str.end(), str.begin(), wcharToLower );
311 }
312 
313 std::wstring GetMainPartOfName(std::wstring wname, uint32 declension);
314 
315 bool utf8ToConsole(const std::string& utf8str, std::string& conStr);
316 bool consoleToUtf8(const std::string& conStr, std::string& utf8str);
317 bool Utf8FitTo(const std::string& str, std::wstring search);
318 void hexEncodeByteArray(uint8* bytes, uint32 arrayLen, std::string& result);
319 std::string ByteArrayToHexStr(uint8* bytes, uint32 length);
320 
321 #if PLATFORM == PLATFORM_WINDOWS
322 #define UTF8PRINTF(OUT,FRM,RESERR) \
323  { \
324  char temp_buf[6000]; \
325  va_list ap; \
326  va_start(ap, FRM); \
327  size_t temp_len = vsnprintf(temp_buf,6000,FRM,ap); \
328  va_end(ap); \
329  \
330  wchar_t wtemp_buf[6000]; \
331  size_t wtemp_len = 6000-1; \
332  if (!Utf8toWStr(temp_buf,temp_len,wtemp_buf,wtemp_len)) \
333  return RESERR; \
334  CharToOemBuffW(&wtemp_buf[0],&temp_buf[0],wtemp_len+1);\
335  fprintf(OUT,temp_buf); \
336  }
337 #else
338 #define UTF8PRINTF(OUT,FRM,RESERR) \
339  { \
340  va_list ap; \
341  va_start(ap, FRM); \
342  vfprintf(OUT, FRM, ap ); \
343  va_end(ap); \
344  }
345 #endif
346 
347 bool IsIPAddress(char const* ipaddress);
348 uint32 CreatePIDFile(const std::string& filename);
349 
350 
351 /* Select a random element from a container. Note: make sure you explicitly empty check the container */
352 template <class C> typename C::value_type const& SelectRandomContainerElement(C const& container)
353 {
354  typename C::const_iterator it = container.begin();
355  std::advance(it, urand(0, container.size() - 1));
356  return *it;
357 }
358 
359 #endif
std::string TimeToTimestampStr(time_t t)
Definition: Util.cpp:177
uint32 rand32()
Definition: Util.cpp:43
wchar_t wcharToUpperOnlyLatin(wchar_t wchar)
Definition: Util.h:275
bool isBasicLatinString(std::wstring wstr, bool numericOrSpace)
Definition: Util.h:220
bool isExtendedLatinString(std::wstring wstr, bool numericOrSpace)
Definition: Util.h:228
void ApplyModFloatVar(float &var, float val, bool apply)
Definition: Util.h:81
size_t utf8length(std::string &utf8str)
Definition: Util.cpp:221
T RoundToInterval(T &num, T floor, T ceil)
Definition: Util.h:115
std::vector< std::string > Tokens
Definition: Util.h:26
void utf8truncate(std::string &utf8str, size_t len)
Definition: Util.cpp:234
bool isCyrillicCharacter(wchar_t wchar)
Definition: Util.h:166
float frand(float min, float max)
Definition: Util.cpp:38
bool isExtendedLatinCharacter(wchar_t wchar)
Definition: Util.h:145
bool isEastAsianString(std::wstring wstr, bool numericOrSpace)
Definition: Util.h:244
T ApplyPct(T &base, U pct)
Definition: Util.h:109
bool roll_chance_f(float chance)
Definition: Util.h:61
ACE_INT32 int32
Definition: Define.h:67
bool isEastAsianCharacter(wchar_t wchar)
Definition: Util.h:175
bool isCyrillicString(std::wstring wstr, bool numericOrSpace)
Definition: Util.h:236
bool IsIPAddress(char const *ipaddress)
Definition: Util.cpp:192
void stripLineInvisibleChars(std::string &src)
Definition: Util.cpp:76
void apply(T *val)
Definition: ByteConverter.h:41
void wstrToLower(std::wstring &str)
Definition: Util.h:308
bool consoleToUtf8(const std::string &conStr, std::string &utf8str)
Definition: Util.cpp:404
void hexEncodeByteArray(uint8 *bytes, uint32 arrayLen, std::string &result)
Definition: Util.cpp:435
bool utf8ToConsole(const std::string &utf8str, std::string &conStr)
Definition: Util.cpp:387
uint32 urand(uint32 min, uint32 max)
Definition: Util.cpp:33
bool roll_chance_i(int chance)
Definition: Util.h:67
Tokens StrSplit(const std::string &src, const std::string &sep)
Definition: Util.cpp:58
ACE_UINT8 uint8
Definition: Define.h:73
bool isBasicLatinCharacter(wchar_t wchar)
Definition: Util.h:136
double rand_chance()
Definition: Util.cpp:53
std::string secsToTimeString(uint32 timeInSecs, bool shortText=false, bool hoursOnly=false)
Definition: Util.cpp:109
std::string ByteArrayToHexStr(uint8 *bytes, uint32 length)
Definition: Util.cpp:454
uint32 secsToTimeBitFields(time_t secs)
Definition: Util.h:36
bool Utf8toWStr(const std::string &utf8str, std::wstring &wstr)
Definition: Util.cpp:281
double rand_norm()
Definition: Util.cpp:48
T CalculatePct(T base, U pct)
Definition: Util.h:97
void ApplyPercentModFloatVar(float &var, float val, bool apply)
Definition: Util.h:88
bool isNumeric(wchar_t wchar)
Definition: Util.h:196
uint32 TimeStringToSecs(const std::string &timestring)
Definition: Util.cpp:132
bool isNumericOrSpace(wchar_t wchar)
Definition: Util.h:206
wchar_t wcharToLower(wchar_t wchar)
Definition: Util.h:280
bool WStrToUtf8(std::wstring wstr, std::string &utf8str)
Definition: Util.cpp:319
T AddPct(T &base, U pct)
Definition: Util.h:103
uint32 CreatePIDFile(const std::string &filename)
Definition: Util.cpp:203
void wstrToUpper(std::wstring &str)
Definition: Util.h:303
wchar_t wcharToUpper(wchar_t wchar)
Definition: Util.h:252
ACE_UINT16 uint16
Definition: Define.h:72
int32 irand(int32 min, int32 max)
Definition: Util.cpp:28
ACE_UINT32 uint32
Definition: Define.h:71
std::wstring GetMainPartOfName(std::wstring wname, uint32 declension)
Definition: Util.cpp:341
C::value_type const & SelectRandomContainerElement(C const &container)
Definition: Util.h:352
void ApplyModUInt32Var(uint32 &var, int32 val, bool apply)
Definition: Util.h:72
bool Utf8FitTo(const std::string &str, std::wstring search)
Definition: Util.cpp:419