初版
This commit is contained in:
125
src/Tool.cpp
Normal file
125
src/Tool.cpp
Normal file
@@ -0,0 +1,125 @@
|
||||
#include "Tool.h"
|
||||
#include <string.h>
|
||||
#include <chrono>
|
||||
#include <ctime>
|
||||
using namespace std;
|
||||
|
||||
Tool::Tool(/* args */)
|
||||
{
|
||||
}
|
||||
|
||||
Tool::~Tool()
|
||||
{
|
||||
}
|
||||
|
||||
int Tool::ByteLittleToInt(unsigned char *Count)
|
||||
{
|
||||
int int1 = Count[0] & 0xff;
|
||||
int int2 = (Count[1] & 0xff) << 8;
|
||||
int int3 = (Count[2] & 0xff) << 16;
|
||||
int int4 = (Count[3] & 0xff) << 24;
|
||||
|
||||
return int1 | int2 | int3 | int4;
|
||||
}
|
||||
|
||||
unsigned char *Tool::IntToByteLittle(int Count)
|
||||
{
|
||||
unsigned char *b = new unsigned char[4];
|
||||
b[0] = (Count & 0xff);
|
||||
b[1] = (Count & 0xff00) >> 8;
|
||||
b[2] = (Count & 0xff0000) >> 16;
|
||||
b[3] = (Count & 0xff000000) >> 24;
|
||||
|
||||
return b;
|
||||
}
|
||||
|
||||
unsigned char *Tool::StringToByte(std::string str)
|
||||
{
|
||||
char *cstr = (char *)str.c_str();
|
||||
int Count = strlen(cstr);
|
||||
unsigned char *temp = IntToByteLittle(Count);
|
||||
unsigned char *buffer = new unsigned char[Count + 4];
|
||||
for (size_t i = 0; i < 4; i++)
|
||||
{
|
||||
buffer[i] = temp[i];
|
||||
}
|
||||
delete[] temp;
|
||||
for (size_t z = 4; z < Count + 4; z++)
|
||||
{
|
||||
buffer[z] = cstr[z - 4];
|
||||
}
|
||||
return buffer;
|
||||
}
|
||||
|
||||
void Tool::Error(std::string O)
|
||||
{
|
||||
#ifdef Development_mode
|
||||
std::cout << "\n----[ERROR]----\n";
|
||||
std::cout << "\n----[By "
|
||||
<< "YosinLenheart"
|
||||
<< "]----\n";
|
||||
std::cout << O << std::endl;
|
||||
std::cout << "\n----------------\n";
|
||||
#endif
|
||||
}
|
||||
void Tool::Warring(std::string W)
|
||||
{
|
||||
#ifdef Development_mode
|
||||
std::cout << "\n----[WARRING]----\n";
|
||||
std::cout << "\n----[By "
|
||||
<< "YosinLenheart"
|
||||
<< "]----\n";
|
||||
std::cout << W << std::endl;
|
||||
std::cout << "\n----------------\n";
|
||||
#endif
|
||||
}
|
||||
void Tool::Logger(std::string L)
|
||||
{
|
||||
#ifdef Development_mode
|
||||
std::cout << "\n----[LOGGER]----\n";
|
||||
std::cout << "\n----[By "
|
||||
<< "YosinLenheart"
|
||||
<< "]----\n";
|
||||
std::cout << L << std::endl;
|
||||
std::cout << "\n----------------\n";
|
||||
#endif
|
||||
}
|
||||
|
||||
long long Tool::get_cur_time()
|
||||
{
|
||||
// 获取操作系统当前时间点(精确到微秒)
|
||||
chrono::time_point<chrono::system_clock, chrono::microseconds> tpMicro = chrono::time_point_cast<chrono::microseconds>(chrono::system_clock::now());
|
||||
// (微秒精度的)时间点 => (微秒精度的)时间戳
|
||||
time_t totalMicroSeconds = tpMicro.time_since_epoch().count();
|
||||
|
||||
long long currentTime = ((long long)totalMicroSeconds) / 1000;
|
||||
|
||||
return currentTime;
|
||||
}
|
||||
|
||||
int Tool::UTF8ToGB2312(char *szSrc, size_t iSrcLen, char *szDst, size_t iDstLen)
|
||||
{
|
||||
iconv_t cd = iconv_open("gb2312//IGNORE", "utf-8//IGNORE"); // take care of "//IGNORE", it will ignore those invalid code
|
||||
if (0 == cd)
|
||||
return -2;
|
||||
memset(szDst, 0, iDstLen);
|
||||
char **src = &szSrc;
|
||||
char **dst = &szDst;
|
||||
if (-1 == (int)iconv(cd, src, &iSrcLen, dst, &iDstLen))
|
||||
return -1;
|
||||
iconv_close(cd);
|
||||
return 0;
|
||||
}
|
||||
int Tool::GB2312ToUTF8(char *szSrc, size_t iSrcLen, char *szDst, size_t iDstLen)
|
||||
{
|
||||
iconv_t cd = iconv_open("utf-8//IGNORE", "gb2312//IGNORE");
|
||||
if (0 == cd)
|
||||
return -2;
|
||||
memset(szDst, 0, iDstLen);
|
||||
char **src = &szSrc;
|
||||
char **dst = &szDst;
|
||||
if (-1 == (int)iconv(cd, src, &iSrcLen, dst, &iDstLen))
|
||||
return -1;
|
||||
iconv_close(cd);
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user