1 | /* |
---|
2 | * Copyright (C) 2005-2008 MaNGOS <http://www.mangosproject.org/> |
---|
3 | * |
---|
4 | * Copyright (C) 2008 Trinity <http://www.trinitycore.org/> |
---|
5 | * |
---|
6 | * This program is free software; you can redistribute it and/or modify |
---|
7 | * it under the terms of the GNU General Public License as published by |
---|
8 | * the Free Software Foundation; either version 2 of the License, or |
---|
9 | * (at your option) any later version. |
---|
10 | * |
---|
11 | * This program is distributed in the hope that it will be useful, |
---|
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
14 | * GNU General Public License for more details. |
---|
15 | * |
---|
16 | * You should have received a copy of the GNU General Public License |
---|
17 | * along with this program; if not, write to the Free Software |
---|
18 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
---|
19 | */ |
---|
20 | |
---|
21 | #ifndef DBCFILE_H |
---|
22 | #define DBCFILE_H |
---|
23 | #include "Platform/Define.h" |
---|
24 | #include "Utilities/ByteConverter.h" |
---|
25 | #include <cassert> |
---|
26 | |
---|
27 | enum |
---|
28 | { |
---|
29 | FT_NA='x', //not used or unknown, 4 byte size |
---|
30 | FT_NA_BYTE='X', //not used or unknown, byte |
---|
31 | FT_STRING='s', //char* |
---|
32 | FT_FLOAT='f', //float |
---|
33 | FT_INT='i', //uint32 |
---|
34 | FT_BYTE='b', //uint8 |
---|
35 | FT_SORT='d', //sorted by this field, field is not included |
---|
36 | FT_IND='n', //the same,but parsed to data |
---|
37 | FT_LOGIC='l' //Logical (boolean) |
---|
38 | }; |
---|
39 | |
---|
40 | class DBCFile |
---|
41 | { |
---|
42 | public: |
---|
43 | DBCFile(); |
---|
44 | ~DBCFile(); |
---|
45 | |
---|
46 | bool Load(const char *filename, const char *fmt); |
---|
47 | |
---|
48 | class Record |
---|
49 | { |
---|
50 | public: |
---|
51 | float getFloat(size_t field) const |
---|
52 | { |
---|
53 | assert(field < file.fieldCount); |
---|
54 | float val = *reinterpret_cast<float*>(offset+file.GetOffset(field)); |
---|
55 | EndianConvert(val); |
---|
56 | return val; |
---|
57 | } |
---|
58 | uint32 getUInt(size_t field) const |
---|
59 | { |
---|
60 | assert(field < file.fieldCount); |
---|
61 | uint32 val = *reinterpret_cast<uint32*>(offset+file.GetOffset(field)); |
---|
62 | EndianConvert(val); |
---|
63 | return val; |
---|
64 | } |
---|
65 | uint8 getUInt8(size_t field) const |
---|
66 | { |
---|
67 | assert(field < file.fieldCount); |
---|
68 | return *reinterpret_cast<uint8*>(offset+file.GetOffset(field)); |
---|
69 | } |
---|
70 | |
---|
71 | const char *getString(size_t field) const |
---|
72 | { |
---|
73 | assert(field < file.fieldCount); |
---|
74 | size_t stringOffset = getUInt(field); |
---|
75 | assert(stringOffset < file.stringSize); |
---|
76 | return reinterpret_cast<char*>(file.stringTable + stringOffset); |
---|
77 | } |
---|
78 | |
---|
79 | private: |
---|
80 | Record(DBCFile &file_, unsigned char *offset_): offset(offset_), file(file_) {} |
---|
81 | unsigned char *offset; |
---|
82 | DBCFile &file; |
---|
83 | |
---|
84 | friend class DBCFile; |
---|
85 | |
---|
86 | }; |
---|
87 | |
---|
88 | // Get record by id |
---|
89 | Record getRecord(size_t id); |
---|
90 | /// Get begin iterator over records |
---|
91 | |
---|
92 | uint32 GetNumRows() const { return recordCount;} |
---|
93 | uint32 GetCols() const { return fieldCount; } |
---|
94 | uint32 GetOffset(size_t id) const { return (fieldsOffset != NULL && id < fieldCount) ? fieldsOffset[id] : 0; } |
---|
95 | bool IsLoaded() {return (data!=NULL);} |
---|
96 | char* AutoProduceData(const char* fmt, uint32& count, char**& indexTable); |
---|
97 | char* AutoProduceStrings(const char* fmt, char* dataTable); |
---|
98 | static uint32 GetFormatRecordSize(const char * format, int32 * index_pos = NULL); |
---|
99 | private: |
---|
100 | |
---|
101 | uint32 recordSize; |
---|
102 | uint32 recordCount; |
---|
103 | uint32 fieldCount; |
---|
104 | uint32 stringSize; |
---|
105 | uint32 *fieldsOffset; |
---|
106 | unsigned char *data; |
---|
107 | unsigned char *stringTable; |
---|
108 | }; |
---|
109 | #endif |
---|