lfilesystem  0.0.1
C++ filesystem library
lfilesystem_Directory.h
Go to the documentation of this file.
1 /*
2  * ======================================================================================
3  * __ ____ __ __ ____ ___
4  * ( ) (_ _)( \/ )( ___)/ __)
5  * )(__ _)(_ ) ( )__) \__ \
6  * (____)(____)(_/\/\_)(____)(___/
7  *
8  * This file is part of the Limes open source library and is licensed under the terms of the GNU Public License.
9  *
10  * Commercial licenses are available; contact the maintainers at ben.the.vining@gmail.com to inquire for details.
11  *
12  * ======================================================================================
13  */
14 
15 #pragma once
16 
17 #include <functional>
18 #include <cstdint>
19 #include <string>
20 #include <vector>
21 #include <memory>
22 #include <iterator>
23 #include "lfilesystem/lfilesystem_Export.h"
27 
34 namespace limes::files
35 {
36 
53 class LFILE_EXPORT Directory final : public FilesystemEntry
54 {
55 public:
57 
58  Directory (const Directory&) = default;
59  Directory& operator= (const Directory&) = default;
60 
61  Directory (Directory&&) = default;
62  Directory& operator= (Directory&&) = default;
63 
68 
70  using FileCallback = std::function<void (const File&)>;
71 
73  using DirectoryCallback = std::function<void (const Directory&)>;
74 
76  using SymLinkCallback = std::function<void (const SymLink&)>;
77 
79  using FilesystemEntryCallback = std::function<void (const FilesystemEntry&)>;
80 
82 
85 
88  Directory& operator= (const Path& newPath);
89  Directory& operator= (const std::string_view& newPath);
91 
94 
104  [[nodiscard]] bool contains (const FilesystemEntry& entry, std::size_t depthLimit = 50) const;
105 
111  [[nodiscard]] bool contains (const std::string_view& childName) const;
112 
114  [[nodiscard]] bool isEmpty() const;
115 
119  [[nodiscard]] std::uintmax_t sizeInBytes() const final;
120 
121  [[nodiscard]] bool isDirectory() const noexcept final;
122  [[nodiscard]] bool isFile() const noexcept final;
123  [[nodiscard]] bool isSymLink() const noexcept final;
124 
126 
132  bool createIfDoesntExist() const noexcept final;
133 
145  [[nodiscard]] Path getRelativePath (const Path& inputPath) const;
146 
149 
153  [[nodiscard]] File getChildFile (const std::string_view& filename, bool createIfNeeded = false) const;
154 
158  [[nodiscard]] std::vector<File> getChildFiles (bool recurse = true, bool includeHiddenFiles = true) const;
159 
164  void iterateFiles (FileCallback&& callback, bool recurse = true, bool includeHiddenFiles = true) const;
165 
167 
170 
172  [[nodiscard]] bool containsSubdirectories() const;
173 
177  [[nodiscard]] Directory getChildDirectory (const std::string_view& subdirectoryName, bool createIfNeeded = false) const;
178 
182  [[nodiscard]] std::vector<Directory> getChildDirectories (bool recurse = true, bool includeHiddenFiles = true) const;
183 
188  void iterateDirectories (DirectoryCallback&& callback, bool recurse = true, bool includeHiddenFiles = true) const;
189 
191 
194 
202  [[nodiscard]] SymLink createChildSymLink (const std::string_view& symLinkName,
203  const FilesystemEntry& symLinkTarget) const;
204 
208  [[nodiscard]] std::vector<SymLink> getChildSymLinks (bool recurse = true, bool includeHiddenFiles = true) const;
209 
214  void iterateSymLinks (SymLinkCallback&& callback, bool recurse = true, bool includeHiddenFiles = true) const;
215 
217 
220 
224  [[nodiscard]] FilesystemEntry getChild (const std::string_view& childName, bool createIfNeeded = false) const;
225 
229  [[nodiscard]] std::vector<FilesystemEntry> getAllChildren (bool recurse = true, bool includeHiddenFiles = true) const;
230 
236  void iterateAllChildren (FileCallback&& fileCallback,
237  DirectoryCallback&& directoryCallback,
238  SymLinkCallback&& symLinkCallback,
239  bool recurse = true,
240  bool includeHiddenFiles = true) const;
241 
246  void iterateAllChildren (FilesystemEntryCallback&& callback,
247  bool recurse = true,
248  bool includeHiddenFiles = true) const;
249 
251 
258  struct LFILE_EXPORT Iterator final
259  {
260  public:
261  using iterator_category = std::forward_iterator_tag;
262  using value_type = FilesystemEntry;
263  using difference_type = std::ptrdiff_t;
264  using pointer = FilesystemEntry*;
265  using reference = FilesystemEntry&;
266 
267  Iterator& operator++();
268  [[nodiscard]] Iterator operator++ (int);
269  bool operator== (Iterator other);
270  bool operator!= (Iterator other);
271 
272  reference operator*() const;
273  pointer operator->() const;
274 
275  explicit Iterator();
276 
277  Iterator (const Iterator&) = default;
278  Iterator& operator= (const Iterator&) = default;
279 
280  Iterator (Iterator&&) = default;
281  Iterator& operator= (Iterator&&) = default;
282 
283  private:
284  using VectorType = std::vector<FilesystemEntry>;
285 
286  explicit Iterator (VectorType&& v);
287 
288  std::shared_ptr<VectorType> entries { nullptr };
289 
290  VectorType::size_type idx { 0UL };
291 
292  friend class Directory;
293  };
294 
296  [[nodiscard]] Iterator begin() const;
297 
299  [[nodiscard]] Iterator end() const;
300 
303 
308  bool setAsWorkingDirectory() const;
309 
314  [[nodiscard]] bool isCurrentWorkingDirectory() const;
315 
317 };
318 
319 } // namespace limes::files
This class represents a directory on the filesystem.
bool contains(const std::string_view &childName) const
Returns true if this directory contains a child with the specified name.
std::function< void(const FilesystemEntry &)> FilesystemEntryCallback
A callback for FilesystemEntry objects.
std::function< void(const SymLink &)> SymLinkCallback
A callback for SymLink objects.
bool setAsWorkingDirectory() const
Sets this directory as the current working directory.
std::uintmax_t sizeInBytes() const final
Returns the size of this directory, calculated as the cumulative size of all of this directory's cont...
bool isEmpty() const
Returns true if this directory contains no children.
bool contains(const FilesystemEntry &entry, std::size_t depthLimit=50) const
Returns true if this directory contains the passed FilesystemEntry.
std::function< void(const File &)> FileCallback
A callback for File objects.
bool isCurrentWorkingDirectory() const
Returns true if this directory is the system's current working directory.
Iterator begin() const
Returns an iterator to the first entry in this directory.
Iterator end() const
Returns an iterator to the last entry in this directory.
std::function< void(const Directory &)> DirectoryCallback
A callback for Directory objects.
This class represents a file on the filesystem.
The base class for any kind of object on the filesystem.
FilesystemEntry()=default
Creates a FilesystemEntry with an empty path.
std::filesystem::path Path
Convenience typedef for filesystem paths.
This file defines the File and TempFile classes.
This file defines the FilesystemEntry class.
Filesystem utilities.
An iterator class that allows iterating a directory like a standard C++ container.