File classes

The File classes provide abstraction of the IO operation, allowing for the same format to be used with on-disk files, network files, memory-mapped files, compressed files, etc.

Interface for files

class chemfiles::File

Abstract base class for file representation.

Subclassed by chemfiles::NcFile, chemfiles::TextFile, chemfiles::TNGFile

Public Types

enum Mode

Possible modes for opening a file.


READ = 'r'

Open in read-only mode.

WRITE = 'w'

Open in read-write mode, and replace the file if it is already present.

APPEND = 'a'

Open in read-write mode, and append new data at the end of the file.

Public Functions

const std::string &filename() const

File name, i.e. complete path to this file on disk.

Mode mode() const

File opening mode.

class chemfiles::TextFile

Abstract base class representing a text file. This class is inteded to be inherited by any form of text files: compressed files, memory-mapped files, etc.

All failling operations should throw a FileError instead of waiting for the user of the class to the current state.

Inherits from chemfiles::File, iostream

Subclassed by chemfiles::BasicFile

Public Functions

virtual std::string readline() = 0

Read a line from the file.

virtual std::vector<std::string> readlines(size_t n) = 0

Read n lines from the file.

virtual void rewind() = 0

Reset the file cursor.

virtual bool eof() = 0

Are we at the end of the file ?

Public Static Functions

std::unique_ptr<TextFile> create(const std::string &path, File::Mode mode)

Open the most adaptated text file class for the given path and mode

Implemented classes

These classes implement the File interface defined previously.

class chemfiles::BasicFile

Basic text file, only a thin wrapper on top of standard C++ fstreams.

Inherits from chemfiles::TextFile

class chemfiles::NcFile

RAII wrapper around NetCDF 3 binary files

This interface only provide basic functionalities needed by the Amber NetCDF format. All the operation are guaranteed to return a valid value or throw an error.

The template functions are manually specialized for float and char data types.

Inherits from chemfiles::File

class chemfiles::TNGFile

Simple RAII capsule for tng_trajectory_t, handling the creation and destruction of the file as needed.

Inherits from chemfiles::File