TrinityCore
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
dbcfile.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2008-2016 TrinityCore <http://www.trinitycore.org/>
3  * Copyright (C) 2005-2011 MaNGOS <http://getmangos.com/>
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License as published by the
7  * Free Software Foundation; either version 2 of the License, or (at your
8  * option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13  * more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 #ifndef DBCFILE_H
20 #define DBCFILE_H
21 #include <cassert>
22 #include <string>
23 #include "CascLib.h"
24 
25 class DBCFile
26 {
27  public:
28  DBCFile(HANDLE mpq, const char* filename);
29  ~DBCFile();
30 
31  // Open database. It must be openened before it can be used.
32  bool open();
33 
34  // Database exceptions
35  class Exception
36  {
37  public:
38  Exception(const std::string &message) : message(message) { }
39  virtual ~Exception() { }
40  const std::string &getMessage() { return message; }
41  private:
42  std::string message;
43  };
44 
45  class NotFound: public Exception
46  {
47  public:
48  NotFound(): Exception("Key was not found") { }
49  };
50 
51  // Iteration over database
52  class Iterator;
53  class Record
54  {
55  public:
56  float getFloat(size_t field) const
57  {
58  assert(field < file._fieldCount);
59  return *reinterpret_cast<float*>(offset + field * 4);
60  }
61 
62  unsigned int getUInt(size_t field) const
63  {
64  assert(field < file._fieldCount);
65  return *reinterpret_cast<unsigned int*>(offset + field * 4);
66  }
67 
68  int getInt(size_t field) const
69  {
70  assert(field < file._fieldCount);
71  return *reinterpret_cast<int*>(offset + field * 4);
72  }
73 
74  char const* getString(size_t field) const
75  {
76  assert(field < file._fieldCount);
77  size_t stringOffset = getUInt(field);
78  assert(stringOffset < file._stringSize);
79  return reinterpret_cast<char*>(file._stringTable + stringOffset);
80  }
81 
82  private:
83  Record(DBCFile& file, unsigned char* offset): file(file), offset(offset) {}
84  DBCFile& file;
85  unsigned char* offset;
86 
87  friend class DBCFile;
88  friend class DBCFile::Iterator;
89 
90  Record& operator=(Record const&);
91  Record(Record const& right) : file(right.file), offset(right.offset)
92  {
93  }
94  };
97  class Iterator
98  {
99  public:
100  Iterator(DBCFile &file, unsigned char* offset) : record(file, offset) { }
101 
102  Iterator(Iterator const& right) : record(right.record)
103  {
104  }
105 
108  {
110  return *this;
111  }
112 
114  Record const& operator*() const { return record; }
115  Record const* operator->() const { return &record; }
116 
118  bool operator==(Iterator const& b) const
119  {
120  return record.offset == b.record.offset;
121  }
122 
123  bool operator!=(Iterator const& b) const
124  {
125  return record.offset != b.record.offset;
126  }
127 
128  Iterator& operator=(Iterator const& right)
129  {
130  record.offset = right.record.offset;
131  return *this;
132  }
133  private:
134  Record record;
135 
136  };
137 
138  // Get record by id
139  Record getRecord(size_t id);
141  Iterator begin();
143  Iterator end();
145  size_t getRecordCount() const { return _recordCount; }
146  size_t getFieldCount() const { return _fieldCount; }
147  size_t getMaxId();
148 
149  private:
151  const char* _filename;
152  HANDLE _file;
153  size_t _recordSize;
154  size_t _recordCount;
155  size_t _fieldCount;
156  size_t _stringSize;
157  unsigned char *_data;
158  unsigned char* _stringTable;
159 };
160 
161 #endif
const std::string & getMessage()
Definition: dbcfile.h:40
Record getRecord(size_t id)
Definition: dbcfile.cpp:81
void * HANDLE
Definition: CascPort.h:146
Definition: dbcfile.h:25
Definition: dbcfile.h:35
size_t getFieldCount() const
Definition: dbcfile.h:146
HANDLE _file
Definition: dbcfile.h:139
~DBCFile()
Definition: dbcfile.cpp:76
Iterator end()
Get begin iterator over records.
Definition: dbcfile.cpp:105
Record record
Definition: dbcfile.h:122
bool open()
Definition: dbcfile.cpp:29
unsigned char * _data
Definition: dbcfile.h:144
Iterator(DBCFile &file, unsigned char *offset)
Definition: dbcfile.h:100
Record(Record const &right)
Definition: dbcfile.h:91
char const * getString(size_t field) const
Definition: dbcfile.h:74
size_t _recordSize
Definition: dbcfile.h:140
Exception(const std::string &message)
Definition: dbcfile.h:38
Definition: dbcfile.h:53
Record(DBCFile &file, unsigned char *offset)
Definition: dbcfile.h:83
size_t getMaxId()
Definition: dbcfile.cpp:87
std::string message
Definition: dbcfile.h:42
unsigned char * _stringTable
Definition: dbcfile.h:145
unsigned char * offset
Definition: dbcfile.h:85
size_t _recordCount
Definition: dbcfile.h:141
DBCFile & file
Definition: dbcfile.h:84
virtual ~Exception()
Definition: dbcfile.h:39
int getInt(size_t field) const
Definition: dbcfile.h:68
HANDLE _mpq
Definition: dbcfile.h:150
Iterator begin()
Get begin iterator over records.
Definition: dbcfile.cpp:99
bool operator!=(Iterator const &b) const
Definition: dbcfile.h:123
float getFloat(size_t field) const
Definition: dbcfile.h:56
const char * _filename
Definition: dbcfile.h:151
size_t _stringSize
Definition: dbcfile.h:143
unsigned int getUInt(size_t field) const
Definition: dbcfile.h:62
DBCFile(HANDLE file)
Definition: dbcfile.cpp:23
size_t getRecordCount() const
Trivial.
Definition: dbcfile.h:145
size_t _fieldCount
Definition: dbcfile.h:142
Iterator(Iterator const &right)
Definition: dbcfile.h:102
Record const * operator->() const
Definition: dbcfile.h:115
bool operator==(Iterator const &b) const
Comparison.
Definition: dbcfile.h:118
Definition: dbcfile.h:94
Iterator & operator=(Iterator const &right)
Definition: dbcfile.h:128
NotFound()
Definition: dbcfile.h:48
Record & operator=(Record const &right)
Record const & operator*() const
Return address of current instance.
Definition: dbcfile.h:114
Iterator & operator++()
Advance (prefix only)
Definition: dbcfile.h:107