3#include <Foundation/Containers/DynamicArray.h>
4#include <Foundation/IO/OSFile.h>
5#include <Foundation/Types/UniquePtr.h>
34 plResult AddFile(
plStringView sPath,
const T& value,
bool* out_pFileExistsAlready, T* out_pOldValue);
88 EnsureTrailingSlash(currentDirAbsPath);
90 if (m_sTopLevelDirPath.IsEmpty())
92 m_sTopLevelDirPath = currentDirAbsPath;
93 currentDirAbsPath.
Shrink(0, 1);
95 DirEntry* currentDir = &m_TopLevelDir;
100 files.
StartSearch(currentDirAbsPath.
GetData(), plFileSystemIteratorFlags::ReportFilesAndFoldersRecursive);
108 PL_ASSERT_DEV(m_dirStack.
GetCount() > 0,
"Unexpected file iteration order");
112 RemoveTrailingSlash(currentDirAbsPath);
119 EnsureTrailingSlash(subdirName);
120 auto insertIt = currentDir->m_subDirectories.Insert(subdirName, DirEntry());
121 currentDir = &insertIt.Value();
126 currentDir->m_files.Insert(std::move(stats.
m_sName), T{});
129 if (out_pDirectoryExistsAlready !=
nullptr)
131 *out_pDirectoryExistsAlready =
false;
136 DirEntry* parentDir = FindDirectory(currentDirAbsPath);
137 if (parentDir ==
nullptr)
142 if (out_pDirectoryExistsAlready !=
nullptr)
144 *out_pDirectoryExistsAlready = currentDirAbsPath.
IsEmpty();
147 while (!currentDirAbsPath.
IsEmpty())
151 auto insertIt = parentDir->m_subDirectories.Insert(subdirName, DirEntry());
152 parentDir = &insertIt.Value();
164 DirEntry* dir = FindDirectory(sPath);
172 while (szSlashPos !=
nullptr)
175 auto insertIt = dir->m_subDirectories.Insert(subdirName, DirEntry());
176 dir = &insertIt.Value();
181 auto it = dir->m_files.Find(sPath);
185 dir->m_files.Insert(sPath, value);
186 if (out_pFileExistsAlready !=
nullptr)
188 *out_pFileExistsAlready =
false;
193 if (out_pFileExistsAlready !=
nullptr)
195 *out_pFileExistsAlready =
true;
197 if (out_pOldValue !=
nullptr)
199 *out_pOldValue = it.Value();
210 DirEntry* dir = FindDirectory(sPath);
221 if (dir->m_files.GetCount() == 0)
226 auto it = dir->m_files.Find(sPath);
232 dir->m_files.Remove(it);
242 EnsureTrailingSlash(parentPath);
244 EnsureTrailingSlash(dirName);
246 DirEntry* parentDir = FindDirectory(parentPath);
247 if (parentDir ==
nullptr || !parentPath.
IsEmpty())
252 if (!parentDir->m_subDirectories.Remove(dirName))
266 EnsureTrailingSlash(sFromPath);
268 EnsureTrailingSlash(sFromName);
274 EnsureTrailingSlash(sToPath);
276 EnsureTrailingSlash(sToName);
278 DirEntry* moveFromDir = FindDirectory(sFromPath);
283 PL_ASSERT_DEV(sFromPath.
IsEmpty(),
"move from directory should fully exist");
285 DirEntry* moveToDir = FindDirectory(sToPath);
297 auto insertIt = moveToDir->m_subDirectories.Insert(subdirName, DirEntry());
298 moveToDir = &insertIt.Value();
305 auto fromIt = moveFromDir->m_subDirectories.Find(sFromName);
306 if (!fromIt.IsValid())
311 movedDir = std::move(fromIt.Value());
312 moveFromDir->m_subDirectories.Remove(fromIt);
315 moveToDir->m_subDirectories.Insert(sToName, std::move(movedDir));
322 template <
typename T>
323 struct plDirEnumerateState
339 DirEntry* dirToEnumerate = FindDirectory(sPath);
340 if (dirToEnumerate ==
nullptr)
348 DirEntry* currentDir = dirToEnumerate;
352 while (currentDir !=
nullptr)
356 DirEntry* nextDir = ¤tSubDirIt.
Value();
358 currentSubDirIt.
Next();
359 dirStack.
PushBack({currentDir, currentSubDirIt});
360 currentDir = nextDir;
365 for (
auto& file : currentDir->m_files)
369 callbackFunc(sFilePath, Type::File);
372 if (currentDir != dirToEnumerate)
378 callbackFunc(sPath, Type::Directory);
383 currentDir =
nullptr;
387 currentDir = dirStack.
PeekBack().dir;
388 currentSubDirIt = dirStack.
PeekBack().subDirIt;
411 DirEntry* currentDir = &m_TopLevelDir;
417 for (
auto& dir : currentDir->m_subDirectories)
421 currentDir = &dir.Value();
void PushBack(const T &value)
Pushes value at the end of the array.
Definition ArrayBase_inl.h:333
void PopBack(plUInt32 uiCountToRemove=1)
Removes count elements from the end of the array.
Definition ArrayBase_inl.h:379
T & PeekBack()
Returns the last element of the array.
Definition ArrayBase_inl.h:388
bool IsEmpty() const
Returns true, if the array does not contain any elements.
Definition ArrayBase_inl.h:178
plUInt32 GetCount() const
Returns the number of active elements in the array.
Definition ArrayBase_inl.h:172
An plFileSystemIterator allows to iterate over all files in a certain directory.
Definition OSFile.h:94
bool IsValid() const
Returns true if the iterator currently points to a valid file entry.
void StartSearch(plStringView sSearchTerm, plBitflags< plFileSystemIteratorFlags > flags=plFileSystemIteratorFlags::Default)
Starts a search at the given folder. Use * and ? as wildcards.
void Next()
Advances the iterator to the next file object. Might recurse into sub-folders.
const plFileStats & GetStats() const
Returns the file stats of the current object that the iterator points to.
Definition OSFile.h:130
Definition FileSystemMirror.h:13
A hybrid array uses in-place storage to handle the first few elements without any allocation....
Definition HybridArray.h:12
plStringBuilder is a class that is meant for creating and modifying strings.
Definition StringBuilder.h:35
bool TrimWordStart(plStringView sWord)
If the string starts with the given word (case insensitive), it is removed and the function returns t...
Definition StringBuilder.cpp:1202
const char * GetData() const
Returns a char pointer to the internal Utf8 data.
Definition StringBuilder_inl.h:148
plUInt32 GetElementCount() const
Returns the number of bytes that this string takes up.
Definition StringBuilder_inl.h:78
void Append(plUInt32 uiChar)
Appends a single Utf32 character.
Definition StringBuilder_inl.h:94
void PathParentDirectory(plUInt32 uiLevelsUp=1)
Modifies this string to point to the parent directory.
Definition StringBuilder.cpp:856
void Shrink(plUInt32 uiShrinkCharsFront, plUInt32 uiShrinkCharsBack)
Removes the first n and last m characters from this string.
Definition StringBuilder.cpp:393
plUInt32 GetCharacterCount() const
Returns the number of characters of which this string consists. Might be less than GetElementCount,...
Definition StringBuilder_inl.h:83
void MakeCleanPath()
Removes "../" where possible, replaces all path separators with /, removes double slashes.
Definition StringBuilder.cpp:751
void AppendPath(plStringView sPath1, plStringView sPath2={}, plStringView sPath3={}, plStringView sPath4={})
Appends several path pieces. Makes sure they are always properly separated by a slash.
Definition StringBuilder.cpp:866
static plUInt32 GetCharacterCount(const char *szUtf8, const char *pStringEnd=plUnicodeUtils::GetMaxStringEnd< char >())
Returns the number of characters (not Bytes!) in a Utf8 string (excluding the zero terminator),...
Definition StringUtils_inl.h:81
plStringView represent a read-only sub-string of a larger string, as it can store a dedicated string ...
Definition StringView.h:34
A generic delegate class which supports static functions and member functions.
Definition Delegate.h:76
Holds the stats for a file.
Definition OSFile.h:34
bool m_bIsDirectory
Whether the file object is a file or folder.
Definition OSFile.h:56
plString m_sName
The name of the file or folder that the stats are for. Does not include the parent path to it....
Definition OSFile.h:47
plStringBuilder m_sParentPath
Path to the parent folder. Append m_sName to m_sParentPath to obtain the full path.
Definition OSFile.h:43
Definition FileSystemMirror.h:22
PL_ALWAYS_INLINE bool IsValid() const
Checks whether this iterator points to a valid element.
Definition Map.h:27
PL_FORCE_INLINE const KeyType & Key() const
Returns the 'key' of the element that this iterator points to.
Definition Map.h:34
void Next()
Advances the iterator to the next element in the map. The iterator will not be valid anymore,...
Definition Map_inl.h:58
Forward Iterator to iterate over all elements in sorted order.
Definition Map.h:103
PL_FORCE_INLINE ValueType & Value()
Returns the 'value' of the element that this iterator points to.
Definition Map.h:119
Default enum for returning failure or success, instead of using a bool.
Definition Types.h:54
bool StartsWith(plStringView sStartsWith) const
Returns true, if this string starts with the given string.
Definition StringBase_inl.h:31
bool IsEmpty() const
Returns whether the string is an empty string.
Definition StringBase_inl.h:25
bool EndsWith(plStringView sEndsWith) const
Returns true, if this string ends with the given string.
Definition StringBase_inl.h:43
const char * FindSubString(plStringView sStringToFind, const char *szStartSearchAt=nullptr) const
Definition StringBase_inl.h:55