xref: /freebsd/contrib/kyua/utils/fs/directory.hpp (revision 02e9120893770924227138ba49df1edb3896112a)
1 // Copyright 2015 The Kyua Authors.
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
6 // met:
7 //
8 // * Redistributions of source code must retain the above copyright
9 //   notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above copyright
11 //   notice, this list of conditions and the following disclaimer in the
12 //   documentation and/or other materials provided with the distribution.
13 // * Neither the name of Google Inc. nor the names of its contributors
14 //   may be used to endorse or promote products derived from this software
15 //   without specific prior written permission.
16 //
17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 
29 /// \file utils/fs/directory.hpp
30 /// Provides the utils::fs::directory class.
31 
32 #if !defined(UTILS_FS_DIRECTORY_HPP)
33 #define UTILS_FS_DIRECTORY_HPP
34 
35 #include "utils/fs/directory_fwd.hpp"
36 
37 #include <memory>
38 #include <ostream>
39 #include <string>
40 
41 #include "utils/fs/path_fwd.hpp"
42 
43 namespace utils {
44 namespace fs {
45 
46 
47 /// Representation of a single directory entry.
48 struct directory_entry {
49     /// Name of the directory entry.
50     std::string name;
51 
52     explicit directory_entry(const std::string&);
53 
54     bool operator==(const directory_entry&) const;
55     bool operator!=(const directory_entry&) const;
56     bool operator<(const directory_entry&) const;
57 };
58 
59 
60 std::ostream& operator<<(std::ostream&, const directory_entry&);
61 
62 
63 namespace detail {
64 
65 
66 /// Forward directory iterator.
67 class directory_iterator {
68     struct impl;
69 
70     /// Internal implementation details.
71     std::shared_ptr< impl > _pimpl;
72 
73     directory_iterator(std::shared_ptr< impl >);
74 
75     friend class fs::directory;
76     static directory_iterator new_begin(const path&);
77     static directory_iterator new_end(void);
78 
79 public:
80     ~directory_iterator();
81 
82     bool operator==(const directory_iterator&) const;
83     bool operator!=(const directory_iterator&) const;
84     directory_iterator& operator++(void);
85 
86     const directory_entry& operator*(void) const;
87     const directory_entry* operator->(void) const;
88 };
89 
90 
91 }  // namespace detail
92 
93 
94 /// Representation of a local filesystem directory.
95 ///
96 /// This class is pretty much stateless.  All the directory manipulation
97 /// operations happen within the iterator.
98 class directory {
99 public:
100     /// Public type for a constant forward directory iterator.
101     typedef detail::directory_iterator const_iterator;
102 
103 private:
104     struct impl;
105 
106     /// Internal implementation details.
107     std::shared_ptr< impl > _pimpl;
108 
109 public:
110     explicit directory(const path&);
111 
112     const_iterator begin(void) const;
113     const_iterator end(void) const;
114 };
115 
116 
117 }  // namespace fs
118 }  // namespace utils
119 
120 #endif  // !defined(UTILS_FS_DIRECTORY_HPP)
121