Skip to content

Commit 0344448

Browse files
committed
Better node/value selection.
1 parent 745a25a commit 0344448

File tree

3 files changed

+93
-3
lines changed

3 files changed

+93
-3
lines changed

README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,24 @@ for node in smbios.nodes():
4747
print(' {}: {}'.format(value.description,value))
4848
```
4949

50+
### C++
51+
52+
```C
53+
#include <smbios/node.h>
54+
#include <iostream>
55+
56+
using namespace std;
57+
58+
int main(int argc, char **argv) {
59+
Node node{"chassis"};
60+
cout << node.name() << " - " << node << endl;
61+
cout << node["manufacturer"] << endl;
62+
return 0;
63+
}
64+
```
65+
66+
67+
5068
5169
5270

src/include/smbios/node.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,9 @@
9494

9595
};
9696

97+
/// @brief Construct an empty node.
98+
Node();
99+
97100
/// @brief Construct node.
98101
/// @param name Node name (empty for the first one)
99102
/// @param index Node index.
@@ -105,11 +108,14 @@
105108
/// @param index Node index.
106109
Node(const char *filename, const char *name, int index = 0);
107110

111+
Node & operator=(const Node & src);
112+
Node & operator=(const char *name);
108113

109114
operator bool() const;
110115

111116
/// @brief Get value by index.
112117
Value operator[](size_t index) const;
118+
Value operator[](const char *name) const;
113119

114120
const char *name() const noexcept;
115121
const char *description() const noexcept;
@@ -146,9 +152,6 @@
146152

147153
private:
148154

149-
/// @brief Construct an empty node.
150-
Node();
151-
152155
std::shared_ptr<Data> data;
153156
int offset = -1;
154157
size_t index = 0;

src/libdmiget/node/node.cc

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,48 @@
7575
return offset >= 0 && header.length >= 4 && header.type != 127;
7676
}
7777

78+
Node & Node::operator=(const Node & src) {
79+
80+
if(src.data) {
81+
data = src.data;
82+
} else {
83+
data = SMBios::Data::factory();
84+
}
85+
86+
offset = src.offset;
87+
index = src.index;
88+
info = src.info;
89+
90+
const uint8_t *ptr = data->get(offset);
91+
header.type = ptr[0];
92+
header.length = ptr[1];
93+
header.handle = WORD(ptr+2);
94+
95+
return *this;
96+
}
97+
98+
Node & Node::operator=(const char *name) {
99+
100+
if(!data) {
101+
data = SMBios::Data::factory();
102+
}
103+
104+
offset = 0;
105+
index = 0;
106+
info = Node::Info::find(0);
107+
108+
const uint8_t *ptr = data->get(offset);
109+
header.type = ptr[0];
110+
header.length = ptr[1];
111+
header.handle = WORD(ptr+2);
112+
113+
if(name && *name) {
114+
next(name);
115+
}
116+
117+
return *this;
118+
}
119+
78120
const char * Node::name() const noexcept {
79121
return info->name;
80122
}
@@ -101,6 +143,29 @@
101143
return Value{data,(size_t) offset,info->values,index};
102144
}
103145

146+
Value Node::operator[](const char *name) const {
147+
148+
if(offset < 0) {
149+
throw runtime_error("Cant search on empty node");
150+
}
151+
152+
Value value{data,(size_t) offset,info->values,0};
153+
154+
if(!(name && *name)) {
155+
return value;
156+
}
157+
158+
while(value) {
159+
if(!strcasecmp(name,value.name())) {
160+
return value;
161+
}
162+
value.next();
163+
}
164+
165+
throw system_error(ENOENT,system_category(),name);
166+
167+
}
168+
104169
size_t Node::Info::size() const noexcept {
105170
size_t rc = 0;
106171
if(values) {
@@ -125,6 +190,10 @@
125190
return *this;
126191
}
127192

193+
if(!data) {
194+
data = SMBios::Data::factory();
195+
}
196+
128197
// Get next node.
129198
do {
130199
index++;

0 commit comments

Comments
 (0)