/* * Copyright (C) 2008-2018 TrinityCore * Copyright (C) 2005-2011 MaNGOS * * This program is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License as published by the * Free Software Foundation; either version 2 of the License, or (at your * option) any later version. * * This program is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for * more details. * * You should have received a copy of the GNU General Public License along * with this program. If not, see . */ #define _CRT_SECURE_NO_DEPRECATE #include "dbcfile.h" DBCFile::DBCFile(HANDLE file) : _file(file), _data(nullptr), _stringTable(nullptr) { } bool DBCFile::open() { char header[4]; unsigned int na, nb, es, ss; DWORD readBytes = 0; SFileReadFile(_file, header, 4, &readBytes, nullptr); if (readBytes != 4) // Number of records return false; if (header[0] != 'W' || header[1] != 'D' || header[2] != 'B' || header[3] != 'C') return false; SFileReadFile(_file, &na, 4, &readBytes, nullptr); if (readBytes != 4) // Number of records return false; SFileReadFile(_file, &nb, 4, &readBytes, nullptr); if (readBytes != 4) // Number of fields return false; SFileReadFile(_file, &es, 4, &readBytes, nullptr); if (readBytes != 4) // Size of a record return false; SFileReadFile(_file, &ss, 4, &readBytes, nullptr); if (readBytes != 4) // String size return false; _recordSize = es; _recordCount = na; _fieldCount = nb; _stringSize = ss; if (_fieldCount * 4 != _recordSize) return false; _data = new unsigned char[_recordSize * _recordCount + _stringSize]; _stringTable = _data + _recordSize*_recordCount; size_t data_size = _recordSize * _recordCount + _stringSize; SFileReadFile(_file, _data, data_size, &readBytes, nullptr); if (readBytes != data_size) return false; return true; } DBCFile::~DBCFile() { delete [] _data; } DBCFile::Record DBCFile::getRecord(size_t id) { assert(_data); return Record(*this, _data + id*_recordSize); } size_t DBCFile::getMaxId() { assert(_data); size_t maxId = 0; for(size_t i = 0; i < getRecordCount(); ++i) if (maxId < getRecord(i).getUInt(0)) maxId = getRecord(i).getUInt(0); return maxId; } DBCFile::Iterator DBCFile::begin() { assert(_data); return Iterator(*this, _data); } DBCFile::Iterator DBCFile::end() { assert(_data); return Iterator(*this, _stringTable); }