forked from VladimirMakeev/D2RSG
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dbf.cpp
318 lines (257 loc) · 7.29 KB
/
dbf.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
/*
* This file is part of the random scenario generator for Disciples 2.
* (https://github.com/VladimirMakeev/D2RSG)
* Copyright (C) 2023 Vladimir Makeev.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "dbf.h"
#include <charconv>
#include <fstream>
namespace rsg {
bool Dbf::Record::value(std::string_view& result, std::uint32_t columnIndex) const
{
if (!dbf) {
return false;
}
const auto column = dbf->column(columnIndex);
if (!column) {
return false;
}
return value(result, *column);
}
bool Dbf::Record::value(std::string_view& result, const char* columnName) const
{
if (!dbf) {
return false;
}
const auto column = dbf->column(columnName);
if (!column) {
return false;
}
return value(result, *column);
}
bool Dbf::Record::value(std::string_view& result, const Column& column) const
{
if (data.empty()) {
return false;
}
if (column.type != Column::Type::Character) {
return false;
}
// + 1 to skip 'deleted' flag
const char* bgn = reinterpret_cast<const char*>(&data[column.dataAddress + 1]);
result = std::string_view(bgn, column.length);
return true;
}
bool Dbf::Record::value(int& result, std::uint32_t columnIndex) const
{
if (!dbf) {
return false;
}
const auto column = dbf->column(columnIndex);
if (!column) {
return false;
}
return value(result, *column);
}
bool Dbf::Record::value(int& result, const char* columnName) const
{
if (!dbf) {
return false;
}
const auto column = dbf->column(columnName);
if (!column) {
return false;
}
return value(result, *column);
}
bool Dbf::Record::value(int& result, const Column& column) const
{
if (data.empty()) {
return false;
}
if (column.type != Column::Type::Number) {
return false;
}
// + 1 to skip 'deleted' flag
const char* first = reinterpret_cast<const char*>(&data[column.dataAddress + 1]);
auto length = column.length;
// skip spaces at the start of the field to std::from_chars work properly
while (*first == ' ' && length) {
first++;
length--;
}
if (!length) {
return false;
}
const char* last = first + length;
int tmpResult{};
auto [p, ec] = std::from_chars(first, last, tmpResult);
if (ec != std::errc()) {
return false;
}
result = tmpResult;
return true;
}
bool Dbf::Record::value(bool& result, std::uint32_t columnIndex) const
{
if (!dbf) {
return false;
}
const auto column = dbf->column(columnIndex);
if (!column) {
return false;
}
return value(result, *column);
}
bool Dbf::Record::value(bool& result, const char* columnName) const
{
if (!dbf) {
return false;
}
const auto column = dbf->column(columnName);
if (!column) {
return false;
}
return value(result, *column);
}
bool Dbf::Record::value(bool& result, const Column& column) const
{
if (data.empty()) {
return false;
}
if (column.type != Column::Type::Logical) {
return false;
}
// + 1 to skip 'deleted' flag
result = data[column.dataAddress + 1] == 'T';
return true;
}
Dbf::Dbf(const std::filesystem::path& filePath)
: dbfFilePath{filePath}
{
std::ifstream stream(filePath, std::ios_base::binary);
if (!stream) {
return;
}
stream.seekg(0, stream.end);
const auto fileSize = stream.tellg();
stream.seekg(0, stream.beg);
if (fileSize < sizeof(Header)) {
return;
}
if (!readHeader(stream)) {
return;
}
const auto recordsDataLength = recordsTotal() * header.recordLength;
const auto recordsEnd = header.headerLength + recordsDataLength;
if (recordsEnd + 1 != fileSize) {
return;
}
if (!readColumns(stream)) {
return;
}
char endOfFields{};
stream.read(&endOfFields, 1);
if (endOfFields != 0xd) {
return;
}
recordsData.resize(recordsDataLength);
// https://en.wikipedia.org/wiki/.dbf#Database_records
// Each record begins with a 1-byte "deletion" flag. The byte's value is a space (0x20), if the
// record is active, or an asterisk (0x2A), if the record is deleted.
char firstChar{};
stream.read(&firstChar, 1);
if (firstChar != ' ' && firstChar != '*') {
// Workaround for different file formats from Sdbf/SergDBF where there is an additional
// EOF/NUL between header and data blocks
stream.read(reinterpret_cast<char*>(&recordsData[0]), recordsDataLength);
} else {
recordsData[0] = firstChar;
stream.read(reinterpret_cast<char*>(&recordsData[1]), recordsDataLength - 1);
}
valid = true;
return;
}
std::uint32_t Dbf::columnsTotal() const
{
return static_cast<std::uint32_t>(columns.size());
}
std::uint32_t Dbf::recordsTotal() const
{
return header.recordsTotal;
}
const Dbf::Column* Dbf::column(std::uint32_t index) const
{
return index < columns.size() ? &columns[index] : nullptr;
}
const Dbf::Column* Dbf::column(const char* name) const
{
const auto it = columnIndices.find(name);
if (it == columnIndices.end()) {
return nullptr;
}
return column(it->second);
}
bool Dbf::record(Record& result, std::uint32_t index) const
{
if (index >= recordsTotal()) {
return false;
}
const auto* bgn = &recordsData[index * header.recordLength];
result = Record(this, Record::Data(bgn, header.recordLength));
return true;
}
bool Dbf::readHeader(std::ifstream& stream)
{
Header tmpHeader;
stream.read(reinterpret_cast<char*>(&tmpHeader), sizeof(tmpHeader));
if (tmpHeader.version.parts.version != 0x3) {
return false;
}
if (tmpHeader.headerLength == 0 || tmpHeader.headerLength < sizeof(Header) + 1) {
return false;
}
if (tmpHeader.recordLength == 0) {
return false;
}
header = tmpHeader;
return true;
}
bool Dbf::readColumns(std::ifstream& stream)
{
const auto columnsTotal = (header.headerLength - sizeof(Header) - 1) / sizeof(Column);
Columns tmpColumns(columnsTotal);
ColumnIndexMap tmpIndices;
std::uint32_t index{0};
std::uint32_t dataAddress{0};
for (auto& column : tmpColumns) {
if (!readColumn(stream, column)) {
return false;
}
column.dataAddress = dataAddress;
dataAddress += column.length;
tmpIndices[column.name] = index++;
}
columns.swap(tmpColumns);
columnIndices.swap(tmpIndices);
return true;
}
bool Dbf::readColumn(std::ifstream& stream, Column& column)
{
stream.read(reinterpret_cast<char*>(&column), sizeof(Column));
return true;
}
} // namespace rsg