Gromacs
2018.8
|
#include <gromacs/utility/textreader.h>
Reads text from a TextInputStream.
This class provides more formatted reading capabilities than reading raw lines from the stream (and a natural place to implement more such capabilities).
All methods that read from the stream can throw any exceptions that the underlying stream throws.
Classes | |
class | Impl |
Implementation class. More... | |
Public Member Functions | |
TextReader (const std::string &filename) | |
Creates a reader that reads from specified file. More... | |
TextReader (TextInputStream *stream) | |
Creates a reader that reads from specified stream. More... | |
TextReader (const TextInputStreamPointer &stream) | |
Creates a reader that reads from specified stream. More... | |
bool | readLine (std::string *line) |
Reads a single line (including newline) from the stream. More... | |
void | setTrimLeadingWhiteSpace (bool doTrimming) |
Sets whether the reader should trim leading whitespace from a line before returning it. More... | |
void | setTrimTrailingWhiteSpace (bool doTrimming) |
Sets whether the reader should trim trailing whitespace from a line before returning it. More... | |
void | setTrimTrailingComment (bool doTrimming, char commentChar) |
Sets whether the reader should trim at trailing comment from a line before returning it. More... | |
std::string | readAll () |
Reads all remaining lines from the stream as a single string. More... | |
void | close () |
Closes the underlying stream. | |
Static Public Member Functions | |
static std::string | readFileToString (const char *filename) |
Reads contents of a file to a std::string. More... | |
static std::string | readFileToString (const std::string &filename) |
Reads contents of a file to a std::string. More... | |
|
explicit |
Creates a reader that reads from 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 reading directly from a file, without the need to construct multiple objects.
|
explicit |
Creates a reader that reads from specified stream.
[in] | stream | Stream to read from. |
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 reader 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 reader that reads from specified stream.
[in] | stream | Stream to read from. |
std::bad_alloc | if out of memory. |
The reader keeps a reference to the stream, so the caller can pass in a temporary if necessary.
std::string gmx::TextReader::readAll | ( | ) |
Reads all remaining lines from the stream as a single string.
|
static |
Reads contents of a file to a std::string.
[in] | filename | Name of the file to read. |
filename
. std::bad_alloc | if out of memory. |
FileIOError | on any I/O error. |
|
static |
Reads contents of a file to a std::string.
[in] | filename | Name of the file to read. |
filename
. std::bad_alloc | if out of memory. |
FileIOError | on any I/O error. |
bool gmx::TextReader::readLine | ( | std::string * | line | ) |
Reads a single line (including newline) from the stream.
[out] | line | String to receive the line. |
false
if nothing was read because the file ended.On error or when false is returned, line
will be empty. Newlines will be returned as part of line
if it was present in the stream. To loop over all lines in the stream, use:
Behaviours such as trimming whitespace or comments can be configured by calling other methods before this one.
void gmx::TextReader::setTrimLeadingWhiteSpace | ( | bool | doTrimming | ) |
Sets whether the reader should trim leading whitespace from a line before returning it.
[in] | doTrimming | Whether trimming should be active. |
void gmx::TextReader::setTrimTrailingComment | ( | bool | doTrimming, |
char | commentChar | ||
) |
Sets whether the reader should trim at trailing comment from a line before returning it.
Note that comment trimming will precede whitespace trimming when both are active.
[in] | commentChar | The character that begins a comment. |
[in] | doTrimming | Whether trimming should be active. |
void gmx::TextReader::setTrimTrailingWhiteSpace | ( | bool | doTrimming | ) |
Sets whether the reader should trim trailing whitespace from a line before returning it.
Note that comment trimming will precede whitespace trimming when both are active.
[in] | doTrimming | Whether trimming should be active. |