[6844] Daily quest fixes.
[getmangos.git] / src / shared / Database / dbcfile.h
blobb2c4bb2d382fb873e2adb6ba3ea4f871e79908d0
1 /*
2 * Copyright (C) 2005-2008 MaNGOS <http://getmangos.com/>
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.
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.
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
19 #ifndef DBCFILE_H
20 #define DBCFILE_H
21 #include "Platform/Define.h"
22 #include "Utilities/ByteConverter.h"
23 #include <cassert>
25 enum
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)
38 class DBCFile
40 public:
41 DBCFile();
42 ~DBCFile();
44 bool Load(const char *filename, const char *fmt);
46 class Record
48 public:
49 float getFloat(size_t field) const
51 assert(field < file.fieldCount);
52 float val = *reinterpret_cast<float*>(offset+file.GetOffset(field));
53 EndianConvert(val);
54 return val;
56 uint32 getUInt(size_t field) const
58 assert(field < file.fieldCount);
59 uint32 val = *reinterpret_cast<uint32*>(offset+file.GetOffset(field));
60 EndianConvert(val);
61 return val;
63 uint8 getUInt8(size_t field) const
65 assert(field < file.fieldCount);
66 return *reinterpret_cast<uint8*>(offset+file.GetOffset(field));
69 const char *getString(size_t field) const
71 assert(field < file.fieldCount);
72 size_t stringOffset = getUInt(field);
73 assert(stringOffset < file.stringSize);
74 return reinterpret_cast<char*>(file.stringTable + stringOffset);
77 private:
78 Record(DBCFile &file_, unsigned char *offset_): offset(offset_), file(file_) {}
79 unsigned char *offset;
80 DBCFile &file;
82 friend class DBCFile;
86 // Get record by id
87 Record getRecord(size_t id);
88 /// Get begin iterator over records
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:
99 uint32 recordSize;
100 uint32 recordCount;
101 uint32 fieldCount;
102 uint32 stringSize;
103 uint32 *fieldsOffset;
104 unsigned char *data;
105 unsigned char *stringTable;
107 #endif