CUGL 1.3
Cornell University Game Library
CUTextWriter.h
1 //
2 // CUTextWriter.h
3 // Cornell University Game Library (CUGL)
4 //
5 // This module provides a simple Java-style writer for writing to 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_WRITER_H__
50 #define __CU_TEXT_WRITER_H__
51 #include <cugl/base/CUBase.h>
52 #include <cugl/util/CUStrings.h>
53 #include <cugl/io/CUPathname.h>
54 #include <SDL/SDL.h>
55 #include <string>
56 
57 namespace cugl {
58 
73 class TextWriter {
74 protected:
76  std::string _name;
78  SDL_RWops* _stream;
79 
81  char* _cbuffer;
83  Uint32 _capacity;
85  Sint32 _bufoff;
86 
87 #pragma mark -
88 #pragma mark Constructors
89 public:
96  TextWriter() : _name(""), _stream(nullptr), _cbuffer(nullptr), _bufoff(-1) {}
97 
104 
121  bool init(const std::string& file) {
122  return init(Pathname(file));
123  }
124 
141  bool init(const char* file) {
142  return init(Pathname(file));
143  }
144 
161  bool init(const Pathname& file);
162 
177  bool init(const std::string& file, unsigned int capacity) {
178  return init(Pathname(file),capacity);
179  }
180 
195  bool init(const char* file, unsigned int capacity) {
196  return init(Pathname(file),capacity);
197  }
198 
199 
214  bool init(const Pathname& file, unsigned int capacity);
215 
216 
217 #pragma mark -
218 #pragma mark Static Constructors
219 
235  static std::shared_ptr<TextWriter> alloc(const std::string& file) {
236  std::shared_ptr<TextWriter> result = std::make_shared<TextWriter>();
237  return (result->init(file) ? result : nullptr);
238  }
239 
256  static std::shared_ptr<TextWriter> alloc(const char* file) {
257  std::shared_ptr<TextWriter> result = std::make_shared<TextWriter>();
258  return (result->init(file) ? result : nullptr);
259  }
260 
277  static std::shared_ptr<TextWriter> alloc(const Pathname& file) {
278  std::shared_ptr<TextWriter> result = std::make_shared<TextWriter>();
279  return (result->init(file) ? result : nullptr);
280  }
281 
296  static std::shared_ptr<TextWriter> alloc(const std::string& file, unsigned int capacity) {
297  std::shared_ptr<TextWriter> result = std::make_shared<TextWriter>();
298  return (result->init(file,capacity) ? result : nullptr);
299  }
300 
315  static std::shared_ptr<TextWriter> alloc(const char* file, unsigned int capacity) {
316  std::shared_ptr<TextWriter> result = std::make_shared<TextWriter>();
317  return (result->init(file,capacity) ? result : nullptr);
318  }
319 
334  static std::shared_ptr<TextWriter> alloc(const Pathname& file, unsigned int capacity) {
335  std::shared_ptr<TextWriter> result = std::make_shared<TextWriter>();
336  return (result->init(file,capacity) ? result : nullptr);
337  }
338 
339 
340 #pragma mark -
341 #pragma mark Stream Management
342 
348  void flush();
349 
357  void close();
358 
359 
360 #pragma mark -
361 #pragma mark Primitive Methods
362 
371  void write(char c);
372 
382  void write(Uint8 b) { write(cugl::to_string(b)); }
383 
393  void write(Sint16 n) { write(cugl::to_string(n)); }
394 
404  void write(Uint16 n) { write(cugl::to_string(n)); }
405 
415  void write(Sint32 n) { write(cugl::to_string(n)); }
416 
426  void write(Uint32 n) { write(cugl::to_string(n)); }
427 
437  void write(Sint64 n) { write(cugl::to_string(n)); }
438 
448  void write(Uint64 n) { write(cugl::to_string(n)); }
449 
461  void write(bool b) {
462  if (b) { write("true"); } else { write("false"); }
463  }
464 
476  void write(float n) { write(cugl::to_string(n)); }
477 
489  void write(double n) { write(cugl::to_string(n)); }
490 
491 
492 #pragma mark -
493 #pragma mark String Methods
494 
503  void write(const char* s);
504 
514  void write(const std::string& s);
515 
525  void writeLine(const char* s);
526 
536  void writeLine(const std::string& s);
537 
538 };
539 
540 }
541 
542 #endif /* __CU_TEXT_WRITER_H__ */
cugl::TextWriter::alloc
static std::shared_ptr< TextWriter > alloc(const Pathname &file, unsigned int capacity)
Definition: CUTextWriter.h:334
cugl::TextWriter::_capacity
Uint32 _capacity
Definition: CUTextWriter.h:83
cugl::TextWriter::alloc
static std::shared_ptr< TextWriter > alloc(const char *file)
Definition: CUTextWriter.h:256
cugl::Pathname
Definition: CUPathname.h:85
cugl::TextWriter::init
bool init(const std::string &file, unsigned int capacity)
Definition: CUTextWriter.h:177
cugl::TextWriter::write
void write(double n)
Definition: CUTextWriter.h:489
cugl::TextWriter::init
bool init(const std::string &file)
Definition: CUTextWriter.h:121
cugl::TextWriter::alloc
static std::shared_ptr< TextWriter > alloc(const std::string &file, unsigned int capacity)
Definition: CUTextWriter.h:296
cugl::TextWriter::alloc
static std::shared_ptr< TextWriter > alloc(const char *file, unsigned int capacity)
Definition: CUTextWriter.h:315
cugl::TextWriter::write
void write(Sint16 n)
Definition: CUTextWriter.h:393
cugl::TextWriter::alloc
static std::shared_ptr< TextWriter > alloc(const Pathname &file)
Definition: CUTextWriter.h:277
cugl::TextWriter::alloc
static std::shared_ptr< TextWriter > alloc(const std::string &file)
Definition: CUTextWriter.h:235
cugl::TextWriter::write
void write(Uint64 n)
Definition: CUTextWriter.h:448
cugl::TextWriter::init
bool init(const char *file)
Definition: CUTextWriter.h:141
cugl::TextWriter::write
void write(Sint64 n)
Definition: CUTextWriter.h:437
cugl::TextWriter::write
void write(char c)
cugl::TextWriter::_stream
SDL_RWops * _stream
Definition: CUTextWriter.h:78
cugl::TextWriter::_cbuffer
char * _cbuffer
Definition: CUTextWriter.h:81
cugl::TextWriter::writeLine
void writeLine(const char *s)
cugl::TextWriter::close
void close()
cugl::TextWriter::write
void write(Uint32 n)
Definition: CUTextWriter.h:426
cugl::TextWriter::flush
void flush()
cugl::TextWriter::write
void write(float n)
Definition: CUTextWriter.h:476
cugl::TextWriter::init
bool init(const char *file, unsigned int capacity)
Definition: CUTextWriter.h:195
cugl::TextWriter::write
void write(Sint32 n)
Definition: CUTextWriter.h:415
cugl::TextWriter::write
void write(Uint16 n)
Definition: CUTextWriter.h:404
cugl::TextWriter::_name
std::string _name
Definition: CUTextWriter.h:76
cugl::TextWriter::_bufoff
Sint32 _bufoff
Definition: CUTextWriter.h:85
cugl::TextWriter::write
void write(Uint8 b)
Definition: CUTextWriter.h:382
cugl::TextWriter::write
void write(bool b)
Definition: CUTextWriter.h:461
cugl::TextWriter::TextWriter
TextWriter()
Definition: CUTextWriter.h:96
cugl::TextWriter
Definition: CUTextWriter.h:73
cugl::TextWriter::~TextWriter
~TextWriter()
Definition: CUTextWriter.h:103