Wayverb
dir.h
1 #pragma once
2 
3 #include <dirent.h>
4 
5 #include <memory>
6 #include <string>
7 #include <vector>
8 
9 class dir final {
10 public:
11  dir(const char* name)
12  : ptr_{opendir(name)} {
13  if (!ptr_) {
14  throw std::runtime_error{"Unable to open directory."};
15  }
16  }
17 
18  auto read() { return readdir(ptr_.get()); }
19 
20  void rewind() { rewinddir(ptr_.get()); }
21 
22 private:
23  struct destructor final {
24  template <typename T>
25  void operator()(T t) {
26  closedir(t);
27  }
28  };
29 
30  std::unique_ptr<DIR, destructor> ptr_;
31 };
32 
33 inline auto list_directory(const char* name) {
34  dir directory{name};
35  std::vector<std::string> ret{};
36 
37  while (const auto ptr = directory.read()) {
38  ret.emplace_back(ptr->d_name, ptr->d_name + ptr->d_namlen);
39  }
40 
41  return ret;
42 }
Definition: dir.h:9