TrinityCore
Loading...
Searching...
No Matches
dbcfile.h
Go to the documentation of this file.
1/*
2 * This file is part of the TrinityCore Project. See AUTHORS file for Copyright information
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the
6 * Free Software Foundation; either version 2 of the License, or (at your
7 * option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#ifndef DBCFILE_H
19#define DBCFILE_H
20
21#include <cassert>
22#include <string>
23
24class DBCFile
25{
26public:
27 DBCFile(const std::string &filename);
29
30 // Open database. It must be openened before it can be used.
31 bool open();
32
33 // TODO: Add a close function?
34
35 // Database exceptions
36 class Exception
37 {
38 public:
39 Exception(const std::string &message): message(message)
40 { }
41 virtual ~Exception()
42 { }
43 const std::string &getMessage() {return message;}
44 private:
45 std::string message;
46 };
47
48 //
49 class NotFound: public Exception
50 {
51 public:
52 NotFound(): Exception("Key was not found")
53 { }
54 };
55
56 // Iteration over database
57 class Iterator;
58 class Record
59 {
60 public:
62 {
63 file = r.file;
64 offset = r.offset;
65 return *this;
66 }
67 float getFloat(size_t field) const
68 {
69 assert(field < file.fieldCount);
70 return *reinterpret_cast<float*>(offset + field * 4);
71 }
72 unsigned int getUInt(size_t field) const
73 {
74 assert(field < file.fieldCount);
75 return *reinterpret_cast<unsigned int*>(offset + (field * 4));
76 }
77 int getInt(size_t field) const
78 {
79 assert(field < file.fieldCount);
80 return *reinterpret_cast<int*>(offset + field * 4);
81 }
82 unsigned char getByte(size_t ofs) const
83 {
84 assert(ofs < file.recordSize);
85 return *reinterpret_cast<unsigned char*>(offset + ofs);
86 }
87 char const *getString(size_t field) const
88 {
89 assert(field < file.fieldCount);
90 size_t stringOffset = getUInt(field);
91 assert(stringOffset < file.stringSize);
92 //char * tmp = (char*)file.stringTable + stringOffset;
93 //unsigned char * tmp2 = file.stringTable + stringOffset;
94 return reinterpret_cast<char*>(file.stringTable + stringOffset);
95 }
96 private:
97 Record(DBCFile& file, unsigned char* offset) : file(file), offset(offset) { }
99 unsigned char* offset;
100
101 friend class DBCFile;
102 friend class Iterator;
103 };
104
105 /* Iterator that iterates over records */
106 class Iterator
107 {
108 public:
109 Iterator(DBCFile &file, unsigned char *offset):
110 record(file, offset) {}
114 return *this;
115 }
117 Record const& operator*() const { return record; }
118 Record const* operator->() const {
119 return &record;
120 }
122 bool operator==(Iterator const& b) const
123 {
124 return record.offset == b.record.offset;
125 }
126
127 private:
129 };
130
131 // Get record by id
132 Record getRecord(size_t id);
138 size_t getRecordCount() const { return recordCount;}
139 size_t getFieldCount() const { return fieldCount; }
140
141private:
142 std::string filename;
143 size_t recordSize;
144 size_t recordCount;
145 size_t fieldCount;
146 size_t stringSize;
147 unsigned char *data;
148 unsigned char *stringTable;
149};
150
151#endif
Exception(const std::string &message)
Definition dbcfile.h:39
std::string message
Definition dbcfile.h:42
virtual ~Exception()
Definition dbcfile.h:41
const std::string & getMessage()
Definition dbcfile.h:43
Record const * operator->() const
Definition dbcfile.h:118
Iterator(DBCFile &file, unsigned char *offset)
Definition dbcfile.h:109
bool operator==(Iterator const &b) const
Comparison.
Definition dbcfile.h:122
Record const & operator*() const
Return address of current instance.
Definition dbcfile.h:117
Iterator & operator++()
Advance (prefix only)
Definition dbcfile.h:112
unsigned char getByte(size_t ofs) const
Definition dbcfile.h:82
Record(DBCFile &file, unsigned char *offset)
Definition dbcfile.h:97
char const * getString(size_t field) const
Definition dbcfile.h:87
float getFloat(size_t field) const
Definition dbcfile.h:67
unsigned int getUInt(size_t field) const
Definition dbcfile.h:72
int getInt(size_t field) const
Definition dbcfile.h:77
DBCFile & file
Definition dbcfile.h:79
unsigned char * offset
Definition dbcfile.h:80
Record & operator=(Record const &right)
DBCFile(const std::string &filename)
size_t fieldCount
Definition dbcfile.h:130
unsigned char * data
Definition dbcfile.h:132
size_t stringSize
Definition dbcfile.h:131
size_t recordCount
Definition dbcfile.h:129
Iterator begin()
Get begin iterator over records.
std::string filename
Definition dbcfile.h:127
Record getRecord(size_t id)
size_t getFieldCount() const
Definition dbcfile.h:139
bool open()
size_t getRecordCount() const
Trivial.
Definition dbcfile.h:138
Iterator end()
Get begin iterator over records.
unsigned char * stringTable
Definition dbcfile.h:133
size_t recordSize
Definition dbcfile.h:128