forked from VladimirMakeev/D2RSG
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dbf.h
290 lines (239 loc) · 7.37 KB
/
dbf.h
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
/*
* 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/>.
*/
#pragma once
#include <cstdint>
#include <filesystem>
#include <gsl/span>
#include <iterator>
#include <map>
#include <optional>
#include <string_view>
#include <vector>
namespace rsg {
class Dbf
{
public:
enum class CodePage : std::uint8_t
{
DosUsa = 1, /**< Code page 437 */
DosMultilingual = 2, /**< Code page 850 */
WinAnsi = 3, /**< Code page 1252 */
StandardMac = 4,
EEMsDos = 0x64, /**< Code page 852 */
NordicMsDos = 0x65, /**< Code page 865 */
RussianMsDos = 0x66, /**< Code page 866 */
IcelandicMsDos = 0x67,
CzechMsDos = 0x68,
PolishMsDos = 0x69,
GreekMsDos = 0x6a,
TurkishMsDos = 0x6b,
RussianMac = 0x96,
EEMac = 0x97,
GreekMac = 0x98,
WinEE = 0xc8, /**< Code page 1250 */
RussianWin = 0xc9,
TurkishWin = 0xca,
GreekWin = 0xcb
};
struct Column
{
enum class Type : char
{
Character = 'C',
Number = 'N',
Logical = 'L'
};
char name[11];
Type type;
std::uint32_t dataAddress; /**< Offset from the start of the record. */
std::uint8_t length; /**< Length of the data in bytes. */
std::uint8_t decimalCount; /**< Number of digits after decimal point. */
char multiUserReserved[2];
std::uint8_t workAreaId;
char multiUserReserved2[2];
bool setFields;
char reserved[7];
bool indexField;
};
static_assert(sizeof(Column) == 32, "Size of Column structure must be exactly 32 bytes");
class Record
{
public:
using Data = gsl::span<const std::uint8_t>;
Record() = default;
Record(const Dbf* dbf, Data recordData)
: dbf{dbf}
, data{recordData}
{ }
// Characters fields access
bool value(std::string_view& result, std::uint32_t columnIndex) const;
bool value(std::string_view& result, const char* columnName) const;
bool value(std::string_view& result, const Column& column) const;
// Numeric fields access
bool value(int& result, std::uint32_t columnIndex) const;
bool value(int& result, const char* columnName) const;
bool value(int& result, const Column& column) const;
// Logical fields access
bool value(bool& result, std::uint32_t columnIndex) const;
bool value(bool& result, const char* columnName) const;
bool value(bool& result, const Column& column) const;
bool deleted() const
{
return data[0] == '*';
}
const Dbf* database() const
{
return dbf;
}
private:
const Dbf* dbf{};
Data data{};
};
struct iterator
{
using iterator_category = std::input_iterator_tag;
using difference_type = std::ptrdiff_t;
using value_type = Record;
using pointer = value_type*;
using reference = value_type&;
iterator(const Dbf* dbf, std::uint32_t index)
: dbf{dbf}
, index{index}
{ }
value_type operator*() const
{
const auto length{dbf->header.recordLength};
const auto ptr = &dbf->recordsData[index * length];
return value_type{dbf, Record::Data{ptr, length}};
}
// Prefix increment
iterator& operator++()
{
++index;
return *this;
}
// Postfix increment
iterator operator++(int)
{
auto tmp = *this;
++(*this);
return tmp;
}
friend bool operator==(const iterator& a, const iterator& b)
{
return a.dbf == b.dbf && a.index == b.index;
}
friend bool operator!=(const iterator& a, const iterator& b)
{
return !(a == b);
}
private:
friend Dbf;
const Dbf* dbf;
std::uint32_t index;
};
Dbf(const std::filesystem::path& filePath);
operator bool() const
{
return valid;
}
std::uint32_t columnsTotal() const;
std::uint32_t recordsTotal() const;
/** Returns nullptr in case of invalid index. */
const Column* column(std::uint32_t index) const;
/** Returns nullptr if column with specified name can not be found. */
const Column* column(const char* name) const;
/**
* Creates thin wrapper for record data access.
* Created records must not outlive DbfFile object that created them.
* @returns false in case of invalid index.
*/
bool record(Record& result, std::uint32_t index) const;
iterator begin()
{
return iterator{this, 0};
}
iterator cbegin() const
{
return iterator{this, 0};
}
iterator end()
{
return iterator(this, recordsTotal());
}
iterator cend() const
{
return iterator(this, recordsTotal());
}
const std::filesystem::path& path() const
{
return dbfFilePath;
}
private:
struct Header
{
union Version
{
struct
{
std::uint8_t version : 3;
std::uint8_t memoFile : 1;
std::uint8_t sqlTable : 3;
std::uint8_t dbtFlag : 1;
} parts;
std::uint8_t data;
} version;
struct UpdateTime
{
std::uint8_t year; /**< Added to a base year of 1900. */
std::uint8_t month;
std::uint8_t day;
} lastUpdate;
std::uint32_t recordsTotal;
std::uint16_t headerLength;
std::uint16_t recordLength;
char reserved[2];
bool incompleteTransaction;
bool encryption;
char multiUserReserved[12];
bool mdxPresent;
CodePage language;
char reserved2[2];
};
static_assert(sizeof(Header) == 32, "Size of Header structure must be exactly 32 bytes");
struct CompareKeys
{
bool operator()(const char* a, const char* b) const
{
return std::strcmp(a, b) < 0;
}
};
using Columns = std::vector<Column>;
using ColumnIndexMap = std::map<const char*, std::uint32_t, CompareKeys>;
bool readHeader(std::ifstream& stream);
bool readColumns(std::ifstream& stream);
bool readColumn(std::ifstream& stream, Column& column);
Header header{};
Columns columns;
ColumnIndexMap columnIndices;
std::vector<std::uint8_t> recordsData;
std::filesystem::path dbfFilePath;
bool valid{};
};
} // namespace rsg