-
Notifications
You must be signed in to change notification settings - Fork 6
/
helper.h
56 lines (44 loc) · 1.55 KB
/
helper.h
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
#ifndef HELPER_H
#define HELPER_H HELPER_H
#include <iomanip>
#include <sstream>
#include <string>
#include <cassert>
/// returns whether the given directory exists
bool dirExists(const std::string& dir);
/// returns whether the given file is readable
bool fileReadable(const std::string& path);
/// returns whether the given string is a number
/// @note doesn't detect all errors, but is fast
bool isNumber(const std::string& str);
/// converts a std::string to a number
/// @note expensive, try to avoid it
/// @note no real error handling
template <class T>
T stringToNumber(const std::string& str) {
assert(!str.empty());
T number = 0;
std::stringstream sstr(str);
sstr >> number;
assert(sstr); // check if conversion succeeded
return number;
}
/// converts a number to a std::string
/// @note expensive, try to avoid it
/// @note no real error handling
template <class T>
std::string numberToString(const T number) {
std::stringstream sstr;
sstr << std::fixed << std::setprecision(2) << number;
assert(!sstr.str().empty());
return sstr.str();
}
/// returns the system uptime in seconds from /proc/uptime
/// or std::numeric_limits<double>::quiet_NaN() on error
double uptime();
/// returns the kernel ticks per second (hz rate) as reported by sysconf
/// according to 'proc/sysinfo.c' from the 'procps' package (where 'top' comes from) this
/// value might be wrong. this file also lists other crappy ways of obtaining this value.
/// htop also uses _SC_CLK_TCK from sysconf().
long getHertz();
#endif // HELPER_H