Mostly rewrote GetPrevDirectoryPath() to correct its behavior in a few cases (#6073)

This commit is contained in:
luphi 2026-08-19 10:53:56 -07:00 • committed by GitHub
commit 1129d42b48
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -2583,20 +2583,28 @@ const char *GetPrevDirectoryPath(const char *dirPath)
{
static char prevDirPath[MAX_FILEPATH_LENGTH] = { 0 };
memset(prevDirPath, 0, MAX_FILEPATH_LENGTH);
int dirPathLength = (int)strlen(dirPath);
if (dirPathLength <= 3) snprintf(prevDirPath, MAX_FILEPATH_LENGTH, "%s", dirPath);
for (int i = (dirPathLength - 1); (i >= 0) && (dirPathLength > 3); i--)
const int lastIndex = (int)strlen(dirPath) - 1;
bool isFile = IsPathFile(dirPath);
for (int i = lastIndex; i >= 0; i--)
{
// If the character is a path separator.
if ((dirPath[i] == '\\') || (dirPath[i] == '/'))
{
// Check for root: "C:\" or "/"
if (((i == 2) && (dirPath[1] ==':')) || (i == 0)) i++;
// If this character is a leading '/' (e.g. the '/' in "/usr") or
// part of a drive root (e.g. "C:\"), include it with the result.
if ((i == 0) || ((i == 2) && (dirPath[1] == ':'))) i += 1;
// If this character is a trailing path separator (e.g. the last
// '/' in "/usr/bin/" or the last '\' in "C:\raylib\"), continue.
else if (i == lastIndex) continue;
if (!isFile)
{
memcpy(prevDirPath, dirPath, i);
break;
}
else isFile = false;
}
}
return prevDirPath;