Merge branch 'master' into 303
[getmangos.git] / src / shared / Util.h
blob6577a7d9fc53d06c0c4ae65deb081ad4f7b71cc8
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(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(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(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 isNumericOrSpace(wchar_t wchar)
175 return (wchar >= L'0' && wchar <=L'9') || wchar == L' ';
178 inline bool isBasicLatinString(std::wstring wstr, bool numericOrSpace)
180 for(size_t i = 0; i < wstr.size(); ++i)
181 if(!isBasicLatinCharacter(wstr[i]) && (!numericOrSpace || !isNumericOrSpace(wstr[i])))
182 return false;
183 return true;
186 inline bool isExtendedLatinString(std::wstring wstr, bool numericOrSpace)
188 for(size_t i = 0; i < wstr.size(); ++i)
189 if(!isExtendedLatinCharacter(wstr[i]) && (!numericOrSpace || !isNumericOrSpace(wstr[i])))
190 return false;
191 return true;
194 inline bool isCyrillicString(std::wstring wstr, bool numericOrSpace)
196 for(size_t i = 0; i < wstr.size(); ++i)
197 if(!isCyrillicCharacter(wstr[i]) && (!numericOrSpace || !isNumericOrSpace(wstr[i])))
198 return false;
199 return true;
202 inline bool isEastAsianString(std::wstring wstr, bool numericOrSpace)
204 for(size_t i = 0; i < wstr.size(); ++i)
205 if(!isEastAsianCharacter(wstr[i]) && (!numericOrSpace || !isNumericOrSpace(wstr[i])))
206 return false;
207 return true;
210 inline wchar_t wcharToUpper(wchar_t wchar)
212 if(wchar >= L'a' && wchar <= L'z') // LATIN SMALL LETTER A - LATIN SMALL LETTER Z
213 return wchar_t(uint16(wchar)-0x0020);
214 if(wchar == 0x00DF) // LATIN SMALL LETTER SHARP S
215 return wchar_t(0x1E9E);
216 if(wchar >= 0x00E0 && wchar <= 0x00F6) // LATIN SMALL LETTER A WITH GRAVE - LATIN SMALL LETTER O WITH DIAERESIS
217 return wchar_t(uint16(wchar)-0x0020);
218 if(wchar >= 0x00F8 && wchar <= 0x00FE) // LATIN SMALL LETTER O WITH STROKE - LATIN SMALL LETTER THORN
219 return wchar_t(uint16(wchar)-0x0020);
220 if(wchar >= 0x0101 && wchar <= 0x012F) // LATIN SMALL LETTER A WITH MACRON - LATIN SMALL LETTER I WITH OGONEK (only %2=1)
222 if(wchar % 2 == 1)
223 return wchar_t(uint16(wchar)-0x0001);
225 if(wchar >= 0x0430 && wchar <= 0x044F) // CYRILLIC SMALL LETTER A - CYRILLIC SMALL LETTER YA
226 return wchar_t(uint16(wchar)-0x0020);
227 if(wchar == 0x0451) // CYRILLIC SMALL LETTER IO
228 return wchar_t(0x0401);
230 return wchar;
233 inline wchar_t wcharToUpperOnlyLatin(wchar_t wchar)
235 return isBasicLatinCharacter(wchar) ? wcharToUpper(wchar) : wchar;
238 inline wchar_t wcharToLower(wchar_t wchar)
240 if(wchar >= L'A' && wchar <= L'Z') // LATIN CAPITAL LETTER A - LATIN CAPITAL LETTER Z
241 return wchar_t(uint16(wchar)+0x0020);
242 if(wchar >= 0x00C0 && wchar <= 0x00D6) // LATIN CAPITAL LETTER A WITH GRAVE - LATIN CAPITAL LETTER O WITH DIAERESIS
243 return wchar_t(uint16(wchar)+0x0020);
244 if(wchar >= 0x00D8 && wchar <= 0x00DE) // LATIN CAPITAL LETTER O WITH STROKE - LATIN CAPITAL LETTER THORN
245 return wchar_t(uint16(wchar)+0x0020);
246 if(wchar >= 0x0100 && wchar <= 0x012E) // LATIN CAPITAL LETTER A WITH MACRON - LATIN CAPITAL LETTER I WITH OGONEK (only %2=0)
248 if(wchar % 2 == 0)
249 return wchar_t(uint16(wchar)+0x0001);
251 if(wchar == 0x1E9E) // LATIN CAPITAL LETTER SHARP S
252 return wchar_t(0x00DF);
253 if(wchar == 0x0401) // CYRILLIC CAPITAL LETTER IO
254 return wchar_t(0x0451);
255 if(wchar >= 0x0410 && wchar <= 0x042F) // CYRILLIC CAPITAL LETTER A - CYRILLIC CAPITAL LETTER YA
256 return wchar_t(uint16(wchar)+0x0020);
258 return wchar;
261 inline void wstrToUpper(std::wstring& str)
263 std::transform( str.begin(), str.end(), str.begin(), wcharToUpper );
266 inline void wstrToLower(std::wstring& str)
268 std::transform( str.begin(), str.end(), str.begin(), wcharToLower );
271 std::wstring GetMainPartOfName(std::wstring wname, uint32 declension);
273 bool utf8ToConsole(std::string utf8str, std::string& conStr);
274 bool consoleToUtf8(std::string conStr,std::string& utf8str);
275 bool Utf8FitTo(std::string str, std::wstring search);
277 #if PLATFORM == PLATFORM_WINDOWS
278 #define UTF8PRINTF(OUT,FRM,RESERR) \
280 char temp_buf[6000]; \
281 va_list ap; \
282 va_start(ap, FRM); \
283 size_t temp_len = vsnprintf(temp_buf,6000,FRM,ap); \
284 va_end(ap); \
286 wchar_t wtemp_buf[6000]; \
287 size_t wtemp_len = 6000-1; \
288 if(!Utf8toWStr(temp_buf,temp_len,wtemp_buf,wtemp_len)) \
289 return RESERR; \
290 CharToOemBuffW(&wtemp_buf[0],&temp_buf[0],wtemp_len+1);\
291 fprintf(OUT,temp_buf); \
293 #else
294 #define UTF8PRINTF(OUT,FRM,RESERR) \
296 va_list ap; \
297 va_start(ap, FRM); \
298 vfprintf(OUT, FRM, ap ); \
299 va_end(ap); \
301 #endif
303 bool IsIPAddress(char const* ipaddress);
304 uint32 CreatePIDFile(std::string filename);
306 #endif