[CORE] Bug and formating fixes for GetApplicatonDir (#2285)
* Fix formating problems with GetApplicationDir. Don't ever return an empty string * always return a valid path even if it's ./ * remove the need for the dll and just use the normal GetModuleFileName function
This commit is contained in:
parent
f57727995a
commit
f4dea6919a
1 changed files with 61 additions and 88 deletions
149
src/rcore.c
149
src/rcore.c
|
@ -166,29 +166,13 @@
|
||||||
#ifndef MAX_PATH
|
#ifndef MAX_PATH
|
||||||
#define MAX_PATH 1025
|
#define MAX_PATH 1025
|
||||||
#endif
|
#endif
|
||||||
void *LoadLibraryA(void *lpLibFileName);
|
unsigned int GetModuleFileNameA( void* hModule, const char* lpFilename, unsigned int nSize);
|
||||||
void *LoadLibraryW(void *lpLibFileName);
|
unsigned int GetModuleFileNameW( void* hModule, const unsigned short* lpFilename, unsigned int nSize);
|
||||||
|
|
||||||
#ifdef UNICODE
|
|
||||||
#define LoadLibrary LoadLibraryW
|
|
||||||
#else
|
|
||||||
#define LoadLibrary LoadLibraryA
|
|
||||||
#endif // !UNICODE
|
|
||||||
|
|
||||||
void *GetProcAddress(void *hModule, void *lpProcName);
|
|
||||||
|
|
||||||
void *GetCurrentProcess(void);
|
|
||||||
bool FreeLibrary(void *hLibModule);
|
|
||||||
|
|
||||||
int WideCharToMultiByte(unsigned int cp, unsigned long flags, const unsigned short *widestr, int cchwide, char *str, int cbmb, const char *defchar, int *used_default);
|
int WideCharToMultiByte(unsigned int cp, unsigned long flags, const unsigned short *widestr, int cchwide, char *str, int cbmb, const char *defchar, int *used_default);
|
||||||
|
|
||||||
const char pathDelim = '\\';
|
|
||||||
#elif defined(__linux__)
|
#elif defined(__linux__)
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
const char pathDelim = '/';
|
|
||||||
#elif defined(__APPLE__)
|
#elif defined(__APPLE__)
|
||||||
#include <sys/syslimits.h>
|
#include <sys/syslimits.h>
|
||||||
const char pathDelim = '/';
|
|
||||||
#endif // OSs
|
#endif // OSs
|
||||||
#endif // PLATFORM_DESKTOP
|
#endif // PLATFORM_DESKTOP
|
||||||
|
|
||||||
|
@ -3003,86 +2987,75 @@ const char *GetWorkingDirectory(void)
|
||||||
|
|
||||||
const char *GetApplicationDirectory(void)
|
const char *GetApplicationDirectory(void)
|
||||||
{
|
{
|
||||||
static char appDir[MAX_FILEPATH_LENGTH] = { 0 };
|
static char appDir[MAX_FILEPATH_LENGTH] = { 0 };
|
||||||
memset(appDir, 0, MAX_FILEPATH_LENGTH);
|
memset(appDir, 0, MAX_FILEPATH_LENGTH);
|
||||||
|
|
||||||
#if defined(_WIN32)
|
#if defined(_WIN32)
|
||||||
typedef unsigned long(*GetModuleFileNameFunc)(void*, void*, void*, unsigned long);
|
int len = 0;
|
||||||
|
|
||||||
GetModuleFileNameFunc getModuleFileNameExWPtr = NULL;
|
|
||||||
void *lib = LoadLibrary(L"psapi.dll");
|
|
||||||
|
|
||||||
if (lib == NULL)
|
|
||||||
{
|
|
||||||
appDir[0] = '\\';
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
#if defined (UNICODE)
|
#if defined (UNICODE)
|
||||||
getModuleFileNameExWPtr = (GetModuleFileNameFunc)GetProcAddress(lib, "GetModuleFileNameExW");
|
unsigned short widePath[MAX_PATH];
|
||||||
|
len = GetModuleFileNameW(NULL, widePath, MAX_PATH);
|
||||||
|
len = WideCharToMultiByte(0, 0, widePath, len, appDir, MAX_PATH, NULL, NULL);
|
||||||
#else
|
#else
|
||||||
getModuleFileNameExWPtr = (GetModuleFileNameFunc)GetProcAddress(lib, "GetModuleFileNameExA");
|
len = GetModuleFileNameA(NULL, appDir, MAX_PATH);
|
||||||
#endif
|
#endif
|
||||||
|
if (len > 0)
|
||||||
|
{
|
||||||
|
for (int i = len; i >= 0; --i)
|
||||||
|
{
|
||||||
|
if (appDir[i] == '\\')
|
||||||
|
{
|
||||||
|
appDir[i + 1] = '\0';
|
||||||
|
i = -1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
appDir[0] = '.';
|
||||||
|
appDir[1] = '\\';
|
||||||
|
}
|
||||||
|
|
||||||
if (getModuleFileNameExWPtr == NULL)
|
|
||||||
{
|
|
||||||
appDir[0] = '\\';
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
int len = 0;
|
|
||||||
#if defined (UNICODE)
|
|
||||||
unsigned short widePath[MAX_PATH];
|
|
||||||
len = getModuleFileNameExWPtr(GetCurrentProcess(), NULL, widePath, MAX_PATH);
|
|
||||||
len = WideCharToMultiByte(0, 0, widePath, len, appDir, MAX_PATH, NULL, NULL);
|
|
||||||
#else
|
|
||||||
len = getModuleFileNameExWPtr(GetCurrentProcess(), NULL, appDir, MAX_PATH);
|
|
||||||
#endif
|
|
||||||
if (len > 0)
|
|
||||||
{
|
|
||||||
for (int i = len; i >= 0; --i)
|
|
||||||
{
|
|
||||||
if (appDir[i] == '\\')
|
|
||||||
{
|
|
||||||
appDir[i + 1] = '\0';
|
|
||||||
i = -1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
FreeLibrary(lib);
|
|
||||||
}
|
|
||||||
#elif defined(__linux__)
|
#elif defined(__linux__)
|
||||||
unsigned int size = sizeof(appDir);
|
unsigned int size = sizeof(appDir);
|
||||||
ssize_t len = readlink("/proc/self/exe", appDir, size);
|
ssize_t len = readlink("/proc/self/exe", appDir, size);
|
||||||
|
|
||||||
if (len > 0)
|
if (len > 0)
|
||||||
{
|
{
|
||||||
for (int i = len; i >= 0; --i)
|
for (int i = len; i >= 0; --i)
|
||||||
{
|
{
|
||||||
if (appDir[i] == '/')
|
if (appDir[i] == '/')
|
||||||
{
|
{
|
||||||
appDir[i + 1] = '\0';
|
appDir[i + 1] = '\0';
|
||||||
i = -1;
|
i = -1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
appDir[0] = '.';
|
||||||
|
appDir[1] = '/';
|
||||||
|
}
|
||||||
#elif defined(__APPLE__)
|
#elif defined(__APPLE__)
|
||||||
uint32_t size = sizeof(appDir);
|
uint32_t size = sizeof(appDir);
|
||||||
|
|
||||||
if (_NSGetExecutablePath(appDir, &size) == 0)
|
if (_NSGetExecutablePath(appDir, &size) == 0)
|
||||||
{
|
{
|
||||||
int len = strlen(appDir);
|
int len = strlen(appDir);
|
||||||
for (int i = len; i >= 0; --i)
|
for (int i = len; i >= 0; --i)
|
||||||
{
|
{
|
||||||
if (appDir[i] == '/')
|
if (appDir[i] == '/')
|
||||||
{
|
{
|
||||||
appDir[i + 1] = '\0';
|
appDir[i + 1] = '\0';
|
||||||
i = -1;
|
i = -1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
appDir[0] = '.';
|
||||||
|
appDir[1] = '/';
|
||||||
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
return appDir;
|
return appDir;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue