CUGL 1.3
Cornell University Game Library
All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends
CUTextReader.h
1 //
2 // CUTextReader.h
3 // Cornell University Game Library (CUGL)
4 //
5 // This module provides a simple Java-style reader for reading from text files.
6 // It supports both ASCII and UTF8 encoding. No other encodings are supported
7 // (nor should they be since they are not cross-platform).
8 //
9 // By default, this module (and every module in the io package) accesses the
10 // application save directory. If you want to access another directory, you
11 // will need to specify an absolute path for the file name. Keep in mind that
12 // absolute paths are very dangerous on mobile devices, because they do not
13 // have proper file systems. You should confine all files to either the asset
14 // or the save directory.
15 //
16 // This class uses our standard shared-pointer architecture.
17 //
18 // 1. The constructor does not perform any initialization; it just sets all
19 // attributes to their defaults.
20 //
21 // 2. All initialization takes place via init methods, which can fail if an
22 // object is initialized more than once.
23 //
24 // 3. All allocation takes place via static constructors which return a shared
25 // pointer.
26 //
27 // CUGL MIT License:
28 // This software is provided 'as-is', without any express or implied
29 // warranty. In no event will the authors be held liable for any damages
30 // arising from the use of this software.
31 //
32 // Permission is granted to anyone to use this software for any purpose,
33 // including commercial applications, and to alter it and redistribute it
34 // freely, subject to the following restrictions:
35 //
36 // 1. The origin of this software must not be misrepresented; you must not
37 // claim that you wrote the original software. If you use this software
38 // in a product, an acknowledgment in the product documentation would be
39 // appreciated but is not required.
40 //
41 // 2. Altered source versions must be plainly marked as such, and must not
42 // be misrepresented as being the original software.
43 //
44 // 3. This notice may not be removed or altered from any source distribution.
45 //
46 // Author: Walker White
47 // Version: 11/22/16
48 //
49 #ifndef __CU_TEXT_READER_H__
50 #define __CU_TEXT_READER_H__
51 #include <cugl/base/CUBase.h>
52 #include <SDL/SDL.h>
53 #include <string>
54 #include "CUPathname.h"
55 
56 namespace cugl {
57 
58 #pragma mark -
59 #pragma mark TextReader
60 
75 class TextReader {
76 protected:
78  std::string _name;
80  SDL_RWops* _stream;
82  Sint64 _ssize;
84  Sint64 _scursor;
85 
87  std::string _sbuffer;
89  char* _cbuffer;
91  Uint32 _capacity;
93  Sint32 _bufoff;
94 
95 #pragma mark -
96 #pragma mark Internal Methods
97 
103  void fill();
104 
105 #pragma mark -
106 #pragma mark Constructors
107 public:
114  TextReader() : _name(""), _stream(nullptr), _ssize(-1), _scursor(-1),
115  _sbuffer(""), _cbuffer(nullptr), _capacity(0), _bufoff(-1) {}
116 
123 
139  bool init(const std::string& file) {
140  return init(Pathname(file));
141  }
142 
158  bool init(const char* file) {
159  return init(Pathname(file));
160  }
161 
177  bool init(const Pathname& file);
178 
192  bool init(const std::string& file, unsigned int capacity) {
193  return init(Pathname(file),capacity);
194  }
195 
209  bool init(const char* file, unsigned int capacity) {
210  return init(Pathname(file),capacity);
211  }
212 
226  bool init(const Pathname& file, unsigned int capacity);
227 
242  bool initWithAsset(const std::string& file) {
243  return initWithAsset(file.c_str());
244  }
245 
260  bool initWithAsset(const char* file);
261 
274  bool initWithAsset(const std::string& file, unsigned int capacity) {
275  return initWithAsset(file.c_str(),capacity);
276  }
277 
290  bool initWithAsset(const char* file, unsigned int capacity);
291 
292 
293 #pragma mark -
294 #pragma mark Static Constructors
295 
310  static std::shared_ptr<TextReader> alloc(const std::string& file) {
311  std::shared_ptr<TextReader> result = std::make_shared<TextReader>();
312  return (result->init(file) ? result : nullptr);
313  }
314 
330  static std::shared_ptr<TextReader> alloc(const char* file) {
331  std::shared_ptr<TextReader> result = std::make_shared<TextReader>();
332  return (result->init(file) ? result : nullptr);
333  }
334 
350  static std::shared_ptr<TextReader> alloc(const Pathname& file) {
351  std::shared_ptr<TextReader> result = std::make_shared<TextReader>();
352  return (result->init(file) ? result : nullptr);
353  }
354 
368  static std::shared_ptr<TextReader> alloc(const std::string& file, unsigned int capacity) {
369  std::shared_ptr<TextReader> result = std::make_shared<TextReader>();
370  return (result->init(file,capacity) ? result : nullptr);
371  }
372 
386  static std::shared_ptr<TextReader> alloc(const char* file, unsigned int capacity) {
387  std::shared_ptr<TextReader> result = std::make_shared<TextReader>();
388  return (result->init(file,capacity) ? result : nullptr);
389  }
390 
404  static std::shared_ptr<TextReader> alloc(const Pathname& file, unsigned int capacity) {
405  std::shared_ptr<TextReader> result = std::make_shared<TextReader>();
406  return (result->init(file,capacity) ? result : nullptr);
407  }
408 
423  static std::shared_ptr<TextReader> allocWithAsset(const std::string& file) {
424  std::shared_ptr<TextReader> result = std::make_shared<TextReader>();
425  return (result->initWithAsset(file) ? result : nullptr);
426  }
427 
442  static std::shared_ptr<TextReader> allocWithAsset(const char* file) {
443  std::shared_ptr<TextReader> result = std::make_shared<TextReader>();
444  return (result->initWithAsset(file) ? result : nullptr);
445  }
446 
460  static std::shared_ptr<TextReader> allocWithAsset(const std::string& file, unsigned int capacity) {
461  std::shared_ptr<TextReader> result = std::make_shared<TextReader>();
462  return (result->initWithAsset(file,capacity) ? result : nullptr);
463  }
464 
478  static std::shared_ptr<TextReader> allocWithAsset(const char* file, unsigned int capacity) {
479  std::shared_ptr<TextReader> result = std::make_shared<TextReader>();
480  return (result->initWithAsset(file,capacity) ? result : nullptr);
481  }
482 
483 
484 #pragma mark -
485 #pragma mark Stream Management
486 
492  void reset();
493 
500  void close();
501 
509  bool ready() const { return _bufoff < _sbuffer.size() || _scursor < _ssize; }
510 
511 
512 #pragma mark -
513 #pragma mark Read Methods
514 
523  char read();
524 
538  std::string& read(std::string& data);
539 
549  std::string readUTF8();
550 
563  std::string& readUTF8(std::string& data);
564 
576  std::string readLine();
577 
593  std::string& readLine(std::string& data);
594 
603  std::string readAll();
604 
617  std::string& readAll(std::string& data);
618 
625  void skip();
626 };
627 
628 }
629 #endif /* __CU_TEXT_READER_H__ */
cugl::TextReader::_scursor
Sint64 _scursor
Definition: CUTextReader.h:84
cugl::TextReader::alloc
static std::shared_ptr< TextReader > alloc(const char *file)
Definition: CUTextReader.h:330
cugl::TextReader::alloc
static std::shared_ptr< TextReader > alloc(const Pathname &file)
Definition: CUTextReader.h:350
cugl::TextReader::ready
bool ready() const
Definition: CUTextReader.h:509
cugl::Pathname
Definition: CUPathname.h:85
cugl::TextReader::readAll
std::string readAll()
cugl::TextReader::_sbuffer
std::string _sbuffer
Definition: CUTextReader.h:87
cugl::TextReader::_ssize
Sint64 _ssize
Definition: CUTextReader.h:82
cugl::TextReader::allocWithAsset
static std::shared_ptr< TextReader > allocWithAsset(const char *file)
Definition: CUTextReader.h:442
cugl::TextReader::init
bool init(const std::string &file, unsigned int capacity)
Definition: CUTextReader.h:192
cugl::TextReader::read
char read()
cugl::TextReader::_name
std::string _name
Definition: CUTextReader.h:78
cugl::TextReader::alloc
static std::shared_ptr< TextReader > alloc(const Pathname &file, unsigned int capacity)
Definition: CUTextReader.h:404
cugl::TextReader::TextReader
TextReader()
Definition: CUTextReader.h:114
cugl::TextReader::reset
void reset()
cugl::TextReader::initWithAsset
bool initWithAsset(const std::string &file)
Definition: CUTextReader.h:242
cugl::TextReader::initWithAsset
bool initWithAsset(const std::string &file, unsigned int capacity)
Definition: CUTextReader.h:274
cugl::TextReader::readLine
std::string readLine()
cugl::TextReader::_stream
SDL_RWops * _stream
Definition: CUTextReader.h:80
cugl::TextReader::allocWithAsset
static std::shared_ptr< TextReader > allocWithAsset(const char *file, unsigned int capacity)
Definition: CUTextReader.h:478
cugl::TextReader::skip
void skip()
cugl::TextReader::alloc
static std::shared_ptr< TextReader > alloc(const char *file, unsigned int capacity)
Definition: CUTextReader.h:386
cugl::TextReader::fill
void fill()
cugl::TextReader::allocWithAsset
static std::shared_ptr< TextReader > allocWithAsset(const std::string &file, unsigned int capacity)
Definition: CUTextReader.h:460
cugl::TextReader::init
bool init(const char *file, unsigned int capacity)
Definition: CUTextReader.h:209
cugl::TextReader::close
void close()
cugl::TextReader::init
bool init(const std::string &file)
Definition: CUTextReader.h:139
cugl::TextReader::_bufoff
Sint32 _bufoff
Definition: CUTextReader.h:93
cugl::TextReader::readUTF8
std::string readUTF8()
cugl::TextReader::alloc
static std::shared_ptr< TextReader > alloc(const std::string &file, unsigned int capacity)
Definition: CUTextReader.h:368
cugl::TextReader::init
bool init(const char *file)
Definition: CUTextReader.h:158
cugl::TextReader::alloc
static std::shared_ptr< TextReader > alloc(const std::string &file)
Definition: CUTextReader.h:310
cugl::TextReader
Definition: CUTextReader.h:75
cugl::TextReader::_capacity
Uint32 _capacity
Definition: CUTextReader.h:91
cugl::TextReader::_cbuffer
char * _cbuffer
Definition: CUTextReader.h:89
cugl::TextReader::allocWithAsset
static std::shared_ptr< TextReader > allocWithAsset(const std::string &file)
Definition: CUTextReader.h:423
cugl::TextReader::~TextReader
~TextReader()
Definition: CUTextReader.h:122