Gromacs  2016.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::TextReader Class Reference

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

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...
 
bool readLineTrimmed (std::string *line)
 Reads a single line from the stream. 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))
{
// ...
}
bool gmx::TextReader::readLineTrimmed ( std::string *  line)

Reads a single line 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. Works as readLine(), except that trailing whitespace will be removed from line.

See Also
readLine()

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