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#include <cassert>
21#include <string>
22
24{
25public:
26 DBCFile(const std::string &filename);
27 ~DBCFile();
28
29 // Open database. It must be openened before it can be used.
30 bool open();
31
32 // Database exceptions
34 {
35 public:
36 Exception(const std::string &message): message(message)
37 { }
38 virtual ~Exception()
39 { }
40 const std::string &getMessage() {return message;}
41 private:
42 std::string message;
43 };
44 class NotFound: public Exception
45 {
46 public:
47 NotFound(): Exception("Key was not found")
48 { }
49 };
50 // Iteration over database
51 class Iterator;
52 class Record
53 {
54 public:
55 float getFloat(size_t field) const
56 {
57 assert(field < file.fieldCount);
58 return *reinterpret_cast<float*>(offset + field * 4);
59 }
60 unsigned int getUInt(size_t field) const
61 {
62 assert(field < file.fieldCount);
63 return *reinterpret_cast<unsigned int*>(offset + field * 4);
64 }
65 int getInt(size_t field) const
66 {
67 assert(field < file.fieldCount);
68 return *reinterpret_cast<int*>(offset + field * 4);
69 }
70 char const*getString(size_t field) const
71 {
72 assert(field < file.fieldCount);
73 size_t stringOffset = getUInt(field);
74 assert(stringOffset < file.stringSize);
75 return reinterpret_cast<char*>(file.stringTable + stringOffset);
76 }
77 private:
78 Record(DBCFile& file, unsigned char* offset) : file(file), offset(offset) { }
80 unsigned char* offset;
81
82 friend class DBCFile;
83 friend class DBCFile::Iterator;
84
85 Record& operator=(Record const& right);
86 };
90 {
91 public:
92 Iterator(DBCFile& file, unsigned char* offset) :
93 record(file, offset) {}
97 return *this;
98 }
100 Record const& operator*() const { return record; }
101 Record const* operator->() const {
102 return &record;
103 }
105 bool operator==(Iterator const& b) const
106 {
107 return record.offset == b.record.offset;
108 }
109
110 private:
112
114 };
115
116 // Get record by id
117 Record getRecord(size_t id);
119 Iterator begin();
121 Iterator end();
123 size_t getRecordCount() const { return recordCount;}
124 size_t getFieldCount() const { return fieldCount; }
125 size_t getMaxId();
126private:
127 std::string filename;
132 unsigned char* data;
133 unsigned char* stringTable;
134};
135
136#endif
Exception(const std::string &message)
Definition dbcfile.h:36
std::string message
Definition dbcfile.h:42
virtual ~Exception()
Definition dbcfile.h:38
const std::string & getMessage()
Definition dbcfile.h:40
Record const * operator->() const
Definition dbcfile.h:101
Iterator & operator=(Iterator const &right)
Iterator(DBCFile &file, unsigned char *offset)
Definition dbcfile.h:92
bool operator==(Iterator const &b) const
Comparison.
Definition dbcfile.h:105
Record const & operator*() const
Return address of current instance.
Definition dbcfile.h:100
Iterator & operator++()
Advance (prefix only)
Definition dbcfile.h:95
Record(DBCFile &file, unsigned char *offset)
Definition dbcfile.h:78
char const * getString(size_t field) const
Definition dbcfile.h:70
float getFloat(size_t field) const
Definition dbcfile.h:55
unsigned int getUInt(size_t field) const
Definition dbcfile.h:60
int getInt(size_t field) const
Definition dbcfile.h:65
DBCFile & file
Definition dbcfile.h:79
unsigned char * offset
Definition dbcfile.h:80
Record & operator=(Record const &right)
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
Record getRecord(size_t id)
Definition dbcfile.cpp:69
~DBCFile()
Definition dbcfile.cpp:64
std::string filename
Definition dbcfile.h:127
Iterator end()
Get begin iterator over records.
Definition dbcfile.cpp:93
size_t getFieldCount() const
Definition dbcfile.h:124
size_t getMaxId()
Definition dbcfile.cpp:75
Iterator begin()
Get begin iterator over records.
Definition dbcfile.cpp:88
bool open()
Definition dbcfile.cpp:27
size_t getRecordCount() const
Trivial.
Definition dbcfile.h:123
unsigned char * stringTable
Definition dbcfile.h:133
size_t recordSize
Definition dbcfile.h:128