Cleanup unused spell in HandleProcTriggerSpell
[getmangos.git] / src / shared / Util.h
blob6a77d9c9a32f701be141a098d148e799eba807d4
1 /*
2 * Copyright (C) 2005-2008 MaNGOS <http://getmangos.com/>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 #ifndef _UTIL_H
20 #define _UTIL_H
22 #include "Common.h"
24 #include <string>
25 #include <vector>
27 typedef std::vector<std::string> Tokens;
29 Tokens StrSplit(const std::string &src, const std::string &sep);
31 void stripLineInvisibleChars(std::string &src);
33 std::string secsToTimeString(uint32 timeInSecs, bool shortText = false, bool hoursOnly = false);
34 uint32 TimeStringToSecs(const std::string& timestring);
35 std::string TimeToTimestampStr(time_t t);
37 inline uint32 secsToTimeBitFields(time_t secs)
39 tm* lt = localtime(&secs);
40 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;
43 /* Return a random number in the range min..max; (max-min) must be smaller than 32768. */
44 MANGOS_DLL_SPEC int32 irand(int32 min, int32 max);
46 /* Return a random number in the range min..max (inclusive). For reliable results, the difference
47 * between max and min should be less than RAND32_MAX. */
48 MANGOS_DLL_SPEC uint32 urand(uint32 min, uint32 max);
50 /* Return a random number in the range 0 .. RAND32_MAX. */
51 MANGOS_DLL_SPEC int32 rand32();
53 /* Return a random double from 0.0 to 1.0 (exclusive). Floats support only 7 valid decimal digits.
54 * A double supports up to 15 valid decimal digits and is used internally (RAND32_MAX has 10 digits).
55 * With an FPU, there is usually no difference in performance between float and double. */
56 MANGOS_DLL_SPEC double rand_norm(void);
58 /* Return a random double from 0.0 to 99.9999999999999. Floats support only 7 valid decimal digits.
59 * A double supports up to 15 valid decimal digits and is used internaly (RAND32_MAX has 10 digits).
60 * With an FPU, there is usually no difference in performance between float and double. */
61 MANGOS_DLL_SPEC double rand_chance(void);
63 /* Return true if a random roll fits in the specified chance (range 0-100). */
64 inline bool roll_chance_f(float chance)
66 return chance > rand_chance();
69 /* Return true if a random roll fits in the specified chance (range 0-100). */
70 inline bool roll_chance_i(int chance)
72 return chance > irand(0, 99);
75 inline void ApplyModUInt32Var(uint32& var, int32 val, bool apply)
77 int32 cur = var;
78 cur += (apply ? val : -val);
79 if(cur < 0)
80 cur = 0;
81 var = cur;
84 inline void ApplyModFloatVar(float& var, float val, bool apply)
86 var += (apply ? val : -val);
87 if(var < 0)
88 var = 0;
91 inline void ApplyPercentModFloatVar(float& var, float val, bool apply)
93 if (!apply && val == -100.0f)
94 val = -99.99f;
95 var *= (apply?(100.0f+val)/100.0f : 100.0f / (100.0f+val));
98 bool Utf8toWStr(const std::string& utf8str, std::wstring& wstr);
99 // in wsize==max size of buffer, out wsize==real string size
100 bool Utf8toWStr(char const* utf8str, size_t csize, wchar_t* wstr, size_t& wsize);
101 inline bool Utf8toWStr(const std::string& utf8str, wchar_t* wstr, size_t& wsize)
103 return Utf8toWStr(utf8str.c_str(), utf8str.size(), wstr, wsize);
106 bool WStrToUtf8(std::wstring wstr, std::string& utf8str);
107 // size==real string size
108 bool WStrToUtf8(wchar_t* wstr, size_t size, std::string& utf8str);
110 size_t utf8length(std::string& utf8str); // set string to "" if invalid utf8 sequence
111 void utf8truncate(std::string& utf8str,size_t len);
113 inline bool isBasicLatinCharacter(wchar_t wchar)
115 if(wchar >= L'a' && wchar <= L'z') // LATIN SMALL LETTER A - LATIN SMALL LETTER Z
116 return true;
117 if(wchar >= L'A' && wchar <= L'Z') // LATIN CAPITAL LETTER A - LATIN CAPITAL LETTER Z
118 return true;
119 return false;
122 inline bool isExtendedLatinCharacter(wchar_t wchar)
124 if(isBasicLatinCharacter(wchar))
125 return true;
126 if(wchar >= 0x00C0 && wchar <= 0x00D6) // LATIN CAPITAL LETTER A WITH GRAVE - LATIN CAPITAL LETTER O WITH DIAERESIS
127 return true;
128 if(wchar >= 0x00D8 && wchar <= 0x00DF) // LATIN CAPITAL LETTER O WITH STROKE - LATIN CAPITAL LETTER THORN
129 return true;
130 if(wchar == 0x00DF) // LATIN SMALL LETTER SHARP S
131 return true;
132 if(wchar >= 0x00E0 && wchar <= 0x00F6) // LATIN SMALL LETTER A WITH GRAVE - LATIN SMALL LETTER O WITH DIAERESIS
133 return true;
134 if(wchar >= 0x00F8 && wchar <= 0x00FE) // LATIN SMALL LETTER O WITH STROKE - LATIN SMALL LETTER THORN
135 return true;
136 if(wchar >= 0x0100 && wchar <= 0x012F) // LATIN CAPITAL LETTER A WITH MACRON - LATIN SMALL LETTER I WITH OGONEK
137 return true;
138 if(wchar == 0x1E9E) // LATIN CAPITAL LETTER SHARP S
139 return true;
140 return false;
143 inline bool isCyrillicCharacter(wchar_t wchar)
145 if(wchar >= 0x0410 && wchar <= 0x044F) // CYRILLIC CAPITAL LETTER A - CYRILLIC SMALL LETTER YA
146 return true;
147 if(wchar == 0x0401 || wchar == 0x0451) // CYRILLIC CAPITAL LETTER IO, CYRILLIC SMALL LETTER IO
148 return true;
149 return false;
152 inline bool isEastAsianCharacter(wchar_t wchar)
154 if(wchar >= 0x1100 && wchar <= 0x11F9) // Hangul Jamo
155 return true;
156 if(wchar >= 0x3041 && wchar <= 0x30FF) // Hiragana + Katakana
157 return true;
158 if(wchar >= 0x3131 && wchar <= 0x318E) // Hangul Compatibility Jamo
159 return true;
160 if(wchar >= 0x31F0 && wchar <= 0x31FF) // Katakana Phonetic Ext.
161 return true;
162 if(wchar >= 0x3400 && wchar <= 0x4DB5) // CJK Ideographs Ext. A
163 return true;
164 if(wchar >= 0x4E00 && wchar <= 0x9FC3) // Unified CJK Ideographs
165 return true;
166 if(wchar >= 0xAC00 && wchar <= 0xD7A3) // Hangul Syllables
167 return true;
168 if(wchar >= 0xFF01 && wchar <= 0xFFEE) // Halfwidth forms
169 return true;
170 return false;
173 inline bool isNumeric(wchar_t wchar)
175 return (wchar >= L'0' && wchar <=L'9');
178 inline bool isNumeric(char c)
180 return (c >= '0' && c <='9');
183 inline bool isNumericOrSpace(wchar_t wchar)
185 return isNumeric(wchar) || wchar == L' ';
188 inline bool isBasicLatinString(std::wstring wstr, bool numericOrSpace)
190 for(size_t i = 0; i < wstr.size(); ++i)
191 if(!isBasicLatinCharacter(wstr[i]) && (!numericOrSpace || !isNumericOrSpace(wstr[i])))
192 return false;
193 return true;
196 inline bool isExtendedLatinString(std::wstring wstr, bool numericOrSpace)
198 for(size_t i = 0; i < wstr.size(); ++i)
199 if(!isExtendedLatinCharacter(wstr[i]) && (!numericOrSpace || !isNumericOrSpace(wstr[i])))
200 return false;
201 return true;
204 inline bool isCyrillicString(std::wstring wstr, bool numericOrSpace)
206 for(size_t i = 0; i < wstr.size(); ++i)
207 if(!isCyrillicCharacter(wstr[i]) && (!numericOrSpace || !isNumericOrSpace(wstr[i])))
208 return false;
209 return true;
212 inline bool isEastAsianString(std::wstring wstr, bool numericOrSpace)
214 for(size_t i = 0; i < wstr.size(); ++i)
215 if(!isEastAsianCharacter(wstr[i]) && (!numericOrSpace || !isNumericOrSpace(wstr[i])))
216 return false;
217 return true;
220 inline wchar_t wcharToUpper(wchar_t wchar)
222 if(wchar >= L'a' && wchar <= L'z') // LATIN SMALL LETTER A - LATIN SMALL LETTER Z
223 return wchar_t(uint16(wchar)-0x0020);
224 if(wchar == 0x00DF) // LATIN SMALL LETTER SHARP S
225 return wchar_t(0x1E9E);
226 if(wchar >= 0x00E0 && wchar <= 0x00F6) // LATIN SMALL LETTER A WITH GRAVE - LATIN SMALL LETTER O WITH DIAERESIS
227 return wchar_t(uint16(wchar)-0x0020);
228 if(wchar >= 0x00F8 && wchar <= 0x00FE) // LATIN SMALL LETTER O WITH STROKE - LATIN SMALL LETTER THORN
229 return wchar_t(uint16(wchar)-0x0020);
230 if(wchar >= 0x0101 && wchar <= 0x012F) // LATIN SMALL LETTER A WITH MACRON - LATIN SMALL LETTER I WITH OGONEK (only %2=1)
232 if(wchar % 2 == 1)
233 return wchar_t(uint16(wchar)-0x0001);
235 if(wchar >= 0x0430 && wchar <= 0x044F) // CYRILLIC SMALL LETTER A - CYRILLIC SMALL LETTER YA
236 return wchar_t(uint16(wchar)-0x0020);
237 if(wchar == 0x0451) // CYRILLIC SMALL LETTER IO
238 return wchar_t(0x0401);
240 return wchar;
243 inline wchar_t wcharToUpperOnlyLatin(wchar_t wchar)
245 return isBasicLatinCharacter(wchar) ? wcharToUpper(wchar) : wchar;
248 inline wchar_t wcharToLower(wchar_t wchar)
250 if(wchar >= L'A' && wchar <= L'Z') // LATIN CAPITAL LETTER A - LATIN CAPITAL LETTER Z
251 return wchar_t(uint16(wchar)+0x0020);
252 if(wchar >= 0x00C0 && wchar <= 0x00D6) // LATIN CAPITAL LETTER A WITH GRAVE - LATIN CAPITAL LETTER O WITH DIAERESIS
253 return wchar_t(uint16(wchar)+0x0020);
254 if(wchar >= 0x00D8 && wchar <= 0x00DE) // LATIN CAPITAL LETTER O WITH STROKE - LATIN CAPITAL LETTER THORN
255 return wchar_t(uint16(wchar)+0x0020);
256 if(wchar >= 0x0100 && wchar <= 0x012E) // LATIN CAPITAL LETTER A WITH MACRON - LATIN CAPITAL LETTER I WITH OGONEK (only %2=0)
258 if(wchar % 2 == 0)
259 return wchar_t(uint16(wchar)+0x0001);
261 if(wchar == 0x1E9E) // LATIN CAPITAL LETTER SHARP S
262 return wchar_t(0x00DF);
263 if(wchar == 0x0401) // CYRILLIC CAPITAL LETTER IO
264 return wchar_t(0x0451);
265 if(wchar >= 0x0410 && wchar <= 0x042F) // CYRILLIC CAPITAL LETTER A - CYRILLIC CAPITAL LETTER YA
266 return wchar_t(uint16(wchar)+0x0020);
268 return wchar;
271 inline void wstrToUpper(std::wstring& str)
273 std::transform( str.begin(), str.end(), str.begin(), wcharToUpper );
276 inline void wstrToLower(std::wstring& str)
278 std::transform( str.begin(), str.end(), str.begin(), wcharToLower );
281 std::wstring GetMainPartOfName(std::wstring wname, uint32 declension);
283 bool utf8ToConsole(const std::string& utf8str, std::string& conStr);
284 bool consoleToUtf8(const std::string& conStr,std::string& utf8str);
285 bool Utf8FitTo(const std::string& str, std::wstring search);
287 #if PLATFORM == PLATFORM_WINDOWS
288 #define UTF8PRINTF(OUT,FRM,RESERR) \
290 char temp_buf[32*1024]; \
291 va_list ap; \
292 va_start(ap, FRM); \
293 size_t temp_len = vsnprintf(temp_buf,32*1024,FRM,ap); \
294 va_end(ap); \
296 wchar_t wtemp_buf[32*1024]; \
297 size_t wtemp_len = 32*1024-1; \
298 if(!Utf8toWStr(temp_buf,temp_len,wtemp_buf,wtemp_len)) \
299 return RESERR; \
300 CharToOemBuffW(&wtemp_buf[0],&temp_buf[0],wtemp_len+1);\
301 fprintf(OUT,temp_buf); \
303 #else
304 #define UTF8PRINTF(OUT,FRM,RESERR) \
306 va_list ap; \
307 va_start(ap, FRM); \
308 vfprintf(OUT, FRM, ap ); \
309 va_end(ap); \
311 #endif
313 bool IsIPAddress(char const* ipaddress);
314 uint32 CreatePIDFile(const std::string& filename);
316 #endif