mirror of
https://github.com/raysan5/raylib.git
synced 2025-09-12 22:38:14 +00:00
REVIEW: GetDirectoryPath() and GetPrevDirectoryPath()
This commit is contained in:
42
src/core.c
42
src/core.c
@@ -1814,43 +1814,43 @@ const char *GetFileNameWithoutExt(const char *filePath)
|
|||||||
return fileName;
|
return fileName;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get directory for a given fileName (with path)
|
// Get directory for a given filePath
|
||||||
const char *GetDirectoryPath(const char *fileName)
|
const char *GetDirectoryPath(const char *filePath)
|
||||||
{
|
{
|
||||||
const char *lastSlash = NULL;
|
const char *lastSlash = NULL;
|
||||||
static char filePath[MAX_FILEPATH_LENGTH];
|
static char dirPath[MAX_FILEPATH_LENGTH];
|
||||||
memset(filePath, 0, MAX_FILEPATH_LENGTH);
|
memset(dirPath, 0, MAX_FILEPATH_LENGTH);
|
||||||
|
|
||||||
lastSlash = strprbrk(fileName, "\\/");
|
lastSlash = strprbrk(filePath, "\\/");
|
||||||
if (!lastSlash) return NULL;
|
if (!lastSlash) return NULL;
|
||||||
|
|
||||||
// NOTE: Be careful, strncpy() is not safe, it does not care about '\0'
|
// NOTE: Be careful, strncpy() is not safe, it does not care about '\0'
|
||||||
strncpy(filePath, fileName, strlen(fileName) - (strlen(lastSlash) - 1));
|
strncpy(dirPath, filePath, strlen(filePath) - (strlen(lastSlash) - 1));
|
||||||
filePath[strlen(fileName) - strlen(lastSlash)] = '\0'; // Add '\0' manually
|
dirPath[strlen(filePath) - strlen(lastSlash)] = '\0'; // Add '\0' manually
|
||||||
|
|
||||||
return filePath;
|
return dirPath;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get previous directory path for a given path
|
// Get previous directory path for a given path
|
||||||
const char *GetPrevDirectoryPath(const char *path)
|
const char *GetPrevDirectoryPath(const char *dirPath)
|
||||||
{
|
{
|
||||||
static char prevDir[MAX_FILEPATH_LENGTH];
|
static char prevDirPath[MAX_FILEPATH_LENGTH];
|
||||||
memset(prevDir, 0, MAX_FILEPATH_LENGTH);
|
memset(prevDirPath, 0, MAX_FILEPATH_LENGTH);
|
||||||
int pathLen = strlen(path);
|
int pathLen = strlen(dirPath);
|
||||||
|
|
||||||
for (int i = (pathLen - 1); i >= 0; i--)
|
if (pathLen <= 3) strcpy(prevDirPath, dirPath);
|
||||||
|
|
||||||
|
for (int i = (pathLen - 1); (i > 0) && (pathLen > 3); i--)
|
||||||
{
|
{
|
||||||
if ((path[i] == '\\') || (path[i] == '/'))
|
if ((dirPath[i] == '\\') || (dirPath[i] == '/'))
|
||||||
{
|
{
|
||||||
if ((i != (pathLen - 1)) && (path[pathLen - 2] != ':'))
|
if (i == 2) i++; // Check for root: "C:\"
|
||||||
{
|
strncpy(prevDirPath, dirPath, i);
|
||||||
strncpy(prevDir, path, i);
|
break;
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return prevDir;
|
return prevDirPath;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get current working directory
|
// Get current working directory
|
||||||
|
@@ -940,8 +940,8 @@ RLAPI bool DirectoryExists(const char *dirPath); // Check if a
|
|||||||
RLAPI const char *GetExtension(const char *fileName); // Get pointer to extension for a filename string
|
RLAPI const char *GetExtension(const char *fileName); // Get pointer to extension for a filename string
|
||||||
RLAPI const char *GetFileName(const char *filePath); // Get pointer to filename for a path string
|
RLAPI const char *GetFileName(const char *filePath); // Get pointer to filename for a path string
|
||||||
RLAPI const char *GetFileNameWithoutExt(const char *filePath); // Get filename string without extension (memory should be freed)
|
RLAPI const char *GetFileNameWithoutExt(const char *filePath); // Get filename string without extension (memory should be freed)
|
||||||
RLAPI const char *GetDirectoryPath(const char *fileName); // Get full path for a given fileName (uses static string)
|
RLAPI const char *GetDirectoryPath(const char *filePath); // Get full path for a given fileName with path (uses static string)
|
||||||
RLAPI const char *GetPrevDirectoryPath(const char *path); // Get previous directory path for a given path (uses static string)
|
RLAPI const char *GetPrevDirectoryPath(const char *dirPath); // Get previous directory path for a given path (uses static string)
|
||||||
RLAPI const char *GetWorkingDirectory(void); // Get current working directory (uses static string)
|
RLAPI const char *GetWorkingDirectory(void); // Get current working directory (uses static string)
|
||||||
RLAPI char **GetDirectoryFiles(const char *dirPath, int *count); // Get filenames in a directory path (memory should be freed)
|
RLAPI char **GetDirectoryFiles(const char *dirPath, int *count); // Get filenames in a directory path (memory should be freed)
|
||||||
RLAPI void ClearDirectoryFiles(void); // Clear directory files paths buffers (free memory)
|
RLAPI void ClearDirectoryFiles(void); // Clear directory files paths buffers (free memory)
|
||||||
|
Reference in New Issue
Block a user