blob: ad7a4bb703b72b3ef9a265960583bbc565dd7fbc (
plain)
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
|
/*
* This file is part of the AzerothCore Project. See AUTHORS file for Copyright information
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU Affero 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 Affero 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/>.
*/
#ifndef DBCFILE_H
#define DBCFILE_H
#include <cassert>
#include <string>
class DBCFile
{
public:
DBCFile(std::string filename);
~DBCFile();
// Open database. It must be openened before it can be used.
bool open();
/// @todo: Add a close function?
// Database exceptions
class Exception
{
public:
Exception(std::string message): message(std::move(message))
{ }
virtual ~Exception() = default;
const std::string& getMessage() {return message;}
private:
std::string message;
};
//
class NotFound: public Exception
{
public:
NotFound(): Exception("Key was not found")
{ }
};
// Iteration over database
class Iterator;
class Record
{
public:
Record& operator= (const Record& r)
{
file = r.file;
offset = r.offset;
return *this;
}
[[nodiscard]] float getFloat(std::size_t field) const
{
assert(field < file.fieldCount);
return *reinterpret_cast<float*>(offset + field * 4);
}
[[nodiscard]] unsigned int getUInt(std::size_t field) const
{
assert(field < file.fieldCount);
return *reinterpret_cast<unsigned int*>(offset + (field * 4));
}
[[nodiscard]] int getInt(std::size_t field) const
{
assert(field < file.fieldCount);
return *reinterpret_cast<int*>(offset + field * 4);
}
[[nodiscard]] unsigned char getByte(std::size_t ofs) const
{
assert(ofs < file.recordSize);
return *reinterpret_cast<unsigned char*>(offset + ofs);
}
[[nodiscard]] const char* getString(std::size_t field) const
{
assert(field < file.fieldCount);
std::size_t stringOffset = getUInt(field);
assert(stringOffset < file.stringSize);
//char * tmp = (char*)file.stringTable + stringOffset;
//unsigned char * tmp2 = file.stringTable + stringOffset;
return reinterpret_cast<char*>(file.stringTable + stringOffset);
}
private:
Record(DBCFile& file, unsigned char* offset): file(file), offset(offset) {}
DBCFile& file;
unsigned char* offset;
friend class DBCFile;
friend class Iterator;
};
/* Iterator that iterates over records */
class Iterator
{
public:
Iterator(DBCFile& file, unsigned char* offset):
record(file, offset) {}
/// Advance (prefix only)
Iterator& operator++()
{
record.offset += record.file.recordSize;
return *this;
}
/// Return address of current instance
Record const& operator*() const { return record; }
const Record* operator->() const
{
return &record;
}
/// Comparison
bool operator==(const Iterator& b) const
{
return record.offset == b.record.offset;
}
bool operator!=(const Iterator& b) const
{
return record.offset != b.record.offset;
}
private:
Record record;
};
// Get record by id
Record getRecord(std::size_t id);
/// Get begin iterator over records
Iterator begin();
/// Get begin iterator over records
Iterator end();
/// Trivial
[[nodiscard]] std::size_t getRecordCount() const { return recordCount;}
[[nodiscard]] std::size_t getFieldCount() const { return fieldCount; }
private:
std::string filename;
std::size_t recordSize;
std::size_t recordCount;
std::size_t fieldCount;
std::size_t stringSize;
unsigned char* data;
unsigned char* stringTable;
};
#endif
|