OregonCore  revision 3611e8a-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)
 
int32 rand32 ()
 
float frand (float min, float max)
 
double rand_norm (void)
 
double rand_chance (void)
 
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:320
#define vsnprintf
Definition: Common.h:131

Definition at line 328 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 87 of file Util.h.

88 {
89  var += (apply ? val : -val);
90  if (var < 0)
91  var = 0;
92 }
void apply(T *val)
Definition: ByteConverter.h:41
void ApplyModUInt32Var ( uint32 var,
int32  val,
bool  apply 
)
inline

Definition at line 78 of file Util.h.

79 {
80  int32 cur = var;
81  cur += (apply ? val : -val);
82  if (cur < 0)
83  cur = 0;
84  var = cur;
85 }
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 115 of file Util.h.

References CalculatePct().

116 {
117  return base = CalculatePct(base, pct);
118 }
T CalculatePct(T base, U pct)
Definition: Util.h:103
void ApplyPercentModFloatVar ( float &  var,
float  val,
bool  apply 
)
inline

Definition at line 94 of file Util.h.

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

95 {
96  if (val == -100.0f) // prevent set var to zero
97  val = -99.99f;
98  var *= (apply ? (100.0f + val) / 100.0f : 100.0f / (100.0f + val));
99 }
void apply(T *val)
Definition: ByteConverter.h:41
std::string ByteArrayToHexStr ( uint8 bytes,
uint32  length 
)

Definition at line 493 of file Util.cpp.

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

494 {
495  std::ostringstream ss;
496  for (uint32 i = 0; i < length; ++i)
497  {
498  char buffer[4];
499  sprintf(buffer, "%02X", bytes[i]);
500  ss << buffer;
501  }
502 
503  return ss.str();
504 }
ACE_UINT32 uint32
Definition: Define.h:71
bool consoleToUtf8 ( const std::string &  conStr,
std::string &  utf8str 
)

Definition at line 443 of file Util.cpp.

References WStrToUtf8().

Referenced by wstrToLower().

444 {
445  #if PLATFORM == PLATFORM_WINDOWS
446  std::wstring wstr;
447  wstr.resize(conStr.size());
448  OemToCharBuffW(&conStr[0], &wstr[0], conStr.size());
449 
450  return WStrToUtf8(wstr, utf8str);
451  #else
452  // not implemented yet
453  utf8str = conStr;
454  return true;
455  #endif
456 }
bool WStrToUtf8(wchar_t *wstr, size_t size, std::string &utf8str)
Definition: Util.cpp:338
uint32 CreatePIDFile ( const std::string &  filename)

Definition at line 242 of file Util.cpp.

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

243 {
244  FILE* pid_file = fopen (filename.c_str(), "w" );
245  if (pid_file == NULL)
246  return 0;
247 
248  #ifdef _WIN32
249  DWORD pid = GetCurrentProcessId();
250  #else
251  pid_t pid = getpid();
252  #endif
253 
254  fprintf(pid_file, "%u", pid );
255  fclose(pid_file);
256 
257  return (uint32)pid;
258 }
ACE_UINT32 uint32
Definition: Define.h:71
float frand ( float  min,
float  max 
)

Definition at line 76 of file Util.cpp.

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

77 {
78  return mtRand->randExc (max - min) + min;
79 }
std::wstring GetMainPartOfName ( std::wstring  wname,
uint32  declension 
)

Definition at line 380 of file Util.cpp.

References isCyrillicCharacter().

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

381 {
382  // supported only Cyrillic cases
383  if (wname.size() < 1 || !isCyrillicCharacter(wname[0]) || declension > 5)
384  return wname;
385 
386  // Important: end length must be <= MAX_INTERNAL_PLAYER_NAME-MAX_PLAYER_NAME (3 currently)
387 
388  static wchar_t const a_End[] = { wchar_t(1), wchar_t(0x0430), wchar_t(0x0000)};
389  static wchar_t const o_End[] = { wchar_t(1), wchar_t(0x043E), wchar_t(0x0000)};
390  static wchar_t const ya_End[] = { wchar_t(1), wchar_t(0x044F), wchar_t(0x0000)};
391  static wchar_t const ie_End[] = { wchar_t(1), wchar_t(0x0435), wchar_t(0x0000)};
392  static wchar_t const i_End[] = { wchar_t(1), wchar_t(0x0438), wchar_t(0x0000)};
393  static wchar_t const yeru_End[] = { wchar_t(1), wchar_t(0x044B), wchar_t(0x0000)};
394  static wchar_t const u_End[] = { wchar_t(1), wchar_t(0x0443), wchar_t(0x0000)};
395  static wchar_t const yu_End[] = { wchar_t(1), wchar_t(0x044E), wchar_t(0x0000)};
396  static wchar_t const oj_End[] = { wchar_t(2), wchar_t(0x043E), wchar_t(0x0439), wchar_t(0x0000)};
397  static wchar_t const ie_j_End[] = { wchar_t(2), wchar_t(0x0435), wchar_t(0x0439), wchar_t(0x0000)};
398  static wchar_t const io_j_End[] = { wchar_t(2), wchar_t(0x0451), wchar_t(0x0439), wchar_t(0x0000)};
399  static wchar_t const o_m_End[] = { wchar_t(2), wchar_t(0x043E), wchar_t(0x043C), wchar_t(0x0000)};
400  static wchar_t const io_m_End[] = { wchar_t(2), wchar_t(0x0451), wchar_t(0x043C), wchar_t(0x0000)};
401  static wchar_t const ie_m_End[] = { wchar_t(2), wchar_t(0x0435), wchar_t(0x043C), wchar_t(0x0000)};
402  static wchar_t const soft_End[] = { wchar_t(1), wchar_t(0x044C), wchar_t(0x0000)};
403  static wchar_t const j_End[] = { wchar_t(1), wchar_t(0x0439), wchar_t(0x0000)};
404 
405  static wchar_t const* const dropEnds[6][8] =
406  {
407  { &a_End[1], &o_End[1], &ya_End[1], &ie_End[1], &soft_End[1], &j_End[1], NULL, NULL },
408  { &a_End[1], &ya_End[1], &yeru_End[1], &i_End[1], NULL, NULL, NULL, NULL },
409  { &ie_End[1], &u_End[1], &yu_End[1], &i_End[1], NULL, NULL, NULL, NULL },
410  { &u_End[1], &yu_End[1], &o_End[1], &ie_End[1], &soft_End[1], &ya_End[1], &a_End[1], NULL },
411  { &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 },
412  { &ie_End[1], &i_End[1], NULL, NULL, NULL, NULL, NULL, NULL }
413  };
414 
415  for (wchar_t const * const* itr = &dropEnds[declension][0]; *itr; ++itr)
416  {
417  size_t len = size_t((*itr)[-1]); // get length from string size field
418 
419  if (wname.substr(wname.size() - len, len) == *itr)
420  return wname.substr(0, wname.size() - len);
421  }
422 
423  return wname;
424 }
bool isCyrillicCharacter(wchar_t wchar)
Definition: Util.h:172
void hexEncodeByteArray ( uint8 bytes,
uint32  arrayLen,
std::string &  result 
)

Definition at line 474 of file Util.cpp.

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

475 {
476  std::ostringstream ss;
477  for (uint32 i = 0; i < arrayLen; ++i)
478  {
479  for (uint8 j = 0; j < 2; ++j)
480  {
481  unsigned char nibble = 0x0F & (bytes[i] >> ((1 - j) * 4));
482  char encodedNibble;
483  if (nibble < 0x0A)
484  encodedNibble = '0' + nibble;
485  else
486  encodedNibble = 'A' + nibble - 0x0A;
487  ss << encodedNibble;
488  }
489  }
490  result = ss.str();
491 }
ACE_UINT8 uint8
Definition: Define.h:73
ACE_UINT32 uint32
Definition: Define.h:71
bool isBasicLatinCharacter ( wchar_t  wchar)
inline

Definition at line 142 of file Util.h.

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

143 {
144  if (wchar >= L'a' && wchar <= L'z') // LATIN SMALL LETTER A - LATIN SMALL LETTER Z
145  return true;
146  if (wchar >= L'A' && wchar <= L'Z') // LATIN CAPITAL LETTER A - LATIN CAPITAL LETTER Z
147  return true;
148  return false;
149 }
bool isBasicLatinString ( std::wstring  wstr,
bool  numericOrSpace 
)
inline

Definition at line 226 of file Util.h.

References isBasicLatinCharacter(), and isNumericOrSpace().

Referenced by isValidString().

227 {
228  for (size_t i = 0; i < wstr.size(); ++i)
229  if (!isBasicLatinCharacter(wstr[i]) && (!numericOrSpace || !isNumericOrSpace(wstr[i])))
230  return false;
231  return true;
232 }
bool isBasicLatinCharacter(wchar_t wchar)
Definition: Util.h:142
bool isNumericOrSpace(wchar_t wchar)
Definition: Util.h:212
bool isCyrillicCharacter ( wchar_t  wchar)
inline

Definition at line 172 of file Util.h.

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

173 {
174  if (wchar >= 0x0410 && wchar <= 0x044F) // CYRILLIC CAPITAL LETTER A - CYRILLIC SMALL LETTER YA
175  return true;
176  if (wchar == 0x0401 || wchar == 0x0451) // CYRILLIC CAPITAL LETTER IO, CYRILLIC SMALL LETTER IO
177  return true;
178  return false;
179 }
bool isCyrillicString ( std::wstring  wstr,
bool  numericOrSpace 
)
inline

Definition at line 242 of file Util.h.

References isCyrillicCharacter(), and isNumericOrSpace().

Referenced by isValidString().

243 {
244  for (size_t i = 0; i < wstr.size(); ++i)
245  if (!isCyrillicCharacter(wstr[i]) && (!numericOrSpace || !isNumericOrSpace(wstr[i])))
246  return false;
247  return true;
248 }
bool isCyrillicCharacter(wchar_t wchar)
Definition: Util.h:172
bool isNumericOrSpace(wchar_t wchar)
Definition: Util.h:212
bool isEastAsianCharacter ( wchar_t  wchar)
inline

Definition at line 181 of file Util.h.

Referenced by isEastAsianString().

182 {
183  if (wchar >= 0x1100 && wchar <= 0x11F9) // Hangul Jamo
184  return true;
185  if (wchar >= 0x3041 && wchar <= 0x30FF) // Hiragana + Katakana
186  return true;
187  if (wchar >= 0x3131 && wchar <= 0x318E) // Hangul Compatibility Jamo
188  return true;
189  if (wchar >= 0x31F0 && wchar <= 0x31FF) // Katakana Phonetic Ext.
190  return true;
191  if (wchar >= 0x3400 && wchar <= 0x4DB5) // CJK Ideographs Ext. A
192  return true;
193  if (wchar >= 0x4E00 && wchar <= 0x9FC3) // Unified CJK Ideographs
194  return true;
195  if (wchar >= 0xAC00 && wchar <= 0xD7A3) // Hangul Syllables
196  return true;
197  if (wchar >= 0xFF01 && wchar <= 0xFFEE) // Halfwidth forms
198  return true;
199  return false;
200 }
bool isEastAsianString ( std::wstring  wstr,
bool  numericOrSpace 
)
inline

Definition at line 250 of file Util.h.

References isEastAsianCharacter(), and isNumericOrSpace().

Referenced by isValidString().

251 {
252  for (size_t i = 0; i < wstr.size(); ++i)
253  if (!isEastAsianCharacter(wstr[i]) && (!numericOrSpace || !isNumericOrSpace(wstr[i])))
254  return false;
255  return true;
256 }
bool isEastAsianCharacter(wchar_t wchar)
Definition: Util.h:181
bool isNumericOrSpace(wchar_t wchar)
Definition: Util.h:212
bool isExtendedLatinCharacter ( wchar_t  wchar)
inline

Definition at line 151 of file Util.h.

References isBasicLatinCharacter().

Referenced by isExtendedLatinString().

152 {
153  if (isBasicLatinCharacter(wchar))
154  return true;
155  if (wchar >= 0x00C0 && wchar <= 0x00D6) // LATIN CAPITAL LETTER A WITH GRAVE - LATIN CAPITAL LETTER O WITH DIAERESIS
156  return true;
157  if (wchar >= 0x00D8 && wchar <= 0x00DE) // LATIN CAPITAL LETTER O WITH STROKE - LATIN CAPITAL LETTER THORN
158  return true;
159  if (wchar == 0x00DF) // LATIN SMALL LETTER SHARP S
160  return true;
161  if (wchar >= 0x00E0 && wchar <= 0x00F6) // LATIN SMALL LETTER A WITH GRAVE - LATIN SMALL LETTER O WITH DIAERESIS
162  return true;
163  if (wchar >= 0x00F8 && wchar <= 0x00FE) // LATIN SMALL LETTER O WITH STROKE - LATIN SMALL LETTER THORN
164  return true;
165  if (wchar >= 0x0100 && wchar <= 0x012F) // LATIN CAPITAL LETTER A WITH MACRON - LATIN SMALL LETTER I WITH OGONEK
166  return true;
167  if (wchar == 0x1E9E) // LATIN CAPITAL LETTER SHARP S
168  return true;
169  return false;
170 }
bool isBasicLatinCharacter(wchar_t wchar)
Definition: Util.h:142
bool isExtendedLatinString ( std::wstring  wstr,
bool  numericOrSpace 
)
inline

Definition at line 234 of file Util.h.

References isExtendedLatinCharacter(), and isNumericOrSpace().

Referenced by isValidString().

235 {
236  for (size_t i = 0; i < wstr.size(); ++i)
237  if (!isExtendedLatinCharacter(wstr[i]) && (!numericOrSpace || !isNumericOrSpace(wstr[i])))
238  return false;
239  return true;
240 }
bool isExtendedLatinCharacter(wchar_t wchar)
Definition: Util.h:151
bool isNumericOrSpace(wchar_t wchar)
Definition: Util.h:212
bool IsIPAddress ( char const *  ipaddress)

Definition at line 231 of file Util.cpp.

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

232 {
233  if (!ipaddress)
234  return false;
235 
236  // Let the big boys do it.
237  // Drawback: all valid ip address formats are recognized e.g.: 12.23,121234,0xABCD)
238  return inet_addr(ipaddress) != INADDR_NONE;
239 }
bool isNumeric ( wchar_t  wchar)
inline

Definition at line 202 of file Util.h.

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

203 {
204  return (wchar >= L'0' && wchar <= L'9');
205 }
bool isNumeric ( char  c)
inline

Definition at line 207 of file Util.h.

208 {
209  return (c >= '0' && c <= '9');
210 }
bool isNumeric ( char const *  str)
inline

Definition at line 217 of file Util.h.

References isNumeric().

218 {
219  for (char const* c = str; *c; ++c)
220  if (!isNumeric(*c))
221  return false;
222 
223  return true;
224 }
bool isNumeric(wchar_t wchar)
Definition: Util.h:202
bool isNumericOrSpace ( wchar_t  wchar)
inline

Definition at line 212 of file Util.h.

References isNumeric().

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

213 {
214  return isNumeric(wchar) || wchar == L' ';
215 }
bool isNumeric(wchar_t wchar)
Definition: Util.h:202
int32 rand32 ( )

Definition at line 81 of file Util.cpp.

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

82 {
83  return mtRand->randInt ();
84 }
double rand_chance ( void  )
template<class T >
T RoundToInterval ( T &  num,
floor,
ceil 
)
inline

Definition at line 121 of file Util.h.

References Utf8toWStr().

122 {
123  return num = std::min(std::max(num, floor), ceil);
124 }
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 148 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().

149 {
150  uint32 secs = timeInSecs % MINUTE;
151  uint32 minutes = timeInSecs % HOUR / MINUTE;
152  uint32 hours = timeInSecs % DAY / HOUR;
153  uint32 days = timeInSecs / DAY;
154 
155  std::ostringstream ss;
156  if (days)
157  ss << days << (shortText ? "d" : " Day(s) ");
158  if (hours || hoursOnly)
159  ss << hours << (shortText ? "h" : " Hour(s) ");
160  if (!hoursOnly)
161  {
162  if (minutes)
163  ss << minutes << (shortText ? "m" : " Minute(s) ");
164  if (secs || (!days && !hours && !minutes) )
165  ss << secs << (shortText ? "s" : " Second(s).");
166  }
167 
168  return ss.str();
169 }
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 358 of file Util.h.

References urand().

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

359 {
360  typename C::const_iterator it = container.begin();
361  std::advance(it, urand(0, container.size() - 1));
362  return *it;
363 }
uint32 urand(uint32 min, uint32 max)
Definition: Util.cpp:71
void stripLineInvisibleChars ( std::string &  src)

Definition at line 115 of file Util.cpp.

Referenced by WorldSession::processChatmessageFurtherAfterSecurityChecks().

116 {
117  static std::string const invChars = " \t\7\n";
118 
119  size_t wpos = 0;
120 
121  bool space = false;
122  for (size_t pos = 0; pos < str.size(); ++pos)
123  {
124  if (invChars.find(str[pos]) != std::string::npos)
125  {
126  if (!space)
127  {
128  str[wpos++] = ' ';
129  space = true;
130  }
131  }
132  else
133  {
134  if (wpos != pos)
135  str[wpos++] = str[pos];
136  else
137  ++wpos;
138  space = false;
139  }
140  }
141 
142  if (wpos < str.size())
143  str.erase(wpos, str.size());
144  if (str.find("|TInterface") != std::string::npos)
145  str.clear();
146 }
Tokens StrSplit ( const std::string &  src,
const std::string &  sep 
)

Definition at line 97 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().

98 {
99  Tokens r;
100  std::string s;
101  for (std::string::const_iterator i = src.begin(); i != src.end(); i++)
102  {
103  if (sep.find(*i) != std::string::npos)
104  {
105  if (s.length()) r.push_back(s);
106  s = "";
107  }
108  else
109  s += *i;
110  }
111  if (s.length()) r.push_back(s);
112  return r;
113 }
std::vector< std::string > Tokens
Definition: Util.h:26
uint32 TimeStringToSecs ( const std::string &  timestring)

Definition at line 171 of file Util.cpp.

References DAY, HOUR, and MINUTE.

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

172 {
173  uint32 secs = 0;
174  uint32 buffer = 0;
175  uint32 multiplier = 0;
176 
177  for (std::string::const_iterator itr = timestring.begin(); itr != timestring.end(); itr++ )
178  {
179  if (isdigit(*itr))
180  {
181  std::string str; //very complicated typecast char->const char*; is there no better way?
182  str += *itr;
183  const char* tmp = str.c_str();
184 
185  buffer *= 10;
186  buffer += atoi(tmp);
187  }
188  else
189  {
190  switch (*itr)
191  {
192  case 'd':
193  multiplier = DAY;
194  break;
195  case 'h':
196  multiplier = HOUR;
197  break;
198  case 'm':
199  multiplier = MINUTE;
200  break;
201  case 's':
202  multiplier = 1;
203  break;
204  default :
205  return 0; //bad format
206  }
207  buffer *= multiplier;
208  secs += buffer;
209  buffer = 0;
210  }
211  }
212 
213  return secs;
214 }
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 216 of file Util.cpp.

References snprintf.

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

217 {
218  tm* aTm = localtime(&t);
219  // YYYY year
220  // MM month (2 digits 01-12)
221  // DD day (2 digits 01-31)
222  // HH hour (2 digits 00-23)
223  // MM minutes (2 digits 00-59)
224  // SS seconds (2 digits 00-59)
225  char buf[20];
226  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);
227  return std::string(buf);
228 }
#define snprintf
Definition: Common.h:129
uint32 urand ( uint32  min,
uint32  max 
)

Definition at line 71 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().

72 {
73  return mtRand->randInt (max - min) + min;
74 }
bool Utf8FitTo ( const std::string &  str,
std::wstring  search 
)

Definition at line 458 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().

459 {
460  std::wstring temp;
461 
462  if (!Utf8toWStr(str, temp))
463  return false;
464 
465  // converting to lower case
466  wstrToLower( temp );
467 
468  if (temp.find(search) == std::wstring::npos)
469  return false;
470 
471  return true;
472 }
bool Utf8toWStr(char const *utf8str, size_t csize, wchar_t *wstr, size_t &wsize)
Definition: Util.cpp:294
void wstrToLower(std::wstring &str)
Definition: Util.h:314
size_t utf8length ( std::string &  utf8str)

Definition at line 260 of file Util.cpp.

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

261 {
262  try
263  {
264  return utf8::distance(utf8str.c_str(), utf8str.c_str() + utf8str.size());
265  }
266  catch (std::exception)
267  {
268  utf8str = "";
269  return 0;
270  }
271 }
bool utf8ToConsole ( const std::string &  utf8str,
std::string &  conStr 
)

Definition at line 426 of file Util.cpp.

References Utf8toWStr().

Referenced by wstrToLower().

427 {
428  #if PLATFORM == PLATFORM_WINDOWS
429  std::wstring wstr;
430  if (!Utf8toWStr(utf8str, wstr))
431  return false;
432 
433  conStr.resize(wstr.size());
434  CharToOemBuffW(&wstr[0], &conStr[0], wstr.size());
435  #else
436  // not implemented yet
437  conStr = utf8str;
438  #endif
439 
440  return true;
441 }
bool Utf8toWStr(char const *utf8str, size_t csize, wchar_t *wstr, size_t &wsize)
Definition: Util.cpp:294
bool Utf8toWStr ( const std::string &  utf8str,
std::wstring &  wstr 
)

Definition at line 320 of file Util.cpp.

Referenced by RoundToInterval(), and Utf8toWStr().

321 {
322  try
323  {
324  size_t len = utf8::distance(utf8str.c_str(), utf8str.c_str() + utf8str.size());
325  wstr.resize(len);
326 
327  utf8::utf8to16(utf8str.c_str(), utf8str.c_str() + utf8str.size(), &wstr[0]);
328  }
329  catch (std::exception)
330  {
331  wstr = L"";
332  return false;
333  }
334 
335  return true;
336 }
bool Utf8toWStr ( char const *  utf8str,
size_t  csize,
wchar_t *  wstr,
size_t &  wsize 
)

Definition at line 294 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().

295 {
296  try
297  {
298  size_t len = utf8::distance(utf8str, utf8str + csize);
299  if (len > wsize)
300  {
301  wsize = 0;
302  *wstr = L'\0';
303  return false;
304  }
305 
306  wsize = len;
307  utf8::utf8to16(utf8str, utf8str + csize, wstr);
308  wstr[len] = L'\0';
309  }
310  catch (std::exception)
311  {
312  wsize = 0;
313  *wstr = L'\0';
314  return false;
315  }
316 
317  return true;
318 }
bool Utf8toWStr ( const std::string &  utf8str,
wchar_t *  wstr,
size_t &  wsize 
)
inline

Definition at line 130 of file Util.h.

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

131 {
132  return Utf8toWStr(utf8str.c_str(), utf8str.size(), wstr, wsize);
133 }
bool Utf8toWStr(const std::string &utf8str, std::wstring &wstr)
Definition: Util.cpp:320
void utf8truncate ( std::string &  utf8str,
size_t  len 
)

Definition at line 273 of file Util.cpp.

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

274 {
275  try
276  {
277  size_t wlen = utf8::distance(utf8str.c_str(), utf8str.c_str() + utf8str.size());
278  if (wlen <= len)
279  return;
280 
281  std::wstring wstr;
282  wstr.resize(wlen);
283  utf8::utf8to16(utf8str.c_str(), utf8str.c_str() + utf8str.size(), &wstr[0]);
284  wstr.resize(len);
285  char* oend = utf8::utf16to8(wstr.c_str(), wstr.c_str() + wstr.size(), &utf8str[0]);
286  utf8str.resize(oend - (&utf8str[0])); // remove unused tail
287  }
288  catch (std::exception)
289  {
290  utf8str = "";
291  }
292 }
wchar_t wcharToLower ( wchar_t  wchar)
inline

Definition at line 286 of file Util.h.

Referenced by normalizePlayerName(), and wstrToLower().

287 {
288  if (wchar >= L'A' && wchar <= L'Z') // LATIN CAPITAL LETTER A - LATIN CAPITAL LETTER Z
289  return wchar_t(uint16(wchar) + 0x0020);
290  if (wchar >= 0x00C0 && wchar <= 0x00D6) // LATIN CAPITAL LETTER A WITH GRAVE - LATIN CAPITAL LETTER O WITH DIAERESIS
291  return wchar_t(uint16(wchar) + 0x0020);
292  if (wchar >= 0x00D8 && wchar <= 0x00DE) // LATIN CAPITAL LETTER O WITH STROKE - LATIN CAPITAL LETTER THORN
293  return wchar_t(uint16(wchar) + 0x0020);
294  if (wchar >= 0x0100 && wchar <= 0x012E) // LATIN CAPITAL LETTER A WITH MACRON - LATIN CAPITAL LETTER I WITH OGONEK (only %2=0)
295  {
296  if (wchar % 2 == 0)
297  return wchar_t(uint16(wchar) + 0x0001);
298  }
299  if (wchar == 0x1E9E) // LATIN CAPITAL LETTER SHARP S
300  return wchar_t(0x00DF);
301  if (wchar == 0x0401) // CYRILLIC CAPITAL LETTER IO
302  return wchar_t(0x0451);
303  if (wchar >= 0x0410 && wchar <= 0x042F) // CYRILLIC CAPITAL LETTER A - CYRILLIC CAPITAL LETTER YA
304  return wchar_t(uint16(wchar) + 0x0020);
305 
306  return wchar;
307 }
ACE_UINT16 uint16
Definition: Define.h:72
wchar_t wcharToUpper ( wchar_t  wchar)
inline

Definition at line 258 of file Util.h.

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

259 {
260  if (wchar >= L'a' && wchar <= L'z') // LATIN SMALL LETTER A - LATIN SMALL LETTER Z
261  return wchar_t(uint16(wchar) - 0x0020);
262  if (wchar == 0x00DF) // LATIN SMALL LETTER SHARP S
263  return wchar_t(0x1E9E);
264  if (wchar >= 0x00E0 && wchar <= 0x00F6) // LATIN SMALL LETTER A WITH GRAVE - LATIN SMALL LETTER O WITH DIAERESIS
265  return wchar_t(uint16(wchar) - 0x0020);
266  if (wchar >= 0x00F8 && wchar <= 0x00FE) // LATIN SMALL LETTER O WITH STROKE - LATIN SMALL LETTER THORN
267  return wchar_t(uint16(wchar) - 0x0020);
268  if (wchar >= 0x0101 && wchar <= 0x012F) // LATIN SMALL LETTER A WITH MACRON - LATIN SMALL LETTER I WITH OGONEK (only %2=1)
269  {
270  if (wchar % 2 == 1)
271  return wchar_t(uint16(wchar) - 0x0001);
272  }
273  if (wchar >= 0x0430 && wchar <= 0x044F) // CYRILLIC SMALL LETTER A - CYRILLIC SMALL LETTER YA
274  return wchar_t(uint16(wchar) - 0x0020);
275  if (wchar == 0x0451) // CYRILLIC SMALL LETTER IO
276  return wchar_t(0x0401);
277 
278  return wchar;
279 }
ACE_UINT16 uint16
Definition: Define.h:72
wchar_t wcharToUpperOnlyLatin ( wchar_t  wchar)
inline

Definition at line 281 of file Util.h.

References isBasicLatinCharacter(), and wcharToUpper().

Referenced by AccountMgr::normalizeString().

282 {
283  return isBasicLatinCharacter(wchar) ? wcharToUpper(wchar) : wchar;
284 }
bool isBasicLatinCharacter(wchar_t wchar)
Definition: Util.h:142
wchar_t wcharToUpper(wchar_t wchar)
Definition: Util.h:258
void wstrToUpper ( std::wstring &  str)
inline

Definition at line 309 of file Util.h.

References wcharToUpper().

310 {
311  std::transform( str.begin(), str.end(), str.begin(), wcharToUpper );
312 }
wchar_t wcharToUpper(wchar_t wchar)
Definition: Util.h:258
bool WStrToUtf8 ( std::wstring  wstr,
std::string &  utf8str 
)

Definition at line 358 of file Util.cpp.

Referenced by Utf8toWStr().

359 {
360  try
361  {
362  std::string utf8str2;
363  utf8str2.resize(wstr.size() * 4); // allocate for most long case
364 
365  char* oend = utf8::utf16to8(wstr.c_str(), wstr.c_str() + wstr.size(), &utf8str2[0]);
366  utf8str2.resize(oend - (&utf8str2[0])); // remove unused tail
367  utf8str = utf8str2;
368  }
369  catch (std::exception)
370  {
371  utf8str = "";
372  return false;
373  }
374 
375  return true;
376 }
bool WStrToUtf8 ( wchar_t *  wstr,
size_t  size,
std::string &  utf8str 
)

Definition at line 338 of file Util.cpp.

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

339 {
340  try
341  {
342  std::string utf8str2;
343  utf8str2.resize(size * 4); // allocate for most long case
344 
345  char* oend = utf8::utf16to8(wstr, wstr + size, &utf8str2[0]);
346  utf8str2.resize(oend - (&utf8str2[0])); // remove unused tail
347  utf8str = utf8str2;
348  }
349  catch (std::exception)
350  {
351  utf8str = "";
352  return false;
353  }
354 
355  return true;
356 }