CUGL
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 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_WRITER_H__
50 #define __CU_TEXT_WRITER_H__
51 #include <cugl/util/CUStrings.h>
52 #include <cugl/io/CUPathname.h>
53 #include <SDL/SDL.h>
54 #include <string>
55 
56 namespace cugl {
57 
72 class TextWriter {
73 protected:
75  std::string _name;
77  SDL_RWops* _stream;
78 
80  char* _cbuffer;
82  Uint32 _capacity;
84  Sint32 _bufoff;
85 
86 #pragma mark -
87 #pragma mark Constructors
88 public:
95  TextWriter() : _name(""), _stream(nullptr), _cbuffer(nullptr), _bufoff(-1) {}
96 
103 
120  bool init(const std::string& file) {
121  return init(Pathname(file));
122  }
123 
140  bool init(const char* file) {
141  return init(Pathname(file));
142  }
143 
160  bool init(const Pathname& file);
161 
176  bool init(const std::string& file, unsigned int capacity) {
177  return init(Pathname(file),capacity);
178  }
179 
194  bool init(const char* file, unsigned int capacity) {
195  return init(Pathname(file),capacity);
196  }
197 
198 
213  bool init(const Pathname& file, unsigned int capacity);
214 
215 
216 #pragma mark -
217 #pragma mark Static Constructors
218 
234  static std::shared_ptr<TextWriter> alloc(const std::string& file) {
235  std::shared_ptr<TextWriter> result = std::make_shared<TextWriter>();
236  return (result->init(file) ? result : nullptr);
237  }
238 
255  static std::shared_ptr<TextWriter> alloc(const char* file) {
256  std::shared_ptr<TextWriter> result = std::make_shared<TextWriter>();
257  return (result->init(file) ? result : nullptr);
258  }
259 
276  static std::shared_ptr<TextWriter> alloc(const Pathname& file) {
277  std::shared_ptr<TextWriter> result = std::make_shared<TextWriter>();
278  return (result->init(file) ? result : nullptr);
279  }
280 
295  static std::shared_ptr<TextWriter> alloc(const std::string& file, unsigned int capacity) {
296  std::shared_ptr<TextWriter> result = std::make_shared<TextWriter>();
297  return (result->init(file,capacity) ? result : nullptr);
298  }
299 
314  static std::shared_ptr<TextWriter> alloc(const char* file, unsigned int capacity) {
315  std::shared_ptr<TextWriter> result = std::make_shared<TextWriter>();
316  return (result->init(file,capacity) ? result : nullptr);
317  }
318 
333  static std::shared_ptr<TextWriter> alloc(const Pathname& file, unsigned int capacity) {
334  std::shared_ptr<TextWriter> result = std::make_shared<TextWriter>();
335  return (result->init(file,capacity) ? result : nullptr);
336  }
337 
338 
339 #pragma mark -
340 #pragma mark Stream Management
341 
347  void flush();
348 
356  void close();
357 
358 
359 #pragma mark -
360 #pragma mark Primitive Methods
361 
370  void write(char c);
371 
381  void write(Uint8 b) { write(cugl::to_string(b)); }
382 
392  void write(Sint16 n) { write(cugl::to_string(n)); }
393 
403  void write(Uint16 n) { write(cugl::to_string(n)); }
404 
414  void write(Sint32 n) { write(cugl::to_string(n)); }
415 
425  void write(Uint32 n) { write(cugl::to_string(n)); }
426 
436  void write(Sint64 n) { write(cugl::to_string(n)); }
437 
447  void write(Uint64 n) { write(cugl::to_string(n)); }
448 
460  void write(bool b) {
461  if (b) { write("true"); } else { write("false"); }
462  }
463 
475  void write(float n) { write(cugl::to_string(n)); }
476 
488  void write(double n) { write(cugl::to_string(n)); }
489 
490 
491 #pragma mark -
492 #pragma mark String Methods
493 
502  void write(const char* s);
503 
513  void write(const std::string& s);
514 
524  void writeLine(const char* s);
525 
535  void writeLine(const std::string& s);
536 
537 };
538 
539 }
540 
541 #endif /* __CU_TEXT_WRITER_H__ */
char * _cbuffer
Definition: CUTextWriter.h:80
Uint32 _capacity
Definition: CUTextWriter.h:82
void write(bool b)
Definition: CUTextWriter.h:460
bool init(const char *file)
Definition: CUTextWriter.h:140
Definition: CUTextWriter.h:72
static std::shared_ptr< TextWriter > alloc(const char *file)
Definition: CUTextWriter.h:255
bool init(const std::string &file)
Definition: CUTextWriter.h:120
void write(float n)
Definition: CUTextWriter.h:475
std::string _name
Definition: CUTextWriter.h:75
static std::shared_ptr< TextWriter > alloc(const std::string &file)
Definition: CUTextWriter.h:234
SDL_RWops * _stream
Definition: CUTextWriter.h:77
static std::shared_ptr< TextWriter > alloc(const Pathname &file, unsigned int capacity)
Definition: CUTextWriter.h:333
bool init(const std::string &file, unsigned int capacity)
Definition: CUTextWriter.h:176
static std::shared_ptr< TextWriter > alloc(const char *file, unsigned int capacity)
Definition: CUTextWriter.h:314
Sint32 _bufoff
Definition: CUTextWriter.h:84
void write(Uint16 n)
Definition: CUTextWriter.h:403
void write(Uint32 n)
Definition: CUTextWriter.h:425
TextWriter()
Definition: CUTextWriter.h:95
void write(Sint16 n)
Definition: CUTextWriter.h:392
void writeLine(const char *s)
void write(Sint64 n)
Definition: CUTextWriter.h:436
std::string to_string(Uint8 value)
static std::shared_ptr< TextWriter > alloc(const std::string &file, unsigned int capacity)
Definition: CUTextWriter.h:295
void write(char c)
void write(Uint64 n)
Definition: CUTextWriter.h:447
bool init(const char *file, unsigned int capacity)
Definition: CUTextWriter.h:194
~TextWriter()
Definition: CUTextWriter.h:102
void write(double n)
Definition: CUTextWriter.h:488
static std::shared_ptr< TextWriter > alloc(const Pathname &file)
Definition: CUTextWriter.h:276
Definition: CUAnimationNode.h:52
void write(Uint8 b)
Definition: CUTextWriter.h:381
Definition: CUPathname.h:85
void write(Sint32 n)
Definition: CUTextWriter.h:414