blob: 5be9ff1186ffe711bdccfd671f77ac3e1d5b692d [file] [log] [blame]
Patrick Williams3667cf32015-10-20 22:39:11 -05001#include <cerrno>
2#include <cstring>
3#include <iostream>
4#include "directory.H"
5
6Directory::Directory(const std::string& path) : entry(nullptr)
7{
8 dirp = opendir(path.c_str());
9 if (NULL == dirp)
10 {
11 auto e = errno;
12 std::cerr << "Error opening directory " << path.c_str()
13 << " : " << strerror(e) << std::endl;
14 }
15}
16
17Directory::~Directory()
18{
19 if (dirp)
20 {
21 closedir(dirp);
22 }
23}
24
25bool Directory::next(std::string& name)
26{
27 if (!dirp) return false;
28
29 dirent entry;
30 dirent* result;
31
32 auto rc = readdir_r(dirp, &entry, &result);
33
34 if ((rc) || (NULL == result)) return false;
35
36 name = entry.d_name;
37 return true;
38}