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