blob: cf1ec77546cd7fd535fa719cbb14d338c0432717 [file] [log] [blame]
Deepak Kodihallicb7f2d42019-06-19 13:25:31 +05301#include "bios_table.hpp"
2
3#include <fstream>
4
John Wangf719f3b2020-01-17 08:46:22 +08005#include "bios_table.h"
6
Deepak Kodihallicb7f2d42019-06-19 13:25:31 +05307namespace pldm
8{
9
10namespace responder
11{
12
13namespace bios
14{
15
16BIOSTable::BIOSTable(const char* filePath) : filePath(filePath)
17{
18}
19
20bool BIOSTable::isEmpty() const noexcept
21{
22 bool empty = false;
23 try
24 {
25 empty = fs::is_empty(filePath);
26 }
27 catch (fs::filesystem_error& e)
28 {
29 return true;
30 }
31 return empty;
32}
33
34void BIOSTable::store(const Table& table)
35{
36 std::ofstream stream(filePath.string(), std::ios::out | std::ios::binary);
37 stream.write(reinterpret_cast<const char*>(table.data()), table.size());
38}
39
40void BIOSTable::load(Response& response) const
41{
42 auto currSize = response.size();
43 auto fileSize = fs::file_size(filePath);
44 response.resize(currSize + fileSize);
45 std::ifstream stream(filePath.string(), std::ios::in | std::ios::binary);
46 stream.read(reinterpret_cast<char*>(response.data() + currSize), fileSize);
47}
48
John Wangf719f3b2020-01-17 08:46:22 +080049BIOSStringTable::BIOSStringTable(const char* filePath) : BIOSTable(filePath)
50{
51 if (!isEmpty())
52 {
53 load(stringTable);
54 }
55}
56
57std::string BIOSStringTable::findString(uint16_t handle) const
58{
59 auto stringEntry = pldm_bios_table_string_find_by_handle(
60 stringTable.data(), stringTable.size(), handle);
61 if (stringEntry == nullptr)
62 {
63 throw std::invalid_argument("Invalid String Handle");
64 }
65 auto strLength =
66 pldm_bios_table_string_entry_decode_string_length(stringEntry);
67 std::vector<char> buffer(strLength + 1 /* sizeof '\0' */);
68 pldm_bios_table_string_entry_decode_string(stringEntry, buffer.data(),
69 buffer.size());
70
71 return std::string(buffer.data(), buffer.data() + strLength);
72}
73
Deepak Kodihallicb7f2d42019-06-19 13:25:31 +053074} // namespace bios
75} // namespace responder
76} // namespace pldm