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

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

Description

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...
 

Constructor & Destructor Documentation

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

Creates a reader that reads from 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 reading directly from a file, without the need to construct multiple objects.

gmx::TextReader::TextReader ( TextInputStream stream)
explicit

Creates a reader that reads from specified stream.

Parameters
[in]streamStream to read from.
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 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).

gmx::TextReader::TextReader ( const TextInputStreamPointer stream)
explicit

Creates a reader that reads from specified stream.

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

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

Member Function Documentation

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 char *  filename)
static

Reads contents of a file to a std::string.

Parameters
[in]filenameName of the file to read.
Returns
The contents of filename.
Exceptions
std::bad_allocif out of memory.
FileIOErroron any I/O error.
std::string gmx::TextReader::readFileToString ( const std::string &  filename)
static

Reads contents of a file to a std::string.

Parameters
[in]filenameName of the file to read.
Returns
The contents of filename.
Exceptions
std::bad_allocif out of memory.
FileIOErroron any I/O error.
bool gmx::TextReader::readLine ( std::string *  line)

Reads a single line (including newline) from the stream.

Parameters
[out]lineString 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]doTrimmingWhether 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]commentCharThe character that begins a comment.
[in]doTrimmingWhether 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]doTrimmingWhether trimming should be active.

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