Gromacs  2024.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::TextInputFile Class Reference

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

+ Inheritance diagram for gmx::TextInputFile:
+ Collaboration diagram for gmx::TextInputFile:

Description

Text input stream implementation for reading from a file.

Implementations for the TextInputStream methods throw FileIOError on any I/O error.

Public Member Functions

 TextInputFile (const std::filesystem::path &filename)
 Opens a text file as a stream. More...
 
 TextInputFile (FILE *fp)
 Initializes file object from an existing file handle. More...
 
FILE * handle ()
 Returns a raw handle to the input file. More...
 
bool readLine (std::string *line) override
 Reads a line (with newline included) from the stream. More...
 
void close () override
 Closes the stream. More...
 

Static Public Member Functions

static FilePtr openRawHandle (const std::filesystem::path &filename)
 Opens a file and returns an RAII-style FILE handle. More...
 

Constructor & Destructor Documentation

gmx::TextInputFile::TextInputFile ( const std::filesystem::path &  filename)
explicit

Opens a text file as a stream.

Parameters
[in]filenamePath to the file to open.
Exceptions
std::bad_allocif out of memory.
FileIOErroron any I/O error.
gmx::TextInputFile::TextInputFile ( FILE *  fp)
explicit

Initializes file object from an existing file handle.

Parameters
[in]fpFile handle to use.
Exceptions
std::bad_allocif out of memory.

The caller is responsible of closing the file; close() does nothing for an object constructed this way.

Member Function Documentation

void gmx::TextInputFile::close ( )
overridevirtual

Closes the stream.

It is not allowed to read from a stream after it has been closed. See TextOutputStream::close() for rationale for a close() method separate from the destructor. For input, failures during close should be rare, but it is clearer to keep the interface symmetric.

Implements gmx::TextInputStream.

FILE * gmx::TextInputFile::handle ( )

Returns a raw handle to the input file.

This is provided for interoperability with older C-like code.

FilePtr gmx::TextInputFile::openRawHandle ( const std::filesystem::path &  filename)
static

Opens a file and returns an RAII-style FILE handle.

Parameters
[in]filenamePath of the file to open.
Exceptions
FileIOErroron any I/O error.

Instead of returning NULL on errors, throws an exception with additional details (including the file name and errno).

bool gmx::TextInputFile::readLine ( std::string *  line)
overridevirtual

Reads a line (with newline included) from the stream.

Parameters
[out]lineString to receive the line.
Returns
false if nothing was read because the stream ended.

On error or when false is returned, line will be empty.

Implements gmx::TextInputStream.


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