1
Fork 0
mirror of https://github.com/SapphireServer/Sapphire.git synced 2025-04-26 14:37:44 +00:00
sapphire/src/common/Util/Util.cpp

130 lines
3.1 KiB
C++
Raw Normal View History

2017-08-08 13:53:47 +02:00
#include "Util.h"
#include <chrono>
#include <sstream>
#include <iomanip>
2018-10-25 23:50:18 +02:00
#include <algorithm>
#include <string>
2017-08-08 13:53:47 +02:00
std::string Core::Util::binaryToHexString( uint8_t* pBinData, uint16_t size )
{
std::string outStr;
2017-08-08 13:53:47 +02:00
for( uint32_t i = 0; i < size; i++ )
{
2018-10-25 23:50:18 +02:00
outStr += Util::intToHexString( static_cast< int8_t>( pBinData[ i ] & 0xFF ) );
}
2017-08-08 13:53:47 +02:00
return outStr;
2017-08-08 13:53:47 +02:00
}
2018-10-25 23:50:18 +02:00
std::string Core::Util::toLowerCopy( const std::string& inStr )
{
std::string out;
std::transform( inStr.begin(), inStr.end(), out.begin(), ::tolower );
return out;
}
2017-08-08 13:53:47 +02:00
2018-10-25 23:50:18 +02:00
std::string Core::Util::intToHexString( uint64_t intValue, uint8_t width )
{
std::string hexStr;
/// integer value to hex-string
std::stringstream sstream;
sstream << std::setfill ('0') << std::setw( width )
<< std::hex << intValue;
hexStr = sstream.str();
sstream.clear();
return hexStr;
}
std::string Core::Util::binaryToHexDump( uint8_t* pBinData, uint16_t size )
{
int bytesPerLine = 16;
constexpr char hexChars[] = "0123456789ABCDEF";
int offsetBlock = 8 + 3;
int byteBlock = offsetBlock + bytesPerLine * 3 + ( bytesPerLine - 1 ) / 8 + 2;
int lineLength = byteBlock + bytesPerLine + 1;
std::string line( lineLength, ' ' );
int numLines = ( size + bytesPerLine - 1 ) / bytesPerLine;
std::string outStr;
for( uint32_t i = 0; i < size; i += bytesPerLine )
{
line[ 0 ] = hexChars[ ( i >> 28 ) & 0xF ];
line[ 1 ] = hexChars[ ( i >> 24 ) & 0xF ];
line[ 2 ] = hexChars[ ( i >> 20 ) & 0xF ];
line[ 3 ] = hexChars[ ( i >> 16 ) & 0xF ];
line[ 4 ] = hexChars[ ( i >> 12 ) & 0xF ];
line[ 5 ] = hexChars[ ( i >> 8 ) & 0xF ];
line[ 6 ] = hexChars[ ( i >> 4 ) & 0xF ];
line[ 7 ] = hexChars[ ( i >> 0 ) & 0xF ];
int hexColumn = offsetBlock;
int charColumn = byteBlock;
for( int j = 0; j < bytesPerLine; j++ )
{
if( j > 0 && ( j & 7 ) == 0 )
{
hexColumn++;
}
if( i + j >= size )
{
line[ hexColumn ] = ' ';
line[ hexColumn + 1 ] = ' ';
line[ charColumn ] = ' ';
}
else
{
uint8_t by = pBinData[ i + j ];
line[ hexColumn ] = hexChars[ ( by >> 4 ) & 0xF ];
line[ hexColumn + 1 ] = hexChars[ by & 0xF ];
line[ charColumn ] = by < 32 ? '.' : static_cast< char >( by );
}
hexColumn += 3;
charColumn++;
}
outStr += line + "\n";
}
return outStr;
}
2017-08-08 13:53:47 +02:00
uint64_t Core::Util::getTimeMs()
{
std::chrono::milliseconds epoch = std::chrono::duration_cast< std::chrono::milliseconds >
( std::chrono::system_clock::now().time_since_epoch() );
return epoch.count();
2017-08-08 13:53:47 +02:00
}
int64_t Core::Util::getTimeSeconds()
{
std::chrono::seconds epoch = std::chrono::seconds( std::time( nullptr ) );
return epoch.count();
}
2017-08-08 13:53:47 +02:00
uint64_t Core::Util::getEorzeanTimeStamp()
{
return static_cast< uint64_t >( getTimeSeconds() * 20.571428571428573f );
2017-08-08 13:53:47 +02:00
}
void Core::Util::valueToFlagByteIndexValue( uint32_t inVal, uint8_t& outVal, uint16_t& outIndex )
{
uint32_t id = inVal;
outIndex = id / 8;
uint8_t bitIndex = id % 8;
2017-08-08 13:53:47 +02:00
outVal = 1 << bitIndex;
2017-08-08 13:53:47 +02:00
}