blob: f00ec8963de1e84c98d8cb974b8cf6f0bacdf6c2 [file] [log] [blame]
Brad Bishop29dbfa62016-12-19 13:39:57 -05001/**
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 Williams3667cf32015-10-20 22:39:11 -050016#include <cerrno>
17#include <cstring>
18#include <iostream>
Matthew Barth6292aee2016-10-06 10:15:48 -050019#include "directory.hpp"
Patrick Williams3667cf32015-10-20 22:39:11 -050020
21Directory::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
32Directory::~Directory()
33{
34 if (dirp)
35 {
36 closedir(dirp);
37 }
38}
39
40bool Directory::next(std::string& name)
41{
Brad Bishop6bb97a92016-12-19 13:06:40 -050042 if (!dirp)
43 {
44 return false;
45 }
Patrick Williams3667cf32015-10-20 22:39:11 -050046
47 dirent entry;
48 dirent* result;
49
50 auto rc = readdir_r(dirp, &entry, &result);
51
Brad Bishop6bb97a92016-12-19 13:06:40 -050052 if ((rc) || (NULL == result))
53 {
54 return false;
55 }
Patrick Williams3667cf32015-10-20 22:39:11 -050056
57 name = entry.d_name;
Brad Bishopca085122017-01-05 20:45:36 -050058
59 if (name == "." || name == "..")
60 {
61 return next(name);
62 }
63
Patrick Williams3667cf32015-10-20 22:39:11 -050064 return true;
65}
Brad Bishop03476f12016-12-19 13:09:12 -050066
67// vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4