root/trunk/src/shared/Util.h @ 2

Revision 2, 11.9 kB (checked in by yumileroy, 17 years ago)

[svn] * Proper SVN structure

Original author: Neo2003
Date: 2008-10-02 16:23:55-05:00

Line 
1/*
2 * Copyright (C) 2005-2008 MaNGOS <http://www.mangosproject.org/>
3 *
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.
8 *
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.
13 *
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
17 */
18
19#ifndef _UTIL_H
20#define _UTIL_H
21
22#include "Common.h"
23
24#include <string>
25#include <vector>
26
27typedef std::vector<std::string> Tokens;
28
29Tokens StrSplit(const std::string &src, const std::string &sep);
30
31void stripLineInvisibleChars(std::string &src);
32
33std::string secsToTimeString(uint32 timeInSecs, bool shortText = false, bool hoursOnly = false);
34uint32 TimeStringToSecs(std::string timestring);
35std::string TimeToTimestampStr(time_t t);
36
37inline uint32 secsToTimeBitFields(time_t secs)
38{
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;
41}
42
43/* Return a random number in the range min..max; (max-min) must be smaller than 32768. */
44MANGOS_DLL_SPEC int32 irand(int32 min, int32 max);
45
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. */
48MANGOS_DLL_SPEC uint32 urand(uint32 min, uint32 max);
49
50/* Return a random number in the range 0 .. RAND32_MAX. */
51MANGOS_DLL_SPEC int32 rand32();
52
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. */
56MANGOS_DLL_SPEC double rand_norm(void);
57
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. */
61MANGOS_DLL_SPEC double rand_chance(void);
62
63/* Return true if a random roll fits in the specified chance (range 0-100). */
64inline bool roll_chance_f(float chance)
65{
66    return chance > rand_chance();
67}
68
69/* Return true if a random roll fits in the specified chance (range 0-100). */
70inline bool roll_chance_i(int chance)
71{
72    return chance > irand(0, 99);
73}
74
75inline void ApplyModUInt32Var(uint32& var, int32 val, bool apply)
76{
77    int32 cur = var;
78    cur += (apply ? val : -val);
79    if(cur < 0)
80        cur = 0;
81    var = cur;
82}
83
84inline void ApplyModFloatVar(float& var, float  val, bool apply)
85{
86    var += (apply ? val : -val);
87    if(var < 0)
88        var = 0;
89}
90
91inline void ApplyPercentModFloatVar(float& var, float val, bool apply)
92{
93    if (!apply && val == -100.0f)
94        val = -99.99f;
95    var *= (apply?(100.0f+val)/100.0f : 100.0f / (100.0f+val));
96}
97
98bool Utf8toWStr(std::string utf8str, std::wstring& wstr);
99// in wsize==max size of buffer, out wsize==real string size
100bool Utf8toWStr(char const* utf8str, size_t csize, wchar_t* wstr, size_t& wsize);
101inline bool Utf8toWStr(std::string utf8str, wchar_t* wstr, size_t& wsize)
102{ 
103    return Utf8toWStr(utf8str.c_str(), utf8str.size(), wstr, wsize);
104}
105
106bool WStrToUtf8(std::wstring wstr, std::string& utf8str);
107// size==real string size
108bool WStrToUtf8(wchar_t* wstr, size_t size, std::string& utf8str);
109
110size_t utf8length(std::string& utf8str);                    // set string to "" if invalid utf8 sequence
111void utf8truncate(std::string& utf8str,size_t len);
112
113inline bool isBasicLatinCharacter(wchar_t wchar)
114{
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;
120}
121
122inline bool isExtendedLatinCharacter(wchar_t wchar)
123{
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;
141}
142
143inline bool isCyrillicCharacter(wchar_t wchar)
144{
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;
150}
151
152inline bool isEastAsianCharacter(wchar_t wchar)
153{
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;
171}
172
173inline bool isNumericOrSpace(wchar_t wchar)
174{
175    return (wchar >= L'0' && wchar <=L'9') || wchar == L' ';
176}
177
178inline bool isBasicLatinString(std::wstring wstr, bool numericOrSpace)
179{
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;
184}
185
186inline bool isExtendedLatinString(std::wstring wstr, bool numericOrSpace)
187{
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;
192}
193
194inline bool isCyrillicString(std::wstring wstr, bool numericOrSpace)
195{
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;
200}
201
202inline bool isEastAsianString(std::wstring wstr, bool numericOrSpace)
203{
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;
208}
209
210inline wchar_t wcharToUpper(wchar_t wchar)
211{
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)
221    {
222        if(wchar % 2 == 1)
223            return wchar_t(uint16(wchar)-0x0001);
224    }
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);
229
230    return wchar;
231}
232
233inline wchar_t wcharToUpperOnlyLatin(wchar_t wchar)
234{
235    return isBasicLatinCharacter(wchar) ? wcharToUpper(wchar) : wchar;
236}
237
238inline wchar_t wcharToLower(wchar_t wchar)
239{
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)
247    {
248        if(wchar % 2 == 0)
249            return wchar_t(uint16(wchar)+0x0001);
250    }
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);
257
258    return wchar;
259}
260
261inline void wstrToUpper(std::wstring& str)
262{
263    std::transform( str.begin(), str.end(), str.begin(), wcharToUpper );
264}
265
266inline void wstrToLower(std::wstring& str)
267{
268    std::transform( str.begin(), str.end(), str.begin(), wcharToLower );
269}
270
271std::wstring GetMainPartOfName(std::wstring wname, uint32 declension);
272
273bool utf8ToConsole(std::string utf8str, std::string& conStr);
274bool consoleToUtf8(std::string conStr,std::string& utf8str);
275bool Utf8FitTo(std::string str, std::wstring search);
276
277#if PLATFORM == PLATFORM_WINDOWS
278#define UTF8PRINTF(OUT,FRM,RESERR)                      \
279{                                                       \
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);                                         \
285                                                        \
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);                              \
292}
293#else
294#define UTF8PRINTF(OUT,FRM,RESERR)                      \
295{                                                       \
296    va_list ap;                                         \
297    va_start(ap, FRM);                                  \
298    vfprintf(OUT, FRM, ap );                            \
299    va_end(ap);                                         \
300}
301#endif
302
303bool IsIPAddress(char const* ipaddress);
304uint32 CreatePIDFile(std::string filename);
305
306#endif
Note: See TracBrowser for help on using the browser.