Gromacs  2024.3
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
List of all members | Public Member Functions | Static Public Member Functions
gmx::DirectoryEnumerator Class Reference

#include <gromacs/utility/include/gromacs/utility/directoryenumerator.h>

Description

Lists files in a directory.

If multiple threads share the same DirectoryEnumerator, they must take responsibility for their mutual synchronization, particularly with regard to calling nextFile().

Public Member Functions

 DirectoryEnumerator (const std::filesystem::path &dirname, bool bThrow=true)
 Opens a directory for listing. More...
 
std::optional
< std::filesystem::path > 
nextFile ()
 Gets next file in a directory. More...
 

Static Public Member Functions

static std::vector
< std::filesystem::path > 
enumerateFilesWithExtension (const std::filesystem::path &dirname, const std::string &extension, bool bThrow)
 Convenience function to list files with certain extension from a directory. More...
 

Constructor & Destructor Documentation

gmx::DirectoryEnumerator::DirectoryEnumerator ( const std::filesystem::path &  dirname,
bool  bThrow = true 
)
explicit

Opens a directory for listing.

Parameters
[in]dirnamePath to the directory to list.
[in]bThrowWhether failure to open the directory should throw.
Exceptions
std::bad_allocif out of memory.
FileIOErrorif opening the directory fails and bThrow == true

Member Function Documentation

std::vector< std::filesystem::path > gmx::DirectoryEnumerator::enumerateFilesWithExtension ( const std::filesystem::path &  dirname,
const std::string &  extension,
bool  bThrow 
)
static

Convenience function to list files with certain extension from a directory.

Parameters
[in]dirnamePath to the directory to list.
[in]extensionList files with the given extension (or suffix in file name).
[in]bThrowWhether failure to open the directory should throw.
Returns
List of files with the given extension in dirname.
Exceptions
std::bad_allocif out of memory.
FileIOErrorif opening the directory fails and bThrow == true.
FileIOErrorif some other I/O error occurs.
std::optional< std::filesystem::path > gmx::DirectoryEnumerator::nextFile ( )

Gets next file in a directory.

Returns
Optional name of next file in directory.
Exceptions
std::bad_allocif out of memory.
FileIOErrorif listing the next file fails.

If all files from the directory have been returned (or there are no files in the directory and this is the first call), the method returns std::nullopt. Otherwise, the return value is the optional filename without path information.

If bThrow passed to the constructor was false and the directory was not successfully opened, the first call to this function will return false.

This method is not thread safe when called on the same object by multiple threads. Such use requires external synchronization.


The documentation for this class was generated from the following files: