MaNGOS 0.12 release.
[getmangos.git] / contrib / vmap_extractor_v2 / vmapextract / dbcfile.cpp
blobd8c8d4a663f37c476a1d06abe6d20565d2f1d2e5
1 #include "dbcfile.h"
2 #include "mpq.h"
3 #include "Stormlib.h"
4 #define __STORMLIB_SELF__
6 DBCFile::DBCFile(const std::string &filename) : filename(filename)
8 data = NULL;
11 bool DBCFile::open()
13 MPQFile f(filename.c_str());
15 // Need some error checking, otherwise an unhandled exception error occurs
16 // if people screw with the data path.
17 if (f.isEof() == true)
18 return false;
20 unsigned char header[4];
21 unsigned int na,nb,es,ss;
23 f.read(header,4); // File Header
25 if (header[0]!='W' || header[1]!='D' || header[2]!='B' || header[3] != 'C') {
27 f.close();
28 data = NULL;
29 printf("Critical Error: An error occured while trying to read the DBCFile %s.", filename.c_str());
30 return false;
33 //assert(header[0]=='W' && header[1]=='D' && header[2]=='B' && header[3] == 'C');
35 f.read(&na,4); // Number of records
36 f.read(&nb,4); // Number of fields
37 f.read(&es,4); // Size of a record
38 f.read(&ss,4); // String size
40 recordSize = es;
41 recordCount = na;
42 fieldCount = nb;
43 stringSize = ss;
44 //assert(fieldCount*4 == recordSize);
45 assert(fieldCount*4 >= recordSize);
47 data = new unsigned char[recordSize*recordCount+stringSize];
48 stringTable = data + recordSize*recordCount;
49 f.read(data,recordSize*recordCount+stringSize);
50 f.close();
51 return true;
54 DBCFile::~DBCFile()
56 delete [] data;
59 DBCFile::Record DBCFile::getRecord(size_t id)
61 assert(data);
62 return Record(*this, data + id*recordSize);
65 DBCFile::Iterator DBCFile::begin()
67 assert(data);
68 return Iterator(*this, data);
70 DBCFile::Iterator DBCFile::end()
72 assert(data);
73 return Iterator(*this, stringTable);