#include <gromacs/utility/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.
|
static std::string | readFileToString (const std::string &filename) |
| Reads contents of a file to a std::string. More...
|
|
static std::string | readFileToString (const std::filesystem::path &filename) |
| Reads contents of a file to a std::string. More...
|
|
gmx::TextReader::TextReader |
( |
const std::filesystem::path & |
filename | ) |
|
|
explicit |
Creates a reader that reads from specified file.
- Parameters
-
[in] | filename | Path to the file to open. |
- Exceptions
-
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.
Creates a reader that reads from specified stream.
- Parameters
-
[in] | stream | Stream to read from. |
- Exceptions
-
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).
Creates a reader that reads from specified stream.
- Parameters
-
[in] | stream | Stream to read from. |
- Exceptions
-
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.
- Returns
- Full contents of the stream (from the current point to the end).
std::string gmx::TextReader::readFileToString |
( |
const std::string & |
filename | ) |
|
|
static |
Reads contents of a file to a std::string.
- Parameters
-
[in] | filename | Name of the file to read. |
- Returns
- The contents of
filename
.
- Exceptions
-
std::bad_alloc | if out of memory. |
FileIOError | on any I/O error. |
std::string gmx::TextReader::readFileToString |
( |
const std::filesystem::path & |
filename | ) |
|
|
static |
Reads contents of a file to a std::string.
- Parameters
-
[in] | filename | Name of the file to read. |
- Returns
- The contents of
filename
.
- Exceptions
-
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.
- Parameters
-
[out] | line | String to receive the line. |
- Returns
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:
std::string line;
while (reader.readLine(&line))
{
}
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.
- Parameters
-
[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.
- Parameters
-
[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.
- Parameters
-
[in] | doTrimming | Whether trimming should be active. |
The documentation for this class was generated from the following files: