Project SkyFire Core
SkyFire 5.4.8 server core API documentation
Loading...
Searching...
No Matches
dbcfile.h
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#ifndef DBCFILE_H
7#define DBCFILE_H
8#include <cassert>
9#include <string>
10#include "StormLib.h"
11
12class DBCFile
13{
14 public:
15 DBCFile(HANDLE mpq, const char* filename);
17
18 // Open database. It must be openened before it can be used.
19 bool open();
20
21 // Database exceptions
22 class Exception
23 {
24 public:
25 Exception(const std::string &message) : message(message) { }
26 virtual ~Exception() { }
27 const std::string &getMessage() { return message; }
28 private:
29 std::string message;
30 };
31
32 class NotFound: public Exception
33 {
34 public:
35 NotFound(): Exception("Key was not found") { }
36 };
37
38 // Iteration over database
39 class Iterator;
40 class Record
41 {
42 public:
43 float getFloat(size_t field) const
44 {
45 assert(field < file._fieldCount);
46 return *reinterpret_cast<float*>(offset + field * 4);
47 }
48
49 unsigned int getUInt(size_t field) const
50 {
51 assert(field < file._fieldCount);
52 return *reinterpret_cast<unsigned int*>(offset + field * 4);
53 }
54
55 int getInt(size_t field) const
56 {
57 assert(field < file._fieldCount);
58 return *reinterpret_cast<int*>(offset + field * 4);
59 }
60
61 char const* getString(size_t field) const
62 {
63 assert(field < file._fieldCount);
64 size_t stringOffset = getUInt(field);
65 assert(stringOffset < file._stringSize);
66 return reinterpret_cast<char*>(file._stringTable + stringOffset);
67 }
68
69 private:
70 Record(DBCFile& file, unsigned char* offset): file(file), offset(offset) {}
72 unsigned char* offset;
73
74 friend class DBCFile;
75 friend class DBCFile::Iterator;
76
78 Record(Record const& right) : file(right.file), offset(right.offset)
79 {
80 }
81 };
84 class Iterator
85 {
86 public:
87 Iterator(DBCFile &file, unsigned char* offset) : record(file, offset) { }
88
89 Iterator(Iterator const& right) : record(right.record)
90 {
91 }
92
95 {
96 record.offset += record.file._recordSize;
97 return *this;
98 }
99
101 Record const& operator*() const { return record; }
102 Record const* operator->() const { return &record; }
103
105 bool operator==(Iterator const& b) const
106 {
107 return record.offset == b.record.offset;
108 }
109
110 bool operator!=(Iterator const& b) const
111 {
112 return record.offset != b.record.offset;
113 }
114
116 {
117 record.offset = right.record.offset;
118 return *this;
119 }
120 private:
122 };
123
124 // Get record by id
125 Record getRecord(size_t id);
131 size_t getRecordCount() const { return _recordCount; }
132 size_t getFieldCount() const { return _fieldCount; }
133 size_t getMaxId();
134
135 private:
136 HANDLE _mpq;
137 const char* _filename;
138 HANDLE _file;
139 size_t _recordSize;
140 size_t _recordCount;
141 size_t _fieldCount;
142 size_t _stringSize;
143 unsigned char* _data;
144 unsigned char* _stringTable;
145};
146
147#endif
Exception(const std::string &message)
Definition dbcfile.h:25
std::string message
Definition dbcfile.h:29
virtual ~Exception()
Definition dbcfile.h:26
const std::string & getMessage()
Definition dbcfile.h:27
Record const * operator->() const
Definition dbcfile.h:102
Iterator & operator=(Iterator const &right)
Definition dbcfile.h:115
bool operator!=(Iterator const &b) const
Definition dbcfile.h:110
Iterator(DBCFile &file, unsigned char *offset)
Definition dbcfile.h:87
bool operator==(Iterator const &b) const
Comparison.
Definition dbcfile.h:105
Record const & operator*() const
Return address of current instance.
Definition dbcfile.h:101
Iterator(Iterator const &right)
Definition dbcfile.h:89
Iterator & operator++()
Advance (prefix only).
Definition dbcfile.h:94
Record & operator=(Record const &)
Record(DBCFile &file, unsigned char *offset)
Definition dbcfile.h:70
char const * getString(size_t field) const
Definition dbcfile.h:61
friend class DBCFile
Definition dbcfile.h:74
float getFloat(size_t field) const
Definition dbcfile.h:43
unsigned int getUInt(size_t field) const
Definition dbcfile.h:49
int getInt(size_t field) const
Definition dbcfile.h:55
Record(Record const &right)
Definition dbcfile.h:78
DBCFile & file
Definition dbcfile.h:71
unsigned char * offset
Definition dbcfile.h:72
size_t _stringSize
Definition dbcfile.h:130
HANDLE _mpq
Definition dbcfile.h:136
Iterator begin()
Get begin iterator over records.
unsigned char * _stringTable
Definition dbcfile.h:132
Record getRecord(size_t id)
unsigned char * _data
Definition dbcfile.h:131
size_t getFieldCount() const
Definition dbcfile.h:132
size_t _recordSize
Definition dbcfile.h:127
size_t getMaxId()
size_t _fieldCount
Definition dbcfile.h:129
size_t _recordCount
Definition dbcfile.h:128
bool open()
HANDLE _file
Definition dbcfile.h:126
size_t getRecordCount() const
Trivial.
Definition dbcfile.h:131
const char * _filename
Definition dbcfile.h:137
Iterator end()
Get begin iterator over records.
DBCFile(HANDLE file)
Definition dbcfile.cpp:10