Patrick Williams | 3667cf3 | 2015-10-20 22:39:11 -0500 | [diff] [blame^] | 1 | #include <cerrno> |
| 2 | #include <cstring> |
| 3 | #include <iostream> |
| 4 | #include "directory.H" |
| 5 | |
| 6 | Directory::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 | |
| 17 | Directory::~Directory() |
| 18 | { |
| 19 | if (dirp) |
| 20 | { |
| 21 | closedir(dirp); |
| 22 | } |
| 23 | } |
| 24 | |
| 25 | bool 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 | } |