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/Compiler.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 "../Foundation/Result.h"
11#include "../Foundation/StringPath.h"
12
13namespace SC
14{
15
18
21
40{
42 enum class Type
43 {
44 Directory,
45 File
46 };
47
50 {
51#if SC_PLATFORM_WINDOWS
52 void* fileDescriptor = (void*)(long long)-1;
53#else
54 int fileDescriptor = -1;
55 void* dirEnumerator = nullptr;
56#endif
57 size_t textLengthInBytes = 0;
58 bool gotDot1 = false;
59 bool gotDot2 = false;
60 };
61
63 struct Entry
64 {
68 Type type = Type::File;
69
71 bool isDirectory() const { return type == Type::Directory; }
72
73#if SC_PLATFORM_WINDOWS
74 void* parentFileDescriptor = nullptr;
75#else
76 int parentFileDescriptor = 0;
77#endif
78 };
79
81 struct Options
82 {
83 bool recursive = false;
84 bool forwardSlashes = false;
85 };
86
88
91
94 const Entry& get() const { return currentEntry; }
95
99 {
100 errorsChecked = true;
101 return errorResult;
102 }
103
108 Result init(StringSpan directory, Span<FolderState> recursiveEntries);
109
111
115
119
120 private:
121 static constexpr auto MaxPath = StringPath::MaxPath;
122 struct Internal;
123 struct RecurseStack
124 {
125 Span<FolderState> recursiveEntries;
126
127 int currentEntry = -1;
128
129 FolderState& back();
130
131 void pop_back();
132 Result push_back(const FolderState& other);
133 size_t size() const { return size_t(currentEntry + 1); }
134 bool isEmpty() const { return currentEntry == -1; }
135 };
136 RecurseStack recurseStack;
137
138 Entry currentEntry;
139 Result errorResult = Result(true);
140 bool errorsChecked = false;
141
142#if SC_PLATFORM_WINDOWS
143 bool expectDotDirectories = true;
144 StringPath currentPath;
145 uint64_t dirEnumeratorBuffer[592 / sizeof(uint64_t)];
146#else
147 StringPath currentPath;
148#endif
149
150 Result enumerateNextInternal(Entry& entry);
151 Result recurseSubdirectoryInternal(Entry& entry);
152};
153
155} // 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:64
bool isDirectory() const
Check if current entry is a directory.
Definition FileSystemIterator.h:71
uint32_t level
Current level of nesting from start of iteration.
Definition FileSystemIterator.h:67
StringSpan path
Absolute path of the current entry.
Definition FileSystemIterator.h:66
StringSpan name
Name of current entry (file with extension or directory)
Definition FileSystemIterator.h:65
Type type
Tells if it's a file or a directory.
Definition FileSystemIterator.h:68
Holds state of a folder when recursing into it to list its files.
Definition FileSystemIterator.h:50
Options when iterating (recursive and other options)
Definition FileSystemIterator.h:82
bool forwardSlashes
true will return paths forward slash / even on Windows
Definition FileSystemIterator.h:84
bool recursive
true will recurse automatically into subdirectories
Definition FileSystemIterator.h:83
Iterates files and directories inside a given path without allocating any memory.
Definition FileSystemIterator.h:40
const Entry & get() const
Get current Entry being iterated.
Definition FileSystemIterator.h:94
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:87
Result checkErrors()
Check if any error happened during iteration.
Definition FileSystemIterator.h:98
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:43
An ascii string used as boolean result. SC_TRY macro forwards errors to caller.
Definition Result.h:13
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:49
An read-only view over a string (to avoid including Strings library when parsing is not needed).
Definition StringSpan.h:37