AuroraRuntime/Source/IO/FS/FSRecursion.cpp
Jamie Reece Wilson 83f34b0c47 [*] I was right. String views are [mostly] pointless (*)
03:28:55:638  17>2 of 53388 functions (<0.1%) were compiled, the rest were copied from previous compilation.
03:28:55:638  17>  0 functions were new in current compilation
03:28:55:638  17>  65 functions had inline decision re-evaluated but remain unchanged
03:28:56:749  17>Finished generating code

the header of const AuString & is the same as std::string_view therefore nothing changes. in fact, we still need to alloc strings a bunch of times for a zero terminated string. worse, <c++20 always allocs each time we want to access a hashmap with o(1) lookup, making small hashmaps kinda pointless when we always have to alloc+copy (thx std)

perhaps this will help some language binders
2024-04-19 05:58:08 +01:00

149 lines
3.5 KiB
C++

/***
Copyright (C) 2022 J Reece Wilson (a/k/a "Reece"). All rights reserved.
File: FSRecursion.cpp
Date: 2022-11-06
Author: Reece
***/
#include <Source/RuntimeInternal.hpp>
#include "FS.hpp"
namespace Aurora::IO::FS
{
struct RecursiveDirIterator : IReadDir
{
AuSPtr<IReadDir> pDir;
AuList<AuString> nextLevel;
AuString curPath;
AuString curSubDir;
bool bSymlinkTraversal { true };
AuUInt32 uErrorCount {};
AuList<AuString> failedPaths;
virtual AuUInt32 GetErrorCount() override
{
return this->uErrorCount;
}
virtual AuList<AuString> GetErrorPaths() override
{
return this->failedPaths;
}
bool OpenDir(const AuROString &str)
{
curPath = str;
pDir = ReadDir(str);
return bool(pDir);
}
bool OpenNext(const AuROString &str)
{
curPath = str;
pDir = ReadDir(str);
return bool(pDir);
}
void DoNext()
{
if (this->pDir)
{
if (auto uCount = this->pDir->GetErrorCount())
{
this->failedPaths.push_back(this->curPath + AuString(1, AuFS::kPathSplitter) + this->curSubDir);
this->uErrorCount += uCount;
}
}
this->pDir.reset();
if (!nextLevel.size())
{
return;
}
auto a = nextLevel[0];
nextLevel.erase(nextLevel.begin());
pDir = ReadDir(curPath + "/" + a);
curSubDir = a;
}
virtual StatEx *Next() override
{
AU_DEBUG_MEMCRUNCH;
StatEx *pNext {};
if (!this->pDir)
{
return {};
}
do
{
try
{
pNext = this->pDir->Next();
while (!pNext)
{
DoNext();
if (!this->pDir)
{
return {};
}
pNext = this->pDir->Next();
}
if (curSubDir.size())
{
pNext->fileName.insert(pNext->fileName.begin(), curSubDir.begin(), curSubDir.end());
}
if (!this->bSymlinkTraversal && pNext->bSymLink)
{
continue;
}
if (pNext->bExistsDirectory)
{
nextLevel.push_back(pNext->fileName + "/");
}
}
catch (...)
{
SysPushErrorCatch();
}
}
while (false);
return pNext;
}
};
AUKN_SYM AuSPtr<IReadDir> ReadDirRecursive(const AuROString &string, AuOptional<bool> bTraverseSymlinks)
{
auto pObj = AuMakeShared<RecursiveDirIterator>();
if (!pObj)
{
SysPushErrorMem();
return {};
}
pObj->bSymlinkTraversal = bTraverseSymlinks.ValueOr(true);
try
{
if (!pObj->OpenDir(string))
{
return {};
}
}
catch (...)
{
SysPushErrorCatch();
}
return pObj;
}
}