-
Notifications
You must be signed in to change notification settings - Fork 1
/
util.hpp
76 lines (63 loc) · 1.46 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
#pragma once
#include <chrono>
#include "winux.hpp"
#ifndef B
#define B unsigned char
#endif
#ifndef LL
#define LL long long
#endif
#ifndef BUF_SIZE
#define BUF_SIZE (1 << 8)
#endif
inline int endswith(std::string s, std::string sub) {
return s.rfind(sub) < s.length() && s.rfind(sub) == (s.length() - sub.length());
}
inline void throws(std::string s) {
throw std::runtime_error(s);
}
inline int not_exist(std::string s) {
FILE *fp;
fp = fopen(s.c_str(), "rb");
if (!fp) {
return 1;
}
fclose(fp);
return 0;
}
inline int exec_r(const char *cmd, char *res, int res_size = BUF_SIZE) {
FILE *p = r_popen(cmd);
if (!p) return -1;
int t = 0;
while (!feof(p)) {
if (t + BUF_SIZE < res_size) {
t += fread(res + t, 1, BUF_SIZE, p);
} else {
t += fread(res + t, 1, res_size - t - 1, p);
break;
}
}
pipe_pclose(p);
res[t] = 0;
return t;
}
class Timer {
private:
std::chrono::steady_clock::time_point t0;
public:
LL clk;
Timer(LL c) : clk(c) {}
inline void bg() {
t0 = std::chrono::steady_clock::now();
}
inline void wait() {
auto t1 = std::chrono::steady_clock::now();
while ((LL)std::chrono::duration_cast<std::chrono::microseconds>(t1 - t0).count() < clk) {
t1 = std::chrono::steady_clock::now();
}
t0 = t1;
}
inline void slp(int s) {
second_sleep(s);
}
};