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 | { |
| 42 | if (!dirp) return false; |
| 43 | |
| 44 | dirent entry; |
| 45 | dirent* result; |
| 46 | |
| 47 | auto rc = readdir_r(dirp, &entry, &result); |
| 48 | |
| 49 | if ((rc) || (NULL == result)) return false; |
| 50 | |
| 51 | name = entry.d_name; |
| 52 | return true; |
| 53 | } |