CUGL 1.3
Cornell University Game Library
All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends
CUBinaryReader.h
1 //
2 // CUBinaryReader.h
3 // Cornell University Game Library (CUGL)
4 //
5 // This module provides a simple Java-style reader for decoding binary files.
6 // All data is marshalled from network order, ensuring that the files are
7 // supported across multiple platforms.
8 //
9 // Note that this reader 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_READER_H__
54 #define __CU_BINARY_READER_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 BinaryReader {
81 protected:
83  std::string _name;
85  SDL_RWops* _stream;
87  Sint64 _ssize;
89  Sint64 _scursor;
90 
92  char* _buffer;
94  Uint32 _capacity;
96  Uint32 _bufsize;
98  Sint32 _bufoff;
99 
100 #pragma mark -
101 #pragma mark Internal Methods
102 
110  void fill(unsigned int bytes=1);
111 
112 
113 #pragma mark -
114 #pragma mark Constructors
115 public:
122  BinaryReader() : _name(""), _stream(nullptr), _ssize(-1), _scursor(-1),
123  _buffer(nullptr), _capacity(0), _bufoff(-1), _bufsize(0) {}
124 
131 
147  bool init(const std::string& file) {
148  return init(Pathname(file));
149  }
150 
166  bool init(const char* file) {
167  return init(Pathname(file));
168  }
169 
185  bool init(const Pathname& file);
186 
200  bool init(const std::string& file, unsigned int capacity) {
201  return init(Pathname(file),capacity);
202  }
203 
217  bool init(const char* file, unsigned int capacity) {
218  return init(Pathname(file),capacity);
219  }
220 
234  bool init(const Pathname& file, unsigned int capacity);
235 
250  bool initWithAsset(const std::string& file) {
251  return init(file.c_str());
252  }
253 
268  bool initWithAsset(const char* file);
269 
282  bool initWithAsset(const std::string& file, unsigned int capacity) {
283  return init(file.c_str(),capacity);
284  }
285 
298  bool initWithAsset(const char* file, unsigned int capacity);
299 
300 
301 #pragma mark -
302 #pragma mark Static Constructors
303 
318  static std::shared_ptr<BinaryReader> alloc(const std::string& file) {
319  std::shared_ptr<BinaryReader> result = std::make_shared<BinaryReader>();
320  return (result->init(file) ? result : nullptr);
321  }
322 
338  static std::shared_ptr<BinaryReader> alloc(const char* file) {
339  std::shared_ptr<BinaryReader> result = std::make_shared<BinaryReader>();
340  return (result->init(file) ? result : nullptr);
341  }
342 
356  static std::shared_ptr<BinaryReader> alloc(const std::string& file, unsigned int capacity) {
357  std::shared_ptr<BinaryReader> result = std::make_shared<BinaryReader>();
358  return (result->init(file,capacity) ? result : nullptr);
359  }
360 
374  static std::shared_ptr<BinaryReader> alloc(const char* file, unsigned int capacity) {
375  std::shared_ptr<BinaryReader> result = std::make_shared<BinaryReader>();
376  return (result->init(file,capacity) ? result : nullptr);
377  }
378 
393  static std::shared_ptr<BinaryReader> allocWithAsset(const std::string& file) {
394  std::shared_ptr<BinaryReader> result = std::make_shared<BinaryReader>();
395  return (result->initWithAsset(file) ? result : nullptr);
396  }
397 
412  static std::shared_ptr<BinaryReader> allocWithAsset(const char* file) {
413  std::shared_ptr<BinaryReader> result = std::make_shared<BinaryReader>();
414  return (result->initWithAsset(file) ? result : nullptr);
415  }
416 
430  static std::shared_ptr<BinaryReader> allocWithAsset(const std::string& file, unsigned int capacity) {
431  std::shared_ptr<BinaryReader> result = std::make_shared<BinaryReader>();
432  return (result->initWithAsset(file,capacity) ? result : nullptr);
433  }
434 
448  static std::shared_ptr<BinaryReader> allocWithAsset(const char* file, unsigned int capacity) {
449  std::shared_ptr<BinaryReader> result = std::make_shared<BinaryReader>();
450  return (result->initWithAsset(file,capacity) ? result : nullptr);
451  }
452 
453 
454 #pragma mark -
455 #pragma mark Stream Management
456 
462  void reset();
463 
470  void close();
471 
482  bool ready(unsigned int bytes=1) const;
483 
484 
485 #pragma mark -
486 #pragma mark Single Element Reads
487 
492  char readChar();
493 
499  Uint8 readByte();
500 
509  Sint16 readSint16();
510 
519  Uint16 readUint16();
520 
529  Sint32 readSint32();
530 
539  Uint32 readUint32();
540 
549  Sint64 readSint64();
550 
559  Uint64 readUint64();
560 
569  float readFloat();
570 
579  double readDouble();
580 
581 
582 #pragma mark -
583 #pragma mark Array Reads
584 
596  size_t read(char* buffer, size_t maximum, size_t offset=0);
597 
610  size_t read(Uint8* buffer, size_t maximum, size_t offset=0);
611 
627  size_t read(Sint16* buffer, size_t maximum, size_t offset=0);
628 
644  size_t read(Uint16* buffer, size_t maximum, size_t offset=0);
645 
661  size_t read(Sint32* buffer, size_t maximum, size_t offset=0);
662 
678  size_t read(Uint32* buffer, size_t maximum, size_t offset=0);
679 
695  size_t read(Sint64* buffer, size_t maximum, size_t offset=0);
696 
712  size_t read(Uint64* buffer, size_t maximum, size_t offset=0);
713 
729  size_t read(float* buffer, size_t maximum, size_t offset=0);
730 
746  size_t read(double* buffer, size_t maximum, size_t offset=0);
747 };
748 
749 }
750 #endif /* __CU_BINARY_READER_H__ */
cugl::BinaryReader::readUint32
Uint32 readUint32()
cugl::BinaryReader::readByte
Uint8 readByte()
cugl::BinaryReader::BinaryReader
BinaryReader()
Definition: CUBinaryReader.h:122
cugl::BinaryReader::initWithAsset
bool initWithAsset(const std::string &file, unsigned int capacity)
Definition: CUBinaryReader.h:282
cugl::BinaryReader::readUint64
Uint64 readUint64()
cugl::BinaryReader::close
void close()
cugl::BinaryReader::initWithAsset
bool initWithAsset(const std::string &file)
Definition: CUBinaryReader.h:250
cugl::BinaryReader::_buffer
char * _buffer
Definition: CUBinaryReader.h:92
cugl::BinaryReader::read
size_t read(char *buffer, size_t maximum, size_t offset=0)
cugl::BinaryReader::init
bool init(const char *file)
Definition: CUBinaryReader.h:166
cugl::Pathname
Definition: CUPathname.h:85
cugl::BinaryReader::readFloat
float readFloat()
cugl::BinaryReader
Definition: CUBinaryReader.h:80
cugl::BinaryReader::_bufoff
Sint32 _bufoff
Definition: CUBinaryReader.h:98
cugl::BinaryReader::_name
std::string _name
Definition: CUBinaryReader.h:83
cugl::BinaryReader::fill
void fill(unsigned int bytes=1)
cugl::BinaryReader::allocWithAsset
static std::shared_ptr< BinaryReader > allocWithAsset(const std::string &file)
Definition: CUBinaryReader.h:393
cugl::BinaryReader::readDouble
double readDouble()
cugl::BinaryReader::_ssize
Sint64 _ssize
Definition: CUBinaryReader.h:87
cugl::BinaryReader::readUint16
Uint16 readUint16()
cugl::BinaryReader::readSint16
Sint16 readSint16()
cugl::BinaryReader::_bufsize
Uint32 _bufsize
Definition: CUBinaryReader.h:96
cugl::BinaryReader::alloc
static std::shared_ptr< BinaryReader > alloc(const std::string &file, unsigned int capacity)
Definition: CUBinaryReader.h:356
cugl::BinaryReader::_stream
SDL_RWops * _stream
Definition: CUBinaryReader.h:85
cugl::BinaryReader::allocWithAsset
static std::shared_ptr< BinaryReader > allocWithAsset(const char *file)
Definition: CUBinaryReader.h:412
cugl::BinaryReader::alloc
static std::shared_ptr< BinaryReader > alloc(const std::string &file)
Definition: CUBinaryReader.h:318
cugl::BinaryReader::readSint64
Sint64 readSint64()
cugl::BinaryReader::alloc
static std::shared_ptr< BinaryReader > alloc(const char *file, unsigned int capacity)
Definition: CUBinaryReader.h:374
cugl::BinaryReader::~BinaryReader
~BinaryReader()
Definition: CUBinaryReader.h:130
cugl::BinaryReader::alloc
static std::shared_ptr< BinaryReader > alloc(const char *file)
Definition: CUBinaryReader.h:338
cugl::BinaryReader::readChar
char readChar()
cugl::BinaryReader::readSint32
Sint32 readSint32()
cugl::BinaryReader::init
bool init(const char *file, unsigned int capacity)
Definition: CUBinaryReader.h:217
cugl::BinaryReader::allocWithAsset
static std::shared_ptr< BinaryReader > allocWithAsset(const std::string &file, unsigned int capacity)
Definition: CUBinaryReader.h:430
cugl::BinaryReader::init
bool init(const std::string &file, unsigned int capacity)
Definition: CUBinaryReader.h:200
cugl::BinaryReader::_capacity
Uint32 _capacity
Definition: CUBinaryReader.h:94
cugl::BinaryReader::init
bool init(const std::string &file)
Definition: CUBinaryReader.h:147
cugl::BinaryReader::_scursor
Sint64 _scursor
Definition: CUBinaryReader.h:89
cugl::BinaryReader::reset
void reset()
cugl::BinaryReader::allocWithAsset
static std::shared_ptr< BinaryReader > allocWithAsset(const char *file, unsigned int capacity)
Definition: CUBinaryReader.h:448
cugl::BinaryReader::ready
bool ready(unsigned int bytes=1) const