openMSX
ReadDir.hh
Go to the documentation of this file.
1#ifndef READDIR_HH
2#define READDIR_HH
3
4#include "direntp.hh"
5#include "zstring_view.hh"
6
7#include <memory>
8#include <sys/types.h>
9
10namespace openmsx {
11
18{
19public:
20 explicit ReadDir(zstring_view directory)
21 : dir(opendir(directory.empty() ? "." : directory.c_str())) {}
22
27 [[nodiscard]] struct dirent* getEntry() {
28 if (!dir) return nullptr;
29 return readdir(dir.get());
30 }
31
34 [[nodiscard]] bool isValid() const { return dir != nullptr; }
35
36private:
37 struct CloseDir {
38 void operator()(DIR* d) const { if (d) closedir(d); }
39 };
40 using DIR_t = std::unique_ptr<DIR, CloseDir>;
41 DIR_t dir;
42};
43
44} // namespace openmsx
45
46#endif
Simple wrapper around opendir() / readdir() / closedir() functions.
Definition ReadDir.hh:18
struct dirent * getEntry()
Get directory entry for next file.
Definition ReadDir.hh:27
ReadDir(zstring_view directory)
Definition ReadDir.hh:20
bool isValid() const
Is the given directory valid (does it exist)?
Definition ReadDir.hh:34
Like std::string_view, but with the extra guarantee that it refers to a zero-terminated string.
This file implemented 3 utility functions:
Definition Autofire.cc:11