CUGL 1.3
Cornell University Game Library
All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends
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 MIT 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 <cugl/base/CUBase.h>
56 #include <SDL/SDL.h>
57 #include <cugl/io/CUPathname.h>
58 #include <string>
59 
60 namespace cugl {
61 
80 class BinaryWriter {
81 protected:
83  std::string _name;
85  SDL_RWops* _stream;
86 
88  char* _cbuffer;
90  Uint32 _capacity;
92  Sint32 _bufoff;
93 
94 
95 #pragma mark -
96 #pragma mark Constructors
97 public:
104  BinaryWriter() : _name(""), _stream(nullptr), _cbuffer(nullptr), _bufoff(-1) {}
105 
112 
129  bool init(const std::string& file) {
130  return init(Pathname(file));
131  }
132 
149  bool init(const char* file) {
150  return init(Pathname(file));
151  }
152 
169  bool init(const Pathname& file);
170 
185  bool init(const std::string& file, unsigned int capacity) {
186  return init(Pathname(file),capacity);
187  }
188 
203  bool init(const char* file, unsigned int capacity) {
204  return init(Pathname(file),capacity);
205  }
206 
221  bool init(const Pathname& file, unsigned int capacity);
222 
223 
224 #pragma mark -
225 #pragma mark Static Constructors
226 
242  static std::shared_ptr<BinaryWriter> alloc(const std::string& file) {
243  std::shared_ptr<BinaryWriter> result = std::make_shared<BinaryWriter>();
244  return (result->init(file) ? result : nullptr);
245  }
246 
263  static std::shared_ptr<BinaryWriter> alloc(const char* file) {
264  std::shared_ptr<BinaryWriter> result = std::make_shared<BinaryWriter>();
265  return (result->init(file) ? result : nullptr);
266  }
267 
284  static std::shared_ptr<BinaryWriter> alloc(const Pathname& file) {
285  std::shared_ptr<BinaryWriter> result = std::make_shared<BinaryWriter>();
286  return (result->init(file) ? result : nullptr);
287  }
288 
303  static std::shared_ptr<BinaryWriter> alloc(const std::string& file, unsigned int capacity) {
304  std::shared_ptr<BinaryWriter> result = std::make_shared<BinaryWriter>();
305  return (result->init(file,capacity) ? result : nullptr);
306  }
307 
322  static std::shared_ptr<BinaryWriter> alloc(const char* file, unsigned int capacity) {
323  std::shared_ptr<BinaryWriter> result = std::make_shared<BinaryWriter>();
324  return (result->init(file,capacity) ? result : nullptr);
325  }
326 
341  static std::shared_ptr<BinaryWriter> alloc(const Pathname& file, unsigned int capacity) {
342  std::shared_ptr<BinaryWriter> result = std::make_shared<BinaryWriter>();
343  return (result->init(file,capacity) ? result : nullptr);
344  }
345 
346 
347 #pragma mark -
348 #pragma mark Stream Management
349 
355  void flush();
356 
364  void close();
365 
366 
367 #pragma mark -
368 #pragma mark Single Element Writes
369 
378  void write(char c);
379 
389  void writeUint8(Uint8 c);
390 
403  void writeSint16(Sint16 n);
404 
417  void writeUint16(Uint16 n);
418 
431  void writeSint32(Sint32 n);
432 
445  void writeUint32(Uint32 n);
446 
459  void writeSint64(Sint64 n);
460 
473  void writeUint64(Uint64 n);
474 
487  void writeFloat(float n);
488 
501  void writeDouble(double n);
502 
503 
504 #pragma mark -
505 #pragma mark Array Writes
506 
517  void write(const char* array, size_t length, size_t offset=0);
518 
530  void write(const Uint8* array, size_t length, size_t offset=0);
531 
546  void write(const Sint16* array, size_t length, size_t offset=0);
547 
562  void write(const Uint16* array, size_t length, size_t offset=0);
563 
578  void write(const Sint32* array, size_t length, size_t offset=0);
579 
594  void write(const Uint32* array, size_t length, size_t offset=0);
595 
610  void write(const Sint64* array, size_t length, size_t offset=0);
611 
626  void write(const Uint64* array, size_t length, size_t offset=0);
627 
642  void write(const float* array, size_t length, size_t offset=0);
643 
658  void write(const double* array, size_t length, size_t offset=0);
659 
660 };
661 
662 }
663 
664 #endif /* __CU_BINARY_WRITER_H__ */
cugl::BinaryWriter::alloc
static std::shared_ptr< BinaryWriter > alloc(const Pathname &file, unsigned int capacity)
Definition: CUBinaryWriter.h:341
cugl::BinaryWriter::writeSint32
void writeSint32(Sint32 n)
cugl::BinaryWriter::_stream
SDL_RWops * _stream
Definition: CUBinaryWriter.h:85
cugl::BinaryWriter::flush
void flush()
cugl::BinaryWriter::writeUint64
void writeUint64(Uint64 n)
cugl::BinaryWriter::alloc
static std::shared_ptr< BinaryWriter > alloc(const Pathname &file)
Definition: CUBinaryWriter.h:284
cugl::BinaryWriter::writeUint8
void writeUint8(Uint8 c)
cugl::BinaryWriter::write
void write(char c)
cugl::BinaryWriter::init
bool init(const std::string &file)
Definition: CUBinaryWriter.h:129
cugl::BinaryWriter::_bufoff
Sint32 _bufoff
Definition: CUBinaryWriter.h:92
cugl::BinaryWriter::init
bool init(const char *file, unsigned int capacity)
Definition: CUBinaryWriter.h:203
cugl::BinaryWriter::alloc
static std::shared_ptr< BinaryWriter > alloc(const char *file, unsigned int capacity)
Definition: CUBinaryWriter.h:322
cugl::BinaryWriter::BinaryWriter
BinaryWriter()
Definition: CUBinaryWriter.h:104
cugl::BinaryWriter::writeSint64
void writeSint64(Sint64 n)
cugl::Pathname
Definition: CUPathname.h:85
cugl::BinaryWriter::~BinaryWriter
~BinaryWriter()
Definition: CUBinaryWriter.h:111
cugl::BinaryWriter::writeFloat
void writeFloat(float n)
cugl::BinaryWriter
Definition: CUBinaryWriter.h:80
cugl::BinaryWriter::alloc
static std::shared_ptr< BinaryWriter > alloc(const std::string &file, unsigned int capacity)
Definition: CUBinaryWriter.h:303
cugl::BinaryWriter::close
void close()
cugl::BinaryWriter::init
bool init(const std::string &file, unsigned int capacity)
Definition: CUBinaryWriter.h:185
cugl::BinaryWriter::writeSint16
void writeSint16(Sint16 n)
cugl::BinaryWriter::init
bool init(const char *file)
Definition: CUBinaryWriter.h:149
cugl::BinaryWriter::writeUint32
void writeUint32(Uint32 n)
cugl::BinaryWriter::alloc
static std::shared_ptr< BinaryWriter > alloc(const std::string &file)
Definition: CUBinaryWriter.h:242
cugl::BinaryWriter::writeUint16
void writeUint16(Uint16 n)
cugl::BinaryWriter::writeDouble
void writeDouble(double n)
cugl::BinaryWriter::alloc
static std::shared_ptr< BinaryWriter > alloc(const char *file)
Definition: CUBinaryWriter.h:263
cugl::BinaryWriter::_cbuffer
char * _cbuffer
Definition: CUBinaryWriter.h:88
cugl::BinaryWriter::_capacity
Uint32 _capacity
Definition: CUBinaryWriter.h:90
cugl::BinaryWriter::_name
std::string _name
Definition: CUBinaryWriter.h:83