CUGL
Cornell University Game Library
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 zlib 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 <SDL/SDL.h>
52 #include <string>
53 #include "CUPathname.h"
54 
55 namespace cugl {
56 
57 #pragma mark -
58 #pragma mark TextReader
59 
74 class TextReader {
75 protected:
77  std::string _name;
79  SDL_RWops* _stream;
81  Sint64 _ssize;
83  Sint64 _scursor;
84 
86  std::string _sbuffer;
88  char* _cbuffer;
90  Uint32 _capacity;
92  Sint32 _bufoff;
93 
94 #pragma mark -
95 #pragma mark Internal Methods
96 
102  void fill();
103 
104 #pragma mark -
105 #pragma mark Constructors
106 public:
113  TextReader() : _name(""), _stream(nullptr), _ssize(-1), _scursor(-1),
114  _sbuffer(""), _cbuffer(nullptr), _capacity(0), _bufoff(-1) {}
115 
122 
138  bool init(const std::string& file) {
139  return init(Pathname(file));
140  }
141 
157  bool init(const char* file) {
158  return init(Pathname(file));
159  }
160 
176  bool init(const Pathname& file);
177 
191  bool init(const std::string& file, unsigned int capacity) {
192  return init(Pathname(file),capacity);
193  }
194 
208  bool init(const char* file, unsigned int capacity) {
209  return init(Pathname(file),capacity);
210  }
211 
225  bool init(const Pathname& file, unsigned int capacity);
226 
241  bool initWithAsset(const std::string& file) {
242  return initWithAsset(file.c_str());
243  }
244 
259  bool initWithAsset(const char* file);
260 
273  bool initWithAsset(const std::string& file, unsigned int capacity) {
274  return initWithAsset(file.c_str(),capacity);
275  }
276 
289  bool initWithAsset(const char* file, unsigned int capacity);
290 
291 
292 #pragma mark -
293 #pragma mark Static Constructors
294 
309  static std::shared_ptr<TextReader> alloc(const std::string& file) {
310  std::shared_ptr<TextReader> result = std::make_shared<TextReader>();
311  return (result->init(file) ? result : nullptr);
312  }
313 
329  static std::shared_ptr<TextReader> alloc(const char* file) {
330  std::shared_ptr<TextReader> result = std::make_shared<TextReader>();
331  return (result->init(file) ? result : nullptr);
332  }
333 
349  static std::shared_ptr<TextReader> alloc(const Pathname& file) {
350  std::shared_ptr<TextReader> result = std::make_shared<TextReader>();
351  return (result->init(file) ? result : nullptr);
352  }
353 
367  static std::shared_ptr<TextReader> alloc(const std::string& file, unsigned int capacity) {
368  std::shared_ptr<TextReader> result = std::make_shared<TextReader>();
369  return (result->init(file,capacity) ? result : nullptr);
370  }
371 
385  static std::shared_ptr<TextReader> alloc(const char* file, unsigned int capacity) {
386  std::shared_ptr<TextReader> result = std::make_shared<TextReader>();
387  return (result->init(file,capacity) ? result : nullptr);
388  }
389 
403  static std::shared_ptr<TextReader> alloc(const Pathname& file, unsigned int capacity) {
404  std::shared_ptr<TextReader> result = std::make_shared<TextReader>();
405  return (result->init(file,capacity) ? result : nullptr);
406  }
407 
422  static std::shared_ptr<TextReader> allocWithAsset(const std::string& file) {
423  std::shared_ptr<TextReader> result = std::make_shared<TextReader>();
424  return (result->initWithAsset(file) ? result : nullptr);
425  }
426 
441  static std::shared_ptr<TextReader> allocWithAsset(const char* file) {
442  std::shared_ptr<TextReader> result = std::make_shared<TextReader>();
443  return (result->initWithAsset(file) ? result : nullptr);
444  }
445 
459  static std::shared_ptr<TextReader> allocWithAsset(const std::string& file, unsigned int capacity) {
460  std::shared_ptr<TextReader> result = std::make_shared<TextReader>();
461  return (result->initWithAsset(file,capacity) ? result : nullptr);
462  }
463 
477  static std::shared_ptr<TextReader> allocWithAsset(const char* file, unsigned int capacity) {
478  std::shared_ptr<TextReader> result = std::make_shared<TextReader>();
479  return (result->initWithAsset(file,capacity) ? result : nullptr);
480  }
481 
482 
483 #pragma mark -
484 #pragma mark Stream Management
485 
491  void reset();
492 
499  void close();
500 
508  bool ready() const { return _bufoff < _sbuffer.size() || _scursor < _ssize; }
509 
510 
511 #pragma mark -
512 #pragma mark Read Methods
513 
522  char read();
523 
537  std::string& read(std::string& data);
538 
548  std::string readUTF8();
549 
562  std::string& readUTF8(std::string& data);
563 
575  std::string readLine();
576 
592  std::string& readLine(std::string& data);
593 
602  std::string readAll();
603 
616  std::string& readAll(std::string& data);
617 
624  void skip();
625 };
626 
627 }
628 #endif /* __CU_TEXT_READER_H__ */
Definition: CUTextReader.h:74
std::string _sbuffer
Definition: CUTextReader.h:86
bool ready() const
Definition: CUTextReader.h:508
std::string _name
Definition: CUTextReader.h:77
char * _cbuffer
Definition: CUTextReader.h:88
bool init(const char *file, unsigned int capacity)
Definition: CUTextReader.h:208
static std::shared_ptr< TextReader > alloc(const char *file)
Definition: CUTextReader.h:329
bool init(const char *file)
Definition: CUTextReader.h:157
Sint64 _ssize
Definition: CUTextReader.h:81
static std::shared_ptr< TextReader > alloc(const std::string &file)
Definition: CUTextReader.h:309
static std::shared_ptr< TextReader > alloc(const char *file, unsigned int capacity)
Definition: CUTextReader.h:385
bool init(const std::string &file)
Definition: CUTextReader.h:138
static std::shared_ptr< TextReader > alloc(const Pathname &file, unsigned int capacity)
Definition: CUTextReader.h:403
static std::shared_ptr< TextReader > alloc(const Pathname &file)
Definition: CUTextReader.h:349
static std::shared_ptr< TextReader > allocWithAsset(const char *file, unsigned int capacity)
Definition: CUTextReader.h:477
TextReader()
Definition: CUTextReader.h:113
std::string readAll()
Sint32 _bufoff
Definition: CUTextReader.h:92
bool initWithAsset(const std::string &file)
Definition: CUTextReader.h:241
SDL_RWops * _stream
Definition: CUTextReader.h:79
Uint32 _capacity
Definition: CUTextReader.h:90
std::string readUTF8()
static std::shared_ptr< TextReader > allocWithAsset(const std::string &file)
Definition: CUTextReader.h:422
static std::shared_ptr< TextReader > allocWithAsset(const std::string &file, unsigned int capacity)
Definition: CUTextReader.h:459
std::string readLine()
~TextReader()
Definition: CUTextReader.h:121
static std::shared_ptr< TextReader > alloc(const std::string &file, unsigned int capacity)
Definition: CUTextReader.h:367
bool initWithAsset(const std::string &file, unsigned int capacity)
Definition: CUTextReader.h:273
Definition: CUAnimationNode.h:52
Sint64 _scursor
Definition: CUTextReader.h:83
bool init(const std::string &file, unsigned int capacity)
Definition: CUTextReader.h:191
Definition: CUPathname.h:85
static std::shared_ptr< TextReader > allocWithAsset(const char *file)
Definition: CUTextReader.h:441