TrinityCore
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
DBCFileLoader Class Reference

#include <DBCFileLoader.h>

Classes

class  Record
 

Public Member Functions

 DBCFileLoader ()
 
 ~DBCFileLoader ()
 
bool Load (const char *filename, const char *fmt)
 
Record getRecord (size_t id)
 
uint32 GetNumRows () const
 Get begin iterator over records. More...
 
uint32 GetRowSize () const
 
uint32 GetCols () const
 
uint32 GetOffset (size_t id) const
 
bool IsLoaded () const
 
char * AutoProduceData (const char *fmt, uint32 &count, char **&indexTable, uint32 sqlRecordCount, uint32 sqlHighestIndex, char *&sqlDataTable)
 
char * AutoProduceStrings (const char *fmt, char *dataTable)
 

Static Public Member Functions

static uint32 GetFormatRecordSize (const char *format, int32 *index_pos=NULL)
 

Private Member Functions

 DBCFileLoader (DBCFileLoader const &right)=delete
 
DBCFileLoaderoperator= (DBCFileLoader const &right)=delete
 

Private Attributes

uint32 recordSize
 
uint32 recordCount
 
uint32 fieldCount
 
uint32 stringSize
 
uint32fieldsOffset
 
unsigned char * data
 
unsigned char * stringTable
 

Constructor & Destructor Documentation

DBCFileLoader::DBCFileLoader ( )
uint32 * fieldsOffset
Definition: DBCFileLoader.h:98
uint32 fieldCount
Definition: DBCFileLoader.h:96
uint32 recordCount
Definition: DBCFileLoader.h:95
arena_t NULL
Definition: jemalloc_internal.h:624
unsigned char * data
Definition: DBCFileLoader.h:99
uint32 recordSize
Definition: DBCFileLoader.h:94
uint32 stringSize
Definition: DBCFileLoader.h:97
unsigned char * stringTable
Definition: DBCFileLoader.h:100
DBCFileLoader::~DBCFileLoader ( )
116 {
117  delete[] data;
118 
119  delete[] fieldsOffset;
120 }
uint32 * fieldsOffset
Definition: DBCFileLoader.h:98
unsigned char * data
Definition: DBCFileLoader.h:99
DBCFileLoader::DBCFileLoader ( DBCFileLoader const right)
privatedelete

Member Function Documentation

char * DBCFileLoader::AutoProduceData ( const char *  fmt,
uint32 count,
char **&  indexTable,
uint32  sqlRecordCount,
uint32  sqlHighestIndex,
char *&  sqlDataTable 
)
174 {
175  /*
176  format STRING, NA, FLOAT, NA, INT <=>
177  struct{
178  char* field0,
179  float field1,
180  int field2
181  }entry;
182 
183  this func will generate entry[rows] data;
184  */
185 
186  typedef char* ptr;
187  if (strlen(format) != fieldCount)
188  return NULL;
189 
190  //get struct size and index pos
191  int32 i;
192  uint32 recordsize = GetFormatRecordSize(format, &i);
193 
194  if (i >= 0)
195  {
196  uint32 maxi = 0;
197  //find max index
198  for (uint32 y = 0; y < recordCount; ++y)
199  {
200  uint32 ind = getRecord(y).getUInt(i);
201  if (ind > maxi)
202  maxi = ind;
203  }
204 
205  // If higher index avalible from sql - use it instead of dbcs
206  if (sqlHighestIndex > maxi)
207  maxi = sqlHighestIndex;
208 
209  ++maxi;
210  records = maxi;
211  indexTable = new ptr[maxi];
212  memset(indexTable, 0, maxi * sizeof(ptr));
213  }
214  else
215  {
216  records = recordCount + sqlRecordCount;
217  indexTable = new ptr[recordCount + sqlRecordCount];
218  }
219 
220  char* dataTable = new char[(recordCount + sqlRecordCount) * recordsize];
221 
222  uint32 offset = 0;
223 
224  for (uint32 y = 0; y < recordCount; ++y)
225  {
226  if (i >= 0)
227  indexTable[getRecord(y).getUInt(i)] = &dataTable[offset];
228  else
229  indexTable[y] = &dataTable[offset];
230 
231  for (uint32 x=0; x < fieldCount; ++x)
232  {
233  switch (format[x])
234  {
235  case FT_FLOAT:
236  *((float*)(&dataTable[offset])) = getRecord(y).getFloat(x);
237  offset += sizeof(float);
238  break;
239  case FT_IND:
240  case FT_INT:
241  *((uint32*)(&dataTable[offset])) = getRecord(y).getUInt(x);
242  offset += sizeof(uint32);
243  break;
244  case FT_BYTE:
245  *((uint8*)(&dataTable[offset])) = getRecord(y).getUInt8(x);
246  offset += sizeof(uint8);
247  break;
248  case FT_LONG:
249  *((uint64*)(&dataTable[offset])) = getRecord(y).getUInt64(x);
250  offset += sizeof(uint64);
251  break;
252  case FT_STRING:
253  *((char**)(&dataTable[offset])) = NULL; // will replace non-empty or "" strings in AutoProduceStrings
254  offset += sizeof(char*);
255  break;
256  case FT_NA:
257  case FT_NA_BYTE:
258  case FT_SORT:
259  break;
260  default:
261  ASSERT(false && "Unknown field format character in DBCfmt.h");
262  break;
263  }
264  }
265  }
266 
267  sqlDataTable = dataTable + offset;
268 
269  return dataTable;
270 }
void format(BasicFormatter< Char > &f, const Char *&format_str, const T &value)
Definition: format.h:2963
Definition: Define.h:163
Record getRecord(size_t id)
Definition: DBCFileLoader.cpp:122
Definition: Define.h:161
uint64 getUInt64(size_t field) const
Definition: DBCFileLoader.h:56
uint32 fieldCount
Definition: DBCFileLoader.h:96
uint32 recordCount
Definition: DBCFileLoader.h:95
Definition: Define.h:160
arena_t NULL
Definition: jemalloc_internal.h:624
uint64_t uint64
Definition: g3dmath.h:170
uint8 getUInt8(size_t field) const
Definition: DBCFileLoader.h:51
float getFloat(size_t field) const
Definition: DBCFileLoader.h:37
Definition: Define.h:165
int32_t int32
Definition: Define.h:146
uint32_t uint32
Definition: Define.h:150
uint64_t uint64
Definition: Define.h:149
G3D::int16 y
Definition: Vector2int16.h:38
Definition: Define.h:162
uint8_t uint8
Definition: g3dmath.h:164
static uint32 GetFormatRecordSize(const char *format, int32 *index_pos=NULL)
Definition: DBCFileLoader.cpp:128
Definition: Define.h:158
uint8_t uint8
Definition: Define.h:152
#define ASSERT
Definition: Errors.h:55
Definition: Define.h:164
Definition: Define.h:157
uint32_t uint32
Definition: g3dmath.h:168
G3D::int16 x
Definition: Vector2int16.h:37
uint32 getUInt(size_t field) const
Definition: DBCFileLoader.h:44
Definition: Define.h:156

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

char * DBCFileLoader::AutoProduceStrings ( const char *  fmt,
char *  dataTable 
)
273 {
274  if (strlen(format) != fieldCount)
275  return NULL;
276 
277  char* stringPool = new char[stringSize];
278  memcpy(stringPool, stringTable, stringSize);
279 
280  uint32 offset = 0;
281 
282  for (uint32 y = 0; y < recordCount; ++y)
283  {
284  for (uint32 x = 0; x < fieldCount; ++x)
285  {
286  switch (format[x])
287  {
288  case FT_FLOAT:
289  offset += sizeof(float);
290  break;
291  case FT_IND:
292  case FT_INT:
293  offset += sizeof(uint32);
294  break;
295  case FT_BYTE:
296  offset += sizeof(uint8);
297  break;
298  case FT_LONG:
299  offset += sizeof(uint64);
300  break;
301  case FT_STRING:
302  {
303  // fill only not filled entries
304  char** slot = (char**)(&dataTable[offset]);
305  if (!*slot || !**slot)
306  {
307  const char * st = getRecord(y).getString(x);
308  *slot=stringPool+(st-(const char*)stringTable);
309  }
310  offset += sizeof(char*);
311  break;
312  }
313  case FT_NA:
314  case FT_NA_BYTE:
315  case FT_SORT:
316  break;
317  default:
318  ASSERT(false && "Unknown field format character in DBCfmt.h");
319  break;
320  }
321  }
322  }
323 
324  return stringPool;
325 }
void format(BasicFormatter< Char > &f, const Char *&format_str, const T &value)
Definition: format.h:2963
Definition: Define.h:163
Record getRecord(size_t id)
Definition: DBCFileLoader.cpp:122
Definition: Define.h:161
uint32 fieldCount
Definition: DBCFileLoader.h:96
uint32 recordCount
Definition: DBCFileLoader.h:95
Definition: Define.h:160
arena_t NULL
Definition: jemalloc_internal.h:624
uint64_t uint64
Definition: g3dmath.h:170
uint32 stringSize
Definition: DBCFileLoader.h:97
Definition: Define.h:165
uint32_t uint32
Definition: Define.h:150
G3D::int16 y
Definition: Vector2int16.h:38
unsigned char * stringTable
Definition: DBCFileLoader.h:100
Definition: Define.h:162
uint8_t uint8
Definition: g3dmath.h:164
const char * getString(size_t field) const
Definition: DBCFileLoader.h:63
Definition: Define.h:158
#define ASSERT
Definition: Errors.h:55
Definition: Define.h:164
Definition: Define.h:157
uint32_t uint32
Definition: g3dmath.h:168
G3D::int16 x
Definition: Vector2int16.h:37
Definition: Define.h:156

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

uint32 DBCFileLoader::GetCols ( ) const
inline
86 { return fieldCount; }
uint32 fieldCount
Definition: DBCFileLoader.h:96

+ Here is the caller graph for this function:

uint32 DBCFileLoader::GetFormatRecordSize ( const char *  format,
int32 index_pos = NULL 
)
static
129 {
130  uint32 recordsize = 0;
131  int32 i = -1;
132  for (uint32 x = 0; format[x]; ++x)
133  {
134  switch (format[x])
135  {
136  case FT_FLOAT:
137  recordsize += sizeof(float);
138  break;
139  case FT_INT:
140  recordsize += sizeof(uint32);
141  break;
142  case FT_STRING:
143  recordsize += sizeof(char*);
144  break;
145  case FT_SORT:
146  i = x;
147  break;
148  case FT_IND:
149  i = x;
150  recordsize += sizeof(uint32);
151  break;
152  case FT_BYTE:
153  recordsize += sizeof(uint8);
154  break;
155  case FT_LONG:
156  recordsize += sizeof(uint64);
157  break;
158  case FT_NA:
159  case FT_NA_BYTE:
160  break;
161  default:
162  ASSERT(false && "Unknown field format character in DBCfmt.h");
163  break;
164  }
165  }
166 
167  if (index_pos)
168  *index_pos = i;
169 
170  return recordsize;
171 }
void format(BasicFormatter< Char > &f, const Char *&format_str, const T &value)
Definition: format.h:2963
Definition: Define.h:163
Definition: Define.h:161
Definition: Define.h:160
uint64_t uint64
Definition: g3dmath.h:170
Definition: Define.h:165
int32_t int32
Definition: Define.h:146
uint32_t uint32
Definition: Define.h:150
Definition: Define.h:162
uint8_t uint8
Definition: g3dmath.h:164
Definition: Define.h:158
#define ASSERT
Definition: Errors.h:55
Definition: Define.h:164
Definition: Define.h:157
uint32_t uint32
Definition: g3dmath.h:168
G3D::int16 x
Definition: Vector2int16.h:37
Definition: Define.h:156

+ Here is the caller graph for this function:

uint32 DBCFileLoader::GetNumRows ( ) const
inline

Get begin iterator over records.

84 { return recordCount; }
uint32 recordCount
Definition: DBCFileLoader.h:95

+ Here is the caller graph for this function:

uint32 DBCFileLoader::GetOffset ( size_t  id) const
inline
87 { return (fieldsOffset != NULL && id < fieldCount) ? fieldsOffset[id] : 0; }
uint32 * fieldsOffset
Definition: DBCFileLoader.h:98
uint32 fieldCount
Definition: DBCFileLoader.h:96
arena_t NULL
Definition: jemalloc_internal.h:624
DBCFileLoader::Record DBCFileLoader::getRecord ( size_t  id)
123 {
124  assert(data);
125  return Record(*this, data + id * recordSize);
126 }
unsigned char * data
Definition: DBCFileLoader.h:99
uint32 recordSize
Definition: DBCFileLoader.h:94

+ Here is the caller graph for this function:

uint32 DBCFileLoader::GetRowSize ( ) const
inline
85 { return recordSize; }
uint32 recordSize
Definition: DBCFileLoader.h:94
bool DBCFileLoader::IsLoaded ( ) const
inline
88 { return data != NULL; }
arena_t NULL
Definition: jemalloc_internal.h:624
unsigned char * data
Definition: DBCFileLoader.h:99
bool DBCFileLoader::Load ( const char *  filename,
const char *  fmt 
)
29 {
30  uint32 header;
31  if (data)
32  {
33  delete [] data;
34  data = NULL;
35  }
36 
37  FILE* f = fopen(filename, "rb");
38  if (!f)
39  return false;
40 
41  if (fread(&header, 4, 1, f) != 1) // Number of records
42  {
43  fclose(f);
44  return false;
45  }
46 
47 
48  EndianConvert(header);
49 
50  if (header != 0x43424457) //'WDBC'
51  {
52  fclose(f);
53  return false;
54  }
55 
56  if (fread(&recordCount, 4, 1, f) != 1) // Number of records
57  {
58  fclose(f);
59  return false;
60  }
61 
63 
64  if (fread(&fieldCount, 4, 1, f) != 1) // Number of fields
65  {
66  fclose(f);
67  return false;
68  }
69 
71 
72  if (fread(&recordSize, 4, 1, f) != 1) // Size of a record
73  {
74  fclose(f);
75  return false;
76  }
77 
79 
80  if (fread(&stringSize, 4, 1, f) != 1) // String size
81  {
82  fclose(f);
83  return false;
84  }
85 
87 
89  fieldsOffset[0] = 0;
90  for (uint32 i = 1; i < fieldCount; ++i)
91  {
92  fieldsOffset[i] = fieldsOffset[i - 1];
93  if (fmt[i - 1] == FT_BYTE || fmt[i - 1] == FT_NA_BYTE) // byte fields
94  fieldsOffset[i] += sizeof(uint8);
95  else if (fmt[i - 1] == FT_LONG)
96  fieldsOffset[i] += sizeof(uint64);
97  else // 4 byte fields (int32/float/strings)
98  fieldsOffset[i] += sizeof(uint32);
99  }
100 
101  data = new unsigned char[recordSize * recordCount + stringSize];
103 
104  if (fread(data, recordSize * recordCount + stringSize, 1, f) != 1)
105  {
106  fclose(f);
107  return false;
108  }
109 
110  fclose(f);
111 
112  return true;
113 }
Definition: Define.h:163
uint32 * fieldsOffset
Definition: DBCFileLoader.h:98
uint32 fieldCount
Definition: DBCFileLoader.h:96
uint32 recordCount
Definition: DBCFileLoader.h:95
void EndianConvert(T &val)
Definition: ByteConverter.h:48
arena_t NULL
Definition: jemalloc_internal.h:624
unsigned char * data
Definition: DBCFileLoader.h:99
uint64_t uint64
Definition: g3dmath.h:170
uint32 recordSize
Definition: DBCFileLoader.h:94
uint32 stringSize
Definition: DBCFileLoader.h:97
uint32_t uint32
Definition: Define.h:150
unsigned char * stringTable
Definition: DBCFileLoader.h:100
Definition: Define.h:162
uint8_t uint8
Definition: g3dmath.h:164
Definition: format.h:285
Definition: Define.h:157
uint32_t uint32
Definition: g3dmath.h:168

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

DBCFileLoader& DBCFileLoader::operator= ( DBCFileLoader const right)
privatedelete

Member Data Documentation

unsigned char* DBCFileLoader::data
private
uint32 DBCFileLoader::fieldCount
private
uint32* DBCFileLoader::fieldsOffset
private
uint32 DBCFileLoader::recordCount
private
uint32 DBCFileLoader::recordSize
private
uint32 DBCFileLoader::stringSize
private
unsigned char* DBCFileLoader::stringTable
private

The documentation for this class was generated from the following files: