// ***************************************************************************
//
// Reality - The Matrix Online Server Emulator
// Copyright (C) 2006-2010 Rajko Stojadinovic
// http://mxoemu.info
//
// ---------------------------------------------------------------------------
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 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 Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see .
//
// ---------------------------------------------------------------------------
//
// ***************************************************************************
#include "Common.h"
#include "Util.h"
#include
#include "ByteBuffer.h"
vector StrSplit(const string &src, const string &sep)
{
vector r;
string s;
for (string::const_iterator i = src.begin(); i != src.end(); i++)
{
if (sep.find(*i) != string::npos)
{
if (s.length()) r.push_back(s);
s = "";
}
else
{
s += *i;
}
}
if (s.length()) r.push_back(s);
return r;
}
int random(int low,int high)
{
return rand() % (high - low + 1) + low;
}
string Bin2Str(const byte *data,size_t count)
{
if (data != NULL && count > 0)
{
ostringstream myStream;
//if no spaces, then only one 0x at the start
for (unsigned int j = 0;j < count;j++)
{
byte n = data[j];
if (n < 32) n = 46; //anything below 32 is a .
myStream << n;
}
myStream.flush();
return myStream.str();
}
return string();
}
string Bin2Str(const char *data,size_t count)
{
return Bin2Str((const byte*)data,count);
}
string Bin2Str(const ByteBuffer &sourceBuf)
{
return Bin2Str((const byte*)sourceBuf.contents(),sourceBuf.size());
}
string Bin2Str(const string &sourceStr)
{
return Bin2Str(sourceStr.data(),sourceStr.size());
}
string Bin2Hex(const byte *data,size_t count,uint32 flags)
{
if (data != NULL && count > 0)
{
ostringstream myStream;
//if no spaces, then only one 0x at the start
if ( (flags & BIN2HEX_ZEROES) && !(flags & BIN2HEX_SPACES) )
myStream << "0x";
for (unsigned int j = 0;j < count;j++)
{
byte n = data[j];
//if spaces, then 0x before every byte
if ( (flags & BIN2HEX_ZEROES) && (flags & BIN2HEX_SPACES) )
myStream << "0x";
if (n <= 15)
myStream << "0";
myStream << std::hex << (int)n;
//if no zeroes, just use space to separate
if (flags & BIN2HEX_SPACES && !(flags & BIN2HEX_ZEROES) )
myStream << " ";
// if zeroes, use , to separate
if (flags & BIN2HEX_SPACES && (flags & BIN2HEX_ZEROES) )
myStream << ",";
}
myStream.flush();
//remove the trailing space/,
if (flags & BIN2HEX_SPACES)
return myStream.str().substr(0,myStream.str().length()-1);
else
return myStream.str();
}
return string();
}
string Bin2Hex(const char *data,size_t count,uint32 flags)
{
return Bin2Hex((const byte*)data,count,flags);
}
string Bin2Hex(const ByteBuffer &sourceBuf,uint32 flags)
{
return Bin2Hex(sourceBuf.contents(),sourceBuf.size(),flags);
}
string Bin2Hex(const string &sourceStr,uint32 flags)
{
return Bin2Hex(sourceStr.data(),sourceStr.size(),flags);
}
std::string ClientVersionString(uint32 theVersion)
{
uint16 versions[2];
memcpy(versions,&theVersion,sizeof(theVersion));
std::stringstream minorVersion;
minorVersion << versions[0];
std::string minorVersionReverse = minorVersion.str();
std::reverse(minorVersionReverse.begin(),minorVersionReverse.end());
std::stringstream versionStr;
versionStr << versions[1] << "." << minorVersionReverse;
return versionStr.str();
}
string XOR(string value,string key)
{
string retval(value);
size_t klen=key.length();
size_t vlen=value.length();
size_t k=0;
size_t v=0;
for(v=0;vtm_hour += dLength;
break;
case 'd': // days
ti->tm_mday += dLength;
break;
case 'w': // weeks
ti->tm_mday += 7 * dLength;
break;
case 'm': // months
ti->tm_mon += dLength;
break;
case 'y': // years
// are leap years considered ? do we care ?
ti->tm_year += dLength;
break;
default: // minutes
ti->tm_min += dLength;
break;
}
return mktime(ti);
}