TrinityCore
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
BinaryOutput.h
Go to the documentation of this file.
1 
13 #ifndef G3D_BinaryOutput_h
14 #define G3D_BinaryOutput_h
15 
16 #include "G3D/platform.h"
17 #include <assert.h>
18 #include <string>
19 #include <sys/stat.h>
20 #include <sys/types.h>
21 #include <stdio.h>
22 #include "G3D/unorm8.h"
23 #include "G3D/Color4.h"
24 #include "G3D/Color3.h"
25 #include "G3D/Vector4.h"
26 #include "G3D/Vector3.h"
27 #include "G3D/Vector2.h"
28 #include "G3D/g3dmath.h"
29 #include "G3D/debug.h"
30 #include "G3D/BinaryInput.h"
31 #include "G3D/System.h"
32 
33 #ifdef _MSC_VER
34 # pragma warning (push)
35 // Conditional is constant (wrong in inline)
36 # pragma warning (disable : 4127)
37 #endif
38 namespace G3D {
39 
52 class BinaryOutput {
53 private:
54  std::string m_filename;
55 
57 
60 
65 
67  int m_bitPos;
68 
69  // True if the file endianess does not match the machine endian
71 
73 
75 
77  size_t m_bufferLen;
78 
81 
84 
86  bool m_init;
87 
90 
91  bool m_ok;
92 
93  void reserveBytesWhenOutOfMemory(size_t bytes);
94 
95  void reallocBuffer(size_t bytes, size_t oldBufferLen);
96 
101  inline void reserveBytes(size_t bytes) {
102  debugAssert(bytes > 0);
103  size_t oldBufferLen = m_bufferLen;
104 
105  m_bufferLen = max(m_bufferLen, (size_t)(m_pos + bytes));
106  if (m_bufferLen > m_maxBufferLen) {
107  reallocBuffer(bytes, oldBufferLen);
108  }
109  }
110 
111  // Not implemented on purpose, don't use
112  BinaryOutput(const BinaryOutput&);
114  bool operator==(const BinaryOutput&);
115 
116 public:
117 
121  BinaryOutput();
122 
128  BinaryOutput(
129  const std::string& filename,
130  G3DEndian fileEndian);
131 
132  ~BinaryOutput();
133 
145  void compress(int level = 9);
146 
148  bool ok() const;
149 
153  inline const uint8* getCArray() const {
154  return m_buffer;
155  }
156 
157  void setEndian(G3DEndian fileEndian);
158 
159  G3DEndian endian() const {
160  return m_fileEndian;
161  }
162 
163  std::string getFilename() const {
164  return m_filename;
165  }
166 
180  void commit(bool flush = true);
181 
186  void commit(uint8*);
187 
193  void reset();
194 
195 
196  inline int64 length() const {
197  return m_bufferLen + m_alreadyWritten;
198  }
199 
200  inline int64 size() const {
201  return length();
202  }
203 
213  inline void setLength(int64 n) {
214  n = n - m_alreadyWritten;
215 
216  if (n < 0) {
217  throw "Cannot resize huge files to be shorter.";
218  }
219 
220  if (n < (int64)m_bufferLen) {
221  m_pos = n;
222  }
223  if (n > (int64)m_bufferLen) {
224  reserveBytes((size_t)(n - m_bufferLen));
225  }
226  }
227 
232  inline int64 position() const {
233  return m_pos + m_alreadyWritten;
234  }
235 
236 
244  inline void setPosition(int64 p) {
245  p = p - m_alreadyWritten;
246 
247  if (p > (int64)m_bufferLen) {
248  setLength((int)(p + (int64)m_alreadyWritten));
249  }
250 
251  if (p < 0) {
252  throw "Cannot seek more than 10 MB backwards on huge files.";
253  }
254 
255  m_pos = (int)p;
256  }
257 
258 
259  void writeBytes
260  (const void* b,
261  size_t count) {
262 
263  reserveBytes(count);
264  debugAssert(m_pos >= 0);
265  debugAssert(m_bufferLen >= count);
266  System::memcpy(m_buffer + m_pos, b, count);
267  m_pos += count;
268  }
269 
273  inline void writeInt8(int8 i) {
274  reserveBytes(1);
275  m_buffer[m_pos] = *(uint8*)&i;
276  ++m_pos;
277  }
278 
279  inline void writeBool8(bool b) {
280  writeInt8(b ? 1 : 0);
281  }
282 
283  inline void writeUInt8(uint8 i) {
284  reserveBytes(1);
285  m_buffer[m_pos] = i;
286  ++m_pos;
287  }
288 
289  inline void writeUNorm8(unorm8 i) {
290  writeUInt8(i.bits());
291  }
292 
293  void writeUInt16(uint16 u);
294 
295  inline void writeInt16(int16 i) {
296  writeUInt16(*(uint16*)&i);
297  }
298 
299  void writeUInt32(uint32 u);
300 
301  inline void writeInt32(int32 i) {
302  debugAssert(m_beginEndBits == 0);
303  writeUInt32(*(uint32*)&i);
304  }
305 
306  void writeUInt64(uint64 u);
307 
308  inline void writeInt64(int64 i) {
309  writeUInt64(*(uint64*)&i);
310  }
311 
312  inline void writeFloat32(float32 f) {
313  debugAssert(m_beginEndBits == 0);
314  union {
315  float32 a;
316  uint32 b;
317  };
318  a = f;
319  writeUInt32(b);
320  }
321 
322  inline void writeFloat64(float64 f) {
323  union {
324  float64 a;
325  uint64 b;
326  };
327  a = f;
328  writeUInt64(b);
329  }
330 
334  inline void writeString(const std::string& s) {
335  writeString(s.c_str());
336  }
337 
339  inline void writeString(const std::string& s, int len) {
340  const int pad = len - ((int)s.length() + 1);
341  if (pad >= 0) {
342  writeString(s.c_str());
343  for (int i = 0; i < pad; ++i) {
344  writeUInt8(0);
345  }
346  } else {
347  // Truncate
348  writeBytes(s.c_str(), len);
349  }
350  }
351 
352  void writeString(const char* s);
353 
358  void writeStringEven(const std::string& s) {
359  writeStringEven(s.c_str());
360  }
361 
362  void writeStringEven(const char* s);
363 
364  void writeString32(const char* s);
365 
370  void writeString32(const std::string& s) {
371  writeString32(s.c_str());
372  }
373 
374  void writeVector4(const Vector4& v);
375 
376  void writeVector3(const Vector3& v);
377 
378  void writeVector2(const Vector2& v);
379 
380  void writeColor4(const Color4& v);
381 
382  void writeColor3(const Color3& v);
383 
387  inline void skip(int n) {
388  if (m_pos + n > (int64)m_bufferLen) {
389  setLength(m_pos + m_alreadyWritten + n);
390  }
391  m_pos += n;
392  }
393 
396  void beginBits();
397 
408  void writeBits(uint32 bitString, int numBits);
409 
412  void endBits();
413 
414 
415 # define DECLARE_WRITER(ucase, lcase)\
416  void write##ucase(const lcase* out, int n);\
417  void write##ucase(const std::vector<lcase>& out, int n);\
418  void write##ucase(const Array<lcase>& out, int n);
419 
420  DECLARE_WRITER(Bool8, bool)
421  DECLARE_WRITER(UInt8, uint8)
422  DECLARE_WRITER(Int8, int8)
427  DECLARE_WRITER(UInt64, uint64)
428  DECLARE_WRITER(Int64, int64)
429  DECLARE_WRITER(Float32, float32)
430  DECLARE_WRITER(Float64, float64)
431 # undef DECLARE_WRITER
432 
433 };
434 
435 }
436 
437 #ifdef _MSC_VER
438 # pragma warning (pop)
439 #endif
440 
441 #endif
unorm8
Definition: unorm8.h:33
void setLength(int64 n)
Definition: BinaryOutput.h:213
Definition: Vector2.h:40
void writeVector4(const Vector4 &v)
Definition: BinaryOutput.cpp:475
BinaryOutput()
Definition: BinaryOutput.cpp:201
void writeColor4(const Color4 &v)
Definition: BinaryOutput.cpp:496
void writeInt32(int32 i)
Definition: BinaryOutput.h:301
int8_t int8
Definition: Define.h:148
void skip(int n)
Definition: BinaryOutput.h:387
int16_t int16
Definition: g3dmath.h:165
void setEndian(G3DEndian fileEndian)
Definition: BinaryOutput.cpp:265
static void memcpy(void *dst, const void *src, size_t numBytes)
Definition: System.cpp:643
bool m_committed
Definition: BinaryOutput.h:56
IntFormatSpec< int, AlignTypeSpec< TYPE_CODE >, Char > pad(int value, unsigned width, Char fill= ' ')
size_t m_bufferLen
Definition: BinaryOutput.h:77
int64_t int64
Definition: Define.h:145
void writeColor3(const Color3 &v)
Definition: BinaryOutput.cpp:504
int m_beginEndBits
Definition: BinaryOutput.h:59
std::string getFilename() const
Definition: BinaryOutput.h:163
void writeString32(const char *s)
Definition: BinaryOutput.cpp:463
void setPosition(int64 p)
Definition: BinaryOutput.h:244
void compress(int level=9)
Definition: BinaryOutput.cpp:276
void writeInt64(int64 i)
Definition: BinaryOutput.h:308
void writeUInt16(uint16 u)
Definition: BinaryOutput.cpp:379
double float64
Definition: g3dmath.h:173
int8_t int8
Definition: g3dmath.h:163
#define DECLARE_WRITER(ucase, lcase)
Definition: BinaryOutput.h:415
int64 size() const
Definition: BinaryOutput.h:200
Definition: AABox.h:25
int64 position() const
Definition: BinaryOutput.h:232
void writeString(const std::string &s)
Definition: BinaryOutput.h:334
void writeInt16(int16 i)
Definition: BinaryOutput.h:295
void writeUInt8(uint8 i)
Definition: BinaryOutput.h:283
size_t m_maxBufferLen
Definition: BinaryOutput.h:80
const uint8 * getCArray() const
Definition: BinaryOutput.h:153
bool m_swapBytes
Definition: BinaryOutput.h:70
void writeString(const std::string &s, int len)
Definition: BinaryOutput.h:339
uint64_t uint64
Definition: g3dmath.h:170
void reset()
Definition: BinaryOutput.cpp:240
G3DEndian m_fileEndian
Definition: BinaryOutput.h:72
uint16_t uint16
Definition: g3dmath.h:166
bool m_ok
Definition: BinaryOutput.h:91
short Int16
Definition: bzlib_private.h:46
int64 m_pos
Definition: BinaryOutput.h:83
T max(const T &x, const T &y)
Definition: g3dmath.h:320
Definition: Vector3.h:58
void writeUInt64(uint64 u)
Definition: BinaryOutput.cpp:415
void writeFloat64(float64 f)
Definition: BinaryOutput.h:322
void reserveBytesWhenOutOfMemory(size_t bytes)
Definition: BinaryOutput.cpp:145
#define debugAssert(exp)
Definition: debugAssert.h:160
float float32
Definition: g3dmath.h:172
bool operator==(const BinaryOutput &)
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
int64 m_alreadyWritten
Definition: BinaryOutput.h:89
void endBits()
Definition: BinaryOutput.cpp:539
G3DEndian
The order in which the bytes of an integer are stored on a machine.
Definition: System.h:48
int m_bitPos
Definition: BinaryOutput.h:67
void writeBytes(const void *b, size_t count)
Definition: BinaryOutput.h:260
int64_t int64
Definition: g3dmath.h:169
void commit(bool flush=true)
Definition: BinaryOutput.cpp:322
std::string m_filename
Definition: BinaryOutput.h:54
uint8_t uint8
Definition: g3dmath.h:164
int Int32
Definition: bzlib_private.h:44
void writeBits(uint32 bitString, int numBits)
Definition: BinaryOutput.cpp:519
void reallocBuffer(size_t bytes, size_t oldBufferLen)
Definition: BinaryOutput.cpp:113
Definition: Color3.h:33
void writeStringEven(const std::string &s)
Definition: BinaryOutput.h:358
BinaryOutput & operator=(const BinaryOutput &)
Definition: BinaryOutput.h:52
int8 m_bitString
Definition: BinaryOutput.h:64
void writeVector2(const Vector2 &v)
Definition: BinaryOutput.cpp:490
~BinaryOutput()
Definition: BinaryOutput.cpp:256
int32_t int32
Definition: g3dmath.h:167
void reserveBytes(size_t bytes)
Definition: BinaryOutput.h:101
uint8_t uint8
Definition: Define.h:152
void writeFloat32(float32 f)
Definition: BinaryOutput.h:312
int16_t int16
Definition: Define.h:147
void writeUInt32(uint32 u)
Definition: BinaryOutput.cpp:395
uint32_t uint32
Definition: g3dmath.h:168
bool ok() const
Definition: BinaryOutput.cpp:271
void beginBits()
Definition: BinaryOutput.cpp:511
unsigned short UInt16
Definition: bzlib_private.h:47
G3DEndian endian() const
Definition: BinaryOutput.h:159
bool m_init
Definition: BinaryOutput.h:86
void writeVector3(const Vector3 &v)
Definition: BinaryOutput.cpp:483
void writeString32(const std::string &s)
Definition: BinaryOutput.h:370
Definition: Color4.h:33
void writeInt8(int8 i)
Definition: BinaryOutput.h:273
uint8 * m_buffer
Definition: BinaryOutput.h:74
void writeUNorm8(unorm8 i)
Definition: BinaryOutput.h:289
void writeBool8(bool b)
Definition: BinaryOutput.h:279
int64 length() const
Definition: BinaryOutput.h:196