-
Notifications
You must be signed in to change notification settings - Fork 2
/
Util.hpp
360 lines (328 loc) · 8.37 KB
/
Util.hpp
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
#ifndef UTIL_HPP
#define UTIL_HPP
#include <string>
#include <chrono>
#include <ctime>
#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <sys/time.h>
#include <time.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <thread>
#include <vector>
#include <string.h>
#include <dirent.h>
#include <algorithm>
#include <unordered_map>
#include <map>
#include <mutex>
#include <iconv.h>
#include<pthread.h>
namespace Utils
{
using namespace std;
/********************************
struct timespec
{
time_t tv_sec;
long int tv_nsec;
};
struct tm {
int tm_sec;
int tm_min;
int tm_hour;
int tm_mday;
int tm_mon;
int tm_year;
int tm_wday;
int tm_yday;
int tm_isdst;
};
********************************/
static unsigned long getTimeNs()
{
struct timespec timeStamp = {0, 0};
clock_gettime(CLOCK_REALTIME, &timeStamp);
return timeStamp.tv_sec * 1e9 + timeStamp.tv_nsec;
}
static unsigned long getTimeUs()
{
return getTimeNs() / 1e3;
}
static unsigned long getTimeMs()
{
return getTimeNs() / 1e6;
}
static unsigned long getTimeSec()
{
struct timespec timeStamp = {0, 0};
clock_gettime(CLOCK_REALTIME, &timeStamp);
return timeStamp.tv_sec;
}
static const char *getCurrentTimeSec()
{
time_t current = getTimeSec();
struct tm timeStamp;
localtime_r(¤t, &timeStamp);
static char szBuffer[64] = {0};
strftime(szBuffer, sizeof(szBuffer), "%Y-%m-%d %H:%M:%S", &timeStamp);
return szBuffer;
}
static int getCurrentTodaySec()
{
std::string CurrentTime = getCurrentTimeSec() + 11;
int hour, minute, second;
int n = sscanf(CurrentTime.c_str(), "%d:%d:%d", &hour, &minute, &second);
if (n != 3)
return -1;
return hour * 3600 + minute * 60 + second;
}
static const char *getCurrentTimeMs()
{
unsigned long n = Utils::getTimeMs();
time_t current = n / 1e3;
struct tm timeStamp;
localtime_r(¤t, &timeStamp);
static char szBuffer[64] = {0};
strftime(szBuffer, sizeof(szBuffer), "%Y-%m-%d %H:%M:%S", &timeStamp);
unsigned long mod = 1e3;
unsigned long ret = n % mod;
sprintf(szBuffer, "%s.%03u", szBuffer, ret);
return szBuffer;
}
static const char *getCurrentTimeUs()
{
unsigned long n = Utils::getTimeUs();
time_t current = n / 1e6;
struct tm timeStamp;
localtime_r(¤t, &timeStamp);
static char szBuffer[64] = {0};
strftime(szBuffer, sizeof(szBuffer), "%Y-%m-%d %H:%M:%S", &timeStamp);
unsigned long mod = 1e6;
unsigned long ret = n % mod;
sprintf(szBuffer, "%s.%06u", szBuffer, ret);
return szBuffer;
}
static const char *getCurrentTimeNs()
{
unsigned long n = Utils::getTimeNs();
time_t current = n / 1e9;
struct tm timeStamp;
localtime_r(¤t, &timeStamp);
static char szBuffer[64] = {0};
strftime(szBuffer, sizeof(szBuffer), "%Y-%m-%d %H:%M:%S", &timeStamp);
unsigned long mod = 1e9;
unsigned long ret = n % mod;
sprintf(szBuffer, "%s.%09u", szBuffer, ret);
return szBuffer;
}
static const char *getCurrentDay()
{
unsigned long n = Utils::getTimeNs();
time_t current = n / 1e9;
struct tm timeStamp;
localtime_r(¤t, &timeStamp);
static char szBuffer[64] = {0};
strftime(szBuffer, sizeof(szBuffer), "%Y-%m-%d", &timeStamp);
return szBuffer;
}
static const char *getCurrentNumberDay()
{
unsigned long n = Utils::getTimeNs();
time_t current = n / 1e9;
struct tm timeStamp;
localtime_r(¤t, &timeStamp);
static char szBuffer[64] = {0};
strftime(szBuffer, sizeof(szBuffer), "%Y%m%d", &timeStamp);
return szBuffer;
}
static unsigned long getTimeStamp(const char *str)
{
unsigned long ret = 0;
char szBuffer[64] = {0};
struct tm timeStamp;
strptime(szBuffer, "%Y-%m-%d %H:%M:%S", &timeStamp);
ret = mktime(&timeStamp);
return ret;
}
static long getTimeStampUs(const char *str)
{
int hour, minute, second, microsecond;
int n = sscanf(str, "%d:%d:%d.%d", &hour, &minute, &second, µsecond);
if (n != 4)
return -1;
return (hour * 3600 + minute * 60 + second) * 1000000 + microsecond;
}
static long getTimeStampMs(const char *str)
{
int hour, minute, second, millisecond;
int n = sscanf(str, "%d:%d:%d.%d", &hour, &minute, &second, &millisecond);
if (n != 4)
return -1;
return (hour * 3600 + minute * 60 + second) * 1000 + millisecond;
}
static int64_t TimeDiffUs(const std::string& start, const std::string& end)
{
int hour1, minute1, second1, microsecond1;
int n1 = sscanf(start.c_str(), "%d:%d:%d.%d", &hour1, &minute1, &second1, µsecond1);
int hour2, minute2, second2, microsecond2;
int n2 = sscanf(end.c_str(), "%d:%d:%d.%d", &hour2, &minute2, &second2, µsecond2);
if (n1 == 4 && n2 == 4)
{
return ((hour2 - hour1) * 3600 + (minute2 - minute1) * 60 + (second2 - second1)) * 1000 * 1000 + microsecond2 - microsecond1;
}
else
{
return -1;
}
}
static bool startWith(const std::string &src, const std::string &head)
{
return src.compare(0, head.length(), head) == 0;
}
static bool endWith(const std::string &src, const std::string &tail)
{
return src.compare(src.length() - tail.length(), tail.length(), tail) == 0;
}
static bool equalWith(const std::string &src, const std::string &dest)
{
return src.compare(dest) == 0;
}
static void RightFill(int totalWidth, char fill, std::string& src)
{
int width = totalWidth - src.length();
char* temp = new char[width + 1];
memset(temp, 0, width + 1);
for(int i = 0; i < width; i++)
{
temp[i]= fill;
}
std::string buffer = temp;
src.append(buffer);
delete temp;
temp = NULL;
}
static void LeftFill(int totalWidth, char fill, std::string& src)
{
int width = totalWidth - src.length();
char* temp = new char[width + 1];
memset(temp, 0, width + 1);
for(int i = 0; i < width; i++)
{
temp[i]= fill;
}
std::string buffer = temp;
buffer.append(src);
src = buffer;
delete temp;
temp = NULL;
}
static void Split(const std::string &src, const std::string &delimiter, std::vector<std::string> &value)
{
value.clear();
if (src.size() < 2)
return;
string str("");
for (size_t i = 0; i < src.size(); i++)
{
bool is_equal = false;
for (size_t j = 0; j < delimiter.size(); j++)
{
if (src[i] == delimiter[j])
{
is_equal = true;
break;
}
}
if (is_equal)
{
if (str.size() > 0)
{
value.push_back(str);
str.clear();
}
}
else
{
str += src[i];
}
}
value.push_back(str);
}
static void CreateDirectory(const char *str)
{
mkdir(str, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
}
static int CodeConvert(char *inbuf, size_t inlen, char* outbuf, size_t outlen, const char *from_charset, const char *to_charset)
{
iconv_t cd;
int rc;
char **pin = &inbuf;
char **pout = &outbuf;
cd = iconv_open(to_charset,from_charset);
if (cd == (iconv_t)(-1))
return -1;
memset(outbuf, 0, outlen);
if (iconv(cd,pin, &inlen, pout, &outlen) == (size_t)(-1))
return -1;
iconv_close(cd);
return 0;
}
static bool ThreadBind(pthread_t thread, int cpuid)
{
cpu_set_t mask;
CPU_ZERO(&mask);
CPU_SET(cpuid, &mask);
return pthread_setaffinity_np(thread, sizeof(mask), &mask) == 0;
}
static bool ReadDirectory(const std::string& path, std::vector<std::string>& ret)
{
ret.clear();
bool ok = true;
struct dirent* ptr;
DIR* dir = opendir(path.c_str());
if(dir != NULL)
{
while((ptr = readdir(dir)) != NULL)
{
if(DT_REG == ptr->d_type)
{
ret.push_back(ptr->d_name);
}
}
std::sort(ret.begin(), ret.end());
}
else
{
ok = false;
}
closedir(dir);
return ok;
}
static bool ReadFile(const std::string& path, std::vector<std::string>& ret)
{
ret.clear();
bool ok = true;
FILE* fp = fopen(path.c_str(), "r");
if (NULL != fp)
{
char *line = NULL;
size_t len = 0;
ssize_t read;
while ((read = getline(&line, &len, fp)) != -1)
{
ret.push_back(line);
}
free(line);
}
else
{
ok = false;
}
return ok;
}
}
#endif // UTIL_HPP