REVIEWED: GetDirectoryPath()
This commit is contained in:
parent
f28c1ef675
commit
46774a8167
1 changed files with 20 additions and 5 deletions
25
src/core.c
25
src/core.c
|
@ -1941,16 +1941,31 @@ const char *GetFileNameWithoutExt(const char *filePath)
|
||||||
// Get directory for a given filePath
|
// Get directory for a given filePath
|
||||||
const char *GetDirectoryPath(const char *filePath)
|
const char *GetDirectoryPath(const char *filePath)
|
||||||
{
|
{
|
||||||
|
/*
|
||||||
|
// NOTE: Directory separator is different in Windows and other platforms,
|
||||||
|
// fortunately, Windows also support the '/' separator, that's the one should be used
|
||||||
|
#if defined(_WIN32)
|
||||||
|
char separator = '\\';
|
||||||
|
#else
|
||||||
|
char separator = '/';
|
||||||
|
#endif
|
||||||
|
*/
|
||||||
const char *lastSlash = NULL;
|
const char *lastSlash = NULL;
|
||||||
static char dirPath[MAX_FILEPATH_LENGTH];
|
static char dirPath[MAX_FILEPATH_LENGTH];
|
||||||
memset(dirPath, 0, MAX_FILEPATH_LENGTH);
|
memset(dirPath, 0, MAX_FILEPATH_LENGTH);
|
||||||
|
|
||||||
|
// For security, we set starting path to current directory,
|
||||||
|
// obtained path will be concated to this
|
||||||
|
dirPath[0] = '.';
|
||||||
|
dirPath[1] = '/';
|
||||||
|
|
||||||
lastSlash = strprbrk(filePath, "\\/");
|
lastSlash = strprbrk(filePath, "\\/");
|
||||||
if (!lastSlash) return NULL;
|
if (lastSlash)
|
||||||
|
{
|
||||||
// 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(dirPath, filePath, strlen(filePath) - (strlen(lastSlash) - 1));
|
strncpy(dirPath + 2, filePath, strlen(filePath) - (strlen(lastSlash) - 1));
|
||||||
dirPath[strlen(filePath) - strlen(lastSlash)] = '\0'; // Add '\0' manually
|
dirPath[2 + strlen(filePath) - strlen(lastSlash)] = '\0'; // Add '\0' manually
|
||||||
|
}
|
||||||
|
|
||||||
return dirPath;
|
return dirPath;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue