-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.cpp
More file actions
70 lines (60 loc) · 1.92 KB
/
Copy pathutil.cpp
File metadata and controls
70 lines (60 loc) · 1.92 KB
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
#include "clawguard.h"
namespace clawguard::util {
int64_t now_ms() {
return std::chrono::duration_cast<std::chrono::milliseconds>(
std::chrono::system_clock::now().time_since_epoch()
).count();
}
std::string format_bytes(uint64_t bytes) {
const char* units[] = {"B", "KB", "MB", "GB", "TB"};
int i = 0;
double size = static_cast<double>(bytes);
while (size >= 1024.0 && i < 4) {
size /= 1024.0;
i++;
}
std::ostringstream ss;
ss << std::fixed << std::setprecision(1) << size << " " << units[i];
return ss.str();
}
std::string format_duration(int64_t seconds) {
int64_t days = seconds / 86400;
int64_t hours = (seconds % 86400) / 3600;
int64_t mins = (seconds % 3600) / 60;
std::ostringstream ss;
if (days > 0) ss << days << "d ";
if (hours > 0) ss << hours << "h ";
ss << mins << "m";
return ss.str();
}
std::string format_timestamp(int64_t ms) {
auto tp = std::chrono::system_clock::time_point(std::chrono::milliseconds(ms));
auto time_t_val = std::chrono::system_clock::to_time_t(tp);
std::tm tm_val;
localtime_r(&time_t_val, &tm_val);
char buf[64];
strftime(buf, sizeof(buf), "%Y-%m-%dT%H:%M:%S", &tm_val);
return std::string(buf);
}
std::string escape_json(const std::string& s) {
std::string result;
result.reserve(s.size() + 16);
for (char c : s) {
switch (c) {
case '"': result += "\\\""; break;
case '\\': result += "\\\\"; break;
case '\n': result += "\\n"; break;
case '\r': result += "\\r"; break;
case '\t': result += "\\t"; break;
default: result += c;
}
}
return result;
}
std::string to_json_number(double v) {
if (std::isnan(v) || std::isinf(v)) return "0";
std::ostringstream ss;
ss << std::fixed << std::setprecision(2) << v;
return ss.str();
}
} // namespace clawguard::util