blob: 2ba2536dbb7b15d2165ecfbb65b3579d898756d4 [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
Saqib Khan23d4a7e2017-03-02 09:22:02 -060047 dirent* entry = readdir(dirp);
48 name = entry->d_name;
Brad Bishopca085122017-01-05 20:45:36 -050049
50 if (name == "." || name == "..")
51 {
52 return next(name);
53 }
54
Patrick Williams3667cf32015-10-20 22:39:11 -050055 return true;
56}
Brad Bishop03476f12016-12-19 13:09:12 -050057
58// vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4