blob: 82d4d6a6d1339faa6248dd4a9a827050af2bcd3a [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{
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}