[8587] MaNGOS 0.14 release.
[getmangos.git] / contrib / extractor / dbcfile.h
blob4af2606aff91b5566407edf6061d07681fe4e4f3
1 #ifndef DBCFILE_H
2 #define DBCFILE_H
3 #include <cassert>
4 #include <string>
6 class DBCFile
8 public:
9 DBCFile(const std::string &filename);
10 ~DBCFile();
12 // Open database. It must be openened before it can be used.
13 bool open();
15 // Database exceptions
16 class Exception
18 public:
19 Exception(const std::string &message): message(message)
20 { }
21 virtual ~Exception()
22 { }
23 const std::string &getMessage() {return message;}
24 private:
25 std::string message;
27 class NotFound: public Exception
29 public:
30 NotFound(): Exception("Key was not found")
31 { }
33 // Iteration over database
34 class Iterator;
35 class Record
37 public:
38 float getFloat(size_t field) const
40 assert(field < file.fieldCount);
41 return *reinterpret_cast<float*>(offset+field*4);
43 unsigned int getUInt(size_t field) const
45 assert(field < file.fieldCount);
46 return *reinterpret_cast<unsigned int*>(offset+field*4);
48 int getInt(size_t field) const
50 assert(field < file.fieldCount);
51 return *reinterpret_cast<int*>(offset+field*4);
53 const char *getString(size_t field) const
55 assert(field < file.fieldCount);
56 size_t stringOffset = getUInt(field);
57 assert(stringOffset < file.stringSize);
58 return reinterpret_cast<char*>(file.stringTable + stringOffset);
60 private:
61 Record(DBCFile &file, unsigned char *offset): file(file), offset(offset) {}
62 unsigned char *offset;
63 DBCFile &file;
65 friend class DBCFile;
66 friend class DBCFile::Iterator;
68 /** Iterator that iterates over records
70 class Iterator
72 public:
73 Iterator(DBCFile &file, unsigned char *offset):
74 record(file, offset) {}
75 /// Advance (prefix only)
76 Iterator & operator++() {
77 record.offset += record.file.recordSize;
78 return *this;
80 /// Return address of current instance
81 Record const & operator*() const { return record; }
82 const Record* operator->() const {
83 return &record;
85 /// Comparison
86 bool operator==(const Iterator &b) const
88 return record.offset == b.record.offset;
90 bool operator!=(const Iterator &b) const
92 return record.offset != b.record.offset;
94 private:
95 Record record;
98 // Get record by id
99 Record getRecord(size_t id);
100 /// Get begin iterator over records
101 Iterator begin();
102 /// Get begin iterator over records
103 Iterator end();
104 /// Trivial
105 size_t getRecordCount() const { return recordCount;}
106 size_t getFieldCount() const { return fieldCount; }
107 size_t getMaxId();
108 private:
109 std::string filename;
110 size_t recordSize;
111 size_t recordCount;
112 size_t fieldCount;
113 size_t stringSize;
114 unsigned char *data;
115 unsigned char *stringTable;
118 #endif