Text file implementations

class TextFileImpl

Abstract base class for readers used by text files. This is specialized for compressed files, and might get extended to network or memory mapped files.

Any failed operation should throw a chemfiles::FileError.

Subclassed by chemfiles::Bz2File, chemfiles::GzFile, chemfiles::MemoryFile, chemfiles::PlainFile, chemfiles::XzFile

Public Functions

virtual void clear() noexcept = 0

clear error and eof flags on the file

virtual void seek(uint64_t position) = 0

Set file indicator to position characters after the start of the file.

Throws:

FileError – in case of I/O error while seeking

virtual size_t read(char *data, size_t count) = 0

Fill up the data buffer reading at most count characters from the file.

Returns the amount of characters read. A return value of 0 indicates that the end of file has been reached.

Throws:

FileError – in case of I/O error

virtual void write(const char *data, size_t count) = 0

Write count characters starting at data to the file.

Throws:

FileError – if it could not write all of the data to the file

class PlainFile : public chemfiles::TextFileImpl

Simple TextFileImpl reading plain, uncompressed files using FILE*.

Public Functions

PlainFile(const std::string &path, File::Mode mode)

Open a text file with name filename and mode mode.

virtual size_t read(char *data, size_t count) override

Fill up the data buffer reading at most count characters from the file.

Returns the amount of characters read. A return value of 0 indicates that the end of file has been reached.

Throws:

FileError – in case of I/O error

virtual void write(const char *data, size_t count) override

Write count characters starting at data to the file.

Throws:

FileError – if it could not write all of the data to the file

virtual void clear() noexcept override

clear error and eof flags on the file

virtual void seek(uint64_t position) override

Set file indicator to position characters after the start of the file.

Throws:

FileError – in case of I/O error while seeking

class MemoryFile : public chemfiles::TextFileImpl

Simple TextFile implementation, that wraps memory for reading only.

Public Functions

inline MemoryFile(std::shared_ptr<MemoryBuffer> memory, File::Mode mode)

Open memory as though it were a file in mode mode. No copy of memory is made and the original object MUST not be freed until this object is destroyed

virtual size_t read(char *data, size_t count) override

Fill up the data buffer reading at most count characters from the file.

Returns the amount of characters read. A return value of 0 indicates that the end of file has been reached.

Throws:

FileError – in case of I/O error

virtual void write(const char *data, size_t count) override

Write count characters starting at data to the file.

Throws:

FileError – if it could not write all of the data to the file

inline virtual void clear() noexcept override

clear error and eof flags on the file

virtual void seek(uint64_t position) override

Set file indicator to position characters after the start of the file.

Throws:

FileError – in case of I/O error while seeking

class GzFile : public chemfiles::TextFileImpl

An implementation of TextFile for gzip files.

Public Functions

GzFile(const std::string &path, File::Mode mode)

Open a text file with name filename and mode mode.

virtual size_t read(char *data, size_t count) override

Fill up the data buffer reading at most count characters from the file.

Returns the amount of characters read. A return value of 0 indicates that the end of file has been reached.

Throws:

FileError – in case of I/O error

virtual void write(const char *data, size_t count) override

Write count characters starting at data to the file.

Throws:

FileError – if it could not write all of the data to the file

virtual void clear() noexcept override

clear error and eof flags on the file

virtual void seek(uint64_t position) override

Set file indicator to position characters after the start of the file.

Throws:

FileError – in case of I/O error while seeking

class XzFile : public chemfiles::TextFileImpl

An implementation of TextFile for lzma/xz files.

Public Functions

XzFile(const std::string &path, File::Mode mode)

Open a text file with name filename and mode mode.

virtual size_t read(char *data, size_t count) override

Fill up the data buffer reading at most count characters from the file.

Returns the amount of characters read. A return value of 0 indicates that the end of file has been reached.

Throws:

FileError – in case of I/O error

virtual void write(const char *data, size_t count) override

Write count characters starting at data to the file.

Throws:

FileError – if it could not write all of the data to the file

virtual void clear() noexcept override

clear error and eof flags on the file

virtual void seek(uint64_t position) override

Set file indicator to position characters after the start of the file.

Throws:

FileError – in case of I/O error while seeking

class Bz2File : public chemfiles::TextFileImpl

An implementation of TextFile for bzip2 files.

Public Functions

Bz2File(const std::string &path, File::Mode mode)

Open a text file with name filename and mode mode.

virtual size_t read(char *data, size_t count) override

Fill up the data buffer reading at most count characters from the file.

Returns the amount of characters read. A return value of 0 indicates that the end of file has been reached.

Throws:

FileError – in case of I/O error

virtual void write(const char *data, size_t count) override

Write count characters starting at data to the file.

Throws:

FileError – if it could not write all of the data to the file

virtual void clear() noexcept override

clear error and eof flags on the file

virtual void seek(uint64_t position) override

Set file indicator to position characters after the start of the file.

Throws:

FileError – in case of I/O error while seeking