Repository navigation
Expand file tree
/
Copy pathstruct_parser.h
More file actions
115 lines (101 loc) · 3.47 KB
/
Copy pathstruct_parser.h
File metadata and controls
115 lines (101 loc) · 3.47 KB
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
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <map>
#include <cstring>
#include <stdexcept>
struct Field {
std::string name;
std::string type;
size_t array_size = 1;
size_t offset = 0;
size_t size = 0;
};
struct StructDef {
std::string name;
std::vector<Field> fields;
size_t total_size = 0;
};
class StructRegistry {
public:
void loadFromFile(const std::string& filename) {
std::ifstream file(filename);
if (!file.is_open()) throw std::runtime_error("Cannot open struct file");
std::string line, struct_name;
StructDef current;
while (std::getline(file, line)) {
std::istringstream iss(line);
std::string word;
iss >> word;
if (word == "struct") {
iss >> struct_name;
current = StructDef{};
current.name = struct_name;
} else if (word == "}") {
structs[current.name] = current;
} else if (!word.empty() && word.find("uint") == 0) {
Field f;
f.type = word;
iss >> f.name;
if (f.name.find('[') != std::string::npos) {
auto pos1 = f.name.find('[');
auto pos2 = f.name.find(']');
f.array_size = std::stoul(f.name.substr(pos1 + 1, pos2 - pos1 - 1));
f.name = f.name.substr(0, pos1);
}
f.size = getTypeSize(f.type) * f.array_size;
f.offset = current.total_size;
current.total_size += f.size;
current.fields.push_back(f);
}
}
}
const StructDef& getStruct(const std::string& name) const {
auto it = structs.find(name);
if (it == structs.end()) throw std::runtime_error("Struct not found");
return it->second;
}
private:
std::map<std::string, StructDef> structs;
size_t getTypeSize(const std::string& type) const {
if (type == "uint8_t") return 1;
if (type == "uint16_t") return 2;
if (type == "uint32_t") return 4;
if (type == "uint64_t") return 8;
throw std::runtime_error("Unsupported type: " + type);
}
};
class StructInstance {
public:
StructInstance(const StructDef& def, const std::vector<uint8_t>& data) : def(def), data(data) {
if (data.size() < def.total_size) throw std::runtime_error("Insufficient data");
}
class FieldAccessor {
public:
FieldAccessor(const uint8_t* ptr, size_t elem_size, size_t count) : ptr(ptr), elem_size(elem_size), count(count) {}
template<typename T>
T get(size_t index = 0) const {
if (index >= count) throw std::out_of_range("Array index out of bounds");
T val;
std::memcpy(&val, ptr + index * elem_size, elem_size);
return val;
}
size_t size() const { return count; }
private:
const uint8_t* ptr;
size_t elem_size;
size_t count;
};
FieldAccessor operator[](const std::string& field_name) const {
for (const auto& field : def.fields) {
if (field.name == field_name) {
return FieldAccessor(data.data() + field.offset, field.size / field.array_size, field.array_size);
}
}
throw std::runtime_error("Field not found: " + field_name);
}
private:
const StructDef& def;
const std::vector<uint8_t>& data;
};