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

This commit is contained in:
Luke Philipsen 2026-08-18 19:42:19 -07:00
commit 8081e8e6d5

View file

@ -2583,19 +2583,27 @@ const char *GetPrevDirectoryPath(const char *dirPath)
{ {
static char prevDirPath[MAX_FILEPATH_LENGTH] = { 0 }; static char prevDirPath[MAX_FILEPATH_LENGTH] = { 0 };
memset(prevDirPath, 0, MAX_FILEPATH_LENGTH); memset(prevDirPath, 0, MAX_FILEPATH_LENGTH);
int dirPathLength = (int)strlen(dirPath);
if (dirPathLength <= 3) snprintf(prevDirPath, MAX_FILEPATH_LENGTH, "%s", dirPath); const int lastIndex = (int)strlen(dirPath) - 1;
bool isFile = IsPathFile(dirPath);
for (int i = (dirPathLength - 1); (i >= 0) && (dirPathLength > 3); i--) for (int i = lastIndex; i >= 0; i--)
{ {
// If the character is a path separator.
if ((dirPath[i] == '\\') || (dirPath[i] == '/')) if ((dirPath[i] == '\\') || (dirPath[i] == '/'))
{ {
// Check for root: "C:\" or "/" // If this character is a leading '/' (e.g. the '/' in "/usr") or
if (((i == 2) && (dirPath[1] ==':')) || (i == 0)) i++; // 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); if (!isFile)
break; {
memcpy(prevDirPath, dirPath, i);
break;
}
else isFile = false;
} }
} }