Files
TCPlayerbotCore/src/tools/vmap3_extractor/dbcfile.cpp
T

78 lines
1.7 KiB
C++
Raw Normal View History

2008-10-02 16:23:55 -05:00
#include "dbcfile.h"
2010-06-05 00:59:25 +02:00
#include "mpq_libmpq04.h"
#undef min
#undef max
#include <cstdio>
2008-10-02 16:23:55 -05:00
DBCFile::DBCFile(const std::string &filename) : filename(filename)
{
2009-02-09 08:16:34 -05:00
data = NULL;
2008-10-02 16:23:55 -05:00
}
bool DBCFile::open()
{
2009-02-09 08:16:34 -05:00
MPQFile f(filename.c_str());
2008-10-02 16:23:55 -05:00
2009-02-09 08:16:34 -05:00
// Need some error checking, otherwise an unhandled exception error occurs
// if people screw with the data path.
if (f.isEof() == true)
return false;
2008-10-02 16:23:55 -05:00
2009-02-09 08:16:34 -05:00
unsigned char header[4];
unsigned int na,nb,es,ss;
2008-10-02 16:23:55 -05:00
2009-02-09 08:16:34 -05:00
f.read(header,4); // File Header
2008-10-02 16:23:55 -05:00
if (header[0]!='W' || header[1]!='D' || header[2]!='B' || header[3] != 'C')
{
2009-02-09 08:16:34 -05:00
f.close();
data = NULL;
printf("Critical Error: An error occured while trying to read the DBCFile %s.", filename.c_str());
return false;
}
2008-10-02 16:23:55 -05:00
2009-02-09 08:16:34 -05:00
//assert(header[0]=='W' && header[1]=='D' && header[2]=='B' && header[3] == 'C');
2008-10-02 16:23:55 -05:00
2009-02-09 08:16:34 -05:00
f.read(&na,4); // Number of records
f.read(&nb,4); // Number of fields
f.read(&es,4); // Size of a record
f.read(&ss,4); // String size
2008-10-02 16:23:55 -05:00
2009-02-09 08:16:34 -05:00
recordSize = es;
recordCount = na;
fieldCount = nb;
stringSize = ss;
//assert(fieldCount*4 == recordSize);
assert(fieldCount*4 >= recordSize);
data = new unsigned char[recordSize*recordCount+stringSize];
stringTable = data + recordSize*recordCount;
f.read(data,recordSize*recordCount+stringSize);
f.close();
return true;
2008-10-02 16:23:55 -05:00
}
DBCFile::~DBCFile()
{
2009-02-09 08:16:34 -05:00
delete [] data;
2008-10-02 16:23:55 -05:00
}
DBCFile::Record DBCFile::getRecord(size_t id)
{
2009-02-09 08:16:34 -05:00
assert(data);
return Record(*this, data + id*recordSize);
2008-10-02 16:23:55 -05:00
}
DBCFile::Iterator DBCFile::begin()
{
2009-02-09 08:16:34 -05:00
assert(data);
return Iterator(*this, data);
2008-10-02 16:23:55 -05:00
}
2008-10-02 16:23:55 -05:00
DBCFile::Iterator DBCFile::end()
{
2009-02-09 08:16:34 -05:00
assert(data);
return Iterator(*this, stringTable);
2008-10-02 16:23:55 -05:00
}