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

#include <DB2StorageLoader.h>

Classes

class  Record
 

Public Member Functions

 DB2FileLoader ()
 
 ~DB2FileLoader ()
 
bool Load (const char *filename, const char *fmt)
 
Record getRecord (size_t id)
 
uint32 GetNumRows () const
 Get begin iterator over records. More...
 
uint32 GetCols () const
 
uint32 GetOffset (size_t id) const
 
uint32 GetHash () const
 
bool IsLoaded () const
 
char * AutoProduceData (const char *fmt, uint32 &count, char **&indexTable)
 
char * AutoProduceStringsArrayHolders (const char *fmt, char *dataTable)
 
char * AutoProduceStrings (const char *fmt, char *dataTable, uint32 locale)
 

Static Public Member Functions

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

Private Attributes

char constfileName
 
uint32 recordSize
 
uint32 recordCount
 
uint32 fieldCount
 
uint32 stringSize
 
uint32fieldsOffset
 
unsigned char * data
 
unsigned char * stringTable
 
uint32 tableHash
 
uint32 build
 
int unk1
 
int minIndex
 
int maxIndex
 
int localeMask
 
int unk5
 

Constructor & Destructor Documentation

DB2FileLoader::DB2FileLoader ( )
24 {
25  fileName = nullptr;
26  recordSize = 0;
27  recordCount = 0;
28  fieldCount = 0;
29  stringSize = 0;
30  fieldsOffset = nullptr;
31  data = nullptr;
32  stringTable = nullptr;
33 
34  tableHash = 0;
35  build = 0;
36 
37  unk1 = 0;
38  minIndex = 0;
39  maxIndex = 0;
40  localeMask = 0;
41  unk5 = 0;
42 }
uint32 build
Definition: DB2StorageLoader.h:108
int unk1
Definition: DB2StorageLoader.h:110
uint32 recordCount
Definition: DB2StorageLoader.h:99
uint32 stringSize
Definition: DB2StorageLoader.h:101
int minIndex
Definition: DB2StorageLoader.h:111
int maxIndex
Definition: DB2StorageLoader.h:112
unsigned char * stringTable
Definition: DB2StorageLoader.h:104
uint32 recordSize
Definition: DB2StorageLoader.h:98
char const * fileName
Definition: DB2StorageLoader.h:96
int localeMask
Definition: DB2StorageLoader.h:113
uint32 * fieldsOffset
Definition: DB2StorageLoader.h:102
uint32 fieldCount
Definition: DB2StorageLoader.h:100
unsigned char * data
Definition: DB2StorageLoader.h:103
int unk5
Definition: DB2StorageLoader.h:114
uint32 tableHash
Definition: DB2StorageLoader.h:107
DB2FileLoader::~DB2FileLoader ( )
194 {
195  if (data)
196  delete [] data;
197  if (fieldsOffset)
198  delete [] fieldsOffset;
199 }
uint32 * fieldsOffset
Definition: DB2StorageLoader.h:102
unsigned char * data
Definition: DB2StorageLoader.h:103

Member Function Documentation

char * DB2FileLoader::AutoProduceData ( const char *  fmt,
uint32 count,
char **&  indexTable 
)
266 {
267  typedef char * ptr;
268  if (strlen(format) != fieldCount)
269  return NULL;
270 
271  //get struct size and index pos
272  int32 indexField;
273  uint32 recordsize = GetFormatRecordSize(format, &indexField);
274 
275  if (indexField >= 0)
276  {
277  uint32 maxi = 0;
278  //find max index
279  for (uint32 y = 0; y < recordCount; y++)
280  {
281  uint32 ind = getRecord(y).getUInt(indexField);
282  if (ind > maxi)
283  maxi = ind;
284  }
285 
286  ++maxi;
287  records = maxi;
288  indexTable = new ptr[maxi];
289  memset(indexTable, 0, maxi * sizeof(ptr));
290  }
291  else
292  {
293  records = recordCount;
294  indexTable = new ptr[recordCount];
295  }
296 
297  char* dataTable = new char[recordCount * recordsize];
298 
299  uint32 offset = 0;
300 
301  for (uint32 y = 0; y < recordCount; y++)
302  {
303  if (indexField >= 0)
304  indexTable[getRecord(y).getUInt(indexField)] = &dataTable[offset];
305  else
306  indexTable[y] = &dataTable[offset];
307 
308  for (uint32 x = 0; x < fieldCount; x++)
309  {
310  switch (format[x])
311  {
312  case FT_FLOAT:
313  *((float*)(&dataTable[offset])) = getRecord(y).getFloat(x);
314  offset += 4;
315  break;
316  case FT_IND:
317  case FT_INT:
318  *((uint32*)(&dataTable[offset])) = getRecord(y).getUInt(x);
319  offset += 4;
320  break;
321  case FT_BYTE:
322  *((uint8*)(&dataTable[offset])) = getRecord(y).getUInt8(x);
323  offset += 1;
324  break;
325  case FT_LONG:
326  *((uint64*)(&dataTable[offset])) = getRecord(y).getUInt64(x);
327  offset += 8;
328  break;
329  case FT_STRING:
331  *((char**)(&dataTable[offset])) = nullptr; // will be replaces non-empty or "" strings in AutoProduceStrings
332  offset += sizeof(char*);
333  break;
334  }
335  }
336  }
337 
338  return dataTable;
339 }
void format(BasicFormatter< Char > &f, const Char *&format_str, const T &value)
Definition: format.h:2963
Definition: Define.h:163
uint64 getUInt64(size_t field) const
Definition: DB2StorageLoader.h:57
Record getRecord(size_t id)
Definition: DB2StorageLoader.cpp:201
Definition: Define.h:161
Definition: Define.h:160
uint32 recordCount
Definition: DB2StorageLoader.h:99
arena_t NULL
Definition: jemalloc_internal.h:624
uint32 getUInt(size_t field) const
Definition: DB2StorageLoader.h:45
Definition: Define.h:165
int32_t int32
Definition: Define.h:146
uint32_t uint32
Definition: Define.h:150
uint8 getUInt8(size_t field) const
Definition: DB2StorageLoader.h:52
uint64_t uint64
Definition: Define.h:149
G3D::int16 y
Definition: Vector2int16.h:38
float getFloat(size_t field) const
Definition: DB2StorageLoader.h:38
Definition: Define.h:162
Definition: Define.h:158
uint8_t uint8
Definition: Define.h:152
G3D::int16 x
Definition: Vector2int16.h:37
static uint32 GetFormatRecordSize(const char *format, int32 *index_pos=NULL)
Definition: DB2StorageLoader.cpp:207
Definition: Define.h:159
uint32 fieldCount
Definition: DB2StorageLoader.h:100

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

char * DB2FileLoader::AutoProduceStrings ( const char *  fmt,
char *  dataTable,
uint32  locale 
)
413 {
414  if (strlen(format) != fieldCount)
415  return nullptr;
416 
417  if (!(localeMask & (1 << locale)))
418  {
419  char const* sep = "";
420  std::ostringstream str;
421  for (uint32 i = 0; i < TOTAL_LOCALES; ++i)
422  {
423  if (localeMask & (1 << i))
424  {
425  str << sep << localeNames[i];
426  sep = ", ";
427  }
428  }
429 
430  TC_LOG_ERROR("", "Attempted to load %s which has locales %s as %s. Check if you placed your localized db2 files in correct directory.", fileName, str.str().c_str(), localeNames[locale]);
431  return nullptr;
432  }
433 
434  char* stringPool = new char[stringSize];
435  memcpy(stringPool, stringTable, stringSize);
436 
437  uint32 offset = 0;
438 
439  for (uint32 y = 0; y < recordCount; y++)
440  {
441  for (uint32 x = 0; x < fieldCount; x++)
442  {
443  switch (format[x])
444  {
445  case FT_FLOAT:
446  case FT_IND:
447  case FT_INT:
448  offset += 4;
449  break;
450  case FT_BYTE:
451  offset += 1;
452  break;
453  case FT_LONG:
454  offset += 8;
455  break;
456  case FT_STRING:
457  {
458  // fill only not filled entries
459  LocalizedString* db2str = *(LocalizedString**)(&dataTable[offset]);
460  if (db2str->Str[locale] == nullStr)
461  {
462  char const* st = getRecord(y).getString(x);
463  db2str->Str[locale] = stringPool + (st - (char const*)stringTable);
464  }
465 
466  offset += sizeof(char*);
467  break;
468  }
470  {
471  char** db2str = (char**)(&dataTable[offset]);
472  char const* st = getRecord(y).getString(x);
473  *db2str = stringPool + (st - (char const*)stringTable);
474  offset += sizeof(char*);
475  break;
476  }
477  }
478  }
479  }
480 
481  return stringPool;
482 }
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: DB2StorageLoader.cpp:201
Definition: Define.h:161
static char const *const nullStr
Definition: DB2StorageLoader.cpp:341
Definition: Common.h:146
Definition: Define.h:160
uint32 recordCount
Definition: DB2StorageLoader.h:99
uint32 stringSize
Definition: DB2StorageLoader.h:101
const char * getString(size_t field) const
Definition: DB2StorageLoader.h:64
char const * Str[TOTAL_LOCALES]
Definition: Common.h:148
unsigned char * stringTable
Definition: DB2StorageLoader.h:104
char const * fileName
Definition: DB2StorageLoader.h:96
Definition: Define.h:165
uint32_t uint32
Definition: Define.h:150
G3D::int16 y
Definition: Vector2int16.h:38
int localeMask
Definition: DB2StorageLoader.h:113
Definition: Define.h:162
TC_COMMON_API char const * localeNames[TOTAL_LOCALES]
Definition: Common.cpp:21
Definition: Define.h:158
#define TC_LOG_ERROR(filterType__,...)
Definition: Log.h:207
G3D::int16 x
Definition: Vector2int16.h:37
Definition: Define.h:159
uint32 fieldCount
Definition: DB2StorageLoader.h:100
Definition: Common.h:130

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

char * DB2FileLoader::AutoProduceStringsArrayHolders ( const char *  fmt,
char *  dataTable 
)
344 {
345  if (strlen(format) != fieldCount)
346  return nullptr;
347 
348  // we store flat holders pool as single memory block
349  std::size_t stringFields = GetFormatStringFieldCount(format);
350  if (!stringFields)
351  return nullptr;
352 
353  std::size_t localizedStringFields = GetFormatLocalizedStringFieldCount(format);
354 
355  // each string field at load have array of string for each locale
356  std::size_t stringHolderSize = sizeof(char*) * TOTAL_LOCALES;
357  std::size_t stringHoldersRecordPoolSize = localizedStringFields * stringHolderSize + (stringFields - localizedStringFields) * sizeof(char*);
358  std::size_t stringHoldersPoolSize = stringHoldersRecordPoolSize * recordCount;
359 
360  char* stringHoldersPool = new char[stringHoldersPoolSize];
361 
362  // DB2 strings expected to have at least empty string
363  for (std::size_t i = 0; i < stringHoldersPoolSize / sizeof(char*); ++i)
364  ((char const**)stringHoldersPool)[i] = nullStr;
365 
366  uint32 offset = 0;
367 
368  // assign string holders to string field slots
369  for (uint32 y = 0; y < recordCount; y++)
370  {
371  uint32 stringFieldOffset = 0;
372 
373  for (uint32 x = 0; x < fieldCount; x++)
374  {
375  switch (format[x])
376  {
377  case FT_FLOAT:
378  case FT_IND:
379  case FT_INT:
380  offset += 4;
381  break;
382  case FT_BYTE:
383  offset += 1;
384  break;
385  case FT_LONG:
386  offset += 8;
387  break;
388  case FT_STRING:
390  {
391  // init db2 string field slots by pointers to string holders
392  char const*** slot = (char const***)(&dataTable[offset]);
393  *slot = (char const**)(&stringHoldersPool[stringHoldersRecordPoolSize * y + stringFieldOffset]);
394  if (format[x] == FT_STRING)
395  stringFieldOffset += stringHolderSize;
396  else
397  ++stringFieldOffset;
398 
399  offset += sizeof(char*);
400  break;
401  }
402  default:
403  ASSERT(false, "unknown format character %c", format[x]);
404  }
405  }
406  }
407 
408  //send as char* for store in char* pool list for free at unload
409  return stringHoldersPool;
410 }
void format(BasicFormatter< Char > &f, const Char *&format_str, const T &value)
Definition: format.h:2963
Definition: Define.h:163
Definition: Define.h:161
static char const *const nullStr
Definition: DB2StorageLoader.cpp:341
Definition: Define.h:160
uint32 recordCount
Definition: DB2StorageLoader.h:99
static uint32 GetFormatStringFieldCount(const char *format)
Definition: DB2StorageLoader.cpp:245
static uint32 GetFormatLocalizedStringFieldCount(const char *format)
Definition: DB2StorageLoader.cpp:255
Definition: Define.h:165
uint32_t uint32
Definition: Define.h:150
G3D::int16 y
Definition: Vector2int16.h:38
Definition: Define.h:162
Definition: Define.h:158
#define ASSERT
Definition: Errors.h:55
G3D::int16 x
Definition: Vector2int16.h:37
Definition: Define.h:159
uint32 fieldCount
Definition: DB2StorageLoader.h:100
Definition: Common.h:130

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

uint32 DB2FileLoader::GetCols ( ) const
inline
85 { return fieldCount; }
uint32 fieldCount
Definition: DB2StorageLoader.h:100

+ Here is the caller graph for this function:

uint32 DB2FileLoader::GetFormatLocalizedStringFieldCount ( const char *  format)
static
256 {
257  uint32 stringfields = 0;
258  for (uint32 x = 0; format[x]; ++x)
259  if (format[x] == FT_STRING)
260  ++stringfields;
261 
262  return stringfields;
263 }
void format(BasicFormatter< Char > &f, const Char *&format_str, const T &value)
Definition: format.h:2963
uint32_t uint32
Definition: Define.h:150
Definition: Define.h:158
G3D::int16 x
Definition: Vector2int16.h:37

+ Here is the caller graph for this function:

uint32 DB2FileLoader::GetFormatRecordSize ( const char *  format,
int32 index_pos = NULL 
)
static
208 {
209  uint32 recordsize = 0;
210  int32 i = -1;
211  for (uint32 x=0; format[x]; ++x)
212  {
213  switch (format[x])
214  {
215  case FT_FLOAT:
216  case FT_INT:
217  recordsize += 4;
218  break;
219  case FT_STRING:
221  recordsize += sizeof(char*);
222  break;
223  case FT_SORT:
224  i = x;
225  break;
226  case FT_IND:
227  i = x;
228  recordsize += 4;
229  break;
230  case FT_BYTE:
231  recordsize += 1;
232  break;
233  case FT_LONG:
234  recordsize += 8;
235  break;
236  }
237  }
238 
239  if (index_pos)
240  *index_pos = i;
241 
242  return recordsize;
243 }
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
Definition: Define.h:165
int32_t int32
Definition: Define.h:146
uint32_t uint32
Definition: Define.h:150
Definition: Define.h:162
Definition: Define.h:158
Definition: Define.h:164
G3D::int16 x
Definition: Vector2int16.h:37
Definition: Define.h:159

+ Here is the caller graph for this function:

uint32 DB2FileLoader::GetFormatStringFieldCount ( const char *  format)
static
246 {
247  uint32 stringfields = 0;
248  for (uint32 x = 0; format[x]; ++x)
250  ++stringfields;
251 
252  return stringfields;
253 }
void format(BasicFormatter< Char > &f, const Char *&format_str, const T &value)
Definition: format.h:2963
uint32_t uint32
Definition: Define.h:150
Definition: Define.h:158
G3D::int16 x
Definition: Vector2int16.h:37
Definition: Define.h:159

+ Here is the caller graph for this function:

uint32 DB2FileLoader::GetHash ( ) const
inline
87 { return tableHash; }
uint32 tableHash
Definition: DB2StorageLoader.h:107

+ Here is the caller graph for this function:

uint32 DB2FileLoader::GetNumRows ( ) const
inline

Get begin iterator over records.

84 { return recordCount;}
uint32 recordCount
Definition: DB2StorageLoader.h:99
uint32 DB2FileLoader::GetOffset ( size_t  id) const
inline
86 { return (fieldsOffset != NULL && id < fieldCount) ? fieldsOffset[id] : 0; }
arena_t NULL
Definition: jemalloc_internal.h:624
uint32 * fieldsOffset
Definition: DB2StorageLoader.h:102
uint32 fieldCount
Definition: DB2StorageLoader.h:100
DB2FileLoader::Record DB2FileLoader::getRecord ( size_t  id)
202 {
203  assert(data);
204  return Record(*this, data + id*recordSize);
205 }
uint32 recordSize
Definition: DB2StorageLoader.h:98
unsigned char * data
Definition: DB2StorageLoader.h:103

+ Here is the caller graph for this function:

bool DB2FileLoader::IsLoaded ( ) const
inline
88 { return (data != NULL); }
arena_t NULL
Definition: jemalloc_internal.h:624
unsigned char * data
Definition: DB2StorageLoader.h:103
bool DB2FileLoader::Load ( const char *  filename,
const char *  fmt 
)
45 {
46  if (data)
47  {
48  delete[] data;
49  data = nullptr;
50  }
51 
52  FILE* f = fopen(filename, "rb");
53  if (!f)
54  return false;
55 
56  fileName = filename;
57  uint32 header;
58  if (fread(&header, 4, 1, f) != 1) // Signature
59  {
60  fclose(f);
61  return false;
62  }
63 
64  EndianConvert(header);
65 
66  if (header != 0x32424457)
67  {
68  fclose(f);
69  return false; //'WDB2'
70  }
71 
72  if (fread(&recordCount, 4, 1, f) != 1) // Number of records
73  {
74  fclose(f);
75  return false;
76  }
77 
79 
80  if (fread(&fieldCount, 4, 1, f) != 1) // Number of fields
81  {
82  fclose(f);
83  return false;
84  }
85 
87 
88  if (fread(&recordSize, 4, 1, f) != 1) // Size of a record
89  {
90  fclose(f);
91  return false;
92  }
93 
95 
96  if (fread(&stringSize, 4, 1, f) != 1) // String size
97  {
98  fclose(f);
99  return false;
100  }
101 
103 
104  /* NEW WDB2 FIELDS*/
105  if (fread(&tableHash, 4, 1, f) != 1) // Table hash
106  {
107  fclose(f);
108  return false;
109  }
110 
112 
113  if (fread(&build, 4, 1, f) != 1) // Build
114  {
115  fclose(f);
116  return false;
117  }
118 
120 
121  if (fread(&unk1, 4, 1, f) != 1) // Unknown WDB2
122  {
123  fclose(f);
124  return false;
125  }
126 
128 
129  if (fread(&minIndex, 4, 1, f) != 1) // MinIndex WDB2
130  {
131  fclose(f);
132  return false;
133  }
134 
136 
137  if (fread(&maxIndex, 4, 1, f) != 1) // MaxIndex WDB2
138  {
139  fclose(f);
140  return false;
141  }
142 
144 
145  if (fread(&localeMask, 4, 1, f) != 1) // Locales
146  {
147  fclose(f);
148  return false;
149  }
150 
152 
153  if (fread(&unk5, 4, 1, f) != 1) // Unknown WDB2
154  {
155  fclose(f);
156  return false;
157  }
158 
160 
161  if (maxIndex != 0)
162  {
163  int32 diff = maxIndex - minIndex + 1;
164  fseek(f, diff * 4 + diff * 2, SEEK_CUR); // diff * 4: an index for rows, diff * 2: a memory allocation bank
165  }
166 
168  fieldsOffset[0] = 0;
169  for (uint32 i = 1; i < fieldCount; i++)
170  {
171  fieldsOffset[i] = fieldsOffset[i - 1];
172  if (fmt[i - 1] == FT_BYTE) // byte fields
173  fieldsOffset[i] += 1;
174  else if (fmt[i - 1] == FT_LONG)
175  fieldsOffset[i] += 8;
176  else // 4 byte fields (int32/float/strings)
177  fieldsOffset[i] += 4;
178  }
179 
180  data = new unsigned char[recordSize * recordCount + stringSize];
182 
183  if (fread(data, recordSize * recordCount + stringSize, 1, f) != 1)
184  {
185  fclose(f);
186  return false;
187  }
188 
189  fclose(f);
190  return true;
191 }
Definition: Define.h:163
uint32 build
Definition: DB2StorageLoader.h:108
#define SEEK_CUR
Definition: zconf.h:476
int unk1
Definition: DB2StorageLoader.h:110
void EndianConvert(T &val)
Definition: ByteConverter.h:48
uint32 recordCount
Definition: DB2StorageLoader.h:99
uint32 stringSize
Definition: DB2StorageLoader.h:101
int minIndex
Definition: DB2StorageLoader.h:111
int maxIndex
Definition: DB2StorageLoader.h:112
unsigned char * stringTable
Definition: DB2StorageLoader.h:104
uint32 recordSize
Definition: DB2StorageLoader.h:98
char const * fileName
Definition: DB2StorageLoader.h:96
int32_t int32
Definition: Define.h:146
uint32_t uint32
Definition: Define.h:150
int localeMask
Definition: DB2StorageLoader.h:113
uint32 * fieldsOffset
Definition: DB2StorageLoader.h:102
Definition: Define.h:162
Definition: format.h:285
uint32 fieldCount
Definition: DB2StorageLoader.h:100
unsigned char * data
Definition: DB2StorageLoader.h:103
int unk5
Definition: DB2StorageLoader.h:114
uint32 tableHash
Definition: DB2StorageLoader.h:107

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

Member Data Documentation

uint32 DB2FileLoader::build
private
unsigned char* DB2FileLoader::data
private
uint32 DB2FileLoader::fieldCount
private
uint32* DB2FileLoader::fieldsOffset
private
char const* DB2FileLoader::fileName
private
int DB2FileLoader::localeMask
private
int DB2FileLoader::maxIndex
private
int DB2FileLoader::minIndex
private
uint32 DB2FileLoader::recordCount
private
uint32 DB2FileLoader::recordSize
private
uint32 DB2FileLoader::stringSize
private
unsigned char* DB2FileLoader::stringTable
private
uint32 DB2FileLoader::tableHash
private
int DB2FileLoader::unk1
private
int DB2FileLoader::unk5
private

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