Advertisement
nevadies

Untitled

Jul 23rd, 2014
253
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.23 KB | None | 0 0
  1. /*
  2. * Copyright (C) 2008-2014 TrinityCore <http://www.trinitycore.org/>
  3. * Copyright (C) 2005-2009 MaNGOS <http://getmangos.com/>
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License as published by the
  7. * Free Software Foundation; either version 2 of the License, or (at your
  8. * option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful, but WITHOUT
  11. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  13. * more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along
  16. * with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18.  
  19. #include "Util.h"
  20. #include "Common.h"
  21. #include "utf8.h"
  22. #include "SFMT.h"
  23. #include "Errors.h" // for ASSERT
  24. #include <ace/TSS_T.h>
  25.  
  26. typedef ACE_TSS<SFMTRand> SFMTRandTSS;
  27. static SFMTRandTSS sfmtRand;
  28.  
  29. int32 irand(int32 min, int32 max)
  30. {
  31. ASSERT(max >= min);
  32. return int32(sfmtRand->IRandom(min, max));
  33. }
  34.  
  35. uint32 urand(uint32 min, uint32 max)
  36. {
  37. ASSERT(max >= min);
  38. return sfmtRand->URandom(min, max);
  39. }
  40.  
  41. float frand(float min, float max)
  42. {
  43. ASSERT(max >= min);
  44. return float(sfmtRand->Random() * (max - min) + min);
  45. }
  46.  
  47. int32 rand32()
  48. {
  49. return int32(sfmtRand->BRandom());
  50. }
  51.  
  52. double rand_norm(void)
  53. {
  54. return sfmtRand->Random();
  55. }
  56.  
  57. double rand_chance(void)
  58. {
  59. return sfmtRand->Random() * 100.0;
  60. }
  61.  
  62. Tokenizer::Tokenizer(const std::string &src, const char sep, uint32 vectorReserve)
  63. {
  64. m_str = new char[src.length() + 1];
  65. memcpy(m_str, src.c_str(), src.length() + 1);
  66.  
  67. if (vectorReserve)
  68. m_storage.reserve(vectorReserve);
  69.  
  70. char* posold = m_str;
  71. char* posnew = m_str;
  72.  
  73. for (;;)
  74. {
  75. if (*posnew == sep)
  76. {
  77. m_storage.push_back(posold);
  78. posold = posnew + 1;
  79.  
  80. *posnew = '\0';
  81. }
  82. else if (*posnew == '\0')
  83. {
  84. // Hack like, but the old code accepted these kind of broken strings,
  85. // so changing it would break other things
  86. if (posold != posnew)
  87. m_storage.push_back(posold);
  88.  
  89. break;
  90. }
  91.  
  92. ++posnew;
  93. }
  94. }
  95.  
  96. void stripLineInvisibleChars(std::string &str)
  97. {
  98. static std::string const invChars = " \t\7\n";
  99.  
  100. size_t wpos = 0;
  101.  
  102. bool space = false;
  103. for (size_t pos = 0; pos < str.size(); ++pos)
  104. {
  105. if (invChars.find(str[pos])!=std::string::npos)
  106. {
  107. if (!space)
  108. {
  109. str[wpos++] = ' ';
  110. space = true;
  111. }
  112. }
  113. else
  114. {
  115. if (wpos!=pos)
  116. str[wpos++] = str[pos];
  117. else
  118. ++wpos;
  119. space = false;
  120. }
  121. }
  122.  
  123. if (wpos < str.size())
  124. str.erase(wpos, str.size());
  125. if (str.find("|TInterface")!=std::string::npos)
  126. str.clear();
  127.  
  128. }
  129.  
  130. std::string secsToTimeString(uint64 timeInSecs, bool shortText, bool hoursOnly)
  131. {
  132. uint64 secs = timeInSecs % MINUTE;
  133. uint64 minutes = timeInSecs % HOUR / MINUTE;
  134. uint64 hours = timeInSecs % DAY / HOUR;
  135. uint64 days = timeInSecs / DAY;
  136.  
  137. std::ostringstream ss;
  138. if (days)
  139. ss << days << (shortText ? "d" : " Day(s) ");
  140. if (hours || hoursOnly)
  141. ss << hours << (shortText ? "h" : " Hour(s) ");
  142. if (!hoursOnly)
  143. {
  144. if (minutes)
  145. ss << minutes << (shortText ? "m" : " Minute(s) ");
  146. if (secs || (!days && !hours && !minutes) )
  147. ss << secs << (shortText ? "s" : " Second(s).");
  148. }
  149.  
  150. return ss.str();
  151. }
  152.  
  153. int64 MoneyStringToMoney(const std::string& moneyString)
  154. {
  155. int64 money = 0;
  156.  
  157. if (!(std::count(moneyString.begin(), moneyString.end(), 'g') == 1 ||
  158. std::count(moneyString.begin(), moneyString.end(), 's') == 1 ||
  159. std::count(moneyString.begin(), moneyString.end(), 'c') == 1))
  160. return 0; // Bad format
  161.  
  162. Tokenizer tokens(moneyString, ' ');
  163. for (Tokenizer::const_iterator itr = tokens.begin(); itr != tokens.end(); ++itr)
  164. {
  165. std::string tokenString(*itr);
  166. size_t gCount = std::count(tokenString.begin(), tokenString.end(), 'g');
  167. size_t sCount = std::count(tokenString.begin(), tokenString.end(), 's');
  168. size_t cCount = std::count(tokenString.begin(), tokenString.end(), 'c');
  169. if (gCount + sCount + cCount != 1)
  170. return 0;
  171.  
  172. uint64 amount = atol(*itr);
  173. if (gCount == 1)
  174. money += amount * 100 * 100;
  175. else if (sCount == 1)
  176. money += amount * 100;
  177. else if (cCount == 1)
  178. money += amount;
  179. }
  180.  
  181. return money;
  182. }
  183.  
  184. uint32 TimeStringToSecs(const std::string& timestring)
  185. {
  186. uint32 secs = 0;
  187. uint32 buffer = 0;
  188. uint32 multiplier = 0;
  189.  
  190. for (std::string::const_iterator itr = timestring.begin(); itr != timestring.end(); ++itr)
  191. {
  192. if (isdigit(*itr))
  193. {
  194. buffer*=10;
  195. buffer+= (*itr)-'0';
  196. }
  197. else
  198. {
  199. switch (*itr)
  200. {
  201. case 'd': multiplier = DAY; break;
  202. case 'h': multiplier = HOUR; break;
  203. case 'm': multiplier = MINUTE; break;
  204. case 's': multiplier = 1; break;
  205. default : return 0; //bad format
  206. }
  207. buffer*=multiplier;
  208. secs+=buffer;
  209. buffer=0;
  210. }
  211. }
  212.  
  213. return secs;
  214. }
  215.  
  216. std::string TimeToTimestampStr(time_t t)
  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. }
  229.  
  230. /// Check if the string is a valid ip address representation
  231. bool IsIPAddress(char const* ipaddress)
  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. }
  240.  
  241. std::string GetAddressString(ACE_INET_Addr const& addr)
  242. {
  243. char buf[ACE_MAX_FULLY_QUALIFIED_NAME_LEN + 16];
  244. addr.addr_to_string(buf, ACE_MAX_FULLY_QUALIFIED_NAME_LEN + 16);
  245. return buf;
  246. }
  247.  
  248. bool IsIPAddrInNetwork(ACE_INET_Addr const& net, ACE_INET_Addr const& addr, ACE_INET_Addr const& subnetMask)
  249. {
  250. uint32 mask = subnetMask.get_ip_address();
  251. if ((net.get_ip_address() & mask) == (addr.get_ip_address() & mask))
  252. return true;
  253. return false;
  254. }
  255.  
  256. /// create PID file
  257. uint32 CreatePIDFile(const std::string& filename)
  258. {
  259. FILE* pid_file = fopen (filename.c_str(), "w" );
  260. if (pid_file == NULL)
  261. return 0;
  262.  
  263. #ifdef _WIN32
  264. DWORD pid = GetCurrentProcessId();
  265. #else
  266. pid_t pid = getpid();
  267. #endif
  268.  
  269. fprintf(pid_file, "%u", pid );
  270. fclose(pid_file);
  271.  
  272. return (uint32)pid;
  273. }
  274.  
  275. size_t utf8length(std::string& utf8str)
  276. {
  277. try
  278. {
  279. return utf8::distance(utf8str.c_str(), utf8str.c_str()+utf8str.size());
  280. }
  281. catch(std::exception)
  282. {
  283. utf8str = "";
  284. return 0;
  285. }
  286. }
  287.  
  288. void utf8truncate(std::string& utf8str, size_t len)
  289. {
  290. try
  291. {
  292. size_t wlen = utf8::distance(utf8str.c_str(), utf8str.c_str()+utf8str.size());
  293. if (wlen <= len)
  294. return;
  295.  
  296. std::wstring wstr;
  297. wstr.resize(wlen);
  298. utf8::utf8to16(utf8str.c_str(), utf8str.c_str()+utf8str.size(), &wstr[0]);
  299. wstr.resize(len);
  300. char* oend = utf8::utf16to8(wstr.c_str(), wstr.c_str()+wstr.size(), &utf8str[0]);
  301. utf8str.resize(oend-(&utf8str[0])); // remove unused tail
  302. }
  303. catch(std::exception)
  304. {
  305. utf8str = "";
  306. }
  307. }
  308.  
  309. bool Utf8toWStr(char const* utf8str, size_t csize, wchar_t* wstr, size_t& wsize)
  310. {
  311. try
  312. {
  313. size_t len = utf8::distance(utf8str, utf8str+csize);
  314. if (len > wsize)
  315. {
  316. if (wsize > 0)
  317. wstr[0] = L'\0';
  318. wsize = 0;
  319. return false;
  320. }
  321.  
  322. wsize = len;
  323. utf8::utf8to16(utf8str, utf8str+csize, wstr);
  324. wstr[len] = L'\0';
  325. }
  326. catch(std::exception)
  327. {
  328. if (wsize > 0)
  329. wstr[0] = L'\0';
  330. wsize = 0;
  331. return false;
  332. }
  333.  
  334. return true;
  335. }
  336.  
  337. bool Utf8toWStr(const std::string& utf8str, std::wstring& wstr)
  338. {
  339. try
  340. {
  341. if (size_t len = utf8::distance(utf8str.c_str(), utf8str.c_str()+utf8str.size()))
  342. {
  343. wstr.resize(len);
  344. utf8::utf8to16(utf8str.c_str(), utf8str.c_str()+utf8str.size(), &wstr[0]);
  345. }
  346. }
  347. catch(std::exception)
  348. {
  349. wstr = L"";
  350. return false;
  351. }
  352.  
  353. return true;
  354. }
  355.  
  356. bool WStrToUtf8(wchar_t* wstr, size_t size, std::string& utf8str)
  357. {
  358. try
  359. {
  360. std::string utf8str2;
  361. utf8str2.resize(size*4); // allocate for most long case
  362.  
  363. if (size)
  364. {
  365. char* oend = utf8::utf16to8(wstr, wstr+size, &utf8str2[0]);
  366. utf8str2.resize(oend-(&utf8str2[0])); // remove unused tail
  367. }
  368. utf8str = utf8str2;
  369. }
  370. catch(std::exception)
  371. {
  372. utf8str = "";
  373. return false;
  374. }
  375.  
  376. return true;
  377. }
  378.  
  379. bool WStrToUtf8(std::wstring wstr, std::string& utf8str)
  380. {
  381. try
  382. {
  383. std::string utf8str2;
  384. utf8str2.resize(wstr.size()*4); // allocate for most long case
  385.  
  386. if (wstr.size())
  387. {
  388. char* oend = utf8::utf16to8(wstr.c_str(), wstr.c_str()+wstr.size(), &utf8str2[0]);
  389. utf8str2.resize(oend-(&utf8str2[0])); // remove unused tail
  390. }
  391. utf8str = utf8str2;
  392. }
  393. catch(std::exception)
  394. {
  395. utf8str = "";
  396. return false;
  397. }
  398.  
  399. return true;
  400. }
  401.  
  402. typedef wchar_t const* const* wstrlist;
  403.  
  404. std::wstring GetMainPartOfName(std::wstring wname, uint32 declension)
  405. {
  406. // supported only Cyrillic cases
  407. if (wname.size() < 1 || !isCyrillicCharacter(wname[0]) || declension > 5)
  408. return wname;
  409.  
  410. // Important: end length must be <= MAX_INTERNAL_PLAYER_NAME-MAX_PLAYER_NAME (3 currently)
  411.  
  412. static wchar_t const a_End[] = { wchar_t(1), wchar_t(0x0430), wchar_t(0x0000)};
  413. static wchar_t const o_End[] = { wchar_t(1), wchar_t(0x043E), wchar_t(0x0000)};
  414. static wchar_t const ya_End[] = { wchar_t(1), wchar_t(0x044F), wchar_t(0x0000)};
  415. static wchar_t const ie_End[] = { wchar_t(1), wchar_t(0x0435), wchar_t(0x0000)};
  416. static wchar_t const i_End[] = { wchar_t(1), wchar_t(0x0438), wchar_t(0x0000)};
  417. static wchar_t const yeru_End[] = { wchar_t(1), wchar_t(0x044B), wchar_t(0x0000)};
  418. static wchar_t const u_End[] = { wchar_t(1), wchar_t(0x0443), wchar_t(0x0000)};
  419. static wchar_t const yu_End[] = { wchar_t(1), wchar_t(0x044E), wchar_t(0x0000)};
  420. static wchar_t const oj_End[] = { wchar_t(2), wchar_t(0x043E), wchar_t(0x0439), wchar_t(0x0000)};
  421. static wchar_t const ie_j_End[] = { wchar_t(2), wchar_t(0x0435), wchar_t(0x0439), wchar_t(0x0000)};
  422. static wchar_t const io_j_End[] = { wchar_t(2), wchar_t(0x0451), wchar_t(0x0439), wchar_t(0x0000)};
  423. static wchar_t const o_m_End[] = { wchar_t(2), wchar_t(0x043E), wchar_t(0x043C), wchar_t(0x0000)};
  424. static wchar_t const io_m_End[] = { wchar_t(2), wchar_t(0x0451), wchar_t(0x043C), wchar_t(0x0000)};
  425. static wchar_t const ie_m_End[] = { wchar_t(2), wchar_t(0x0435), wchar_t(0x043C), wchar_t(0x0000)};
  426. static wchar_t const soft_End[] = { wchar_t(1), wchar_t(0x044C), wchar_t(0x0000)};
  427. static wchar_t const j_End[] = { wchar_t(1), wchar_t(0x0439), wchar_t(0x0000)};
  428.  
  429. static wchar_t const* const dropEnds[6][8] = {
  430. { &a_End[1], &o_End[1], &ya_End[1], &ie_End[1], &soft_End[1], &j_End[1], NULL, NULL },
  431. { &a_End[1], &ya_End[1], &yeru_End[1], &i_End[1], NULL, NULL, NULL, NULL },
  432. { &ie_End[1], &u_End[1], &yu_End[1], &i_End[1], NULL, NULL, NULL, NULL },
  433. { &u_End[1], &yu_End[1], &o_End[1], &ie_End[1], &soft_End[1], &ya_End[1], &a_End[1], NULL },
  434. { &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 },
  435. { &ie_End[1], &i_End[1], NULL, NULL, NULL, NULL, NULL, NULL }
  436. };
  437.  
  438. for (wchar_t const* const* itr = &dropEnds[declension][0]; *itr; ++itr)
  439. {
  440. size_t len = size_t((*itr)[-1]); // get length from string size field
  441.  
  442. if (wname.substr(wname.size()-len, len)==*itr)
  443. return wname.substr(0, wname.size()-len);
  444. }
  445.  
  446. return wname;
  447. }
  448.  
  449. bool utf8ToConsole(const std::string& utf8str, std::string& conStr)
  450. {
  451. #if PLATFORM == PLATFORM_WINDOWS
  452. std::wstring wstr;
  453. if (!Utf8toWStr(utf8str, wstr))
  454. return false;
  455.  
  456. conStr.resize(wstr.size());
  457. CharToOemBuffW(&wstr[0], &conStr[0], wstr.size());
  458. #else
  459. // not implemented yet
  460. conStr = utf8str;
  461. #endif
  462.  
  463. return true;
  464. }
  465.  
  466. bool consoleToUtf8(const std::string& conStr, std::string& utf8str)
  467. {
  468. #if PLATFORM == PLATFORM_WINDOWS
  469. std::wstring wstr;
  470. wstr.resize(conStr.size());
  471. OemToCharBuffW(&conStr[0], &wstr[0], conStr.size());
  472.  
  473. return WStrToUtf8(wstr, utf8str);
  474. #else
  475. // not implemented yet
  476. utf8str = conStr;
  477. return true;
  478. #endif
  479. }
  480.  
  481. bool Utf8FitTo(const std::string& str, std::wstring search)
  482. {
  483. std::wstring temp;
  484.  
  485. if (!Utf8toWStr(str, temp))
  486. return false;
  487.  
  488. // converting to lower case
  489. wstrToLower( temp );
  490.  
  491. if (temp.find(search) == std::wstring::npos)
  492. return false;
  493.  
  494. return true;
  495. }
  496.  
  497. void utf8printf(FILE* out, const char *str, ...)
  498. {
  499. va_list ap;
  500. va_start(ap, str);
  501. vutf8printf(out, str, &ap);
  502. va_end(ap);
  503. }
  504.  
  505. void vutf8printf(FILE* out, const char *str, va_list* ap)
  506. {
  507. #if PLATFORM == PLATFORM_WINDOWS
  508. char temp_buf[32*1024];
  509. wchar_t wtemp_buf[32*1024];
  510.  
  511. size_t temp_len = vsnprintf(temp_buf, 32*1024, str, *ap);
  512.  
  513. size_t wtemp_len = 32*1024-1;
  514. Utf8toWStr(temp_buf, temp_len, wtemp_buf, wtemp_len);
  515.  
  516. CharToOemBuffW(&wtemp_buf[0], &temp_buf[0], wtemp_len+1);
  517. fprintf(out, "%s", temp_buf);
  518. #else
  519. vfprintf(out, str, *ap);
  520. #endif
  521. }
  522.  
  523. std::string ByteArrayToHexStr(uint8 const* bytes, uint32 arrayLen, bool reverse /* = false */)
  524. {
  525. int32 init = 0;
  526. int32 end = arrayLen;
  527. int8 op = 1;
  528.  
  529. if (reverse)
  530. {
  531. init = arrayLen - 1;
  532. end = -1;
  533. op = -1;
  534. }
  535.  
  536. std::ostringstream ss;
  537. for (int32 i = init; i != end; i += op)
  538. {
  539. char buffer[4];
  540. sprintf(buffer, "%02X", bytes[i]);
  541. ss << buffer;
  542. }
  543.  
  544. return ss.str();
  545. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement