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.

Depending on the kind of format, either a TextFile or a BinaryFile is needed, and will provide different informations.

Abstract classes

These classes only define the interface, which as to be implemented by all sub-classes.

class chemfiles::File

Abstract base class for file representation.

Public Type

Mode enum

Possible modes for opening a file.

Values:

  • 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

virtual bool is_open() = 0

Is the file opended ?

virtual void sync() = 0

Sync any content in the underlying buffer to the disk.

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.

Public Functions

virtual const std::string & getline() = 0

Read a line from the file.

virtual TextFile & operator>>(std::string & line) = 0

Read a line from the file, stream version.

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

Read n lines from the file.

virtual void rewind() = 0

Reset the file cursor.

virtual size_t nlines() = 0

Number of lines in the file.

virtual bool eof() = 0

Are we at the end of the file ?

virtual void writeline(const std::string &) = 0

Write a string to the file.

virtual void writelines(const std::vector< std::string > &) = 0

Write a vector of lines to the file.

TextFile & operator<<(const char * val)

Needed for resolving the overload ambiguity when using const char[] or const char* arguments.

class chemfiles::BinaryFile

Abstract base class for binary files representation

Because the binary formats can be everything, this class does not provides any of the usual methods for working with streams, and is not intended to be instanciated, but rather to serve as a base class for all the binary file classes.

Implemented classes

These classes implement the interface defined previously.

class chemfiles::BasicFile

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

Public Functions

BasicFile(const std::string & filename, File::Mode mode)

Open a text file with name filename and mode mode.

An FileError exception is thrown if the file does not exists in 'r' or 'a' mode.

virtual const std::string & getline()

Read a line from the file.

virtual BasicFile & operator>>(std::string & line)

Read a line from the file, stream version.

virtual const std::vector< std::string > & readlines(size_t n)

Read n lines from the file.

virtual void rewind()

Reset the file cursor.

virtual size_t nlines()

Number of lines in the file.

virtual bool is_open()

Is the file opended ?

virtual bool eof()

Are we at the end of the file ?

virtual void sync()

Sync any content in the underlying buffer to the disk.

virtual void writeline(const std::string &)

Write a string to the file.

virtual void writelines(const std::vector< std::string > &)

Write a vector of lines to the file.

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.

Public Type

NcMode enum

Possible file mode. By default, files are in the DATA mode.

Values:

  • DEFINE -

    Files in DEFINE mode can have there attributes, dimmensions and variables modified, but no data can be read or written using NcVariable.

  • DATA -

    Files in data mode acces read and write access to NcVariables.

Public Functions

void set_nc_mode(NcMode mode)

Set the file mode for this file.

NcMode nc_mode() const

Get the file mode for this file.

netcdf_id_t netcdf_id() const

Get the NetCDF id of this file.

std::string global_attribute(const std::string & name) const

Get a global string attribut from the file.

void add_global_attribute(const std::string & name, const std::string & value)

Create a global attribut in the file_.

size_t dimension(const std::string & name) const

Get the value of a specific dimmension.

void add_dimension(const std::string & name, size_t value = NC_UNLIMITED)

Create a dimmension with the specified value. If value == NC_UNLIMITED, then the dimension is infinite.

bool variable_exists(const std::string & name) const

Check if a variable exists.

template <typename NcType>
NcVariable< NcType > variable(const std::string & name)

Get a NetCDF variable.

template <typename NcType, typename... Dims>
NcVariable< NcType > add_variable(const std::string & name, Dims... dims)

Create a new variable of type NcType with name name along the dimensions in dims. dims must be string or string-like values.

virtual bool is_open()

Is the file opended ?

virtual void sync()

Sync any content in the underlying buffer to the disk.