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
parent c58072d595
commit 1129d42b48

View File

@@ -2583,19 +2583,27 @@ 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;
memcpy(prevDirPath, dirPath, i);
break;
if (!isFile)
{
memcpy(prevDirPath, dirPath, i);
break;
}
else isFile = false;
}
}