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::TNGFile, chemfiles::TextFile, chemfiles::XDRFile
Public Types
Public Functions
-
const std::string &
path() const¶ Get the file path used to open this file.
-
Compression
compression() const¶ Get the compression used to open this file.
-
const std::string &
-
class
chemfiles::TextFile: public chemfiles::File¶ Line-oriented text file reader and writer, using buffered read and fast lines search.
This class reads text file line by line. It does so in a efficient way by storing a chunk of the file in a memory buffer, and searching for new line indicators (either
\nor\r\n) in this buffer. It then returnsstring_viewinside this buffer, removing the need to allocate a newstd::stringfor each line.Writing to the files is done without buffering or considering lines.
Public Functions
-
TextFile(std::string path, File::Mode mode, File::Compression compression)¶ Open the file at the given
pathwith the requestedmodeandcompressionmethod, picking the bestTextFileImpl.- Exceptions
FileError: if the file does not exist in read mode, or if the file is invalid for the given compression method
Use the given
MemoryBufferwith the requestedmodeandcompressionmethod. AMemoryFilewill be used as theTextFileImpl.- Exceptions
FileError: if the file mode is append, or if trying to write to a compressed file.
-
uint64_t
tellpos() const¶ Returns the current position indicator, i.e. the number of characters from the beginning of the file.
-
void
seekpos(uint64_t position)¶ Set the position indicator to
position.
-
void
rewind()¶ Reset the position indicator to the beginning of the file, and clear end-of-file flag.
-
bool
eof() const¶ Check if end-of-file has been reached.
-
void
clear()¶ Clear end-of-file flags on the file.
-
string_view
readline()¶ Read a single line from the file. The returned
string_viewpoints into an internal buffer, and can be invalidated after another call toreadline. If storing the line is necessary, transform it to an owned string usingstring_view::to_string().
-
std::string
readall()¶ Read the full file into an owned string. This is a convenience method for format that need the full file read before parsing can start.
-
Implemented classes¶
These classes implement the File interface defined previously.
-
class
chemfiles::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
filenameand modemode.
-
size_t
read(char *data, size_t count) override¶ Fill up the
databuffer reading at mostcountcharacters from the file.Returns the amount of characters read. A return value of 0 indicates that the end of file has been reached.
- Exceptions
FileError: in case of I/O error
-
void
write(const char *data, size_t count) override¶ Write
countcharacters starting atdatato the file.- Exceptions
FileError: if it could not write all of the data to the file
-
void
clear() noexcept override¶ clear error and eof flags on the file
-
-
class
chemfiles::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
filenameand modemode.
-
size_t
read(char *data, size_t count) override¶ Fill up the
databuffer reading at mostcountcharacters from the file.Returns the amount of characters read. A return value of 0 indicates that the end of file has been reached.
- Exceptions
FileError: in case of I/O error
-
void
write(const char *data, size_t count) override¶ Write
countcharacters starting atdatato the file.- Exceptions
FileError: if it could not write all of the data to the file
-
void
clear() noexcept override¶ clear error and eof flags on the file
-
-
class
chemfiles::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
filenameand modemode.
-
size_t
read(char *data, size_t count) override¶ Fill up the
databuffer reading at mostcountcharacters from the file.Returns the amount of characters read. A return value of 0 indicates that the end of file has been reached.
- Exceptions
FileError: in case of I/O error
-
void
write(const char *data, size_t count) override¶ Write
countcharacters starting atdatato the file.- Exceptions
FileError: if it could not write all of the data to the file
-
void
clear() noexcept override¶ clear error and eof flags on the file
-
-
class
chemfiles::NcFile: public chemfiles::File¶ 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 Types
-
enum
NcMode¶ Possible file mode. By default, files are in the DATA mode.
Values:
-
enumerator
DEFINE¶ Files in DEFINE mode can have there attributes, dimmensions and variables modified, but no data can be read or written using NcVariable.
-
enumerator
DATA¶ Files in data mode acces read and write access to NcVariables.
-
enumerator
Public Functions
-
nc::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 attribute from the file.
-
void
add_global_attribute(const std::string &name, const std::string &value)¶ Create a global attribute in the file_.
-
size_t
dimension(const std::string &name) const¶ Get the value of a specific dimmension.
-
size_t
optional_dimension(const std::string &name, size_t value) const¶ Get the value of an optional dimmension, or the default
valueif the dimmension is not in the file
-
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.
-
enum
-
class
TNGFile: public chemfiles::File¶ Simple RAII capsule for
tng_trajectory_t, handling the creation and destruction of the file as needed.
-
class
chemfiles::XDRFile: public chemfiles::File¶ Simple RAII capsule for
XDRFILE*, handling the creation and destruction of the file as needed. Reads the file header and stores the offsets for individual frames.Public Types
Public Functions
-
unsigned long
nframes() const¶ get the number of frames/steps in the file, as indicated in the file header
-
int64_t
offset(size_t step) const¶ get the offset corresponding to a specific frame/step
-
int
natoms() const¶ get the number of atoms, as indicated in the file header
-
void
set_natoms(int natoms)¶ set the number of atoms
-
unsigned long