forked from dearlulu/hive
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtools.h
139 lines (114 loc) · 2.79 KB
/
tools.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/*
** repository: https://github.com/trumanzhao/luna
** trumanzhao, 2016/10/19, [email protected]
*/
#pragma once
#ifdef _MSC_VER
#include <time.h>
#include <direct.h>
#include <windows.h>
using int64_t = long long;
using uint64_t = unsigned long long;
#define getcwd _getcwd
#define strdup _strdup
#define tzset _tzset
#endif
#if defined(__linux) || defined(__APPLE__)
#include <stdint.h>
#include <unistd.h>
#include <fcntl.h>
#include <netdb.h>
#include <sys/stat.h>
using BYTE = unsigned char;
#endif
#include <string>
#include <chrono>
#include <thread>
inline int64_t get_time_ns() { return std::chrono::high_resolution_clock::now().time_since_epoch().count(); }
inline int64_t get_time_ms() { return get_time_ns() / 1000 / 1000; }
inline void sleep_ms(int ms) { std::this_thread::sleep_for(std::chrono::milliseconds(ms)); }
time_t get_file_time(const char* file_name);
template <typename T, int N>
void safe_cpy(T (&buffer)[N], const T* str)
{
if (str == nullptr)
{
buffer[0] = 0;
return;
}
int n = 0;
while (n < N && *str)
{
buffer[n++] = *str++;
}
if (n >= N)
n--;
buffer[n] = 0;
}
#ifdef _MSC_VER
inline struct tm* localtime_r(const time_t* timep, struct tm* result)
{
errno_t nErr = localtime_s(result, timep);
return (nErr == 0) ? result : nullptr;
}
#endif
#ifdef _MSC_VER
inline bool make_dir(const char cszDir[]) { return (_mkdir(cszDir) != -1); }
#endif
#if defined(__linux) || defined(__APPLE__)
inline bool make_dir(const char cszDir[]) { return (mkdir(cszDir, 0777) != -1); }
#endif
#ifdef _MSC_VER
inline uint64_t get_thread_id() { return GetCurrentThreadId(); }
#endif
#if defined(__linux) || defined(__APPLE__)
inline uint64_t get_thread_id() { return (uint64_t)pthread_self(); }
#endif
#define FAILED_JUMP(C) \
do \
{ \
if (!(C)) goto Exit0; \
} while (0)
#define SAFE_FREE(p) \
do \
{ \
if (p) \
{ \
free(p); \
(p) = nullptr; \
} \
} while (0)
#define SAFE_DELETE(p) \
do \
{ \
if (p) \
{ \
delete (p); \
(p) = nullptr; \
} \
} while (0)
#define SAFE_DELETE_ARRAY(p) \
do \
{ \
if (p) \
{ \
delete[] (p); \
(p) = nullptr; \
} \
} while (0)
#define SAFE_RELEASE(p) \
do \
{ \
if (p) \
{ \
(p)->release(); \
(p) = nullptr; \
} \
} while (0)
#if defined(__linux) || defined(__APPLE__)
template <typename T, int N>
constexpr int _countof(T(&_array)[N]) { return N; }
#endif
#define MAX_ERROR_TXT 128
char* get_error_string(char buffer[], int len, int no);
void get_error_string(std::string& err, int no);