[*] Update AuFS::GoUpToSeparator / AuFS::GetDirectoryFromPath

Got sick of writing

```
                    AuROString out;
                    if (AuEndsWith(uri, '/'))
                    {
                        AuFS::GoUpToSeparator(out, uri);
                        AuFS::GoUpToSeparator(out, out);
                    }
                    else
                    {
                        AuFS::GoUpToSeparator(out, uri);
                    }

```

To be fair, these apis havent been touched in like 2-3 years
This commit is contained in:
Reece Wilson 2024-12-04 11:00:56 +00:00
parent 588a6f3a7a
commit 500a35d38b

View File

@ -753,8 +753,8 @@ namespace Aurora::IO::FS
{
AuUInt indexA {}, indexB {};
auto a = path.find_last_of('\\');
if (a != AuString::npos)
auto a = path.FindLastOf('\\');
if (a != AuROString::npos)
{
indexA = a;
#if defined(AURORA_IS_POSIX_DERIVED)
@ -768,99 +768,136 @@ namespace Aurora::IO::FS
#endif
}
auto b = path.find_last_of('/');
if (b != AuString::npos) indexB = b;
auto b = path.FindLastOf('/');
if (b != AuROString::npos)
{
indexB = b;
}
return AuMax(indexA, indexB);
}
AUKN_SYM bool GetFileFromPath(AuROString &out, const AuROString &path)
{
try
{
if (path.empty()) return false;
if (path[path.size() - 1] == '.') return false;
out = {};
AuUInt max = GetLastSplitterIndex(path);
if (max == path.size()) return false;
out = path.substr(max + 1);
return true;
}
catch (...)
{
return false;
}
}
AUKN_SYM bool GetDirectoryFromPath(AuROString &out, const AuROString &path)
{
if (path.empty())
if (path.Empty())
{
return false;
}
if (path[path.size() - 1] == '.')
{
if (path.size() > 2 && (path[path.size() - 2] == '\\' || path[path.size() - 2] == '/'))
{
out = path.substr(0, path.size() - 1);
}
else
if (path[path.Size() - 1] == '.')
{
return false;
}
}
AuUInt max = GetLastSplitterIndex(path);
if (!max)
{
if (path.Size() == 1 &&
(path[0] == '/' || path[0] == '\\'))
{
return false;
}
if (max == path.size())
out = path;
return true;
}
out = path.SubStr(max + 1);
return bool(out.Size());
}
AUKN_SYM bool GetDirectoryFromPath(AuROString &out, const AuROString &path)
{
AuROString toScan { path };
bool bOtherOK {};
out = {};
if (path.Empty())
{
return false;
}
if (path[path.Size() - 1] == '.')
{
if (path.Size() > 2 && (path[path.Size() - 2] == '\\' || path[path.Size() - 2] == '/'))
{
toScan = path.SubStr(0, path.Size() - 1);
bOtherOK = true;
}
}
AuUInt max = GetLastSplitterIndex(toScan);
if (!max)
{
if (bOtherOK)
{
out = toScan;
return true;
}
// after GetLastSplitterIndex, implies / or \ rootfs string
if (path.Size() && (toScan[0] == '/' || toScan[0] == '\\'))
{
out = path;
return true;
}
out = path.substr(0, max + 1);
return false;
}
while (max >= 1 &&
toScan.Size() >= 1)
{
if (toScan[max - 1] == '/' || toScan[max - 1] == '\\')
{
max--;
}
else
{
break;
}
}
out = toScan.SubStr(0, max + 1);
return true;
}
AUKN_SYM bool GoUpToSeparator(AuROString &out, const AuROString &path)
{
if (path.empty())
AuROString toScan { path };
out = {};
if (path.Empty())
{
return false;
}
if (path[path.size() - 1] == '.')
if (path[path.Size() - 1] == '.')
{
if (path.size() > 2 && (path[path.size() - 2] == '\\' || path[path.size() - 2] == '/'))
if (path.Size() > 2 && (path[path.Size() - 2] == '\\' || path[path.Size() - 2] == '/'))
{
out = path.substr(0, path.size() - 2);
}
else
{
return false;
out = path.SubStr(0, path.Size() - 2);
return true;
}
}
AuUInt max = GetLastSplitterIndex(path);
AuUInt max = GetLastSplitterIndex(toScan);
if (!max)
{
return false;
}
if (max == path.size())
if (max == toScan.Size() - 1)
{
out = path.substr(0, max - 1);
out = toScan.SubStr(0, max - 1);
return true;
}
out = path.substr(0, max);
out = toScan.SubStr(0, max);
return true;
}
}