Gromacs  2024.4
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
List of all members | Public Member Functions | Static Public Member Functions
gmx::TextWriter Class Reference

#include <gromacs/utility/include/gromacs/utility/textwriter.h>

Description

Writes text into a TextOutputStream.

This class provides more formatting and line-oriented writing capabilities than writing raw strings into the stream.

All methods that write to the stream can throw any exceptions that the underlying stream throws.

Public Member Functions

 TextWriter (const std::string &filename)
 Creates a writer that writes to specified file. More...
 
 TextWriter (FILE *fp)
 Creates a writer that writes to specified file. More...
 
 TextWriter (TextOutputStream *stream)
 Creates a writer that writes to specified stream. More...
 
 TextWriter (const TextOutputStreamPointer &stream)
 Creates a writer that writes to specified stream. More...
 
TextLineWrapperSettingswrapperSettings ()
 Allows adjusting wrapping settings for the writer. More...
 
void writeString (const char *str)
 Writes a string to the stream. More...
 
void writeString (const std::string &str)
 Writes a string to the stream. More...
 
void writeStringFormatted (const char *fmt,...)
 Writes a string to the stream, with printf-style formatting.
 
void writeLine (const char *line)
 Writes a line to the stream. More...
 
void writeLine (const std::string &line)
 Writes a line to the stream. More...
 
void writeLineFormatted (const char *fmt,...)
 Writes a line to the stream, with printf-style formatting.
 
void writeLine ()
 Writes a newline to the stream.
 
void ensureLineBreak ()
 Writes a newline if previous output did not end in one. More...
 
void ensureEmptyLine ()
 Ensures that the next string written starts after an empty line. More...
 
void close ()
 Closes the underlying stream.
 

Static Public Member Functions

static void writeFileFromString (const std::string &filename, const std::string &text)
 Convenience method for writing a file from a string in a single call. More...
 

Constructor & Destructor Documentation

gmx::TextWriter::TextWriter ( const std::string &  filename)
explicit

Creates a writer that writes to specified file.

Parameters
[in]filenamePath to the file to open.
Exceptions
std::bad_allocif out of memory.
FileIOErroron any I/O error.

This constructor is provided for convenience for writing directly to a file, without the need to construct multiple objects.

gmx::TextWriter::TextWriter ( FILE *  fp)
explicit

Creates a writer that writes to specified file.

Parameters
[in]fpFile handle to write to.
Exceptions
std::bad_allocif out of memory.
FileIOErroron any I/O error.

This constructor is provided for interoperability with C-like code for writing directly to an already opened file, without the need to construct multiple objects.

The caller is responsible of closing fp; it is not allowed to call close() on the writer.

gmx::TextWriter::TextWriter ( TextOutputStream stream)
explicit

Creates a writer that writes to specified stream.

Parameters
[in]streamStream to write to.
Exceptions
std::bad_allocif out of memory.

The caller is responsible of the lifetime of the stream (should remain in existence as long as the writer exists).

This constructor is provided for convenience for cases where the stream is not allocated with new and/or not managed by a std::shared_ptr (e.g., if the stream is an object on the stack).

gmx::TextWriter::TextWriter ( const TextOutputStreamPointer stream)
explicit

Creates a writer that writes to specified stream.

Parameters
[in]streamStream to write to.
Exceptions
std::bad_allocif out of memory.

The writer keeps a reference to the stream, so the caller can pass in a temporary if necessary.

Member Function Documentation

void gmx::TextWriter::ensureEmptyLine ( )

Ensures that the next string written starts after an empty line.

Always terminates the current line (as with ensureLineBreak()), but the empty line is only written out when the next line is written, so that trailing newlines after final output can be avoided.

If nothing has been written using the writer, this method does nothing.

void gmx::TextWriter::ensureLineBreak ( )

Writes a newline if previous output did not end in one.

If nothing has been written using the writer, this method does nothing.

TextLineWrapperSettings & gmx::TextWriter::wrapperSettings ( )

Allows adjusting wrapping settings for the writer.

Todo:
Wrapping is not currently implemented for code that writes partial lines with writeString().
void gmx::TextWriter::writeFileFromString ( const std::string &  filename,
const std::string &  text 
)
static

Convenience method for writing a file from a string in a single call.

Parameters
[in]filenameName of the file to read.
[in]textString to write to filename.
Exceptions
std::bad_allocif out of memory.
FileIOErroron any I/O error.

If filename exists, it is overwritten.

void gmx::TextWriter::writeLine ( const char *  line)

Writes a line to the stream.

Parameters
[in]lineLine to write.

If line does not end in a newline, one newline is appended. Otherwise, works as writeString().

void gmx::TextWriter::writeLine ( const std::string &  line)

Writes a line to the stream.

Parameters
[in]lineLine to write.

If line does not end in a newline, one newline is appended. Otherwise, works as writeString().

void gmx::TextWriter::writeString ( const char *  str)

Writes a string to the stream.

Parameters
[in]strString to write.
void gmx::TextWriter::writeString ( const std::string &  str)

Writes a string to the stream.

Parameters
[in]strString to write.

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