Sane C++ Libraries
C++ Platform Abstraction Libraries
FileSystemIterator.h
1// Copyright (c) Stefano Cristiano
2// SPDX-License-Identifier: MIT
3#pragma once
4#include "../File/FileDescriptor.h"
5#include "../Foundation/OpaqueObject.h"
6#include "../Foundation/Result.h"
7#include "../Strings/StringView.h"
8
9namespace SC
10{
11struct FileSystemIterator;
12} // namespace SC
13
16
19
34{
35 private:
36 struct Internal;
37
38 public:
40 enum class Type
41 {
42 Directory,
43 File
44 };
45
47 struct Entry
48 {
52 Type type = Type::File;
53
55 bool isDirectory() const { return type == Type::Directory; }
56
57 private:
58 FileDescriptor parentFileDescriptor;
59 friend struct Internal;
60 };
61
63 struct Options
64 {
65 bool recursive = false;
66 bool forwardSlashes = false;
67 };
68
70
73
76 const Entry& get() const { return currentEntry; }
77
80 [[nodiscard]] Result checkErrors()
81 {
82 errorsChecked = true;
83 return errorResult;
84 }
85
89 [[nodiscard]] Result init(StringView directory);
90
92
95 [[nodiscard]] Result enumerateNext();
96
99 [[nodiscard]] Result recurseSubdirectory();
100
101 private:
102 struct InternalDefinition
103 {
104 static constexpr int Windows = 4272;
105 static constexpr int Apple = 2104;
106 static constexpr int Default = 2088;
107
108 static constexpr size_t Alignment = sizeof(uint64_t);
109
110 using Object = Internal;
111 };
112
113 public:
114 using InternalOpaque = OpaqueObject<InternalDefinition>;
115
116 private:
117 InternalOpaque internal;
118
119 Entry currentEntry;
120 Result errorResult = Result(true);
121 bool errorsChecked = false;
122};
123
unsigned long long uint64_t
Platform independent (8) bytes unsigned int.
Definition: PrimitiveTypes.h:42
unsigned int uint32_t
Platform independent (4) bytes unsigned int.
Definition: PrimitiveTypes.h:38
Wraps an OS File descriptor to read and write to and from it.
Definition: FileDescriptor.h:57
Contains information on a file or directory.
Definition: FileSystemIterator.h:48
bool isDirectory() const
Check if current entry is a directory.
Definition: FileSystemIterator.h:55
StringView name
Name of current entry (file with extension or directory)
Definition: FileSystemIterator.h:49
uint32_t level
Current level of nesting from start of iteration.
Definition: FileSystemIterator.h:51
StringView path
Absolute path of the current entry.
Definition: FileSystemIterator.h:50
Type type
Tells if it's a file or a directory.
Definition: FileSystemIterator.h:52
Options when iterating (recursive and other options)
Definition: FileSystemIterator.h:64
bool forwardSlashes
true will return paths forward slash / even on Windows
Definition: FileSystemIterator.h:66
bool recursive
true will recurse automatically into subdirectories
Definition: FileSystemIterator.h:65
Iterates files and directories inside a given path.
Definition: FileSystemIterator.h:34
const Entry & get() const
Get current Entry being iterated.
Definition: FileSystemIterator.h:76
Options options
Options to control recursive behaviour and other options.
Definition: FileSystemIterator.h:69
Result checkErrors()
Check if any error happened during iteration.
Definition: FileSystemIterator.h:80
Result init(StringView directory)
Initializes the iterator on a given directory.
Result recurseSubdirectory()
Recurse into current item (assuming Entry::isDirectory == true)
Result enumerateNext()
Returned string is only valid until next enumerateNext call and/or another init call.
~FileSystemIterator()
Destroys the FileSystemIterator object.
Type
Entry type (File or Directory)
Definition: FileSystemIterator.h:41
An ascii string used as boolean result. SC_TRY macro forwards errors to caller.
Definition: Result.h:11
Non-owning view over a range of characters with UTF Encoding.
Definition: StringView.h:47