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 "../Common/CompilerMacrosExport.h"
5#ifndef SC_EXPORT_LIBRARY_FILE_SYSTEM_ITERATOR
6#define SC_EXPORT_LIBRARY_FILE_SYSTEM_ITERATOR 0
7#endif
8#define SC_FILE_SYSTEM_ITERATOR_EXPORT SC_COMPILER_LIBRARY_EXPORT(SC_EXPORT_LIBRARY_FILE_SYSTEM_ITERATOR)
9
10#include "../Common/Assert.h"
11#include "../Common/IGrowableBufferStringPath.h"
12#include "../Common/Result.h"
13
14namespace SC
15{
16SC_DECLARE_ASSERT_PROVIDER(FileSystemIteratorAssert, SC_FILE_SYSTEM_ITERATOR_EXPORT);
17
18#define SC_FILE_SYSTEM_ITERATOR_ASSERT_RELEASE(e) SC_ASSERT_PROVIDER_RELEASE(SC::FileSystemIteratorAssert, e)
19#define SC_FILE_SYSTEM_ITERATOR_ASSERT_DEBUG(e) SC_ASSERT_PROVIDER_DEBUG(SC::FileSystemIteratorAssert, e)
20#define SC_FILE_SYSTEM_ITERATOR_TRUST_RESULT(expression) SC_FILE_SYSTEM_ITERATOR_ASSERT_RELEASE(expression)
21
24
27
46{
48 enum class Type
49 {
50 Directory,
51 File
52 };
53
56 {
57#if SC_PLATFORM_WINDOWS
58 void* fileDescriptor = (void*)(long long)-1;
59#else
60 int fileDescriptor = -1;
61 void* dirEnumerator = nullptr;
62#endif
63 size_t textLengthInBytes = 0;
64 bool gotDot1 = false;
65 bool gotDot2 = false;
66 };
67
69 struct Entry
70 {
71 StringSpan name;
72 StringSpan path;
73 uint32_t level = 0;
74 Type type = Type::File;
75
77 bool isDirectory() const { return type == Type::Directory; }
78
79#if SC_PLATFORM_WINDOWS
80 void* parentFileDescriptor = nullptr;
81#else
82 int parentFileDescriptor = 0;
83#endif
84 };
85
87 struct Options
88 {
89 bool recursive = false;
90 bool forwardSlashes = false;
91 };
92
94
97
100 const Entry& get() const { return currentEntry; }
101
104 Result checkErrors()
105 {
106 errorsChecked = true;
107 return errorResult;
108 }
109
114 Result init(StringSpan directory, Span<FolderState> recursiveEntries);
115
117
121
125
126 private:
127 static constexpr auto MaxPath = StringPath::MaxPath;
128 struct Internal;
129 struct RecurseStack
130 {
131 Span<FolderState> recursiveEntries;
132
133 int currentEntry = -1;
134
135 FolderState& back();
136
137 void pop_back();
138 Result push_back(const FolderState& other);
139 size_t size() const { return size_t(currentEntry + 1); }
140 bool isEmpty() const { return currentEntry == -1; }
141 };
142 RecurseStack recurseStack;
143
144 Entry currentEntry;
145 Result errorResult = Result(true);
146 bool errorsChecked = false;
147
148#if SC_PLATFORM_WINDOWS
149 bool expectDotDirectories = true;
150 StringPath currentPath;
151 StringPath outputPath;
152 uint64_t dirEnumeratorBuffer[592 / sizeof(uint64_t)];
153#else
154 StringPath currentPath;
155#endif
156
157 Result enumerateNextInternal(Entry& entry);
158 Result recurseSubdirectoryInternal(Entry& entry);
159};
160
162} // namespace SC
Contains information on a file or directory.
Definition FileSystemIterator.h:70
bool isDirectory() const
Check if current entry is a directory.
Definition FileSystemIterator.h:77
uint32_t level
Current level of nesting from start of iteration.
Definition FileSystemIterator.h:73
StringSpan path
Absolute path of the current entry.
Definition FileSystemIterator.h:72
StringSpan name
Name of current entry (file with extension or directory)
Definition FileSystemIterator.h:71
Type type
Tells if it's a file or a directory.
Definition FileSystemIterator.h:74
Holds state of a folder when recursing into it to list its files.
Definition FileSystemIterator.h:56
Options when iterating (recursive and other options)
Definition FileSystemIterator.h:88
bool forwardSlashes
true will return paths forward slash / even on Windows
Definition FileSystemIterator.h:90
bool recursive
true will recurse automatically into subdirectories
Definition FileSystemIterator.h:89
Iterates files and directories inside a given path without allocating any memory.
Definition FileSystemIterator.h:46
const Entry & get() const
Get current Entry being iterated.
Definition FileSystemIterator.h:100
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:93
Result checkErrors()
Check if any error happened during iteration.
Definition FileSystemIterator.h:104
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:49