-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.cpp
83 lines (76 loc) · 2.28 KB
/
utils.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
#include "header.h"
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <sstream>
#include <iomanip>
/// @brief fmt bytes to required format
/// @param bytes the bytes needed
/// @return formatted bytes str
std::string fmtbytes(long long bytes)
{
const int KB = 1024;
const int MB = KB * 1024;
const int GB = MB * 1024;
std::ostringstream result;
if (bytes >= GB)
{
result << std::fixed << setprecision(2) << static_cast<double>(bytes) / GB << " GB";
}
else if (bytes >= MB)
{
result << std::fixed << setprecision(2) << static_cast<double>(bytes) / MB << " MB";
}
else if (bytes >= KB)
{
result << std::fixed << setprecision(2) << static_cast<double>(bytes) / KB << " KB";
}
else
{
result << bytes << " bytes";
}
return result.str();
}
/// @brief simplifies `NetInterface` RX data to a presentable format
/// @return Array of `PresentableNetInterface`
std::vector<PresentableNetInterface> SimplifyNetDataRX()
{
std::vector<PresentableNetInterface> presentableinterfaces;
for (const auto &interface : ParseNetInterfaces())
{
PresentableNetInterface pinterface;
pinterface.Name = interface.Name;
pinterface.data.first = interface.recvStats.bytes;
pinterface.data.second = fmtbytes(interface.recvStats.bytes);
presentableinterfaces.push_back(pinterface);
}
return presentableinterfaces;
}
/// @brief simplifies `NetInterface` TX data to a presentable format
/// @return Array of `PresentableNetInterface`
std::vector<PresentableNetInterface> SimplifyNetDataTX()
{
std::vector<PresentableNetInterface> presentableinterfaces;
for (const auto &interface : ParseNetInterfaces())
{
PresentableNetInterface pinterface;
pinterface.Name = interface.Name;
pinterface.data.first = interface.transStats.bytes;
pinterface.data.second = fmtbytes(interface.transStats.bytes);
presentableinterfaces.push_back(pinterface);
}
return presentableinterfaces;
}
/// @brief Trim leading whitespaces from a string
/// @param str The input string
/// @return A new string with leading whitespaces removed
std::string ltrim(const std::string &str)
{
std::size_t start = 0;
while (start < str.size() && std::isspace(static_cast<unsigned char>(str[start])))
{
++start;
}
return str.substr(start);
}