TrinityCore
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
DB2StorageLoader.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2008-2016 TrinityCore <http://www.trinitycore.org/>
3  *
4  * This program is free software; you can redistribute it and/or modify it
5  * under the terms of the GNU General Public License as published by the
6  * Free Software Foundation; either version 2 of the License, or (at your
7  * option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12  * more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #ifndef DB2_FILE_LOADER_H
19 #define DB2_FILE_LOADER_H
20 
21 #include "Define.h"
24 #include <cassert>
25 #include <list>
26 
28 {
29  public:
30  DB2FileLoader();
31  ~DB2FileLoader();
32 
33  bool Load(const char *filename, const char *fmt);
34 
35  class Record
36  {
37  public:
38  float getFloat(size_t field) const
39  {
40  assert(field < file.fieldCount);
41  float val = *reinterpret_cast<float*>(offset + file.GetOffset(field));
42  EndianConvert(val);
43  return val;
44  }
45  uint32 getUInt(size_t field) const
46  {
47  assert(field < file.fieldCount);
48  uint32 val = *reinterpret_cast<uint32*>(offset + file.GetOffset(field));
49  EndianConvert(val);
50  return val;
51  }
52  uint8 getUInt8(size_t field) const
53  {
54  assert(field < file.fieldCount);
55  return *reinterpret_cast<uint8*>(offset + file.GetOffset(field));
56  }
57  uint64 getUInt64(size_t field) const
58  {
59  assert(field < file.fieldCount);
60  uint64 val = *reinterpret_cast<uint64*>(offset + file.GetOffset(field));
61  EndianConvert(val);
62  return val;
63  }
64  const char *getString(size_t field) const
65  {
66  assert(field < file.fieldCount);
67  size_t stringOffset = getUInt(field);
68  assert(stringOffset < file.stringSize);
69  return reinterpret_cast<char*>(file.stringTable + stringOffset);
70  }
71 
72  private:
73  Record(DB2FileLoader &file_, unsigned char *offset_): offset(offset_), file(file_) {}
74  unsigned char *offset;
76 
77  friend class DB2FileLoader;
78  };
79 
80  // Get record by id
81  Record getRecord(size_t id);
83 
84  uint32 GetNumRows() const { return recordCount;}
85  uint32 GetCols() const { return fieldCount; }
86  uint32 GetOffset(size_t id) const { return (fieldsOffset != NULL && id < fieldCount) ? fieldsOffset[id] : 0; }
87  uint32 GetHash() const { return tableHash; }
88  bool IsLoaded() const { return (data != NULL); }
89  char* AutoProduceData(const char* fmt, uint32& count, char**& indexTable);
90  char* AutoProduceStringsArrayHolders(const char* fmt, char* dataTable);
91  char* AutoProduceStrings(const char* fmt, char* dataTable, uint32 locale);
92  static uint32 GetFormatRecordSize(const char * format, int32 * index_pos = NULL);
93  static uint32 GetFormatStringFieldCount(const char * format);
95 private:
96  char const* fileName;
97 
103  unsigned char *data;
104  unsigned char *stringTable;
105 
106  // WDB2 / WCH2 fields
107  uint32 tableHash; // WDB2
108  uint32 build; // WDB2
109 
110  int unk1; // WDB2 (Unix time in WCH2)
111  int minIndex; // WDB2
112  int maxIndex; // WDB2 (index table)
113  int localeMask; // WDB2
114  int unk5; // WDB2
115 };
116 
118 {
119 public:
120  explicit DB2DatabaseLoader(std::string const& storageName) : _storageName(storageName) { }
121 
122  char* Load(const char* format, HotfixDatabaseStatements preparedStatement, uint32& records, char**& indexTable, char*& stringHolders, std::list<char*>& stringPool);
123  void LoadStrings(const char* format, HotfixDatabaseStatements preparedStatement, uint32 locale, char**& indexTable, std::list<char*>& stringPool);
124  static char* AddString(char const** holder, std::string const& value);
125 
126 private:
127  std::string _storageName;
128 };
129 
130 #endif
void format(BasicFormatter< Char > &f, const Char *&format_str, const T &value)
Definition: format.h:2963
uint64 getUInt64(size_t field) const
Definition: DB2StorageLoader.h:57
HotfixDatabaseStatements
Definition: HotfixDatabase.h:24
uint32 build
Definition: DB2StorageLoader.h:108
Record getRecord(size_t id)
Definition: DB2StorageLoader.cpp:201
int unk1
Definition: DB2StorageLoader.h:110
unsigned char * offset
Definition: DB2StorageLoader.h:74
uint32 GetHash() const
Definition: DB2StorageLoader.h:87
void EndianConvert(T &val)
Definition: ByteConverter.h:48
uint32 recordCount
Definition: DB2StorageLoader.h:99
static uint32 GetFormatStringFieldCount(const char *format)
Definition: DB2StorageLoader.cpp:245
arena_t NULL
Definition: jemalloc_internal.h:624
uint32 stringSize
Definition: DB2StorageLoader.h:101
int minIndex
Definition: DB2StorageLoader.h:111
char * AutoProduceStringsArrayHolders(const char *fmt, char *dataTable)
Definition: DB2StorageLoader.cpp:343
const char * getString(size_t field) const
Definition: DB2StorageLoader.h:64
DB2FileLoader & file
Definition: DB2StorageLoader.h:75
bool IsLoaded() const
Definition: DB2StorageLoader.h:88
uint32 getUInt(size_t field) const
Definition: DB2StorageLoader.h:45
uint32 GetNumRows() const
Get begin iterator over records.
Definition: DB2StorageLoader.h:84
int maxIndex
Definition: DB2StorageLoader.h:112
static uint32 GetFormatLocalizedStringFieldCount(const char *format)
Definition: DB2StorageLoader.cpp:255
unsigned char * stringTable
Definition: DB2StorageLoader.h:104
char * AutoProduceData(const char *fmt, uint32 &count, char **&indexTable)
Definition: DB2StorageLoader.cpp:265
uint32 recordSize
Definition: DB2StorageLoader.h:98
char const * fileName
Definition: DB2StorageLoader.h:96
Definition: DB2StorageLoader.h:27
uint32 GetCols() const
Definition: DB2StorageLoader.h:85
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
#define TC_SHARED_API
Definition: Define.h:128
float getFloat(size_t field) const
Definition: DB2StorageLoader.h:38
int localeMask
Definition: DB2StorageLoader.h:113
uint32 * fieldsOffset
Definition: DB2StorageLoader.h:102
uint32 GetOffset(size_t id) const
Definition: DB2StorageLoader.h:86
Definition: DB2StorageLoader.h:117
std::string _storageName
Definition: DB2StorageLoader.h:127
char * AutoProduceStrings(const char *fmt, char *dataTable, uint32 locale)
Definition: DB2StorageLoader.cpp:412
uint8_t uint8
Definition: Define.h:152
Definition: format.h:285
const FieldDescriptor value
Definition: descriptor.h:1522
static uint32 GetFormatRecordSize(const char *format, int32 *index_pos=NULL)
Definition: DB2StorageLoader.cpp:207
uint32 fieldCount
Definition: DB2StorageLoader.h:100
Definition: DB2StorageLoader.h:35
DB2DatabaseLoader(std::string const &storageName)
Definition: DB2StorageLoader.h:120
Record(DB2FileLoader &file_, unsigned char *offset_)
Definition: DB2StorageLoader.h:73
unsigned char * data
Definition: DB2StorageLoader.h:103
int unk5
Definition: DB2StorageLoader.h:114
uint32 tableHash
Definition: DB2StorageLoader.h:107