-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathhelper.cpp
57 lines (45 loc) · 1.27 KB
/
helper.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
#include "helper.h"
#include <fstream>
#include <iostream>
#include <limits>
#include <sstream>
#include <string>
#include <cassert>
#include <cstdlib>
#include <cstring>
#include <sys/stat.h>
#include <unistd.h>
#include <errno.h>
bool dirExists(const std::string& dir) {
assert(!dir.empty());
struct stat st;
stat(dir.c_str(), &st);
return S_ISDIR(st.st_mode);
}
bool fileReadable(const std::string& path) {
assert(!path.empty());
return access(path.c_str(), R_OK) == 0;
}
bool isNumber(const std::string& str) {
return !str.empty() && str.find_first_not_of("0123456789.-") == std::string::npos;
}
double uptime() {
static const std::string fileName = "/proc/uptime";
std::ifstream file(fileName.c_str(), std::ifstream::in);
if (!file.good()) {
std::cerr << "could not open " << fileName << ": " << strerror(errno) << std::endl;
assert(false);
return std::numeric_limits<double>::quiet_NaN();
}
std::string time;
file >> time;
assert(!time.empty());
const double ret = stringToNumber<double>(time);
assert(ret > 0.0);
return ret;
}
long getHertz() {
static long hertz = sysconf(_SC_CLK_TCK); // should not change during runtime
assert(hertz > 0);
return hertz;
}