1 Star 0 Fork 1

SpringJoy/server

加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
Util.cpp 13.37 KB
一键复制 编辑 原始数据 按行查看 历史
Staden 提交于 2017-05-12 06:38 +08:00 . Revert "Fix ACE_TSS usage (#664)"
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571
/*
* Copyright (C) 2005-2011 MaNGOS <http://getmangos.com/>
* Copyright (C) 2009-2011 MaNGOSZero <https://github.com/mangos/zero>
* Copyright (C) 2011-2016 Nostalrius <https://nostalrius.org>
* Copyright (C) 2016-2017 Elysium Project <https://github.com/elysium-project>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "Util.h"
#include "Timer.h"
#include "utf8cpp/utf8.h"
#include "mersennetwister/MersenneTwister.h"
#include <ace/TSS_T.h>
#include <ace/OS_NS_arpa_inet.h>
typedef ACE_TSS<MTRand> MTRandTSS;
static MTRandTSS mtRand;
Tokenizer::Tokenizer(const std::string &src, const char sep, uint32 vectorReserve)
{
m_str = new char[src.length() + 1];
memcpy(m_str, src.c_str(), src.length() + 1);
if (vectorReserve)
m_storage.reserve(vectorReserve);
char* posold = m_str;
char* posnew = m_str;
for (;;)
{
if (*posnew == sep)
{
m_storage.push_back(posold);
posold = posnew + 1;
*posnew = '\0';
}
else if (*posnew == '\0')
{
// Hack like, but the old code accepted these kind of broken strings,
// so changing it would break other things
if (posold != posnew)
m_storage.push_back(posold);
break;
}
++posnew;
}
}
static ACE_Time_Value g_SystemTickTime = ACE_OS::gettimeofday();
uint32 WorldTimer::m_iTime = 0;
uint32 WorldTimer::m_iPrevTime = 0;
uint32 WorldTimer::tickTime() { return m_iTime; }
uint32 WorldTimer::tickPrevTime() { return m_iPrevTime; }
uint32 WorldTimer::tick()
{
//save previous world tick time
m_iPrevTime = m_iTime;
//get the new one and don't forget to persist current system time in m_SystemTickTime
m_iTime = WorldTimer::getMSTime_internal(true);
//return tick diff
return getMSTimeDiff(m_iPrevTime, m_iTime);
}
uint32 WorldTimer::getMSTime()
{
return getMSTime_internal();
}
uint32 WorldTimer::getMSTime_internal(bool savetime /*= false*/)
{
//get current time
const ACE_Time_Value currTime = ACE_OS::gettimeofday();
//calculate time diff between two world ticks
//special case: curr_time < old_time - we suppose that our time has not ticked at all
//this should be constant value otherwise it is possible that our time can start ticking backwards until next world tick!!!
uint64 diff = 0;
(currTime - g_SystemTickTime).msec(diff);
//lets calculate current world time
uint32 iRes = uint32(diff % UI64LIT(0x00000000FFFFFFFF));
return iRes;
}
//////////////////////////////////////////////////////////////////////////
int32 irand (int32 min, int32 max)
{
return int32 (mtRand->randInt (max - min)) + min;
}
uint32 urand (uint32 min, uint32 max)
{
return mtRand->randInt (max - min) + min;
}
float frand (float min, float max)
{
return mtRand->randExc (max - min) + min;
}
int32 rand32 ()
{
return mtRand->randInt ();
}
double rand_norm(void)
{
return mtRand->randExc ();
}
float rand_norm_f(void)
{
return (float)mtRand->randExc ();
}
double rand_chance (void)
{
return mtRand->randExc (100.0);
}
float rand_chance_f(void)
{
return (float)mtRand->randExc (100.0);
}
Milliseconds randtime(Milliseconds const& min, Milliseconds const& max)
{
long long diff = max.count() - min.count();
MANGOS_ASSERT(diff >= 0);
MANGOS_ASSERT(diff <= (uint32)-1);
return min + Milliseconds(urand(0, diff));
}
Tokens StrSplit(const std::string &src, const std::string &sep)
{
Tokens r;
std::string s;
for (std::string::const_iterator i = src.begin(); i != src.end(); i++)
{
if (sep.find(*i) != std::string::npos)
{
if (s.length()) r.push_back(s);
s = "";
}
else
{
s += *i;
}
}
if (s.length()) r.push_back(s);
return r;
}
uint32 GetUInt32ValueFromArray(Tokens const& data, uint16 index)
{
if(index >= data.size())
return 0;
return (uint32)atoi(data[index].c_str());
}
float GetFloatValueFromArray(Tokens const& data, uint16 index)
{
float result;
uint32 temp = GetUInt32ValueFromArray(data,index);
memcpy(&result, &temp, sizeof(result));
return result;
}
void stripLineInvisibleChars(std::string &str)
{
static std::string invChars = " \t\7\n";
size_t wpos = 0;
bool space = false;
for(size_t pos = 0; pos < str.size(); ++pos)
{
if(invChars.find(str[pos])!=std::string::npos)
{
if(!space)
{
str[wpos++] = ' ';
space = true;
}
}
else
{
if(wpos!=pos)
str[wpos++] = str[pos];
else
++wpos;
space = false;
}
}
if(wpos < str.size())
str.erase(wpos,str.size());
}
std::string secsToTimeString(time_t timeInSecs, bool shortText, bool hoursOnly)
{
time_t secs = timeInSecs % MINUTE;
time_t minutes = timeInSecs % HOUR / MINUTE;
time_t hours = timeInSecs % DAY / HOUR;
time_t days = timeInSecs / DAY;
std::ostringstream ss;
if(days)
{
ss << days;
if (shortText)
ss << "d";
else if (days == 1)
ss << " Day ";
else
ss << " Days ";
}
if(hours || hoursOnly)
{
ss << hours;
if (shortText)
ss << "h";
else if (hours <= 1)
ss << " Hour ";
else
ss << " Hours ";
}
if(!hoursOnly)
{
if(minutes)
{
ss << minutes;
if (shortText)
ss << "m";
else if (minutes == 1)
ss << " Minute ";
else
ss << " Minutes ";
}
if (secs || (!days && !hours && !minutes))
{
ss << secs;
if (shortText)
ss << "s";
else if (secs <= 1)
ss << " Second.";
else
ss << " Seconds.";
}
}
return ss.str();
}
uint32 TimeStringToSecs(const std::string& timestring)
{
uint32 secs = 0;
uint32 buffer = 0;
uint32 multiplier = 0;
for(std::string::const_iterator itr = timestring.begin(); itr != timestring.end(); itr++ )
{
if(isdigit(*itr))
{
buffer*=10;
buffer+= (*itr)-'0';
}
else
{
switch(*itr)
{
case 'd': multiplier = DAY; break;
case 'h': multiplier = HOUR; break;
case 'm': multiplier = MINUTE; break;
case 's': multiplier = 1; break;
default : return 0; //bad format
}
buffer*=multiplier;
secs+=buffer;
buffer=0;
}
}
return secs;
}
std::string TimeToTimestampStr(time_t t)
{
tm* aTm = localtime(&t);
// YYYY year
// MM month (2 digits 01-12)
// DD day (2 digits 01-31)
// HH hour (2 digits 00-23)
// MM minutes (2 digits 00-59)
// SS seconds (2 digits 00-59)
char buf[20];
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);
return std::string(buf);
}
/// Check if the string is a valid ip address representation
bool IsIPAddress(char const* ipaddress)
{
if(!ipaddress)
return false;
// Let the big boys do it.
// Drawback: all valid ip address formats are recognized e.g.: 12.23,121234,0xABCD)
return ACE_OS::inet_addr(ipaddress) != INADDR_NONE;
}
/// create PID file
uint32 CreatePIDFile(const std::string& filename)
{
FILE * pid_file = fopen (filename.c_str(), "w" );
if (pid_file == NULL)
return 0;
#ifdef WIN32
DWORD pid = GetCurrentProcessId();
#else
pid_t pid = getpid();
#endif
fprintf(pid_file, "%d", pid );
fclose(pid_file);
return (uint32)pid;
}
size_t utf8length(std::string& utf8str)
{
try
{
return utf8::distance(utf8str.c_str(),utf8str.c_str()+utf8str.size());
}
catch(std::exception)
{
utf8str = "";
return 0;
}
}
void utf8truncate(std::string& utf8str,size_t len)
{
try
{
size_t wlen = utf8::distance(utf8str.c_str(),utf8str.c_str()+utf8str.size());
if(wlen <= len)
return;
std::wstring wstr;
wstr.resize(wlen);
utf8::utf8to16(utf8str.c_str(),utf8str.c_str()+utf8str.size(),&wstr[0]);
wstr.resize(len);
char* oend = utf8::utf16to8(wstr.c_str(),wstr.c_str()+wstr.size(),&utf8str[0]);
utf8str.resize(oend-(&utf8str[0])); // remove unused tail
}
catch(std::exception)
{
utf8str = "";
}
}
bool Utf8toWStr(char const* utf8str, size_t csize, wchar_t* wstr, size_t& wsize)
{
try
{
size_t len = utf8::distance(utf8str,utf8str+csize);
if(len > wsize)
{
if(wsize > 0)
wstr[0] = L'\0';
wsize = 0;
return false;
}
wsize = len;
utf8::utf8to16(utf8str,utf8str+csize,wstr);
wstr[len] = L'\0';
}
catch(std::exception)
{
if(wsize > 0)
wstr[0] = L'\0';
wsize = 0;
return false;
}
return true;
}
bool Utf8toWStr(const std::string& utf8str, std::wstring& wstr)
{
try
{
size_t len = utf8::distance(utf8str.c_str(),utf8str.c_str()+utf8str.size());
wstr.resize(len);
if (len)
utf8::utf8to16(utf8str.c_str(),utf8str.c_str()+utf8str.size(),&wstr[0]);
}
catch(std::exception)
{
wstr = L"";
return false;
}
return true;
}
bool WStrToUtf8(wchar_t* wstr, size_t size, std::string& utf8str)
{
try
{
std::string utf8str2;
utf8str2.resize(size*4); // allocate for most long case
char* oend = utf8::utf16to8(wstr,wstr+size,&utf8str2[0]);
utf8str2.resize(oend-(&utf8str2[0])); // remove unused tail
utf8str = utf8str2;
}
catch(std::exception)
{
utf8str = "";
return false;
}
return true;
}
bool WStrToUtf8(std::wstring wstr, std::string& utf8str)
{
try
{
std::string utf8str2;
utf8str2.resize(wstr.size()*4); // allocate for most long case
char* oend = utf8::utf16to8(wstr.c_str(),wstr.c_str()+wstr.size(),&utf8str2[0]);
utf8str2.resize(oend-(&utf8str2[0])); // remove unused tail
utf8str = utf8str2;
}
catch(std::exception)
{
utf8str = "";
return false;
}
return true;
}
typedef wchar_t const* const* wstrlist;
bool utf8ToConsole(const std::string& utf8str, std::string& conStr)
{
#if PLATFORM == PLATFORM_WINDOWS
std::wstring wstr;
if(!Utf8toWStr(utf8str,wstr))
return false;
conStr.resize(wstr.size());
CharToOemBuffW(&wstr[0],&conStr[0],wstr.size());
#else
// not implemented yet
conStr = utf8str;
#endif
return true;
}
bool consoleToUtf8(const std::string& conStr,std::string& utf8str)
{
#if PLATFORM == PLATFORM_WINDOWS
std::wstring wstr;
wstr.resize(conStr.size());
OemToCharBuffW(&conStr[0],&wstr[0],conStr.size());
return WStrToUtf8(wstr,utf8str);
#else
// not implemented yet
utf8str = conStr;
return true;
#endif
}
bool Utf8FitTo(const std::string& str, std::wstring search)
{
std::wstring temp;
if(!Utf8toWStr(str,temp))
return false;
// converting to lower case
wstrToLower( temp );
if(temp.find(search) == std::wstring::npos)
return false;
return true;
}
void utf8printf(FILE *out, const char *str, ...)
{
va_list ap;
va_start(ap, str);
vutf8printf(stdout, str, &ap);
va_end(ap);
}
void vutf8printf(FILE *out, const char *str, va_list* ap)
{
#if PLATFORM == PLATFORM_WINDOWS
char temp_buf[32*1024];
wchar_t wtemp_buf[32*1024];
size_t temp_len = vsnprintf(temp_buf, 32*1024, str, *ap);
size_t wtemp_len = 32*1024-1;
Utf8toWStr(temp_buf, temp_len, wtemp_buf, wtemp_len);
CharToOemBuffW(&wtemp_buf[0], &temp_buf[0], wtemp_len+1);
fprintf(out, "%s", temp_buf);
#else
vfprintf(out, str, *ap);
#endif
}
void hexEncodeByteArray(uint8* bytes, uint32 arrayLen, std::string& result)
{
std::ostringstream ss;
for(uint32 i=0; i<arrayLen; ++i)
{
for(uint8 j=0; j<2; ++j)
{
unsigned char nibble = 0x0F & (bytes[i]>>((1-j)*4));
char encodedNibble;
if(nibble < 0x0A)
encodedNibble = '0'+nibble;
else
encodedNibble = 'A'+nibble-0x0A;
ss << encodedNibble;
}
}
result = ss.str();
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C++
1
https://gitee.com/SpringJoy/server.git
git@gitee.com:SpringJoy/server.git
SpringJoy
server
server
development

搜索帮助