OregonCore  revision fb2a440-git
Your Favourite TBC server
Util.h File Reference
#include "Common.h"
#include <string>
#include <vector>

Go to the source code of this file.

Macros

#define UTF8PRINTF(OUT, FRM, RESERR)
 

Typedefs

typedef std::vector< std::string > Tokens
 

Functions

Tokens StrSplit (const std::string &src, const std::string &sep)
 
void stripLineInvisibleChars (std::string &src)
 
std::string secsToTimeString (uint32 timeInSecs, bool shortText=false, bool hoursOnly=false)
 
uint32 TimeStringToSecs (const std::string &timestring)
 
std::string TimeToTimestampStr (time_t t)
 
uint32 secsToTimeBitFields (time_t secs)
 
int32 irand (int32 min, int32 max)
 
uint32 urand (uint32 min, uint32 max)
 
uint32 rand32 ()
 
float frand (float min, float max)
 
double rand_norm ()
 
double rand_chance ()
 
bool roll_chance_f (float chance)
 
bool roll_chance_i (int chance)
 
void ApplyModUInt32Var (uint32 &var, int32 val, bool apply)
 
void ApplyModFloatVar (float &var, float val, bool apply)
 
void ApplyPercentModFloatVar (float &var, float val, bool apply)
 
template<class T , class U >
CalculatePct (T base, U pct)
 
template<class T , class U >
AddPct (T &base, U pct)
 
template<class T , class U >
ApplyPct (T &base, U pct)
 
template<class T >
RoundToInterval (T &num, T floor, T ceil)
 
bool Utf8toWStr (const std::string &utf8str, std::wstring &wstr)
 
bool Utf8toWStr (char const *utf8str, size_t csize, wchar_t *wstr, size_t &wsize)
 
bool Utf8toWStr (const std::string &utf8str, wchar_t *wstr, size_t &wsize)
 
bool WStrToUtf8 (std::wstring wstr, std::string &utf8str)
 
bool WStrToUtf8 (wchar_t *wstr, size_t size, std::string &utf8str)
 
size_t utf8length (std::string &utf8str)
 
void utf8truncate (std::string &utf8str, size_t len)
 
bool isBasicLatinCharacter (wchar_t wchar)
 
bool isExtendedLatinCharacter (wchar_t wchar)
 
bool isCyrillicCharacter (wchar_t wchar)
 
bool isEastAsianCharacter (wchar_t wchar)
 
bool isNumeric (wchar_t wchar)
 
bool isNumeric (char c)
 
bool isNumericOrSpace (wchar_t wchar)
 
bool isNumeric (char const *str)
 
bool isBasicLatinString (std::wstring wstr, bool numericOrSpace)
 
bool isExtendedLatinString (std::wstring wstr, bool numericOrSpace)
 
bool isCyrillicString (std::wstring wstr, bool numericOrSpace)
 
bool isEastAsianString (std::wstring wstr, bool numericOrSpace)
 
wchar_t wcharToUpper (wchar_t wchar)
 
wchar_t wcharToUpperOnlyLatin (wchar_t wchar)
 
wchar_t wcharToLower (wchar_t wchar)
 
void wstrToUpper (std::wstring &str)
 
void wstrToLower (std::wstring &str)
 
std::wstring GetMainPartOfName (std::wstring wname, uint32 declension)
 
bool utf8ToConsole (const std::string &utf8str, std::string &conStr)
 
bool consoleToUtf8 (const std::string &conStr, std::string &utf8str)
 
bool Utf8FitTo (const std::string &str, std::wstring search)
 
void hexEncodeByteArray (uint8 *bytes, uint32 arrayLen, std::string &result)
 
std::string ByteArrayToHexStr (uint8 *bytes, uint32 length)
 
bool IsIPAddress (char const *ipaddress)
 
uint32 CreatePIDFile (const std::string &filename)
 
template<class C >
C::value_type const & SelectRandomContainerElement (C const &container)
 

Macro Definition Documentation

#define UTF8PRINTF (   OUT,
  FRM,
  RESERR 
)
Value:
{ \
char temp_buf[6000]; \
va_list ap; \
va_start(ap, FRM); \
size_t temp_len = vsnprintf(temp_buf,6000,FRM,ap); \
va_end(ap); \
\
wchar_t wtemp_buf[6000]; \
size_t wtemp_len = 6000-1; \
if (!Utf8toWStr(temp_buf,temp_len,wtemp_buf,wtemp_len)) \
return RESERR; \
CharToOemBuffW(&wtemp_buf[0],&temp_buf[0],wtemp_len+1);\
fprintf(OUT,temp_buf); \
}
bool Utf8toWStr(const std::string &utf8str, std::wstring &wstr)
Definition: Util.cpp:281
#define vsnprintf
Definition: Common.h:131

Definition at line 322 of file Util.h.

Typedef Documentation

typedef std::vector<std::string> Tokens

Definition at line 26 of file Util.h.

Function Documentation

template<class T , class U >
T AddPct ( T &  base,
pct 
)
inline
void ApplyModFloatVar ( float &  var,
float  val,
bool  apply 
)
inline

Definition at line 81 of file Util.h.

82 {
83  var += (apply ? val : -val);
84  if (var < 0)
85  var = 0;
86 }
void apply(T *val)
Definition: ByteConverter.h:41
void ApplyModUInt32Var ( uint32 var,
int32  val,
bool  apply 
)
inline

Definition at line 72 of file Util.h.

73 {
74  int32 cur = var;
75  cur += (apply ? val : -val);
76  if (cur < 0)
77  cur = 0;
78  var = cur;
79 }
ACE_INT32 int32
Definition: Define.h:67
void apply(T *val)
Definition: ByteConverter.h:41
template<class T , class U >
T ApplyPct ( T &  base,
pct 
)
inline

Definition at line 109 of file Util.h.

References CalculatePct().

110 {
111  return base = CalculatePct(base, pct);
112 }
T CalculatePct(T base, U pct)
Definition: Util.h:97
void ApplyPercentModFloatVar ( float &  var,
float  val,
bool  apply 
)
inline

Definition at line 88 of file Util.h.

Referenced by Unit::ApplyAttackTimePercentMod(), Object::ApplyPercentModFloatValue(), Aura::HandleAuraModScale(), Player::HandleBaseModValue(), Aura::HandleModThreat(), and Unit::HandleStatModifier().

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 }
void apply(T *val)
Definition: ByteConverter.h:41
std::string ByteArrayToHexStr ( uint8 bytes,
uint32  length 
)

Definition at line 454 of file Util.cpp.

Referenced by WardenMac::Init(), WardenWin::Init(), and wstrToLower().

455 {
456  std::ostringstream ss;
457  for (uint32 i = 0; i < length; ++i)
458  {
459  char buffer[4];
460  sprintf(buffer, "%02X", bytes[i]);
461  ss << buffer;
462  }
463 
464  return ss.str();
465 }
ACE_UINT32 uint32
Definition: Define.h:71
bool consoleToUtf8 ( const std::string &  conStr,
std::string &  utf8str 
)

Definition at line 404 of file Util.cpp.

References WStrToUtf8().

Referenced by wstrToLower().

405 {
406  #if PLATFORM == PLATFORM_WINDOWS
407  std::wstring wstr;
408  wstr.resize(conStr.size());
409  OemToCharBuffW(&conStr[0], &wstr[0], conStr.size());
410 
411  return WStrToUtf8(wstr, utf8str);
412  #else
413  // not implemented yet
414  utf8str = conStr;
415  return true;
416  #endif
417 }
bool WStrToUtf8(wchar_t *wstr, size_t size, std::string &utf8str)
Definition: Util.cpp:299
uint32 CreatePIDFile ( const std::string &  filename)

Definition at line 203 of file Util.cpp.

Referenced by main(), and Master::Run().

204 {
205  FILE* pid_file = fopen (filename.c_str(), "w" );
206  if (pid_file == NULL)
207  return 0;
208 
209  #ifdef _WIN32
210  DWORD pid = GetCurrentProcessId();
211  #else
212  pid_t pid = getpid();
213  #endif
214 
215  fprintf(pid_file, "%u", pid );
216  fclose(pid_file);
217 
218  return (uint32)pid;
219 }
ACE_UINT32 uint32
Definition: Define.h:71
float frand ( float  min,
float  max 
)

Definition at line 38 of file Util.cpp.

Referenced by FleeingMovementGenerator< T >::_getPoint(), and secsToTimeBitFields().

39 {
40  return float(sfmtRand->Random() * (max - min) + min);
41 }
std::wstring GetMainPartOfName ( std::wstring  wname,
uint32  declension 
)

Definition at line 341 of file Util.cpp.

References isCyrillicCharacter().

Referenced by ObjectMgr::CheckDeclinedNames(), WorldSession::HandleDeclinedPlayerNameOpcode(), WorldSession::HandlePetRename(), and wstrToLower().

342 {
343  // supported only Cyrillic cases
344  if (wname.size() < 1 || !isCyrillicCharacter(wname[0]) || declension > 5)
345  return wname;
346 
347  // Important: end length must be <= MAX_INTERNAL_PLAYER_NAME-MAX_PLAYER_NAME (3 currently)
348 
349  static wchar_t const a_End[] = { wchar_t(1), wchar_t(0x0430), wchar_t(0x0000)};
350  static wchar_t const o_End[] = { wchar_t(1), wchar_t(0x043E), wchar_t(0x0000)};
351  static wchar_t const ya_End[] = { wchar_t(1), wchar_t(0x044F), wchar_t(0x0000)};
352  static wchar_t const ie_End[] = { wchar_t(1), wchar_t(0x0435), wchar_t(0x0000)};
353  static wchar_t const i_End[] = { wchar_t(1), wchar_t(0x0438), wchar_t(0x0000)};
354  static wchar_t const yeru_End[] = { wchar_t(1), wchar_t(0x044B), wchar_t(0x0000)};
355  static wchar_t const u_End[] = { wchar_t(1), wchar_t(0x0443), wchar_t(0x0000)};
356  static wchar_t const yu_End[] = { wchar_t(1), wchar_t(0x044E), wchar_t(0x0000)};
357  static wchar_t const oj_End[] = { wchar_t(2), wchar_t(0x043E), wchar_t(0x0439), wchar_t(0x0000)};
358  static wchar_t const ie_j_End[] = { wchar_t(2), wchar_t(0x0435), wchar_t(0x0439), wchar_t(0x0000)};
359  static wchar_t const io_j_End[] = { wchar_t(2), wchar_t(0x0451), wchar_t(0x0439), wchar_t(0x0000)};
360  static wchar_t const o_m_End[] = { wchar_t(2), wchar_t(0x043E), wchar_t(0x043C), wchar_t(0x0000)};
361  static wchar_t const io_m_End[] = { wchar_t(2), wchar_t(0x0451), wchar_t(0x043C), wchar_t(0x0000)};
362  static wchar_t const ie_m_End[] = { wchar_t(2), wchar_t(0x0435), wchar_t(0x043C), wchar_t(0x0000)};
363  static wchar_t const soft_End[] = { wchar_t(1), wchar_t(0x044C), wchar_t(0x0000)};
364  static wchar_t const j_End[] = { wchar_t(1), wchar_t(0x0439), wchar_t(0x0000)};
365 
366  static wchar_t const* const dropEnds[6][8] =
367  {
368  { &a_End[1], &o_End[1], &ya_End[1], &ie_End[1], &soft_End[1], &j_End[1], NULL, NULL },
369  { &a_End[1], &ya_End[1], &yeru_End[1], &i_End[1], NULL, NULL, NULL, NULL },
370  { &ie_End[1], &u_End[1], &yu_End[1], &i_End[1], NULL, NULL, NULL, NULL },
371  { &u_End[1], &yu_End[1], &o_End[1], &ie_End[1], &soft_End[1], &ya_End[1], &a_End[1], NULL },
372  { &oj_End[1], &io_j_End[1], &ie_j_End[1], &o_m_End[1], &io_m_End[1], &ie_m_End[1], &yu_End[1], NULL },
373  { &ie_End[1], &i_End[1], NULL, NULL, NULL, NULL, NULL, NULL }
374  };
375 
376  for (wchar_t const * const* itr = &dropEnds[declension][0]; *itr; ++itr)
377  {
378  size_t len = size_t((*itr)[-1]); // get length from string size field
379 
380  if (wname.substr(wname.size() - len, len) == *itr)
381  return wname.substr(0, wname.size() - len);
382  }
383 
384  return wname;
385 }
bool isCyrillicCharacter(wchar_t wchar)
Definition: Util.h:166
void hexEncodeByteArray ( uint8 bytes,
uint32  arrayLen,
std::string &  result 
)

Definition at line 435 of file Util.cpp.

Referenced by AccountMgr::CalculateShaPassHash(), and wstrToLower().

436 {
437  std::ostringstream ss;
438  for (uint32 i = 0; i < arrayLen; ++i)
439  {
440  for (uint8 j = 0; j < 2; ++j)
441  {
442  unsigned char nibble = 0x0F & (bytes[i] >> ((1 - j) * 4));
443  char encodedNibble;
444  if (nibble < 0x0A)
445  encodedNibble = '0' + nibble;
446  else
447  encodedNibble = 'A' + nibble - 0x0A;
448  ss << encodedNibble;
449  }
450  }
451  result = ss.str();
452 }
ACE_UINT8 uint8
Definition: Define.h:73
ACE_UINT32 uint32
Definition: Define.h:71
bool isBasicLatinCharacter ( wchar_t  wchar)
inline

Definition at line 136 of file Util.h.

Referenced by isBasicLatinString(), isExtendedLatinCharacter(), and wcharToUpperOnlyLatin().

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 }
bool isBasicLatinString ( std::wstring  wstr,
bool  numericOrSpace 
)
inline

Definition at line 220 of file Util.h.

References isBasicLatinCharacter(), and isNumericOrSpace().

Referenced by isValidString().

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 }
bool isBasicLatinCharacter(wchar_t wchar)
Definition: Util.h:136
bool isNumericOrSpace(wchar_t wchar)
Definition: Util.h:206
bool isCyrillicCharacter ( wchar_t  wchar)
inline

Definition at line 166 of file Util.h.

Referenced by GetMainPartOfName(), WorldSession::HandleDeclinedPlayerNameOpcode(), and isCyrillicString().

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 }
bool isCyrillicString ( std::wstring  wstr,
bool  numericOrSpace 
)
inline

Definition at line 236 of file Util.h.

References isCyrillicCharacter(), and isNumericOrSpace().

Referenced by isValidString().

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 }
bool isCyrillicCharacter(wchar_t wchar)
Definition: Util.h:166
bool isNumericOrSpace(wchar_t wchar)
Definition: Util.h:206
bool isEastAsianCharacter ( wchar_t  wchar)
inline

Definition at line 175 of file Util.h.

Referenced by isEastAsianString().

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 }
bool isEastAsianString ( std::wstring  wstr,
bool  numericOrSpace 
)
inline

Definition at line 244 of file Util.h.

References isEastAsianCharacter(), and isNumericOrSpace().

Referenced by isValidString().

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 }
bool isEastAsianCharacter(wchar_t wchar)
Definition: Util.h:175
bool isNumericOrSpace(wchar_t wchar)
Definition: Util.h:206
bool isExtendedLatinCharacter ( wchar_t  wchar)
inline

Definition at line 145 of file Util.h.

References isBasicLatinCharacter().

Referenced by isExtendedLatinString().

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 }
bool isBasicLatinCharacter(wchar_t wchar)
Definition: Util.h:136
bool isExtendedLatinString ( std::wstring  wstr,
bool  numericOrSpace 
)
inline

Definition at line 228 of file Util.h.

References isExtendedLatinCharacter(), and isNumericOrSpace().

Referenced by isValidString().

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 }
bool isExtendedLatinCharacter(wchar_t wchar)
Definition: Util.h:145
bool isNumericOrSpace(wchar_t wchar)
Definition: Util.h:206
bool IsIPAddress ( char const *  ipaddress)

Definition at line 192 of file Util.cpp.

Referenced by ChatHandler::HandleBanHelper(), ChatHandler::HandleBanInfoIPCommand(), and ChatHandler::HandleUnBanHelper().

193 {
194  if (!ipaddress)
195  return false;
196 
197  // Let the big boys do it.
198  // Drawback: all valid ip address formats are recognized e.g.: 12.23,121234,0xABCD)
199  return inet_addr(ipaddress) != INADDR_NONE;
200 }
bool isNumeric ( wchar_t  wchar)
inline

Definition at line 196 of file Util.h.

Referenced by ChatHandler::GetDeletedCharacterInfoList(), ChatHandler::HandleCharacterDeletedOldCommand(), ChatHandler::HandleWritePDumpCommand(), isNumeric(), and isNumericOrSpace().

197 {
198  return (wchar >= L'0' && wchar <= L'9');
199 }
bool isNumeric ( char  c)
inline

Definition at line 201 of file Util.h.

202 {
203  return (c >= '0' && c <= '9');
204 }
bool isNumeric ( char const *  str)
inline

Definition at line 211 of file Util.h.

References isNumeric().

212 {
213  for (char const* c = str; *c; ++c)
214  if (!isNumeric(*c))
215  return false;
216 
217  return true;
218 }
bool isNumeric(wchar_t wchar)
Definition: Util.h:196
bool isNumericOrSpace ( wchar_t  wchar)
inline

Definition at line 206 of file Util.h.

References isNumeric().

Referenced by isBasicLatinString(), isCyrillicString(), isEastAsianString(), and isExtendedLatinString().

207 {
208  return isNumeric(wchar) || wchar == L' ';
209 }
bool isNumeric(wchar_t wchar)
Definition: Util.h:196
uint32 rand32 ( )

Definition at line 43 of file Util.cpp.

Referenced by WardenWin::RequestData(), secsToTimeBitFields(), and Spell::SpellDamageSchoolDmg().

44 {
45  return sfmtRand->BRandom();
46 }
double rand_chance ( )

Definition at line 53 of file Util.cpp.

Referenced by GetItemEnchantMod(), ChatHandler::HandleGMTicketCloseByIdCommand(), LootTemplate::LootGroup::Roll(), roll_chance_f(), PoolGroup< T >::RollOne(), and secsToTimeBitFields().

54 {
55  return sfmtRand->Random() * 100.0;
56 }
template<class T >
T RoundToInterval ( T &  num,
floor,
ceil 
)
inline

Definition at line 115 of file Util.h.

References Utf8toWStr().

116 {
117  return num = std::min(std::max(num, floor), ceil);
118 }
uint32 secsToTimeBitFields ( time_t  secs)
inline

Definition at line 36 of file Util.h.

References frand(), irand(), rand32(), rand_chance(), rand_norm(), and urand().

Referenced by AuctionHouseMgr::SendAuctionSalePendingMail(), and Player::SendInitialPacketsBeforeAddToMap().

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 }
ACE_UINT32 uint32
Definition: Define.h:71
std::string secsToTimeString ( uint32  timeInSecs,
bool  shortText = false,
bool  hoursOnly = false 
)

Definition at line 109 of file Util.cpp.

References DAY, HOUR, and MINUTE.

Referenced by ChatHandler::HandleBanHelper(), ChatHandler::HandleBanInfoHelper(), ChatHandler::HandleBanInfoIPCommand(), ChatHandler::HandleEventInfoCommand(), ChatHandler::HandleGMTicketGetByIdCommand(), ChatHandler::HandleGMTicketGetByNameCommand(), ChatHandler::HandleGMTicketListClosedCommand(), ChatHandler::HandleGMTicketListCommand(), ChatHandler::HandleGMTicketListOnlineCommand(), WorldSession::HandleMessagechatOpcode(), ChatHandler::HandleNpcInfoCommand(), ChatHandler::HandlePInfoCommand(), ChatHandler::HandleServerInfoCommand(), ChatHandler::HandleTargetObjectCommand(), WorldSession::HandleTextEmoteOpcode(), and World::ShutdownMsg().

110 {
111  uint32 secs = timeInSecs % MINUTE;
112  uint32 minutes = timeInSecs % HOUR / MINUTE;
113  uint32 hours = timeInSecs % DAY / HOUR;
114  uint32 days = timeInSecs / DAY;
115 
116  std::ostringstream ss;
117  if (days)
118  ss << days << (shortText ? "d" : " Day(s) ");
119  if (hours || hoursOnly)
120  ss << hours << (shortText ? "h" : " Hour(s) ");
121  if (!hoursOnly)
122  {
123  if (minutes)
124  ss << minutes << (shortText ? "m" : " Minute(s) ");
125  if (secs || (!days && !hours && !minutes) )
126  ss << secs << (shortText ? "s" : " Second(s).");
127  }
128 
129  return ss.str();
130 }
Definition: Common.h:181
Definition: Common.h:179
Definition: Common.h:180
ACE_UINT32 uint32
Definition: Define.h:71
template<class C >
C::value_type const& SelectRandomContainerElement ( C const &  container)

Definition at line 352 of file Util.h.

References urand().

Referenced by Unit::SelectNearbyTarget(), UnitAI::SelectTarget(), and PoolGroup< Quest >::SpawnObject().

353 {
354  typename C::const_iterator it = container.begin();
355  std::advance(it, urand(0, container.size() - 1));
356  return *it;
357 }
uint32 urand(uint32 min, uint32 max)
Definition: Util.cpp:33
void stripLineInvisibleChars ( std::string &  src)

Definition at line 76 of file Util.cpp.

Referenced by WorldSession::processChatmessageFurtherAfterSecurityChecks().

77 {
78  static std::string const invChars = " \t\7\n";
79 
80  size_t wpos = 0;
81 
82  bool space = false;
83  for (size_t pos = 0; pos < str.size(); ++pos)
84  {
85  if (invChars.find(str[pos]) != std::string::npos)
86  {
87  if (!space)
88  {
89  str[wpos++] = ' ';
90  space = true;
91  }
92  }
93  else
94  {
95  if (wpos != pos)
96  str[wpos++] = str[pos];
97  else
98  ++wpos;
99  space = false;
100  }
101  }
102 
103  if (wpos < str.size())
104  str.erase(wpos, str.size());
105  if (str.find("|TInterface") != std::string::npos)
106  str.clear();
107 }
Tokens StrSplit ( const std::string &  src,
const std::string &  sep 
)

Definition at line 58 of file Util.cpp.

Referenced by InstanceSaveManager::_DelHelper(), Object::_LoadIntoDataField(), Player::BuildEnumData(), AHBConfig::IgnoreItemsIds(), Database::Initialize(), DisableMgr::LoadDisables(), Item::LoadFromDB(), Pet::LoadPetFromDB(), PlayerTaxi::LoadTaxiDestinationsFromString(), PlayerTaxi::LoadTaxiMask(), Object::LoadValues(), Player::LoadValuesArrayFromDB(), and RealmList::UpdateRealm().

59 {
60  Tokens r;
61  std::string s;
62  for (std::string::const_iterator i = src.begin(); i != src.end(); i++)
63  {
64  if (sep.find(*i) != std::string::npos)
65  {
66  if (s.length()) r.push_back(s);
67  s = "";
68  }
69  else
70  s += *i;
71  }
72  if (s.length()) r.push_back(s);
73  return r;
74 }
std::vector< std::string > Tokens
Definition: Util.h:26
uint32 TimeStringToSecs ( const std::string &  timestring)

Definition at line 132 of file Util.cpp.

References DAY, HOUR, and MINUTE.

Referenced by World::BanAccount(), and ChatHandler::HandleBanHelper().

133 {
134  uint32 secs = 0;
135  uint32 buffer = 0;
136  uint32 multiplier = 0;
137 
138  for (std::string::const_iterator itr = timestring.begin(); itr != timestring.end(); itr++ )
139  {
140  if (isdigit(*itr))
141  {
142  std::string str; //very complicated typecast char->const char*; is there no better way?
143  str += *itr;
144  const char* tmp = str.c_str();
145 
146  buffer *= 10;
147  buffer += atoi(tmp);
148  }
149  else
150  {
151  switch (*itr)
152  {
153  case 'd':
154  multiplier = DAY;
155  break;
156  case 'h':
157  multiplier = HOUR;
158  break;
159  case 'm':
160  multiplier = MINUTE;
161  break;
162  case 's':
163  multiplier = 1;
164  break;
165  default :
166  return 0; //bad format
167  }
168  buffer *= multiplier;
169  secs += buffer;
170  buffer = 0;
171  }
172  }
173 
174  return secs;
175 }
Definition: Common.h:181
Definition: Common.h:179
Definition: Common.h:180
ACE_UINT32 uint32
Definition: Define.h:71
std::string TimeToTimestampStr ( time_t  t)

Definition at line 177 of file Util.cpp.

References snprintf.

Referenced by ChatHandler::HandleCharacterDeletedListHelper(), and ChatHandler::HandleEventInfoCommand().

178 {
179  tm* aTm = localtime(&t);
180  // YYYY year
181  // MM month (2 digits 01-12)
182  // DD day (2 digits 01-31)
183  // HH hour (2 digits 00-23)
184  // MM minutes (2 digits 00-59)
185  // SS seconds (2 digits 00-59)
186  char buf[20];
187  snprintf(buf, 20, "%04d-%02d-%02d_%02d-%02d-%02d", aTm->tm_year + 1900, aTm->tm_mon + 1, aTm->tm_mday, aTm->tm_hour, aTm->tm_min, aTm->tm_sec);
188  return std::string(buf);
189 }
#define snprintf
Definition: Common.h:129
uint32 urand ( uint32  min,
uint32  max 
)

Definition at line 33 of file Util.cpp.

Referenced by RandomMovementGenerator< T >::_setRandomLocation(), FleeingMovementGenerator< T >::_setTargetLocation(), TargetedMovementGeneratorMedium< T, D >::_setTargetLocation(), BattlegroundAV::AddAVCreature(), AuctionHouseBot::addNewAuctionBuyerBotBid(), AuctionHouseBot::addNewAuctions(), Unit::CalcAbsorbResist(), Unit::CalculateDamage(), BattlegroundAV::ChangeMineOwner(), Spell::CheckCast(), Pet::CheckLearning(), Group::CountTheRoll(), SimpleAI::DamageTaken(), Unit::DealDamage(), Spell::EffectDispel(), Spell::EffectDummy(), Spell::EffectEnergize(), Spell::EffectScriptEffect(), Spell::EffectStealBeneficialBuff(), Spell::EffectTeleportUnits(), Spell::EffectTransmitted(), SimpleAI::EnterCombat(), Loot::generateMoneyLoot(), ObjectMgr::GeneratePetName(), ObjectMgr::GetCreatureModelRandomGender(), Player::GetNextRandomRaidMember(), CreatureInfo::GetRandomValidModelId(), ObjectMgr::GetTaxiMount(), Aura::HandleAuraDummy(), Aura::HandleAuraModShapeshift(), Player::HandleDrowning(), WorldSession::HandlePetActionHelper(), WorldSession::HandlePetCastSpellOpcode(), WorldSession::HandleRandomRollOpcode(), Battleground::HandleTriggerBuff(), ChaseMovementGenerator< T >::Initialize(), FollowMovementGenerator< T >::Initialize(), SimpleAI::KilledUnit(), Player::LoadFromDB(), LootItem::LootItem(), Unit::MagicSpellHitResult(), Unit::MeleeSpellHitResult(), SmartScript::ProcessAction(), CreatureEventAI::ProcessAction(), RAND(), Oregon::RandomResizeList(), SmartScript::RecalcTimer(), Weather::ReGenerate(), EventMap::Repeat(), BattlegroundAV::ResetBGSubclass(), Player::RewardHonor(), Unit::RollMeleeOutcomeAgainst(), PoolGroup< T >::RollOne(), secsToTimeBitFields(), Creature::SelectLevel(), SelectRandomContainerElement(), UnitAI::SelectTargetList(), CreatureTextMgr::SendChat(), Player::SendLoot(), BattlegroundAB::StartingEventOpenDoors(), BattlegroundEY::StartingEventOpenDoors(), ConfusedMovementGenerator< T >::Update(), BattlegroundQueue::Update(), WaypointMovementGenerator< T >::Update(), Creature::Update(), BattlegroundAV::Update(), PetAI::UpdateAI(), SimpleAI::UpdateAI(), CreatureEventAIHolder::UpdateRepeatTimer(), and Player::UpdateSkill().

34 {
35  return sfmtRand->URandom(min, max);
36 }
bool Utf8FitTo ( const std::string &  str,
std::wstring  search 
)

Definition at line 419 of file Util.cpp.

References Utf8toWStr(), and wstrToLower().

Referenced by AuctionHouseObject::BuildListAuctionItems(), ChatHandler::HandleLearnAllRecipesCommand(), ChatHandler::HandleLookupAreaCommand(), ChatHandler::HandleLookupCreatureCommand(), ChatHandler::HandleLookupEventCommand(), ChatHandler::HandleLookupFactionCommand(), ChatHandler::HandleLookupItemCommand(), ChatHandler::HandleLookupItemSetCommand(), ChatHandler::HandleLookupObjectCommand(), ChatHandler::HandleLookupQuestCommand(), ChatHandler::HandleLookupSkillCommand(), ChatHandler::HandleLookupSpellCommand(), ChatHandler::HandleLookupTitleCommand(), WorldSession::HandleWhoOpcode(), and wstrToLower().

420 {
421  std::wstring temp;
422 
423  if (!Utf8toWStr(str, temp))
424  return false;
425 
426  // converting to lower case
427  wstrToLower( temp );
428 
429  if (temp.find(search) == std::wstring::npos)
430  return false;
431 
432  return true;
433 }
bool Utf8toWStr(char const *utf8str, size_t csize, wchar_t *wstr, size_t &wsize)
Definition: Util.cpp:255
void wstrToLower(std::wstring &str)
Definition: Util.h:308
size_t utf8length ( std::string &  utf8str)

Definition at line 221 of file Util.cpp.

Referenced by AccountMgr::ChangePassword(), AccountMgr::ChangeUsername(), AccountMgr::CreateAccount(), and Utf8toWStr().

222 {
223  try
224  {
225  return utf8::distance(utf8str.c_str(), utf8str.c_str() + utf8str.size());
226  }
227  catch (std::exception)
228  {
229  utf8str = "";
230  return 0;
231  }
232 }
bool utf8ToConsole ( const std::string &  utf8str,
std::string &  conStr 
)

Definition at line 387 of file Util.cpp.

References Utf8toWStr().

Referenced by wstrToLower().

388 {
389  #if PLATFORM == PLATFORM_WINDOWS
390  std::wstring wstr;
391  if (!Utf8toWStr(utf8str, wstr))
392  return false;
393 
394  conStr.resize(wstr.size());
395  CharToOemBuffW(&wstr[0], &conStr[0], wstr.size());
396  #else
397  // not implemented yet
398  conStr = utf8str;
399  #endif
400 
401  return true;
402 }
bool Utf8toWStr(char const *utf8str, size_t csize, wchar_t *wstr, size_t &wsize)
Definition: Util.cpp:255
bool Utf8toWStr ( const std::string &  utf8str,
std::wstring &  wstr 
)

Definition at line 281 of file Util.cpp.

Referenced by RoundToInterval(), and Utf8toWStr().

282 {
283  try
284  {
285  size_t len = utf8::distance(utf8str.c_str(), utf8str.c_str() + utf8str.size());
286  wstr.resize(len);
287 
288  utf8::utf8to16(utf8str.c_str(), utf8str.c_str() + utf8str.size(), &wstr[0]);
289  }
290  catch (std::exception)
291  {
292  wstr = L"";
293  return false;
294  }
295 
296  return true;
297 }
bool Utf8toWStr ( char const *  utf8str,
size_t  csize,
wchar_t *  wstr,
size_t &  wsize 
)

Definition at line 255 of file Util.cpp.

Referenced by ObjectMgr::AddGameTele(), ObjectMgr::CheckDeclinedNames(), ObjectMgr::DeleteGameTele(), Log::DoLog(), ObjectMgr::GetGameTele(), WorldSession::HandleAuctionListItems(), WorldSession::HandleDeclinedPlayerNameOpcode(), ChatHandler::HandleLearnAllRecipesCommand(), ChatHandler::HandleLookupAreaCommand(), ChatHandler::HandleLookupCreatureCommand(), ChatHandler::HandleLookupEventCommand(), ChatHandler::HandleLookupFactionCommand(), ChatHandler::HandleLookupItemCommand(), ChatHandler::HandleLookupItemSetCommand(), ChatHandler::HandleLookupObjectCommand(), ChatHandler::HandleLookupQuestCommand(), ChatHandler::HandleLookupSkillCommand(), ChatHandler::HandleLookupSpellCommand(), ChatHandler::HandleLookupTeleCommand(), ChatHandler::HandleLookupTitleCommand(), ChatHandler::HandleModifyRepCommand(), WorldSession::HandlePetRename(), WorldSession::HandleWhoOpcode(), ObjectMgr::IsValidCharterName(), ObjectMgr::IsValidName(), ObjectMgr::IsValidPetName(), ObjectMgr::LoadGameTele(), normalizePlayerName(), AccountMgr::normalizeString(), Utf8FitTo(), and utf8ToConsole().

256 {
257  try
258  {
259  size_t len = utf8::distance(utf8str, utf8str + csize);
260  if (len > wsize)
261  {
262  wsize = 0;
263  *wstr = L'\0';
264  return false;
265  }
266 
267  wsize = len;
268  utf8::utf8to16(utf8str, utf8str + csize, wstr);
269  wstr[len] = L'\0';
270  }
271  catch (std::exception)
272  {
273  wsize = 0;
274  *wstr = L'\0';
275  return false;
276  }
277 
278  return true;
279 }
bool Utf8toWStr ( const std::string &  utf8str,
wchar_t *  wstr,
size_t &  wsize 
)
inline

Definition at line 124 of file Util.h.

References utf8length(), Utf8toWStr(), utf8truncate(), and WStrToUtf8().

125 {
126  return Utf8toWStr(utf8str.c_str(), utf8str.size(), wstr, wsize);
127 }
bool Utf8toWStr(const std::string &utf8str, std::wstring &wstr)
Definition: Util.cpp:281
void utf8truncate ( std::string &  utf8str,
size_t  len 
)

Definition at line 234 of file Util.cpp.

Referenced by PlayerSocial::SetFriendNote(), Guild::SetGuildBankTabText(), and Utf8toWStr().

235 {
236  try
237  {
238  size_t wlen = utf8::distance(utf8str.c_str(), utf8str.c_str() + utf8str.size());
239  if (wlen <= len)
240  return;
241 
242  std::wstring wstr;
243  wstr.resize(wlen);
244  utf8::utf8to16(utf8str.c_str(), utf8str.c_str() + utf8str.size(), &wstr[0]);
245  wstr.resize(len);
246  char* oend = utf8::utf16to8(wstr.c_str(), wstr.c_str() + wstr.size(), &utf8str[0]);
247  utf8str.resize(oend - (&utf8str[0])); // remove unused tail
248  }
249  catch (std::exception)
250  {
251  utf8str = "";
252  }
253 }
wchar_t wcharToLower ( wchar_t  wchar)
inline

Definition at line 280 of file Util.h.

Referenced by normalizePlayerName(), and wstrToLower().

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 }
ACE_UINT16 uint16
Definition: Define.h:72
wchar_t wcharToUpper ( wchar_t  wchar)
inline

Definition at line 252 of file Util.h.

Referenced by normalizePlayerName(), wcharToUpperOnlyLatin(), and wstrToUpper().

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 }
ACE_UINT16 uint16
Definition: Define.h:72
wchar_t wcharToUpperOnlyLatin ( wchar_t  wchar)
inline

Definition at line 275 of file Util.h.

References isBasicLatinCharacter(), and wcharToUpper().

Referenced by AccountMgr::normalizeString().

276 {
277  return isBasicLatinCharacter(wchar) ? wcharToUpper(wchar) : wchar;
278 }
bool isBasicLatinCharacter(wchar_t wchar)
Definition: Util.h:136
wchar_t wcharToUpper(wchar_t wchar)
Definition: Util.h:252
void wstrToUpper ( std::wstring &  str)
inline

Definition at line 303 of file Util.h.

References wcharToUpper().

304 {
305  std::transform( str.begin(), str.end(), str.begin(), wcharToUpper );
306 }
wchar_t wcharToUpper(wchar_t wchar)
Definition: Util.h:252
bool WStrToUtf8 ( std::wstring  wstr,
std::string &  utf8str 
)

Definition at line 319 of file Util.cpp.

Referenced by Utf8toWStr().

320 {
321  try
322  {
323  std::string utf8str2;
324  utf8str2.resize(wstr.size() * 4); // allocate for most long case
325 
326  char* oend = utf8::utf16to8(wstr.c_str(), wstr.c_str() + wstr.size(), &utf8str2[0]);
327  utf8str2.resize(oend - (&utf8str2[0])); // remove unused tail
328  utf8str = utf8str2;
329  }
330  catch (std::exception)
331  {
332  utf8str = "";
333  return false;
334  }
335 
336  return true;
337 }
bool WStrToUtf8 ( wchar_t *  wstr,
size_t  size,
std::string &  utf8str 
)

Definition at line 299 of file Util.cpp.

Referenced by consoleToUtf8(), normalizePlayerName(), AccountMgr::normalizeString(), and WheatyExceptionReport::WriteStackDetails().

300 {
301  try
302  {
303  std::string utf8str2;
304  utf8str2.resize(size * 4); // allocate for most long case
305 
306  char* oend = utf8::utf16to8(wstr, wstr + size, &utf8str2[0]);
307  utf8str2.resize(oend - (&utf8str2[0])); // remove unused tail
308  utf8str = utf8str2;
309  }
310  catch (std::exception)
311  {
312  utf8str = "";
313  return false;
314  }
315 
316  return true;
317 }