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