CUGL 2.1
Cornell University Game Library
Public Member Functions | Static Public Member Functions | Protected Attributes | List of all members
cugl::TextWriter Class Reference

#include <CUTextWriter.h>

Inheritance diagram for cugl::TextWriter:
cugl::JsonWriter

Public Member Functions

 TextWriter ()
 
 ~TextWriter ()
 
bool init (const std::string file)
 
bool init (const std::string file, unsigned int capacity)
 
void flush ()
 
void close ()
 
void write (char c)
 
void write (Uint8 b)
 
void write (Sint16 n)
 
void write (Uint16 n)
 
void write (Sint32 n)
 
void write (Uint32 n)
 
void write (Sint64 n)
 
void write (Uint64 n)
 
void write (bool b)
 
void write (float n)
 
void write (double n)
 
void write (const char *s)
 
void write (const std::string &s)
 
void writeLine (const char *s)
 
void writeLine (const std::string &s)
 

Static Public Member Functions

static std::shared_ptr< TextWriteralloc (const std::string file)
 
static std::shared_ptr< TextWriteralloc (const std::string file, unsigned int capacity)
 

Protected Attributes

std::string _name
 
SDL_RWops * _stream
 
char * _cbuffer
 
Uint32 _capacity
 
Sint32 _bufoff
 

Detailed Description

Simple text-based writer for ASCII or UTF8 files.

This class provides a simple Java-style writer for writing to text files. It supports both ASCII and UTF8 encoding. No other encodings are supported (nor should they be since they are not cross-platform).

By default, this class (and every class in the io package) accesses the application save directory {

See also
Application::getSaveDirectory()}. If you want to access another directory, you will need to specify an absolute path for the file name. Keep in mind that absolute paths are very dangerous on mobile devices, because they do not have proper file systems. You should confine all files to either the asset or the save directory.

Constructor & Destructor Documentation

◆ TextWriter()

cugl::TextWriter::TextWriter ( )
inline

Creates a text writer with no assigned file.

NEVER USE A CONSTRUCTOR WITH NEW. If you want to allocate an object on the heap, use one of the static constructors instead.

◆ ~TextWriter()

cugl::TextWriter::~TextWriter ( )
inline

Deletes this writer and all of its resources.

Calls to the destructor will close the file if it is not already closed.

Member Function Documentation

◆ alloc() [1/2]

static std::shared_ptr<TextWriter> cugl::TextWriter::alloc ( const std::string  file)
inlinestatic

Returns a newly allocated writer for the given file.

The writer will have the default buffer capacity for writing chunks to the file.

If the file is a relative path, this reader will look for the file in the application save directory {

See also
Application::getSaveDirectory()}. If you wish to write a file in any other directory, you must provide an absolute path. Be warned, however, that write priviledges are heavily restricted on mobile platforms.
Parameters
filethe path (absolute or relative) to the file
Returns
a newly allocated writer for the given file.

◆ alloc() [2/2]

static std::shared_ptr<TextWriter> cugl::TextWriter::alloc ( const std::string  file,
unsigned int  capacity 
)
inlinestatic

Returns a newly allocated writer for the given file with the specified capacity.

If the file is a relative path, this reader will look for the file in the application save directory {

See also
Application::getSaveDirectory()}. If you wish to write a file in any other directory, you must provide an absolute path. Be warned, however, that write priviledges are heavily restricted on mobile platforms.
Parameters
filethe path (absolute or relative) to the file
capacitythe buffer capacity for reading chunks
Returns
a newly allocated writer for the given file with the specified capacity.

◆ close()

void cugl::TextWriter::close ( )

Closes the stream, releasing all resources

The contents of the buffer are flushed before the file is closed. Any attempts to write to a closed stream will fail. Calling this method on a previously closed stream has no effect.

◆ flush()

void cugl::TextWriter::flush ( )

Flushes the contents of the write buffer to the file.

It is usually unnecessary to call this method. It is called automatically when the buffer fills, or just before the file is closed.

◆ init() [1/2]

bool cugl::TextWriter::init ( const std::string  file)

Initializes a writer for the given file.

The writer will have the default buffer capacity for writing chunks to the file.

If the file is a relative path, this reader will look for the file in the application save directory {

See also
Application::getSaveDirectory()}. If you wish to write a file in any other directory, you must provide an absolute path. Be warned, however, that write priviledges are heavily restricted on mobile platforms.
Parameters
filethe path (absolute or relative) to the file
Returns
true if the writer is initialized properly, false otherwise.

◆ init() [2/2]

bool cugl::TextWriter::init ( const std::string  file,
unsigned int  capacity 
)

Initializes a writer for the given file with the specified capacity.

If the file is a relative path, this reader will look for the file in the application save directory {

See also
Application::getSaveDirectory()}. If you wish to write a file in any other directory, you must provide an absolute path. Be warned, however, that write priviledges are heavily restricted on mobile platforms.
Parameters
filethe path (absolute or relative) to the file
capacitythe buffer capacity for reading chunks
Returns
true if the writer is initialized properly, false otherwise.

◆ write() [1/13]

void cugl::TextWriter::write ( bool  b)
inline

Writes a boolean value to the file.

The boolean will be written as the string "false" or "true".

The value is written to the internal buffer, but is not necessarily flushed automatically. It will be written when the buffer reaches capacity or the file is closed.

Parameters
bthe boolean value to write

◆ write() [2/13]

void cugl::TextWriter::write ( char  c)

Writes a single character to the file.

The value is written to the internal buffer, but is not necessarily flushed automatically. It will be written when the buffer reaches capacity or the file is closed.

Parameters
cthe character to write

◆ write() [3/13]

void cugl::TextWriter::write ( const char *  s)

Writes a string (ASCII or UTF8) to the file

The value is written to the internal buffer, but is not necessarily flushed automatically. It will be written when the buffer reaches capacity or the file is closed.

Parameters
sthe string to write

◆ write() [4/13]

void cugl::TextWriter::write ( const std::string &  s)

Writes a string (ASCII or UTF8) to the file

The value is written to the internal buffer, but is not necessarily flushed automatically. It will be written when the buffer reaches capacity or the file is closed.

Parameters
sthe string to write

◆ write() [5/13]

void cugl::TextWriter::write ( double  n)
inline

Writes a double value to the file.

The value will be written with full precision.

The value is written to the internal buffer, but is not necessarily flushed automatically. It will be written when the buffer reaches capacity or the file is closed.

Parameters
nthe double value to write

◆ write() [6/13]

void cugl::TextWriter::write ( float  n)
inline

Writes a float value to the file.

The value will be written with full precision.

The value is written to the internal buffer, but is not necessarily flushed automatically. It will be written when the buffer reaches capacity or the file is closed.

Parameters
nthe float value to write

◆ write() [7/13]

void cugl::TextWriter::write ( Sint16  n)
inline

Writes a signed 16 bit integer to the file.

The value is written to the internal buffer, but is not necessarily flushed automatically. It will be written when the buffer reaches capacity or the file is closed.

Parameters
nthe signed 16 bit integer to write

◆ write() [8/13]

void cugl::TextWriter::write ( Sint32  n)
inline

Writes a signed 32 bit integer to the file.

The value is written to the internal buffer, but is not necessarily flushed automatically. It will be written when the buffer reaches capacity or the file is closed.

Parameters
nthe signed 32 bit integer to write

◆ write() [9/13]

void cugl::TextWriter::write ( Sint64  n)
inline

Writes a signed 64 bit integer to the file.

The value is written to the internal buffer, but is not necessarily flushed automatically. It will be written when the buffer reaches capacity or the file is closed.

Parameters
nthe signed 64 bit integer to write

◆ write() [10/13]

void cugl::TextWriter::write ( Uint16  n)
inline

Writes a unsigned 16 bit integer to the file.

The value is written to the internal buffer, but is not necessarily flushed automatically. It will be written when the buffer reaches capacity or the file is closed.

Parameters
nthe unsigned 16 bit integer to write

◆ write() [11/13]

void cugl::TextWriter::write ( Uint32  n)
inline

Writes a unsigned 32 bit integer to the file.

The value is written to the internal buffer, but is not necessarily flushed automatically. It will be written when the buffer reaches capacity or the file is closed.

Parameters
nthe unsigned 32 bit integer to write

◆ write() [12/13]

void cugl::TextWriter::write ( Uint64  n)
inline

Writes a unsigned 64 bit integer to the file.

The value is written to the internal buffer, but is not necessarily flushed automatically. It will be written when the buffer reaches capacity or the file is closed.

Parameters
nthe unsigned 64 bit integer to write

◆ write() [13/13]

void cugl::TextWriter::write ( Uint8  b)
inline

Writes a byte value to the file.

The value is written to the internal buffer, but is not necessarily flushed automatically. It will be written when the buffer reaches capacity or the file is closed.

Parameters
bthe byte value to write

◆ writeLine() [1/2]

void cugl::TextWriter::writeLine ( const char *  s)

Writes a string (ASCII or UTF8) to the file, followed by a newline

The newline used is a standard Unix newline '\n'. You should not expect Windows-style carriage returns (e.g. '\r'). This method automatically flushes the buffer when done.

Parameters
sthe string to write

◆ writeLine() [2/2]

void cugl::TextWriter::writeLine ( const std::string &  s)

Writes a string (ASCII or UTF8) to the file, followed by a newline

The newline used is a standard Unix newline '\n'. You should not expect Windows-style carriage returns (e.g. '\r'). This method automatically flushes the buffer when done.

Parameters
sthe string to write

Member Data Documentation

◆ _bufoff

Sint32 cugl::TextWriter::_bufoff
protected

The current offset in the writer buffer

◆ _capacity

Uint32 cugl::TextWriter::_capacity
protected

The buffer capacity

◆ _cbuffer

char* cugl::TextWriter::_cbuffer
protected

The buffer for cutting down on file access

◆ _name

std::string cugl::TextWriter::_name
protected

The (full) path for the file

◆ _stream

SDL_RWops* cugl::TextWriter::_stream
protected

The SDL I/O stream for writing


The documentation for this class was generated from the following file: