Project SkyFire Core
SkyFire 5.4.8 server core API documentation
Loading...
Searching...
No Matches
dbcfile.cpp
Go to the documentation of this file.
1/*
2* This file is part of Project SkyFire https://www.projectskyfire.org.
3* See LICENSE.md file for Copyright information
4*/
5
6#define _CRT_SECURE_NO_DEPRECATE
7
8#include "dbcfile.h"
9
10DBCFile::DBCFile(HANDLE file) :
11 _file(file), _recordSize(0), _recordCount(0), _fieldCount(0),
12 _stringSize(0), _data(NULL), _stringTable(NULL)
13{
14}
15
17{
18 char header[4];
19 unsigned int na, nb, es, ss;
20
21 DWORD readBytes = 0;
22 SFileReadFile(_file, header, 4, &readBytes, NULL);
23 if (readBytes != 4) // Number of records
24 return false;
25
26 if (header[0] != 'W' || header[1] != 'D' || header[2] != 'B' || header[3] != 'C')
27 return false;
28
29 SFileReadFile(_file, &na, 4, &readBytes, NULL);
30 if (readBytes != 4) // Number of records
31 return false;
32
33 SFileReadFile(_file, &nb, 4, &readBytes, NULL);
34 if (readBytes != 4) // Number of fields
35 return false;
36
37 SFileReadFile(_file, &es, 4, &readBytes, NULL);
38 if (readBytes != 4) // Size of a record
39 return false;
40
41 SFileReadFile(_file, &ss, 4, &readBytes, NULL);
42 if (readBytes != 4) // String size
43 return false;
44
45 _recordSize = es;
46 _recordCount = na;
47 _fieldCount = nb;
48 _stringSize = ss;
49 if (_fieldCount * 4 != _recordSize)
50 return false;
51
52 _data = new unsigned char[_recordSize * _recordCount + _stringSize];
54
55 size_t data_size = _recordSize * _recordCount + _stringSize;
56 SFileReadFile(_file, _data, data_size, &readBytes, NULL);
57 if (readBytes != data_size)
58 return false;
59
60 return true;
61}
62
64{
65 delete [] _data;
66}
67
69{
70 assert(_data);
71 return Record(*this, _data + id*_recordSize);
72}
73
75{
76 assert(_data);
77
78 size_t maxId = 0;
79 for(size_t i = 0; i < getRecordCount(); ++i)
80 if (maxId < getRecord(i).getUInt(0))
81 maxId = getRecord(i).getUInt(0);
82
83 return maxId;
84}
85
87{
88 assert(_data);
89 return Iterator(*this, _data);
90}
91
93{
94 assert(_data);
95 return Iterator(*this, _stringTable);
96}
97
unsigned int getUInt(size_t field) const
Definition dbcfile.h:49
size_t _stringSize
Definition dbcfile.h:130
Record getRecord(size_t id)
Definition dbcfile.cpp:68
~DBCFile()
Definition dbcfile.cpp:63
unsigned char * _stringTable
Definition dbcfile.h:132
unsigned char * _data
Definition dbcfile.h:131
Iterator end()
Get begin iterator over records.
Definition dbcfile.cpp:92
size_t _recordSize
Definition dbcfile.h:127
size_t getMaxId()
Definition dbcfile.cpp:74
Iterator begin()
Get begin iterator over records.
Definition dbcfile.cpp:86
size_t _fieldCount
Definition dbcfile.h:129
size_t _recordCount
Definition dbcfile.h:128
virtual bool open()
Definition dbcfile.cpp:16
HANDLE _file
Definition dbcfile.h:126
size_t getRecordCount() const
Trivial.
Definition dbcfile.h:121
DBCFile(HANDLE file)
Definition dbcfile.cpp:10