-
Notifications
You must be signed in to change notification settings - Fork 2
/
BdfLookupTable.cpp
193 lines (146 loc) · 3.06 KB
/
BdfLookupTable.cpp
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
#include "Bdf.hpp"
#include "BdfHelpers.hpp"
#include <iostream>
#include <vector>
#include <string>
#include <string.h>
using namespace Bdf;
using namespace BdfHelpers;
BdfLookupTable::BdfLookupTable(BdfReader* pReader)
{
reader = pReader;
keys_mapped = NULL;
keys_size_mapped = 0;
keys_start = NULL;
keys_endp = &keys_start;
keys_size = 0;
}
BdfLookupTable::BdfLookupTable(BdfReader* pReader, const char* data, int size) : BdfLookupTable(pReader)
{
for(int i=0;i<size;)
{
unsigned char key_size = *(unsigned char*)(&data[i]);
i += 1;
if(i + key_size > size) {
return;
}
Item* key_new = new Item();
key_new->key = std::string(data + i, key_size);
key_new->next = NULL;
*keys_endp = key_new;
keys_endp = &key_new->next;
keys_size += 1;
i += key_size;
}
}
BdfLookupTable::~BdfLookupTable()
{
free(keys_mapped);
Item* cur = keys_start;
Item* next;
while(cur != NULL)
{
next = cur->next;
delete cur;
cur = next;
}
}
void BdfLookupTable::remapKeys()
{
if(keys_size != keys_size_mapped)
{
keys_mapped = (Item**)realloc(keys_mapped, keys_size * sizeof(Item*));
}
keys_size_mapped = keys_size;
Item* cur = keys_start;
int upto = 0;
while(cur != NULL)
{
keys_mapped[upto] = cur;
cur = cur->next;
upto += 1;
}
}
unsigned int BdfLookupTable::getLocation(std::string key)
{
Item* cur = keys_start;
int upto = 0;
while(cur != NULL)
{
if(cur->key == key)
{
return upto;
}
cur = cur->next;
upto += 1;
}
Item* item = new Item();
item->key = key;
item->next = NULL;
*this->keys_endp = item;
this->keys_endp = &item->next;
return keys_size++;
}
std::string BdfLookupTable::getName(unsigned int key)
{
if(keys_size != keys_size_mapped) remapKeys();
if(key < 0 || key >= keys_size) return "";
return keys_mapped[key]->key;
}
bool BdfLookupTable::hasKeyLocation(unsigned int key) {
return key >= 0 && key < keys_size;
}
int BdfLookupTable::serialize(char* data, int* locations, int locations_size)
{
int upto = 0;
for(int i=0;i<locations_size;i++)
{
int loc = locations[i];
if(loc == -1) {
continue;
}
std::string key = keys_mapped[i]->key;
memcpy(data + upto + 1, key.c_str(), key.size());
data[upto] = (char) key.size();
upto += key.size() + 1;
}
return upto;
}
int BdfLookupTable::serializeSeeker(int* locations, int locations_size)
{
int size = 0;
for(int i=0;i<locations_size;i++)
{
// Skip this key if the location is unset (the key has been culled)
int loc = locations[i];
if(loc == -1) {
continue;
}
size += keys_mapped[i]->key.size() + 1;
}
return size;
}
void BdfLookupTable::serializeGetLocations(int* locations)
{
if(keys_size != keys_size_mapped) remapKeys();
int* uses = new int[keys_size];
int next = 0;
// Fill the uses array with zeros
for(unsigned int i=0;i<keys_size;i++) {
uses[i] = 0;
}
reader->getObject()->getLocationUses(uses);
for(unsigned int i=0;i<keys_size;i++)
{
if(uses[i] > 0) {
locations[i] = next;
next += 1;
} else {
locations[i] = -1;
}
}
delete[] uses;
}
int BdfLookupTable::size() {
return keys_size;
}