TrinityCore
Loading...
Searching...
No Matches
dbcfile.cpp
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#include "dbcfile.h"
19#include "mpq_libmpq.h"
20
21DBCFile::DBCFile(const std::string& filename):
22 filename(filename), recordSize(0), recordCount(0), fieldCount(0), stringSize(0), data(nullptr), stringTable(nullptr)
23{
24
25}
26
28{
29 MPQFile f(filename.c_str());
30 char header[4];
31 unsigned int na,nb,es,ss;
32
33 if(f.read(header,4)!=4) // Number of records
34 return false;
35
36 if(header[0]!='W' || header[1]!='D' || header[2]!='B' || header[3]!='C')
37 return false;
38
39 if(f.read(&na,4)!=4) // Number of records
40 return false;
41 if(f.read(&nb,4)!=4) // Number of fields
42 return false;
43 if(f.read(&es,4)!=4) // Size of a record
44 return false;
45 if(f.read(&ss,4)!=4) // String size
46 return false;
47
48 recordSize = es;
49 recordCount = na;
50 fieldCount = nb;
51 stringSize = ss;
52 if(fieldCount*4 != recordSize)
53 return false;
54
55 data = new unsigned char[recordSize*recordCount+stringSize];
57
58 size_t data_size = recordSize*recordCount+stringSize;
59 if(f.read(data,data_size)!=data_size)
60 return false;
61 f.close();
62 return true;
63}
65{
66 delete [] data;
67}
68
70{
71 assert(data);
72 return Record(*this, data + id*recordSize);
73}
74
76{
77 assert(data);
78
79 size_t maxId = 0;
80 for(size_t i = 0; i < getRecordCount(); ++i)
81 {
82 if(maxId < getRecord(i).getUInt(0))
83 maxId = getRecord(i).getUInt(0);
84 }
85 return maxId;
86}
87
89{
90 assert(data);
91 return Iterator(*this, data);
92}
94{
95 assert(data);
96 return Iterator(*this, stringTable);
97}
unsigned int getUInt(size_t field) const
Definition dbcfile.h:60
DBCFile(const std::string &filename)
Definition dbcfile.cpp:21
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 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
size_t read(void *dest, size_t bytes)
void close()