coan  6.0.1
A C/C++ Configuration Analyzer
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
directory_nix.h
Go to the documentation of this file.
1 #ifndef DIRECTORY_NIX_H
2 #define DIRECTORY_NIX_H
3 #pragma once
4 /***************************************************************************
5  * Copyright (C) 2007-2013 Mike Kinghan, imk@burroingroingjoing.com *
6  * All rights reserved. *
7  * *
8  * Contributed originally by Mike Kinghan, imk@burroingroingjoing.com *
9  * *
10  * Redistribution and use in source and binary forms, with or without *
11  * modification, are permitted provided that the following conditions *
12  * are met: *
13  * *
14  * Redistributions of source code must retain the above copyright *
15  * notice, this list of conditions and the following disclaimer. *
16  * *
17  * Redistributions in binary form must reproduce the above copyright *
18  * notice, this list of conditions and the following disclaimer in the *
19  * documentation and/or other materials provided with the distribution. *
20  * *
21  * Neither the name of Mike Kinghan nor the names of its contributors *
22  * may be used to endorse or promote products derived from this software *
23  * without specific prior written permission. *
24  * *
25  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS *
26  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT *
27  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS *
28  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE *
29  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, *
30  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, *
31  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS *
32  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED *
33  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,*
34  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF *
35  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH *
36  * DAMAGE. *
37  * *
38  **************************************************************************/
39 #include "prohibit.h"
40 #include "directory_common.h"
41 #include <dirent.h>
42 #include <cerrno>
43 
48 namespace nix
50 {
51 
55 struct directory : common::directory, private no_copy {
59  explicit directory(std::string const & path)
60  : common::directory(path),_dir(nullptr) {
61  open();
62  }
63 
66  close();
67  }
68 
70  std::string const cur_memb() const {
71  return _entry.d_name;
72  }
73 
77  bool open() {
78  _dir = opendir(_abs_path.c_str());
79  return !_dir ? !(_last_error = errno) : true;
80  }
81 
85  bool close() {
86  return (_dir && closedir(_dir) != 0) ? !(_last_error = errno) : true;
87  }
88 
93  std::string next() {
94  struct dirent * entry = nullptr;
95  _entry.d_name[0] = '\0';
96  int error = readdir_r(_dir,&_entry,&entry);
97  if (error) {
99  } else if (entry) {
100  if (is_dot_name(_entry.d_name)) {
101  return next();
102  }
103  }
104  return _entry.d_name;
105  }
106 
107 private:
108 
110  DIR * _dir;
112  struct dirent _entry;
113 };
114 
115 } //namespace nix
116 
117 #endif
std::string const cur_memb() const
Get the leafname of the current member of the directory.
Definition: directory_nix.h:70
directory(std::string const &path)
Explicitly construct a directory given a path.
Definition: directory_nix.h:59
std::string next()
Move to the next entry in the directory.
Definition: directory_nix.h:93
static bool is_dot_name(char const *leafname)
Say whether a file leafname consists of 1 or 2 dots.
bool open()
Open the directory.
Definition: directory_nix.h:77
~directory()
Destructor.
Definition: directory_nix.h:65
struct common::directory encapsulates OS-neutral directory functionality.
A utility class to prevent copying of containing class.
Definition: prohibit.h:68
struct dirent _entry
The current entry in this directory.
bool close()
Close the directory.
Definition: directory_nix.h:85
std::string _abs_path
The absolute pathname of the directory.
An error diagnostic.
Class nix::directory encapsulates linux/unix-specific directory functionality.
Definition: directory_nix.h:55
Encapsulates a filesystem path.
Definition: path.h:59
unsigned _last_error
The last error system code returned by a directory operation.
DIR * _dir
The directory handle of this directory handle.