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#undef min
21#undef max
22
23#include <cstdio>
24
25DBCFile::DBCFile(const std::string& filename):
26 filename(filename), recordSize(0), recordCount(0), fieldCount(0), stringSize(0), data(nullptr), stringTable(nullptr)
27{
28
29}
30
31bool DBCFile::open()
32{
33 MPQFile f(filename.c_str());
34
35 // Need some error checking, otherwise an unhandled exception error occurs
36 // if people screw with the data path.
37 if (f.isEof() == true)
38 return false;
39
40 unsigned char header[4];
41 unsigned int na,nb,es,ss;
42
43 f.read(header,4); // File Header
44
45 if (header[0]!='W' || header[1]!='D' || header[2]!='B' || header[3] != 'C')
46 {
47 f.close();
48 data = nullptr;
49 printf("Critical Error: An error occured while trying to read the DBCFile %s.", filename.c_str());
50 return false;
51 }
52
53 //assert(header[0]=='W' && header[1]=='D' && header[2]=='B' && header[3] == 'C');
54
55 f.read(&na,4); // Number of records
56 f.read(&nb,4); // Number of fields
57 f.read(&es,4); // Size of a record
58 f.read(&ss,4); // String size
59
60 recordSize = es;
61 recordCount = na;
62 fieldCount = nb;
63 stringSize = ss;
64 //assert(fieldCount*4 == recordSize);
65 assert(fieldCount*4 >= recordSize);
66
67 data = new unsigned char[recordSize*recordCount+stringSize];
70 f.close();
71 return true;
72}
73
75{
76 delete [] data;
77}
78
80{
81 assert(data);
82 return Record(*this, data + id*recordSize);
83}
84
86{
87 assert(data);
88 return Iterator(*this, data);
89}
90
92{
93 assert(data);
94 return Iterator(*this, stringTable);
95}
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
Iterator begin()
Get begin iterator over records.
Definition dbcfile.cpp:88
bool open()
Definition dbcfile.cpp:27
unsigned char * stringTable
Definition dbcfile.h:133
size_t recordSize
Definition dbcfile.h:128