Files

The files header file contains template functions that enables reading the directory content in efficient way.

namespace utils

Functions

template<typename OutputIt>
OutputIt read_directory(const std::string &path, OutputIt first)

Reads the contents of a directory and copies the paths to an output iterator.

Template Parameters:

OutputIt – Type of the output iterator.

Parameters:
  • path – Path to the directory to be read.

  • first – Output iterator to which the directory contents will be copied.

Throws:

std::filesystem::filesystem_error – if the directory cannot be read. For some compilers, this exception may not be thrown.

Returns:

Output iterator pointing to the end of the copied range.

template<typename OutputIt, typename UnaryPred>
OutputIt read_directory_if(const std::string &path, OutputIt first, UnaryPred p)

Reads the contents of a directory and copies the paths that satisfy a given predicate to an output iterator.

Template Parameters:
  • OutputIt – Type of the output iterator.

  • UnaryPred – Type of the unary predicate.

Parameters:
  • path – Path to the directory to be read.

  • first – Output iterator to which the directory contents will be copied.

  • p – Unary predicate that returns true for the elements to be copied.

Throws:

std::filesystem::filesystem_error – if the directory cannot be read. For some compilers, this exception may not be thrown.

Returns:

Output iterator pointing to the end of the copied range.

template<typename Container = std::vector<std::filesystem::path>>
Container read_directory(const std::string &directory)

Reads the contents of a directory and returns them in a container.

Template Parameters:

Container – Type of the container to store the directory contents. Defaults to std::vector<std::filesystem::path>.

Parameters:

directory – Path to the directory to be read.

Throws:

std::filesystem::filesystem_error – if the directory cannot be read. For some compilers, this exception may not be thrown.

Returns:

A container with the paths of the directory contents.

template<typename Container = std::vector<std::filesystem::path>, typename UnaryPred>
Container read_directory_if(const std::string &path, UnaryPred p)

Reads the contents of a directory and copies the paths that satisfy a given predicate to a container.

Template Parameters:
  • Container – Type of the container to store the directory contents. Defaults to std::vector<std::filesystem::path>.

  • UnaryPred – Type of the unary predicate.

Parameters:
  • path – Path to the directory to be read.

  • p – Unary predicate that returns true for the elements to be copied.

Throws:

std::filesystem::filesystem_error – if the directory cannot be read. For some compilers, this exception may not be thrown.

Returns:

A container with the paths of the directory contents that satisfy the predicate.

Usage

The following examples demonstrates how to use the files header file, but first let’s define the namespace alias:

namespace fs = std::filesystem;

Moreover, we assume that kDirPath is a valid directory path and contains the following files: * a.txt, * b.jpg, * c.html.

  • read_directory

std::vector<fs::path> result;
utils::read_directory(kDirPath, std::back_inserter(result));
std::sort(result.begin(), result.end());
std::cout << "result: ";
for (const auto& elem : result) {
    std::cout << elem << " ";
}

Output:

result: a.txt b.jpg c.html
  • read_directory_if

std::vector<std::filesystem::path> result;
auto predicate = [](const std::filesystem::path &p) {
  return p.extension() == ".txt";
};
utils::read_directory_if(kDirPath, std::back_inserter(result), predicate);
std::cout << "result: ";
for (const auto& elem : result) {
    std::cout << elem << " ";
}

Output:

result: a.txt
  • read_directory<typename Container>

auto result{
    utils::read_directory<std::vector<std::filesystem::path>>(kDirPath)};
std::sort(result.begin(), result.end());
std::cout << "result: ";
for (const auto& elem : result) {
    std::cout << elem << " ";
}

Output:

result: a.txt b.jpg c.html
  • read_directory_if<typename Container>

auto predicate = [](const std::filesystem::path &p) {
  return p.extension() == ".txt";
};
const auto result{utils::read_directory_if(kDirPath, predicate)};
std::cout << "result: ";
for (const auto& elem : result) {
    std::cout << elem << " ";
}

Output:

result: a.txt