-
Notifications
You must be signed in to change notification settings - Fork 17
/
util.cc
107 lines (81 loc) · 1.78 KB
/
util.cc
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
#include <time.h>
#include <limits.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdint.h>
#include <libgen.h>
#include <string>
#include <random>
/*******************************************************************************
** string utils **
******************************************************************************/
std::string
str_realpath(std::string s)
{
char real[PATH_MAX+1];
if(!realpath(s.c_str(), real)) {
return "";
}
return std::string(real);
}
std::string
str_realpath_dir(std::string s)
{
char real[PATH_MAX+1], *dir;
if(!realpath(s.c_str(), real)) {
return "";
}
dir = dirname(real);
return std::string(dir);
}
std::string
str_realpath_base(std::string s)
{
char real[PATH_MAX+1], *base;
if(!realpath(s.c_str(), real)) {
return "";
}
base = basename(real);
return std::string(base);
}
std::string
str_getenv(std::string env)
{
char *e;
e = getenv(env.c_str());
return e ? std::string(e) : "";
}
/*******************************************************************************
** rand functions **
******************************************************************************/
uint64_t
rand64()
{
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_int_distribution<unsigned long long> dis(0, 0xffffffffffffffff);
return dis(gen);
}
uint64_t
xorshift128plus()
{
uint64_t x, y;
static uint64_t s[2];
static int inited = 0;
if(!inited) {
s[0] = rand64();
s[1] = rand64();
inited = 1;
}
x = s[0];
y = s[1];
s[0] = y;
x ^= x << 23;
s[1] = x ^ y ^ (x >> 17) ^ (y >> 26);
return s[1] + y;
}
uint64_t
fast_rand64()
{
return xorshift128plus();
}