TrinityCore
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
BinaryInput.h
Go to the documentation of this file.
1 
13 #ifndef G3D_BinaryInput_h
14 #define G3D_BinaryInput_h
15 
16 #ifdef _MSC_VER
17 // Disable conditional expression is constant, which occurs incorrectly on inlined functions
18 # pragma warning(push)
19 # pragma warning( disable : 4127 )
20 #endif
21 
22 #include <assert.h>
23 #include <string>
24 #include <vector>
25 #include <sys/stat.h>
26 #include <sys/types.h>
27 #include <stdio.h>
28 #include "G3D/platform.h"
29 #include "G3D/unorm8.h"
30 #include "G3D/Array.h"
31 #include "G3D/Color4.h"
32 #include "G3D/Color3.h"
33 #include "G3D/Vector4.h"
34 #include "G3D/Vector3.h"
35 #include "G3D/Vector2.h"
36 #include "G3D/g3dmath.h"
37 #include "G3D/debug.h"
38 #include "G3D/System.h"
39 
40 
41 namespace G3D {
42 
43 #if defined(G3D_WINDOWS) || defined(G3D_LINUX)
44  // Allow writing of integers to non-word aligned locations.
45  // This is legal on x86, but not on other platforms.
46  #define G3D_ALLOW_UNALIGNED_WRITES
47 #endif
48 
69 class BinaryInput {
70 private:
71 
72  // The initial buffer will be no larger than this, but
73  // may grow if a large memory read occurs. 750 MB
74  static const int64
76 #ifdef G3D_64BIT
77  5000000000L // 5 GB
78 #else
79  750000000 // 750 MB
80 #endif
81  ;
82 
87  std::string m_filename;
88 
90 
92  int m_bitPos;
93 
99 
102 
108 
114 
118 
123 
128 
131  void loadIntoMemory(int64 startPosition, int64 minLength = 0);
132 
134  void prepareToRead(int64 nbytes);
135 
136 
137  // Not implemented on purpose, don't use
138  BinaryInput(const BinaryInput&);
140  bool operator==(const BinaryInput&);
141 
143  void decompress();
144 public:
145 
147  static const bool NO_COPY;
148 
157  BinaryInput(
158  const std::string& filename,
159  G3DEndian fileEndian,
160  bool compressed = false);
161 
185  BinaryInput(
186  const uint8* data,
187  int64 dataLen,
188  G3DEndian dataEndian,
189  bool compressed = false,
190  bool copyMemory = true);
191 
192  virtual ~BinaryInput();
193 
197  void setEndian(G3DEndian endian);
198 
199  G3DEndian endian() const {
200  return m_fileEndian;
201  }
202 
203  std::string getFilename() const {
204  return m_filename;
205  }
206 
214  setPosition(n);
215  return readUInt8();
216  }
217 
221  int64 getLength() const {
222  return m_length;
223  }
224 
225  int64 size() const {
226  return getLength();
227  }
228 
233  int64 getPosition() const {
234  return m_pos + m_alreadyRead;
235  }
236 
241  const uint8* getCArray() {
242  if (m_alreadyRead > 0 || m_bufferLength < m_length) {
243  throw "Cannot getCArray for a huge file";
244  }
245  return m_buffer;
246  }
247 
252  void setPosition(int64 p) {
253  debugAssertM(p <= m_length, "Read past end of file");
254  m_pos = p - m_alreadyRead;
255  if ((m_pos < 0) || (m_pos > m_bufferLength)) {
256  loadIntoMemory(m_pos + m_alreadyRead);
257  }
258  }
259 
263  void reset() {
264  setPosition(0);
265  }
266 
267  void readBytes(void* bytes, int64 n);
268 
270  prepareToRead(1);
271  return m_buffer[m_pos++];
272  }
273 
274  bool readBool8() {
275  return (readInt8() != 0);
276  }
277 
279  prepareToRead(1);
280  return ((uint8*)m_buffer)[m_pos++];
281  }
282 
284  return unorm8::fromBits(readUInt8());
285  }
286 
288  prepareToRead(2);
289 
290  m_pos += 2;
291  if (m_swapBytes) {
292  uint8 out[2];
293  out[0] = m_buffer[m_pos - 1];
294  out[1] = m_buffer[m_pos - 2];
295  return *(uint16*)out;
296  } else {
297  #ifdef G3D_ALLOW_UNALIGNED_WRITES
298  return *(uint16*)(&m_buffer[m_pos - 2]);
299  #else
300  uint8 out[2];
301  out[0] = m_buffer[m_pos - 2];
302  out[1] = m_buffer[m_pos - 1];
303  return *(uint16*)out;
304  #endif
305  }
306 
307  }
308 
310  uint16 a = readUInt16();
311  return *(int16*)&a;
312  }
313 
315  prepareToRead(4);
316 
317  m_pos += 4;
318  if (m_swapBytes) {
319  uint8 out[4];
320  out[0] = m_buffer[m_pos - 1];
321  out[1] = m_buffer[m_pos - 2];
322  out[2] = m_buffer[m_pos - 3];
323  out[3] = m_buffer[m_pos - 4];
324  return *(uint32*)out;
325  } else {
326  #ifdef G3D_ALLOW_UNALIGNED_WRITES
327  return *(uint32*)(&m_buffer[m_pos - 4]);
328  #else
329  uint8 out[4];
330  out[0] = m_buffer[m_pos - 4];
331  out[1] = m_buffer[m_pos - 3];
332  out[2] = m_buffer[m_pos - 2];
333  out[3] = m_buffer[m_pos - 1];
334  return *(uint32*)out;
335  #endif
336  }
337  }
338 
339 
341  uint32 a = readUInt32();
342  return *(int32*)&a;
343  }
344 
345  uint64 readUInt64();
346 
348  uint64 a = readUInt64();
349  return *(int64*)&a;
350  }
351 
353  union {
354  uint32 a;
355  float32 b;
356  };
357  a = readUInt32();
358  return b;
359  }
360 
362  union {
363  uint64 a;
364  float64 b;
365  };
366  a = readUInt64();
367  return b;
368  }
369 
373  std::string readString(int64 maxLength);
374 
378  std::string readString();
379 
382  std::string readFixedLengthString(int numBytes);
383 
387  std::string readStringNewline();
388 
395  std::string readStringEven();
396 
398  std::string readString32();
399 
403 
404  Color4 readColor4();
405  Color3 readColor3();
406 
410  void skip(int64 n) {
411  setPosition(m_pos + m_alreadyRead + n);
412  }
413 
417  bool hasMore() const {
418  return m_pos + m_alreadyRead < m_length;
419  }
420 
424  void beginBits();
425 
427  uint32 readBits(int numBits);
428 
430  void endBits();
431 
432 # define DECLARE_READER(ucase, lcase)\
433  void read##ucase(lcase* out, int64 n);\
434  void read##ucase(std::vector<lcase>& out, int64 n);\
435  void read##ucase(Array<lcase>& out, int64 n);
436 
437  DECLARE_READER(Bool8, bool)
438  DECLARE_READER(UInt8, uint8)
439  DECLARE_READER(Int8, int8)
444  DECLARE_READER(UInt64, uint64)
445  DECLARE_READER(Int64, int64)
446  DECLARE_READER(Float32, float32)
447  DECLARE_READER(Float64, float64)
448 # undef DECLARE_READER
449 };
450 
451 
452 }
453 
454 #ifdef _MSC_VER
455 # pragma warning(pop)
456 #endif
457 
458 #endif
unorm8
Definition: unorm8.h:33
BinaryInput & operator=(const BinaryInput &)
Definition: Vector2.h:40
void reset()
Definition: BinaryInput.h:263
G3DEndian m_fileEndian
Definition: BinaryInput.h:86
BinaryInput(const BinaryInput &)
int8 readInt8()
Definition: BinaryInput.h:269
std::string readFixedLengthString(int numBytes)
Definition: BinaryInput.cpp:228
int8_t int8
Definition: Define.h:148
uint8 * m_buffer
Definition: BinaryInput.h:117
float64 readFloat64()
Definition: BinaryInput.h:361
static const int64 INITIAL_BUFFER_LENGTH
Definition: BinaryInput.h:75
int16_t int16
Definition: g3dmath.h:165
void setPosition(int64 p)
Definition: BinaryInput.h:252
void skip(int64 n)
Definition: BinaryInput.h:410
int64_t int64
Definition: Define.h:145
Definition: BinaryInput.h:69
bool readBool8()
Definition: BinaryInput.h:274
std::string readStringNewline()
Definition: BinaryInput.cpp:415
double float64
Definition: g3dmath.h:173
int8_t int8
Definition: g3dmath.h:163
int32 readInt32()
Definition: BinaryInput.h:340
static unorm16 fromBits(uint16 b)
Definition: unorm16.h:43
Definition: AABox.h:25
int64 getPosition() const
Definition: BinaryInput.h:233
virtual ~BinaryInput()
Definition: BinaryInput.cpp:219
Vector4 readVector4()
Definition: BinaryInput.cpp:479
std::string readString()
Definition: BinaryInput.cpp:384
void decompress()
Definition: BinaryInput.cpp:242
uint64_t uint64
Definition: g3dmath.h:170
G3DEndian endian() const
Definition: BinaryInput.h:199
float32 readFloat32()
Definition: BinaryInput.h:352
#define debugAssertM(exp, message)
Definition: debugAssert.h:161
int m_bitPos
Definition: BinaryInput.h:92
int64 m_length
Definition: BinaryInput.h:113
uint16_t uint16
Definition: g3dmath.h:166
short Int16
Definition: bzlib_private.h:46
Vector2 readVector2()
Definition: BinaryInput.cpp:496
int16 readInt16()
Definition: BinaryInput.h:309
Definition: Vector3.h:58
void setEndian(G3DEndian endian)
Definition: BinaryInput.cpp:272
std::string readStringEven()
Definition: BinaryInput.cpp:464
bool m_freeBuffer
Definition: BinaryInput.h:127
void beginBits()
Definition: BinaryInput.cpp:520
uint8 readUInt8()
Definition: BinaryInput.h:278
bool operator==(const BinaryInput &)
void loadIntoMemory(int64 startPosition, int64 minLength=0)
Definition: BinaryInput.cpp:278
int64 m_bufferLength
Definition: BinaryInput.h:116
void readBytes(void *bytes, int64 n)
Definition: BinaryInput.cpp:336
uint32 readUInt32()
Definition: BinaryInput.h:314
int64 m_pos
Definition: BinaryInput.h:122
#define DECLARE_READER(ucase, lcase)
Definition: BinaryInput.h:432
float float32
Definition: g3dmath.h:172
int32_t int32
Definition: Define.h:146
uint32_t uint32
Definition: Define.h:150
unsigned int UInt32
Definition: bzlib_private.h:45
uint64_t uint64
Definition: Define.h:149
uint16_t uint16
Definition: Define.h:151
Definition: Vector4.h:39
void prepareToRead(int64 nbytes)
Definition: BinaryInput.cpp:326
bool m_swapBytes
Definition: BinaryInput.h:89
static const bool NO_COPY
Definition: BinaryInput.h:147
uint32 readBits(int numBits)
Definition: BinaryInput.cpp:530
G3DEndian
The order in which the bytes of an integer are stored on a machine.
Definition: System.h:48
int64 getLength() const
Definition: BinaryInput.h:221
const uint8 * getCArray()
Definition: BinaryInput.h:241
Color4 readColor4()
Definition: BinaryInput.cpp:503
bool hasMore() const
Definition: BinaryInput.h:417
int64_t int64
Definition: g3dmath.h:169
Color3 readColor3()
Definition: BinaryInput.cpp:512
std::string readString32()
Definition: BinaryInput.cpp:473
uint8_t uint8
Definition: g3dmath.h:164
int Int32
Definition: bzlib_private.h:44
int64 m_alreadyRead
Definition: BinaryInput.h:107
Definition: Color3.h:33
std::string getFilename() const
Definition: BinaryInput.h:203
uint64 readUInt64()
Definition: BinaryInput.cpp:346
int32_t int32
Definition: g3dmath.h:167
uint8_t uint8
Definition: Define.h:152
int m_beginEndBits
Definition: BinaryInput.h:101
unorm8 readUNorm8()
Definition: BinaryInput.h:283
Vector3 readVector3()
Definition: BinaryInput.cpp:488
int16_t int16
Definition: Define.h:147
uint32_t uint32
Definition: g3dmath.h:168
void endBits()
Definition: BinaryInput.cpp:558
uint16 readUInt16()
Definition: BinaryInput.h:287
unsigned short UInt16
Definition: bzlib_private.h:47
int64 size() const
Definition: BinaryInput.h:225
int64 readInt64()
Definition: BinaryInput.h:347
std::string m_filename
Definition: BinaryInput.h:87
uint32 m_bitString
Definition: BinaryInput.h:98
Definition: Color4.h:33
uint8 operator[](int64 n)
Definition: BinaryInput.h:213