213 lines
5.0 KiB
C++
213 lines
5.0 KiB
C++
#include <unistd.h>
|
|
#include <sys/time.h>
|
|
#include <sys/stat.h>
|
|
#include <netinet/in.h>
|
|
#include <arpa/inet.h>
|
|
#include <memory.h>
|
|
|
|
#include <a8/a8.h>
|
|
#include <a8/sysutils.h>
|
|
|
|
namespace a8
|
|
{
|
|
a8::tick_t XGetTickCount()
|
|
{
|
|
#if 1
|
|
struct timeval tv;
|
|
gettimeofday(&tv, NULL);
|
|
long long time = tv.tv_usec;
|
|
time /= 1000;
|
|
time += (tv.tv_sec * 1000);
|
|
return time;
|
|
#else
|
|
struct timespec ts;
|
|
clock_gettime(CLOCK_MONOTONIC, &ts);
|
|
return (ts.tv_sec*1000 + ts.tv_nsec/(1000*1000));
|
|
#endif
|
|
}
|
|
|
|
long long XGetNanoSeconds()
|
|
{
|
|
struct timespec ts;
|
|
clock_gettime(CLOCK_MONOTONIC, &ts);
|
|
return (ts.tv_sec*1000000000 + ts.tv_nsec);
|
|
}
|
|
|
|
long long GetMilliSecond()
|
|
{
|
|
struct timeval tv;
|
|
gettimeofday(&tv, NULL);
|
|
long long time = tv.tv_usec;
|
|
time /= 1000;
|
|
time += (tv.tv_sec * 1000);
|
|
return time;
|
|
}
|
|
|
|
int HashString(const std::string& str)
|
|
{
|
|
return 0;
|
|
}
|
|
|
|
a8::XValue Get(std::map<std::string, a8::XValue> &map, const char* key, const char* defval)
|
|
{
|
|
auto itr = map.find(std::string(key));
|
|
return itr != map.end() ? itr->second : a8::XValue(defval);
|
|
}
|
|
|
|
int RandEx(int min, int max)
|
|
{
|
|
if (min == max) {
|
|
return min;
|
|
}
|
|
|
|
if (min > max) {
|
|
int tmp = min;
|
|
min = max;
|
|
max = tmp;
|
|
}
|
|
|
|
return min + std::max(0, rand() % (max - min + 1));
|
|
}
|
|
|
|
int GetRangeValue(int src_value, int min, int max)
|
|
{
|
|
if (src_value < min) {
|
|
return min;
|
|
}
|
|
if (src_value > max) {
|
|
return max;
|
|
}
|
|
return rand() % max;
|
|
}
|
|
|
|
void SetBitFlag(int& flag_bits, int bit)
|
|
{
|
|
flag_bits |= (int)1 << bit;
|
|
}
|
|
|
|
void UnSetBitFlag(int& flag_bits, int bit)
|
|
{
|
|
flag_bits &= ~((int)1 << bit);
|
|
}
|
|
|
|
bool HasBitFlag(const int& flag_bits, int bit)
|
|
{
|
|
return (flag_bits & ((int)1 << bit)) != 0;
|
|
}
|
|
|
|
void SetBitFlag(long long& flag_bits, int bit)
|
|
{
|
|
flag_bits |= (long long)1 << bit;
|
|
}
|
|
|
|
void UnSetBitFlag(long long& flag_bits, int bit)
|
|
{
|
|
flag_bits &= ~((long long)1 << bit);
|
|
}
|
|
|
|
bool HasBitFlag(const long long& flag_bits, int bit)
|
|
{
|
|
return (flag_bits & ((long long)1 << bit)) != 0;
|
|
}
|
|
|
|
void XPrintf(const char* fmt, std::initializer_list<a8::XValue> args)
|
|
{
|
|
time_t nowtime = time(nullptr);
|
|
struct tm tm_nowtime = {0};
|
|
struct tm *ptr = localtime_r(&nowtime, &tm_nowtime);
|
|
char strtime[80];
|
|
strftime(strtime, 80, "%F %T ", ptr);
|
|
|
|
std::string data;
|
|
data.append((char*)strtime);
|
|
data += a8::Format(fmt, args);
|
|
|
|
printf("%s", data.c_str());
|
|
fflush(stdout);
|
|
}
|
|
|
|
bool MkDir(const std::string& path)
|
|
{
|
|
if (access(path.c_str(), W_OK) == 0) {
|
|
return true;
|
|
}
|
|
int status = mkdir(path.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
|
|
return status == 0;
|
|
}
|
|
|
|
void ForceCreateDir(const std::string& path)
|
|
{
|
|
if (access(path.c_str(), W_OK) == 0) {
|
|
return;
|
|
}
|
|
std::vector<std::string> strings;
|
|
a8::Split(path, strings, '/');
|
|
std::string cur_dir = "/";
|
|
for (auto& tmp_str : strings) {
|
|
cur_dir += tmp_str + "/";
|
|
if (access(cur_dir.c_str(), W_OK) != 0) {
|
|
a8::MkDir(cur_dir);
|
|
}
|
|
}
|
|
}
|
|
|
|
std::string GetIpAddress(unsigned long ip_saddr)
|
|
{
|
|
in_addr addr;
|
|
memset(&addr, 0, sizeof(addr));
|
|
addr.s_addr = ip_saddr;
|
|
char buf[255];
|
|
buf[0] = '\0';
|
|
inet_ntop(AF_INET, &addr, buf, 255);
|
|
return std::string(buf);
|
|
}
|
|
|
|
std::string FormatDateTime(time_t time_val)
|
|
{
|
|
struct tm tm_time = {0};
|
|
localtime_r(&time_val, &tm_time);
|
|
|
|
char buff[256];
|
|
buff[0] = '\0';
|
|
strftime(buff, a8::ArraySize(buff), "%F %T", &tm_time);
|
|
return std::string(buff);
|
|
}
|
|
|
|
time_t GetDaySeconds(time_t time_val, int incdays)
|
|
{
|
|
int g_time_zone = 8;
|
|
return time_t((time_val + g_time_zone * 3600)/3600/24 + incdays) * 3600 * 24 - 3600 * g_time_zone;
|
|
}
|
|
|
|
time_t BetweenDays(time_t time_val1, time_t time_val2)
|
|
{
|
|
int g_time_zone = 8;
|
|
return (time_val1 + g_time_zone*3600)/3600/24 - (time_val2 + g_time_zone*3600)/3600/24;
|
|
}
|
|
|
|
time_t GetDateTimeSeconds(const char* datetime_str)
|
|
{
|
|
int g_time_zone = 8;
|
|
struct tm stm = {0};
|
|
strptime(datetime_str, "%Y-%m-%d %H:%M:%S", &stm);
|
|
long t = mktime(&stm);
|
|
return t + g_time_zone*3600;
|
|
}
|
|
|
|
long long MakeInt64(int low32, int high32)
|
|
{
|
|
return low32 + ((long long)high32 << 32);
|
|
}
|
|
|
|
int Low32(long long int64_val)
|
|
{
|
|
return (int)int64_val;
|
|
}
|
|
|
|
int High32(long long int64_val)
|
|
{
|
|
return (int)(int64_val >> 32);
|
|
}
|
|
|
|
}
|