mirror of
https://github.com/raysan5/raylib.git
synced 2026-08-26 09:01:35 +00:00
Mostly rewrote GetPrevDirectoryPath() to correct its behavior in a few cases (#6073)
This commit is contained in:
24
src/rcore.c
24
src/rcore.c
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user