CUGL
Cornell University Game Library
CUBinaryWriter.h
1 //
2 // CUBinaryWriter.h
3 // Cornell University Game Library (CUGL)
4 //
5 // This module provides a simple Java-style writer for encoding binary files.
6 // All data is marshalled to network order, ensuring that the files are the
7 // same across multiple platforms.
8 //
9 // Note that this writer does not refer to the integral types as short, int,
10 // long, etc. Those types are NOT cross-platform. For example, a long is
11 // 8 bytes on Unix/OS X, but 4 bytes on Win32 platforms.
12 //
13 // By default, this module (and every module in the io package) accesses the
14 // application save directory. If you want to access another directory, you
15 // will need to specify an absolute path for the file name. Keep in mind that
16 // absolute paths are very dangerous on mobile devices, because they do not
17 // have proper file systems. You should confine all files to either the asset
18 // or the save directory.
19 //
20 // This class uses our standard shared-pointer architecture.
21 //
22 // 1. The constructor does not perform any initialization; it just sets all
23 // attributes to their defaults.
24 //
25 // 2. All initialization takes place via init methods, which can fail if an
26 // object is initialized more than once.
27 //
28 // 3. All allocation takes place via static constructors which return a shared
29 // pointer.
30 //
31 // CUGL zlib License:
32 // This software is provided 'as-is', without any express or implied
33 // warranty. In no event will the authors be held liable for any damages
34 // arising from the use of this software.
35 //
36 // Permission is granted to anyone to use this software for any purpose,
37 // including commercial applications, and to alter it and redistribute it
38 // freely, subject to the following restrictions:
39 //
40 // 1. The origin of this software must not be misrepresented; you must not
41 // claim that you wrote the original software. If you use this software
42 // in a product, an acknowledgment in the product documentation would be
43 // appreciated but is not required.
44 //
45 // 2. Altered source versions must be plainly marked as such, and must not
46 // be misrepresented as being the original software.
47 //
48 // 3. This notice may not be removed or altered from any source distribution.
49 //
50 // Author: Walker White
51 // Version: 11/28/16
52 //
53 #ifndef __CU_BINARY_WRITER_H__
54 #define __CU_BINARY_WRITER_H__
55 #include <SDL/SDL.h>
56 #include <cugl/io/CUPathname.h>
57 #include <string>
58 
59 namespace cugl {
60 
79 class BinaryWriter {
80 protected:
82  std::string _name;
84  SDL_RWops* _stream;
85 
87  char* _cbuffer;
89  Uint32 _capacity;
91  Sint32 _bufoff;
92 
93 
94 #pragma mark -
95 #pragma mark Constructors
96 public:
103  BinaryWriter() : _name(""), _stream(nullptr), _cbuffer(nullptr), _bufoff(-1) {}
104 
111 
128  bool init(const std::string& file) {
129  return init(Pathname(file));
130  }
131 
148  bool init(const char* file) {
149  return init(Pathname(file));
150  }
151 
168  bool init(const Pathname& file);
169 
184  bool init(const std::string& file, unsigned int capacity) {
185  return init(Pathname(file),capacity);
186  }
187 
202  bool init(const char* file, unsigned int capacity) {
203  return init(Pathname(file),capacity);
204  }
205 
220  bool init(const Pathname& file, unsigned int capacity);
221 
222 
223 #pragma mark -
224 #pragma mark Static Constructors
225 
241  static std::shared_ptr<BinaryWriter> alloc(const std::string& file) {
242  std::shared_ptr<BinaryWriter> result = std::make_shared<BinaryWriter>();
243  return (result->init(file) ? result : nullptr);
244  }
245 
262  static std::shared_ptr<BinaryWriter> alloc(const char* file) {
263  std::shared_ptr<BinaryWriter> result = std::make_shared<BinaryWriter>();
264  return (result->init(file) ? result : nullptr);
265  }
266 
283  static std::shared_ptr<BinaryWriter> alloc(const Pathname& file) {
284  std::shared_ptr<BinaryWriter> result = std::make_shared<BinaryWriter>();
285  return (result->init(file) ? result : nullptr);
286  }
287 
302  static std::shared_ptr<BinaryWriter> alloc(const std::string& file, unsigned int capacity) {
303  std::shared_ptr<BinaryWriter> result = std::make_shared<BinaryWriter>();
304  return (result->init(file,capacity) ? result : nullptr);
305  }
306 
321  static std::shared_ptr<BinaryWriter> alloc(const char* file, unsigned int capacity) {
322  std::shared_ptr<BinaryWriter> result = std::make_shared<BinaryWriter>();
323  return (result->init(file,capacity) ? result : nullptr);
324  }
325 
340  static std::shared_ptr<BinaryWriter> alloc(const Pathname& file, unsigned int capacity) {
341  std::shared_ptr<BinaryWriter> result = std::make_shared<BinaryWriter>();
342  return (result->init(file,capacity) ? result : nullptr);
343  }
344 
345 
346 #pragma mark -
347 #pragma mark Stream Management
348 
354  void flush();
355 
363  void close();
364 
365 
366 #pragma mark -
367 #pragma mark Single Element Writes
368 
377  void write(char c);
378 
388  void writeUint8(Uint8 c);
389 
402  void writeSint16(Sint16 n);
403 
416  void writeUint16(Uint16 n);
417 
430  void writeSint32(Sint32 n);
431 
444  void writeUint32(Uint32 n);
445 
458  void writeSint64(Sint64 n);
459 
472  void writeUint64(Uint64 n);
473 
486  void writeFloat(float n);
487 
500  void writeDouble(double n);
501 
502 
503 #pragma mark -
504 #pragma mark Array Writes
505 
516  void write(const char* array, size_t length, size_t offset=0);
517 
529  void write(const Uint8* array, size_t length, size_t offset=0);
530 
545  void write(const Sint16* array, size_t length, size_t offset=0);
546 
561  void write(const Uint16* array, size_t length, size_t offset=0);
562 
577  void write(const Sint32* array, size_t length, size_t offset=0);
578 
593  void write(const Uint32* array, size_t length, size_t offset=0);
594 
609  void write(const Sint64* array, size_t length, size_t offset=0);
610 
625  void write(const Uint64* array, size_t length, size_t offset=0);
626 
641  void write(const float* array, size_t length, size_t offset=0);
642 
657  void write(const double* array, size_t length, size_t offset=0);
658 
659 };
660 
661 }
662 
663 #endif /* __CU_BINARY_WRITER_H__ */
static std::shared_ptr< BinaryWriter > alloc(const std::string &file)
Definition: CUBinaryWriter.h:241
void writeUint64(Uint64 n)
std::string _name
Definition: CUBinaryWriter.h:82
static std::shared_ptr< BinaryWriter > alloc(const char *file)
Definition: CUBinaryWriter.h:262
bool init(const char *file, unsigned int capacity)
Definition: CUBinaryWriter.h:202
void writeUint32(Uint32 n)
static std::shared_ptr< BinaryWriter > alloc(const Pathname &file)
Definition: CUBinaryWriter.h:283
void writeFloat(float n)
static std::shared_ptr< BinaryWriter > alloc(const std::string &file, unsigned int capacity)
Definition: CUBinaryWriter.h:302
void writeUint8(Uint8 c)
void writeSint16(Sint16 n)
static std::shared_ptr< BinaryWriter > alloc(const char *file, unsigned int capacity)
Definition: CUBinaryWriter.h:321
void writeUint16(Uint16 n)
Uint32 _capacity
Definition: CUBinaryWriter.h:89
BinaryWriter()
Definition: CUBinaryWriter.h:103
char * _cbuffer
Definition: CUBinaryWriter.h:87
bool init(const std::string &file)
Definition: CUBinaryWriter.h:128
bool init(const char *file)
Definition: CUBinaryWriter.h:148
void writeDouble(double n)
~BinaryWriter()
Definition: CUBinaryWriter.h:110
SDL_RWops * _stream
Definition: CUBinaryWriter.h:84
bool init(const std::string &file, unsigned int capacity)
Definition: CUBinaryWriter.h:184
Sint32 _bufoff
Definition: CUBinaryWriter.h:91
static std::shared_ptr< BinaryWriter > alloc(const Pathname &file, unsigned int capacity)
Definition: CUBinaryWriter.h:340
void write(char c)
Definition: CUAnimationNode.h:52
Definition: CUBinaryWriter.h:79
void writeSint32(Sint32 n)
Definition: CUPathname.h:85
void writeSint64(Sint64 n)