Skip to content

Commit

Permalink
Better node/value selection.
Browse files Browse the repository at this point in the history
  • Loading branch information
PerryWerneck committed Aug 8, 2023
1 parent 745a25a commit 0344448
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 3 deletions.
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,24 @@ for node in smbios.nodes():
print(' {}: {}'.format(value.description,value))
```

### C++

```C
#include <smbios/node.h>
#include <iostream>

using namespace std;

int main(int argc, char **argv) {
Node node{"chassis"};
cout << node.name() << " - " << node << endl;
cout << node["manufacturer"] << endl;
return 0;
}
```
9 changes: 6 additions & 3 deletions src/include/smbios/node.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,9 @@

};

/// @brief Construct an empty node.
Node();

/// @brief Construct node.
/// @param name Node name (empty for the first one)
/// @param index Node index.
Expand All @@ -105,11 +108,14 @@
/// @param index Node index.
Node(const char *filename, const char *name, int index = 0);

Node & operator=(const Node & src);
Node & operator=(const char *name);

operator bool() const;

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

const char *name() const noexcept;
const char *description() const noexcept;
Expand Down Expand Up @@ -146,9 +152,6 @@

private:

/// @brief Construct an empty node.
Node();

std::shared_ptr<Data> data;
int offset = -1;
size_t index = 0;
Expand Down
69 changes: 69 additions & 0 deletions src/libdmiget/node/node.cc
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,48 @@
return offset >= 0 && header.length >= 4 && header.type != 127;
}

Node & Node::operator=(const Node & src) {

if(src.data) {
data = src.data;
} else {
data = SMBios::Data::factory();
}

offset = src.offset;
index = src.index;
info = src.info;

const uint8_t *ptr = data->get(offset);
header.type = ptr[0];
header.length = ptr[1];
header.handle = WORD(ptr+2);

return *this;
}

Node & Node::operator=(const char *name) {

if(!data) {
data = SMBios::Data::factory();
}

offset = 0;
index = 0;
info = Node::Info::find(0);

const uint8_t *ptr = data->get(offset);
header.type = ptr[0];
header.length = ptr[1];
header.handle = WORD(ptr+2);

if(name && *name) {
next(name);
}

return *this;
}

const char * Node::name() const noexcept {
return info->name;
}
Expand All @@ -101,6 +143,29 @@
return Value{data,(size_t) offset,info->values,index};
}

Value Node::operator[](const char *name) const {

if(offset < 0) {
throw runtime_error("Cant search on empty node");
}

Value value{data,(size_t) offset,info->values,0};

if(!(name && *name)) {
return value;
}

while(value) {
if(!strcasecmp(name,value.name())) {
return value;
}
value.next();
}

throw system_error(ENOENT,system_category(),name);

}

size_t Node::Info::size() const noexcept {
size_t rc = 0;
if(values) {
Expand All @@ -125,6 +190,10 @@
return *this;
}

if(!data) {
data = SMBios::Data::factory();
}

// Get next node.
do {
index++;
Expand Down

0 comments on commit 0344448

Please sign in to comment.