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
File
¶ Abstract base class for file representation.
Subclassed by chemfiles::NcFile, chemfiles::TextFile, chemfiles::TNGFile, 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
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 ‘
’ or ‘
’) in this buffer. It then returns
string_view
inside this buffer, removing the need to allocate a newstd::string
for 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
path
with the requestedmode
andcompression
method, 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
-
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_view
points 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
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
filename
and modemode
.
-
size_t
read
(char *data, size_t count)¶ Fill up the
data
buffer reading at mostcount
characters 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)¶ Write
count
characters starting atdata
to the file.- Exceptions
FileError
: if it could not write all of the data to the file
-
void
clear
()¶ clear error and eof flags on the file
-
-
class
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
filename
and modemode
.
-
size_t
read
(char *data, size_t count)¶ Fill up the
data
buffer reading at mostcount
characters 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)¶ Write
count
characters starting atdata
to the file.- Exceptions
FileError
: if it could not write all of the data to the file
-
void
clear
()¶ clear error and eof flags on the file
-
-
class
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
filename
and modemode
.
-
size_t
read
(char *data, size_t count)¶ Fill up the
data
buffer reading at mostcount
characters 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)¶ Write
count
characters starting atdata
to the file.- Exceptions
FileError
: if it could not write all of the data to the file
-
void
clear
()¶ clear error and eof flags on the file
-
-
class
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
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 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.
-
size_t
optional_dimension
(const std::string &name, size_t value) const¶ Get the value of an optional dimmension, or the default
value
if 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.
-
nc::netcdf_id_t