Brad Bishop | 29dbfa6 | 2016-12-19 13:39:57 -0500 | [diff] [blame] | 1 | /** |
| 2 | * Copyright © 2016 IBM Corporation |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
Patrick Williams | 3667cf3 | 2015-10-20 22:39:11 -0500 | [diff] [blame] | 16 | #include <cerrno> |
| 17 | #include <cstring> |
| 18 | #include <iostream> |
Matthew Barth | 6292aee | 2016-10-06 10:15:48 -0500 | [diff] [blame] | 19 | #include "directory.hpp" |
Patrick Williams | 3667cf3 | 2015-10-20 22:39:11 -0500 | [diff] [blame] | 20 | |
| 21 | Directory::Directory(const std::string& path) : entry(nullptr) |
| 22 | { |
| 23 | dirp = opendir(path.c_str()); |
| 24 | if (NULL == dirp) |
| 25 | { |
| 26 | auto e = errno; |
| 27 | std::cerr << "Error opening directory " << path.c_str() |
| 28 | << " : " << strerror(e) << std::endl; |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | Directory::~Directory() |
| 33 | { |
| 34 | if (dirp) |
| 35 | { |
| 36 | closedir(dirp); |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | bool Directory::next(std::string& name) |
| 41 | { |
Brad Bishop | 6bb97a9 | 2016-12-19 13:06:40 -0500 | [diff] [blame] | 42 | if (!dirp) |
| 43 | { |
| 44 | return false; |
| 45 | } |
Patrick Williams | 3667cf3 | 2015-10-20 22:39:11 -0500 | [diff] [blame] | 46 | |
| 47 | dirent entry; |
| 48 | dirent* result; |
| 49 | |
| 50 | auto rc = readdir_r(dirp, &entry, &result); |
| 51 | |
Brad Bishop | 6bb97a9 | 2016-12-19 13:06:40 -0500 | [diff] [blame] | 52 | if ((rc) || (NULL == result)) |
| 53 | { |
| 54 | return false; |
| 55 | } |
Patrick Williams | 3667cf3 | 2015-10-20 22:39:11 -0500 | [diff] [blame] | 56 | |
| 57 | name = entry.d_name; |
Brad Bishop | ca08512 | 2017-01-05 20:45:36 -0500 | [diff] [blame] | 58 | |
| 59 | if (name == "." || name == "..") |
| 60 | { |
| 61 | return next(name); |
| 62 | } |
| 63 | |
Patrick Williams | 3667cf3 | 2015-10-20 22:39:11 -0500 | [diff] [blame] | 64 | return true; |
| 65 | } |
Brad Bishop | 03476f1 | 2016-12-19 13:09:12 -0500 | [diff] [blame] | 66 | |
| 67 | // vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 |