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.
These classes only define the interface, which as to be implemented by all sub-classes.
chemfiles::File
Abstract base class for file representation.
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
getline
() = 0Read a line from the file.
operator>>
(std::string & line) = 0Read a line from the file, stream version.
readlines
(size_t n) = 0Read n
lines from the file.
rewind
() = 0Reset the file cursor.
nlines
() = 0Number of lines in the file.
eof
() = 0Are we at the end of the file ?
writeline
(const std::string &) = 0Write a string to the file.
writelines
(const std::vector< std::string > &) = 0Write a vector of lines to the file.
operator<<
(const char * val)Needed for resolving the overload ambiguity when using const char[] or const char* arguments.
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.
These classes implement the interface defined previously.
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.
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.
getline
()Read a line from the file.
operator>>
(std::string & line)Read a line from the file, stream version.
readlines
(size_t n)Read n
lines from the file.
rewind
()Reset the file cursor.
nlines
()Number of lines in the file.
is_open
()Is the file opended ?
eof
()Are we at the end of the file ?
sync
()Sync any content in the underlying buffer to the disk.
writeline
(const std::string &)Write a string to the file.
writelines
(const std::vector< std::string > &)Write a vector of lines to the file.
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
global_attribute
(const string & name) constGet a global attribut from the file.
dimension
(const string & name) constGet the value of a specific dimmension.
variable
(const string & name) constGet a valid pointer to a NetCDF variable.
attribute
(const string & var, const string & name) constGet an attribute of type T
and name name
from the variable of name var
.
add_global_attribute
(const string & name, const string & value)Create a global attribut in the file.
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
add_variable
(const string & name, Dims... dims)Create a new variable of type T
with name name
along the dimensions in dims
.
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
.
is_open
()Is the file opended ?
sync
()Sync any content in the underlying buffer to the disk.