CUGL
Cornell University Game Library
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 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_READER_H__
54 #define __CU_BINARY_READER_H__
55 #include <SDL/SDL.h>
56 #include <cugl/io/CUPathname.h>
57 #include <string>
58 
59 namespace cugl {
60 
79 class BinaryReader {
80 protected:
82  std::string _name;
84  SDL_RWops* _stream;
86  Sint64 _ssize;
88  Sint64 _scursor;
89 
91  char* _buffer;
93  Uint32 _capacity;
95  Uint32 _bufsize;
97  Sint32 _bufoff;
98 
99 #pragma mark -
100 #pragma mark Internal Methods
101 
109  void fill(unsigned int bytes=1);
110 
111 
112 #pragma mark -
113 #pragma mark Constructors
114 public:
121  BinaryReader() : _name(""), _stream(nullptr), _ssize(-1), _scursor(-1),
122  _buffer(nullptr), _capacity(0), _bufoff(-1), _bufsize(0) {}
123 
130 
146  bool init(const std::string& file) {
147  return init(Pathname(file));
148  }
149 
165  bool init(const char* file) {
166  return init(Pathname(file));
167  }
168 
184  bool init(const Pathname& file);
185 
199  bool init(const std::string& file, unsigned int capacity) {
200  return init(Pathname(file),capacity);
201  }
202 
216  bool init(const char* file, unsigned int capacity) {
217  return init(Pathname(file),capacity);
218  }
219 
233  bool init(const Pathname& file, unsigned int capacity);
234 
249  bool initWithAsset(const std::string& file) {
250  return init(file.c_str());
251  }
252 
267  bool initWithAsset(const char* file);
268 
281  bool initWithAsset(const std::string& file, unsigned int capacity) {
282  return init(file.c_str(),capacity);
283  }
284 
297  bool initWithAsset(const char* file, unsigned int capacity);
298 
299 
300 #pragma mark -
301 #pragma mark Static Constructors
302 
317  static std::shared_ptr<BinaryReader> alloc(const std::string& file) {
318  std::shared_ptr<BinaryReader> result = std::make_shared<BinaryReader>();
319  return (result->init(file) ? result : nullptr);
320  }
321 
337  static std::shared_ptr<BinaryReader> alloc(const char* file) {
338  std::shared_ptr<BinaryReader> result = std::make_shared<BinaryReader>();
339  return (result->init(file) ? result : nullptr);
340  }
341 
355  static std::shared_ptr<BinaryReader> alloc(const std::string& file, unsigned int capacity) {
356  std::shared_ptr<BinaryReader> result = std::make_shared<BinaryReader>();
357  return (result->init(file,capacity) ? result : nullptr);
358  }
359 
373  static std::shared_ptr<BinaryReader> alloc(const char* file, unsigned int capacity) {
374  std::shared_ptr<BinaryReader> result = std::make_shared<BinaryReader>();
375  return (result->init(file,capacity) ? result : nullptr);
376  }
377 
392  static std::shared_ptr<BinaryReader> allocWithAsset(const std::string& file) {
393  std::shared_ptr<BinaryReader> result = std::make_shared<BinaryReader>();
394  return (result->initWithAsset(file) ? result : nullptr);
395  }
396 
411  static std::shared_ptr<BinaryReader> allocWithAsset(const char* file) {
412  std::shared_ptr<BinaryReader> result = std::make_shared<BinaryReader>();
413  return (result->initWithAsset(file) ? result : nullptr);
414  }
415 
429  static std::shared_ptr<BinaryReader> allocWithAsset(const std::string& file, unsigned int capacity) {
430  std::shared_ptr<BinaryReader> result = std::make_shared<BinaryReader>();
431  return (result->initWithAsset(file,capacity) ? result : nullptr);
432  }
433 
447  static std::shared_ptr<BinaryReader> allocWithAsset(const char* file, unsigned int capacity) {
448  std::shared_ptr<BinaryReader> result = std::make_shared<BinaryReader>();
449  return (result->initWithAsset(file,capacity) ? result : nullptr);
450  }
451 
452 
453 #pragma mark -
454 #pragma mark Stream Management
455 
461  void reset();
462 
469  void close();
470 
481  bool ready(unsigned int bytes=1) const;
482 
483 
484 #pragma mark -
485 #pragma mark Single Element Reads
486 
491  char readChar();
492 
498  Uint8 readByte();
499 
508  Sint16 readSint16();
509 
518  Uint16 readUint16();
519 
528  Sint32 readSint32();
529 
538  Uint32 readUint32();
539 
548  Sint64 readSint64();
549 
558  Uint64 readUint64();
559 
568  float readFloat();
569 
578  double readDouble();
579 
580 
581 #pragma mark -
582 #pragma mark Array Reads
583 
595  size_t read(char* buffer, size_t maximum, size_t offset=0);
596 
609  size_t read(Uint8* buffer, size_t maximum, size_t offset=0);
610 
626  size_t read(Sint16* buffer, size_t maximum, size_t offset=0);
627 
643  size_t read(Uint16* buffer, size_t maximum, size_t offset=0);
644 
660  size_t read(Sint32* buffer, size_t maximum, size_t offset=0);
661 
677  size_t read(Uint32* buffer, size_t maximum, size_t offset=0);
678 
694  size_t read(Sint64* buffer, size_t maximum, size_t offset=0);
695 
711  size_t read(Uint64* buffer, size_t maximum, size_t offset=0);
712 
728  size_t read(float* buffer, size_t maximum, size_t offset=0);
729 
745  size_t read(double* buffer, size_t maximum, size_t offset=0);
746 };
747 
748 }
749 #endif /* __CU_BINARY_READER_H__ */
Uint32 _bufsize
Definition: CUBinaryReader.h:95
static std::shared_ptr< BinaryReader > alloc(const std::string &file)
Definition: CUBinaryReader.h:317
Sint64 _scursor
Definition: CUBinaryReader.h:88
bool ready(unsigned int bytes=1) const
~BinaryReader()
Definition: CUBinaryReader.h:129
bool init(const std::string &file, unsigned int capacity)
Definition: CUBinaryReader.h:199
void fill(unsigned int bytes=1)
static std::shared_ptr< BinaryReader > allocWithAsset(const std::string &file)
Definition: CUBinaryReader.h:392
Uint32 _capacity
Definition: CUBinaryReader.h:93
bool initWithAsset(const std::string &file)
Definition: CUBinaryReader.h:249
bool init(const char *file)
Definition: CUBinaryReader.h:165
bool initWithAsset(const std::string &file, unsigned int capacity)
Definition: CUBinaryReader.h:281
SDL_RWops * _stream
Definition: CUBinaryReader.h:84
BinaryReader()
Definition: CUBinaryReader.h:121
static std::shared_ptr< BinaryReader > allocWithAsset(const char *file, unsigned int capacity)
Definition: CUBinaryReader.h:447
static std::shared_ptr< BinaryReader > allocWithAsset(const char *file)
Definition: CUBinaryReader.h:411
size_t read(char *buffer, size_t maximum, size_t offset=0)
Sint64 _ssize
Definition: CUBinaryReader.h:86
static std::shared_ptr< BinaryReader > alloc(const char *file, unsigned int capacity)
Definition: CUBinaryReader.h:373
static std::shared_ptr< BinaryReader > alloc(const std::string &file, unsigned int capacity)
Definition: CUBinaryReader.h:355
std::string _name
Definition: CUBinaryReader.h:82
Definition: CUAnimationNode.h:52
Sint32 _bufoff
Definition: CUBinaryReader.h:97
bool init(const char *file, unsigned int capacity)
Definition: CUBinaryReader.h:216
Definition: CUPathname.h:85
bool init(const std::string &file)
Definition: CUBinaryReader.h:146
Definition: CUBinaryReader.h:79
char * _buffer
Definition: CUBinaryReader.h:91
static std::shared_ptr< BinaryReader > alloc(const char *file)
Definition: CUBinaryReader.h:337
static std::shared_ptr< BinaryReader > allocWithAsset(const std::string &file, unsigned int capacity)
Definition: CUBinaryReader.h:429