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 file);
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) {}
85  unsigned char* offset;
86 
87  friend class DBCFile;
88  friend class DBCFile::Iterator;
89 
90  Record& operator=(Record const& right);
91  };
94  class Iterator
95  {
96  public:
97  Iterator(DBCFile &file, unsigned char* offset) : record(file, offset) { }
98 
101  {
103  return *this;
104  }
105 
107  Record const& operator*() const { return record; }
108  Record const* operator->() const { return &record; }
109 
111  bool operator==(Iterator const& b) const
112  {
113  return record.offset == b.record.offset;
114  }
115 
116  bool operator!=(Iterator const& b) const
117  {
118  return record.offset != b.record.offset;
119  }
120 
121  private:
123 
124  Iterator& operator=(Iterator const& right);
125  };
126 
127  // Get record by id
128  Record getRecord(size_t id);
130  Iterator begin();
132  Iterator end();
134  size_t getRecordCount() const { return _recordCount; }
135  size_t getFieldCount() const { return _fieldCount; }
136  size_t getMaxId();
137 
138  private:
140  size_t _recordSize;
141  size_t _recordCount;
142  size_t _fieldCount;
143  size_t _stringSize;
144  unsigned char* _data;
145  unsigned char* _stringTable;
146 };
147 
148 #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:135
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:97
char const * getString(size_t field) const
Definition: dbcfile.h:74
size_t _recordSize
Definition: dbcfile.h:140
Definition: dbcfile.h:45
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
Iterator begin()
Get begin iterator over records.
Definition: dbcfile.cpp:99
bool operator!=(Iterator const &b) const
Definition: dbcfile.h:116
float getFloat(size_t field) const
Definition: dbcfile.h:56
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:134
size_t _fieldCount
Definition: dbcfile.h:142
Record const * operator->() const
Definition: dbcfile.h:108
bool operator==(Iterator const &b) const
Comparison.
Definition: dbcfile.h:111
Definition: dbcfile.h:94
Iterator & operator=(Iterator const &right)
NotFound()
Definition: dbcfile.h:48
Record & operator=(Record const &right)
Record const & operator*() const
Return address of current instance.
Definition: dbcfile.h:107
Iterator & operator++()
Advance (prefix only)
Definition: dbcfile.h:100