Gromacs
2025-dev-20241002-88a4191
|
#include <gromacs/utility/include/gromacs/utility/textwriter.h>
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::filesystem::path &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... | |
TextLineWrapperSettings & | wrapperSettings () |
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::filesystem::path &filename, const std::string &text) |
Convenience method for writing a file from a string in a single call. More... | |
|
explicit |
Creates a writer that writes to specified file.
[in] | filename | Path to the file to open. |
std::bad_alloc | if out of memory. |
FileIOError | on any I/O error. |
This constructor is provided for convenience for writing directly to a file, without the need to construct multiple objects.
|
explicit |
Creates a writer that writes to specified file.
[in] | fp | File handle to write to. |
std::bad_alloc | if out of memory. |
FileIOError | on 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.
|
explicit |
Creates a writer that writes to specified stream.
[in] | stream | Stream to write to. |
std::bad_alloc | if 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).
|
explicit |
Creates a writer that writes to specified stream.
[in] | stream | Stream to write to. |
std::bad_alloc | if out of memory. |
The writer keeps a reference to the stream, so the caller can pass in a temporary if necessary.
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.
|
static |
Convenience method for writing a file from a string in a single call.
[in] | filename | Name of the file to read. |
[in] | text | String to write to filename . |
std::bad_alloc | if out of memory. |
FileIOError | on any I/O error. |
If filename
exists, it is overwritten.
void gmx::TextWriter::writeLine | ( | const char * | line | ) |
Writes a line to the stream.
[in] | line | Line 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.
[in] | line | Line 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.
[in] | str | String to write. |
void gmx::TextWriter::writeString | ( | const std::string & | str | ) |
Writes a string to the stream.
[in] | str | String to write. |