This repository has been archived by the owner on Dec 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcpuinfo.cpp
94 lines (89 loc) · 1.68 KB
/
cpuinfo.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
84
85
86
87
88
89
90
91
92
93
94
#include "cpuinfo.h"
//去除字符串中的制表符
std::string cpu_trim(std::string &s)
{
if (!s.empty())
{
// cout<<"flag "<<s.find_last_not_of("\t")<<endl;
s.erase(0,s.find_first_not_of(" "));
s.erase(s.find_last_not_of("\t") + 1);
}
return s;
}
// ---- get cpu info ---- //
std::vector<CPU> getCpuInfo() {
std::vector<CPU> cpus;
std::ifstream file;
file.open("/proc/cpuinfo");
if (!file.good())
printf("failed to open cpuinfo\n");
// read file line by line
std::string temp;
std::string name;
std::string value;
int i=0;
CPU cpu;
while (getline(file,temp))
{
// cpu_trim(temp);
// cout<<temp.length()<<endl;
int pos=temp.find_first_of(":");
if(temp=="")
{
continue;
}
name=temp.substr(0,pos-1);
value=temp.substr(pos+1);
// string s;
name=cpu_trim(name);
value=cpu_trim(value);
// cout<<name<<"end"<<endl;
if(name=="Processor")
{
cpu.Processor=value;
}
else if(name == "BogoMIPS")
{
cpu.BogoMIPS=value;
}
else if(name == "Features")
{
cpu.Features=value;
}
else if(name == "CPU implementer")
{
cpu.implementer=value;
}
else if(name == "CPU architectur")
{
cpu.architecture=value;
}
else if(name == "CPU variant")
{
cpu.variant=value;
}
else if(name == "CPU part")
{
cpu.part=value;
}
else if(name == "CPU revision")
{
cpu.revision=value;
}
else if(name == "Hardware")
{
cpu.Hardware=value;
}
}
cpus.push_back(cpu);
file.close();
return cpus;
}
// int main()
// {
// printf("=== cpu infomation ===\n");
// vector<CPU>cpus;
// cpus=getCpuInfo();
// cpu.tostring();
// return 0;
// }