-
Notifications
You must be signed in to change notification settings - Fork 0
/
dmidecode_parser.h
230 lines (204 loc) · 7.61 KB
/
dmidecode_parser.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
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
#ifndef DMIDECODE_PARSER_H
#define DMIDECODE_PARSER_H
#include <string>
#include <vector>
#include <map>
#include <sstream>
#include <iostream>
#include <algorithm>
namespace DMIDecodeParser {
// Represents a single key-value property or a key with a list of values
struct DMIProperty {
std::string key;
std::string value; // Single value or empty
std::vector<std::string> list; // Multi-line values or lists
// Convert property to plain text
std::string toPlainText() const {
std::ostringstream os;
os << " " << key << ": " << value << "\n";
for (const auto& listItem : list) {
os << " - " << listItem << "\n";
}
return os.str();
}
};
// Represents a DMI type with a type ID, name, and list of properties
struct DMIType {
int typeId;
std::string typeName;
std::vector<DMIProperty> properties;
// Convert the entire DMI type to plain text
std::string toPlainText() const {
std::ostringstream os;
os << "Type " << typeId << " - " << typeName << ":\n";
for (const auto& property : properties) {
os << property.toPlainText();
}
os << "\n"; // Add a blank line between types
return os.str();
}
};
class Parser {
private:
std::map<int, std::vector<DMIType>> parsedData; // Store DMI types grouped by type ID
// Helper function to trim whitespace from a string
static std::string trim(const std::string& str) {
size_t first = str.find_first_not_of(" \t");
if (first == std::string::npos) return "";
size_t last = str.find_last_not_of(" \t");
return str.substr(first, last - first + 1);
}
// Helper function to split a string by a delimiter
static std::vector<std::string> splitByDelimiter(const std::string& str, char delimiter) {
std::vector<std::string> tokens;
std::istringstream stream(str);
std::string token;
while (std::getline(stream, token, delimiter)) {
tokens.push_back(trim(token));
}
return tokens;
}
// Detect if a line represents a list item
static bool isListItem(const std::string& line) {
return line.find_first_not_of(" \t") == 8; // List items are indented more deeply
}
// Parse a single DMI section
DMIType parseSection(const std::vector<std::string>& lines) const {
DMIType dmiType;
if (lines.empty()) return dmiType;
// Parse the handle line
const auto& handleLine = lines[0];
if (handleLine.find("Handle") != 0) return dmiType; // Skip invalid sections
auto tokens = splitByDelimiter(handleLine, ',');
if (tokens.size() >= 2) {
dmiType.typeId = std::stoi(tokens[1].substr(9)); // Extract "DMI type"
}
// Parse the title line
if (lines.size() > 1 && !lines[1].empty()) {
dmiType.typeName = trim(lines[1]);
} else {
dmiType.typeName = "Unknown Type";
}
// Parse the properties
DMIProperty currentProperty;
for (size_t i = 2; i < lines.size(); ++i) {
const auto& line = trim(lines[i]);
if (line.empty()) continue;
if (line.find(':') != std::string::npos) {
// Add the previous property
if (!currentProperty.key.empty()) {
dmiType.properties.push_back(currentProperty);
}
currentProperty = {};
auto keyValue = splitByDelimiter(line, ':');
currentProperty.key = keyValue[0];
if (keyValue.size() > 1) {
currentProperty.value = keyValue[1];
}
} else if (isListItem(line)) { // List items
currentProperty.list.push_back(line);
} else if (!line.empty()) { // Continuation lines
currentProperty.value += " " + line;
}
}
// Add the last property
if (!currentProperty.key.empty()) {
dmiType.properties.push_back(currentProperty);
}
return dmiType;
}
// Parse the entire dmidecode output
void parseOutput(const std::string& output) {
std::istringstream stream(output);
std::string line;
std::vector<std::string> section;
while (std::getline(stream, line)) {
if (line.find("Handle") == 0) {
if (!section.empty()) {
auto dmiType = parseSection(section);
parsedData[dmiType.typeId].push_back(dmiType);
section.clear();
}
}
section.push_back(line);
}
if (!section.empty()) {
auto dmiType = parseSection(section);
parsedData[dmiType.typeId].push_back(dmiType);
}
}
public:
// Constructor to initialize the parser with dmidecode output
Parser(const std::string& output) {
parseOutput(output);
}
// Export all data to plain text
std::string exportAllToPlainText() const {
std::ostringstream os;
for (const auto& [typeId, types] : parsedData) {
for (const auto& type : types) {
os << type.toPlainText();
}
}
return os.str();
}
// Export a specific type to plain text
std::string exportTypeToPlainText(int typeId) const {
if (parsedData.find(typeId) == parsedData.end()) {
return "Type " + std::to_string(typeId) + " not found.\n";
}
std::ostringstream os;
for (const auto& type : parsedData.at(typeId)) {
os << type.toPlainText();
}
return os.str();
}
// Export a specific type to Markdown
std::string exportByTypeToMarkdown(int typeId) const {
if (parsedData.find(typeId) == parsedData.end()) {
return "Type " + std::to_string(typeId) + " not found.\n";
}
std::ostringstream os;
for (const auto& type : parsedData.at(typeId)) {
os << exportToMarkdown(type);
}
return os.str();
}
// Helper function to export a single DMIType to Markdown
std::string exportToMarkdown(const DMIType& type) const {
std::ostringstream os;
os << "### Type " << type.typeId << " - " << type.typeName << "\n\n";
os << "| Key | Value |\n";
os << "|-----------------------------------|--------------------------------------|\n";
for (const auto& property : type.properties) {
if (!property.list.empty()) {
os << "| " << property.key << " | ";
for (size_t i = 0; i < property.list.size(); ++i) {
if (i > 0) os << " "; // Align bullets
os << "- " << property.list[i] << "\n";
}
os << " |\n";
} else {
os << "| " << property.key << " | " << property.value << " |\n";
}
}
os << "\n"; // Add a blank line between types
return os.str();
}
// Retrieve a specific key's value
std::string getValueByKey(int typeId, const std::string& key) const {
if (parsedData.find(typeId) == parsedData.end()) {
return "";
}
for (const auto& type : parsedData.at(typeId)) {
for (const auto& property : type.properties) {
if (property.key == key) {
return property.value;
}
}
}
return "";
}
};
} // namespace DMIDecodeParser
#endif // DMIDECODE_PARSER_H