forked from hoytech/strfry
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmisc.cpp
123 lines (89 loc) · 2.53 KB
/
misc.cpp
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
#include <arpa/inet.h>
#ifdef __FreeBSD__
#include <sys/socket.h>
#include <netinet/in.h>
#endif
#include <stdio.h>
#include <signal.h>
#include <algorithm>
#include <string>
#include "golpe.h"
std::string renderIP(std::string_view ipBytes) {
char buf[128];
if (ipBytes.size() == 4) {
inet_ntop(AF_INET, ipBytes.data(), buf, sizeof(buf));
} else if (ipBytes.size() == 16) {
inet_ntop(AF_INET6, ipBytes.data(), buf, sizeof(buf));
} else {
throw herr("invalid size of ipBytes, unable to render IP");
}
return std::string(buf);
}
std::string parseIP(const std::string &ip) {
int af = ip.find(':') != std::string::npos ? AF_INET6 : AF_INET;
unsigned char buf[16];
int ret = inet_pton(af, ip.c_str(), &buf[0]);
if (ret == 0) return "";
return std::string((const char*)&buf[0], af == AF_INET6 ? 16 : 4);
}
std::string renderSize(uint64_t si) {
if (si < 1024) return std::to_string(si) + "b";
double s = si;
char buf[128];
char unit;
do {
s /= 1024;
if (s < 1024) {
unit = 'K';
break;
}
s /= 1024;
if (s < 1024) {
unit = 'M';
break;
}
s /= 1024;
if (s < 1024) {
unit = 'G';
break;
}
s /= 1024;
unit = 'T';
} while(0);
::snprintf(buf, sizeof(buf), "%.2f%c", s, unit);
return std::string(buf);
}
std::string renderPercent(double p) {
char buf[128];
::snprintf(buf, sizeof(buf), "%.1f%%", p * 100);
return std::string(buf);
}
uint64_t parseUint64(const std::string &s) {
auto digitChar = [](char c){
return c >= '0' && c <= '9';
};
if (!std::all_of(s.begin(), s.end(), digitChar)) throw herr("non-digit character");
return std::stoull(s);
}
uint64_t getDBVersion(lmdb::txn &txn) {
uint64_t dbVersion;
{
auto s = env.lookup_Meta(txn, 1);
if (s) {
dbVersion = s->dbVersion();
} else {
dbVersion = 0;
}
}
return dbVersion;
}
std::string padBytes(std::string_view str, size_t n, char padChar) {
if (str.size() > n) throw herr("unable to pad, string longer than expected");
return std::string(str) + std::string(n - str.size(), padChar);
}
void exitOnSigPipe() {
struct sigaction act;
memset(&act, 0, sizeof act);
act.sa_sigaction = [](int, siginfo_t*, void*){ ::exit(1); };
if (sigaction(SIGPIPE, &act, nullptr)) throw herr("couldn't run sigaction(): ", strerror(errno));
}