-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsystem_info_utils.hpp
102 lines (91 loc) · 2.6 KB
/
system_info_utils.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#pragma once
#include "string_utilities/string_utils.hpp"
#include <string>
#include <stdexcept>
#include <sstream>
#if defined(_WIN32) || defined (__WIN32__)
#include <windows.h>
#elif defined (__linux__)
#include <fstream>
#endif
namespace sysinfoutils
{
/**
* @brief Returns the version of the OS.
*
* @return std::string
*/
inline std::string get_os_version()
{
#if defined(_WIN32) || defined (__WIN32__)
std::stringstream ss;
SYSTEM_INFO sysInfo;
// Get system information
GetSystemInfo(&sysInfo);
ss << "Windows " LOBYTE(sysInfo.dwMajorVersion) << "." << HIBYTE(sysInfo.dwMajorVersion);
return ss.str();
#elif defined(__linux__)
std::ifstream file("/proc/version_signature");
std::string line;
if (file.is_open()) {
std::getline(file, line);
file.close();
return line;
}
return "OS not detected";
#endif
}
/**
* @brief Returns the CPU make and model.
*
* @return std::string
*/
inline std::string get_cpu_info()
{
#if defined(_WIN32) || defined (__WIN32__)
std::stringstream ss;
SYSTEM_INFO sysInfo;
// Get system information
GetSystemInfo(&sysInfo);
ss << sysInfo.dwNumberOfProcessors << " cores, " << sysInfo.dwProcessorSpeed / 1000 << " MHz";
return ss.str();
#elif defined(__linux__)
std::ifstream file("/proc/cpuinfo");
std::string line;
if (file.is_open()) {
for (int i = 0; i < 5; ++i)
std::getline(file, line);
file.close();
return strutils::strip_non_printable(line.substr(line.find(":") + 1));
}
return "cpu info not detected";
#endif
}
/**
* @brief Returns the total amount of RAM.
*
* @return std::string
*/
inline std::string get_ram_info()
{
#if defined(_WIN32) || defined (__WIN32__)
std::stringstream ss;
SYSTEM_INFO sysInfo;
// Get system information
GetSystemInfo(&sysInfo);
// Convert RAM size from bytes to Gigabytes
DWORD ramSizeGb = sysInfo.dwTotalPhysicalMem / (1024 * 1024 * 1024);
ss << ramSizeGb << " GB RAM";
return ss.str();
#elif defined(__linux__)
std::ifstream file("/proc/meminfo");
std::string line;
if (file.is_open()) {
std::getline(file, line);
file.close();
return strutils::strip_non_printable(line.substr(line.find(":") + 1));
}
return "RAM info not detected";
#endif
}
}