Thea
Classes | Public Member Functions | Protected Member Functions | List of all members
TextOutputStream Class Reference

Convenient formatting of ASCII text written to a file. More...

#include <TextOutputStream.hpp>

Inheritance diagram for TextOutputStream:
NamedObject Noncopyable INamedObject

Classes

struct  NewlineStyle
 Newline style (enum class). More...
 
class  Settings
 Output configuration optionss. More...
 
struct  WordWrap
 Word-wrap settings (enum class). More...
 

Public Member Functions

bool commit (bool flush=true)
 Write the buffered data to disk. More...
 
void commitToString (std::string &str)
 Commit the buffered data to a string. More...
 
std::string commitToString ()
 Commit the buffered data to a string and return it. More...
 
char const * getName () const
 Get the name of the object. More...
 
std::string getPath () const
 Get the path to the current file being written ("<memory>" for memory streams). More...
 
bool ok () const
 True if no errors have been encountered. More...
 
void popIndent ()
 Decrease indent level by one. More...
 
void printf (char const *fmt,...)
 Use C-style printf syntax to write to the stream. More...
 
void pushIndent ()
 Increase indent level by one. More...
 
int8 setName (char const *s)
 Set the name of the object from a C-style string. More...
 
virtual int8 setName (std::string const &s)
 Set the name of the object from a std::string. More...
 
 TextOutputStream (std::string const &path_, Settings const &settings_=Settings::defaults())
 Construct a stream that writes to a file. More...
 
 TextOutputStream (Settings const &settings_=Settings::defaults())
 Construct a text output stream in memory that can later be committed to a string instead of a file. More...
 
void vprintf (char const *fmt, va_list arg_list)
 Use C-style vprintf syntax to write to the stream. More...
 
void writeBoolean (bool b)
 Write a boolean value, followed by a space. More...
 
void writeNewline ()
 Write a newline character(s). More...
 
void writeNewlines (int numLines)
 Write several newline character(s). More...
 
void writeNumber (double n)
 Write a double precision number, followed by a space. More...
 
void writeString (std::string const &str)
 Write a quoted string. More...
 
void writeSymbol (std::string const &str)
 Write a symbol that can be read by TextInputStream, followed by a space. More...
 
void writeSymbols (std::string const &a, std::string const &b="", std::string const &c="", std::string const &d="", std::string const &e="", std::string const &f="")
 Convenience function for writing multiple symbols in a row, separated by spaces, e.g. writeSymbols("name", "="). More...
 
 ~TextOutputStream ()
 Destructor. More...
 

Protected Member Functions

std::string const & getNameStr () const
 Access the name string directly, for efficiency. More...
 

Detailed Description

Convenient formatting of ASCII text written to a file.

The core writeString(), writeNumber(), and writeSymbol() methods map to TextInputStream's methods. writeNumber() and writeSymbol() each print an additional trailing space that is used to separate adjacent tokens.

TextOutputStream::printf allows arbitrary text to be conveniently dumped en-masse.

When a word-wrap line break occurs, all whitespace between words is replaced with a single newline (the newline may be two characters – see TextOutputStream::Options::NewlineStyle). Word wrapping occurs against the number of columns specified by Options::numColumns, minus the current indent level.

Indenting adds the specified number of spaces immediately after a newline. If a newline was followed by spaces in the original string, these are added to the indent spaces. Indenting will indent blank lines and will leave indents after the last newline of a file (if the indent level is non-zero at the end).

Derived from the G3D library: http://g3d.sourceforge.net

Definition at line 56 of file TextOutputStream.hpp.

Constructor & Destructor Documentation

TextOutputStream ( std::string const &  path_,
TextOutputStream::Settings const &  opt = Settings::defaults() 
)
explicit

Construct a stream that writes to a file.

Use "<memory>" as the path if you're going to commit to memory – this has the same effect as the default constructor. The file and its parent directories will be created if they do not exist, and the file initialized to be blank. If the file cannot be constructed, ok() will return false.

Definition at line 65 of file TextOutputStream.cpp.

Construct a text output stream in memory that can later be committed to a string instead of a file.

Definition at line 52 of file TextOutputStream.cpp.

Destructor.

Automatically calls commit().

Definition at line 81 of file TextOutputStream.cpp.

Member Function Documentation

bool commit ( bool  flush = true)

Write the buffered data to disk.

Parent directories are created as needed if they do not exist.

Parameters
flushIf true (default) the file is ready for reading when the method returns, otherwise the method returns immediately and writes the file in the background.
Returns
True if the commit succeeded, else false.

Definition at line 94 of file TextOutputStream.cpp.

void commitToString ( std::string &  str)

Commit the buffered data to a string.

Definition at line 150 of file TextOutputStream.cpp.

std::string commitToString ( )

Commit the buffered data to a string and return it.

Definition at line 156 of file TextOutputStream.cpp.

char const* getName ( ) const
virtualinherited

Get the name of the object.

Implements INamedObject.

Definition at line 78 of file NamedObject.hpp.

std::string const& getNameStr ( ) const
protectedinherited

Access the name string directly, for efficiency.

Definition at line 98 of file NamedObject.hpp.

std::string getPath ( ) const

Get the path to the current file being written ("<memory>" for memory streams).

Definition at line 209 of file TextOutputStream.hpp.

bool ok ( ) const

True if no errors have been encountered.

Definition at line 88 of file TextOutputStream.cpp.

void popIndent ( )

Decrease indent level by one.

See also
pushIndent()

Definition at line 194 of file TextOutputStream.cpp.

void printf ( char const *  fmt,
  ... 
)

Use C-style printf syntax to write to the stream.

Follows normal printf conventions. Note that the output will be reformatted for word-wrapping and newlines.

Definition at line 288 of file TextOutputStream.cpp.

void pushIndent ( )

Increase indent level by one.

See also
popIndent()

Definition at line 188 of file TextOutputStream.cpp.

int8 setName ( char const *  s)
virtualinherited

Set the name of the object from a C-style string.

Returns
True if the name was successfully set, else false (e.g. if the name is read-only). In the default implementation, the function always returns true.

Implements INamedObject.

Definition at line 86 of file NamedObject.hpp.

virtual int8 setName ( std::string const &  s)
virtualinherited

Set the name of the object from a std::string.

Returns
True if the name was successfully set, else false (e.g. if the name is read-only). In the default implementation, the function always returns true.

Definition at line 94 of file NamedObject.hpp.

void vprintf ( char const *  fmt,
va_list  arg_list 
)

Use C-style vprintf syntax to write to the stream.

Follows normal vprintf conventions. Note that the output will be reformatted for word-wrapping and newlines.

Definition at line 297 of file TextOutputStream.cpp.

void writeBoolean ( bool  b)

Write a boolean value, followed by a space.

Uses the values of trueSymbol and falseSymbol in the settings.

Definition at line 250 of file TextOutputStream.cpp.

void writeNewline ( )

Write a newline character(s).

Definition at line 341 of file TextOutputStream.cpp.

void writeNewlines ( int  numLines)

Write several newline character(s).

Definition at line 350 of file TextOutputStream.cpp.

void writeNumber ( double  n)

Write a double precision number, followed by a space.

A 64-bit double precision number has enough bits to store a 32-bit integer exactly.

Definition at line 256 of file TextOutputStream.cpp.

void writeString ( std::string const &  str)

Write a quoted string.

Special characters in the string (e.g. \, \t, \n) are escaped so that TextInputStream will produce the identical string on reading.

Definition at line 243 of file TextOutputStream.cpp.

void writeSymbol ( std::string const &  str)

Write a symbol that can be read by TextInputStream, followed by a space.

The symbol is written without quotes. Symbols are required to begin with a letter or underscore and contain only letters, underscores, and numbers or be a C++ symbol (e.g. "{", "(", "++" etc.) so that they may be properly parsed by TextInputStream::readSymbol().

Definition at line 262 of file TextOutputStream.cpp.

void writeSymbols ( std::string const &  a,
std::string const &  b = "",
std::string const &  c = "",
std::string const &  d = "",
std::string const &  e = "",
std::string const &  f = "" 
)

Convenience function for writing multiple symbols in a row, separated by spaces, e.g. writeSymbols("name", "=").

Empty symbols are not written.

Definition at line 271 of file TextOutputStream.cpp.


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