Sane C++ Libraries
C++ Platform Abstraction Libraries
Loading...
Searching...
No Matches
FileSystemIterator.h
1// Copyright (c) Stefano Cristiano
2// SPDX-License-Identifier: MIT
3#pragma once
4#include "../Foundation/Result.h"
5#include "../Foundation/StringPath.h"
6
7namespace SC
8{
9
12
15
34{
36 enum class Type
37 {
38 Directory,
39 File
40 };
41
44 {
45#if SC_PLATFORM_WINDOWS
46 void* fileDescriptor = (void*)(long long)-1;
47#else
48 int fileDescriptor = -1;
49 void* dirEnumerator = nullptr;
50#endif
51 size_t textLengthInBytes = 0;
52 bool gotDot1 = false;
53 bool gotDot2 = false;
54 };
55
57 struct Entry
58 {
62 Type type = Type::File;
63
65 bool isDirectory() const { return type == Type::Directory; }
66
67#if SC_PLATFORM_WINDOWS
68 void* parentFileDescriptor = nullptr;
69#else
70 int parentFileDescriptor = 0;
71#endif
72 };
73
75 struct Options
76 {
77 bool recursive = false;
78 bool forwardSlashes = false;
79 };
80
82
85
88 const Entry& get() const { return currentEntry; }
89
93 {
94 errorsChecked = true;
95 return errorResult;
96 }
97
102 Result init(StringSpan directory, Span<FolderState> recursiveEntries);
103
105
109
113
114 private:
115 static constexpr auto MaxPath = StringPath::MaxPath;
116 struct Internal;
117 struct RecurseStack
118 {
119 Span<FolderState> recursiveEntries;
120
121 int currentEntry = -1;
122
123 FolderState& back();
124
125 void pop_back();
126 Result push_back(const FolderState& other);
127 size_t size() const { return size_t(currentEntry + 1); }
128 bool isEmpty() const { return currentEntry == -1; }
129 };
130 RecurseStack recurseStack;
131
132 Entry currentEntry;
133 Result errorResult = Result(true);
134 bool errorsChecked = false;
135
136#if SC_PLATFORM_WINDOWS
137 bool expectDotDirectories = true;
138 wchar_t currentPathString[MaxPath];
139 uint64_t dirEnumeratorBuffer[592 / sizeof(uint64_t)];
140#else
141 char currentPathString[MaxPath];
142#endif
143 Result enumerateNextInternal(Entry& entry);
144 Result recurseSubdirectoryInternal(Entry& entry);
145};
146
148} // namespace SC
unsigned long size_t
Platform independent unsigned size type.
Definition PrimitiveTypes.h:56
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
Contains information on a file or directory.
Definition FileSystemIterator.h:58
bool isDirectory() const
Check if current entry is a directory.
Definition FileSystemIterator.h:65
uint32_t level
Current level of nesting from start of iteration.
Definition FileSystemIterator.h:61
StringSpan path
Absolute path of the current entry.
Definition FileSystemIterator.h:60
StringSpan name
Name of current entry (file with extension or directory)
Definition FileSystemIterator.h:59
Type type
Tells if it's a file or a directory.
Definition FileSystemIterator.h:62
Holds state of a folder when recursing into it to list its files.
Definition FileSystemIterator.h:44
Options when iterating (recursive and other options)
Definition FileSystemIterator.h:76
bool forwardSlashes
true will return paths forward slash / even on Windows
Definition FileSystemIterator.h:78
bool recursive
true will recurse automatically into subdirectories
Definition FileSystemIterator.h:77
Iterates files and directories inside a given path without allocating any memory.
Definition FileSystemIterator.h:34
const Entry & get() const
Get current Entry being iterated.
Definition FileSystemIterator.h:88
Result init(StringSpan directory, Span< FolderState > recursiveEntries)
Initializes the iterator on a given directory.
Options options
Options to control recursive behaviour and other options.
Definition FileSystemIterator.h:81
Result checkErrors()
Check if any error happened during iteration.
Definition FileSystemIterator.h:92
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:37
An ascii string used as boolean result. SC_TRY macro forwards errors to caller.
Definition Result.h:12
View over a contiguous sequence of items (pointer + size in elements).
Definition Span.h:29
static constexpr size_t MaxPath
Maximum size of paths on current native platform.
Definition StringPath.h:17
An read-only view over a string (to avoid including Strings library when parsing is not needed).
Definition StringSpan.h:31