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

const std::string & 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, and any other.

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, const std::string & mode)

Open a text file.

Parameters
  • filename -

    The file path. In "w" or "a" modes, the file is created if it does not exist yet. In “r” mode, and exception is throwed is the file does not exist yet.

  • mode -

    Opening mode for the file. Supported modes are “r” for read, “w” for write, and “a” for append. “w” mode discard any previously existing file.

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

Wrapper around NetCDF 3 binary files.

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

Public Functions

string global_attribute(const string & name) const

Get a global attribut from the file_.

size_t dimension(const string & name) const

Get the value of a specific dimmension.

netCDF::NcVar variable(const string & name) const

Get a valid pointer to a NetCDF variable.

template <typename T>
T attribute(const string & var, const string & name) const

Get an attribute of type T and name name from the variable of name var.

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

Create a global attribut in the file_.

void add_dimension(const string & name, size_t value = static_cast< size_t >(-1))

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

template <typename T, class... Dims>
void add_variable(const string & name, Dims... dims)

Create a new variable of type T with name name along the dimensions in dims.

template <typename T>
void add_attribute(const string & var, const string & name, T value)

Add an attribute of type T and name name to the variable with name var.

virtual bool is_open()

Is the file opended ?

virtual void sync()

Sync any content in the underlying buffer to the disk.