mirror of
https://github.com/libsdl-org/SDL.git
synced 2025-09-29 22:48:30 +00:00
@@ -495,10 +495,13 @@ const char *SDL_GetUserFolder(SDL_Folder folder)
|
||||
|
||||
char *SDL_GetPrefPath(const char *org, const char *app)
|
||||
{
|
||||
char *path = SDL_SYS_GetPrefPath(org, app);
|
||||
return path;
|
||||
return SDL_SYS_GetPrefPath(org, app);
|
||||
}
|
||||
|
||||
char *SDL_GetCurrentDirectory(void)
|
||||
{
|
||||
return SDL_SYS_GetCurrentDirectory();
|
||||
}
|
||||
|
||||
void SDL_InitFilesystem(void)
|
||||
{
|
||||
|
@@ -26,6 +26,7 @@
|
||||
extern char *SDL_SYS_GetBasePath(void);
|
||||
extern char *SDL_SYS_GetPrefPath(const char *org, const char *app);
|
||||
extern char *SDL_SYS_GetUserFolder(SDL_Folder folder);
|
||||
extern char *SDL_SYS_GetCurrentDirectory(void);
|
||||
|
||||
extern bool SDL_SYS_EnumerateDirectory(const char *path, const char *dirname, SDL_EnumerateDirectoryCallback cb, void *userdata);
|
||||
extern bool SDL_SYS_RemovePath(const char *path);
|
||||
|
@@ -45,4 +45,10 @@ char *SDL_SYS_GetUserFolder(SDL_Folder folder)
|
||||
return NULL;
|
||||
}
|
||||
|
||||
char *SDL_SYS_GetCurrentDirectory(void)
|
||||
{
|
||||
SDL_Unsupported();
|
||||
return NULL;
|
||||
}
|
||||
|
||||
#endif // SDL_FILESYSTEM_DUMMY || SDL_FILESYSTEM_DISABLED
|
||||
|
@@ -33,6 +33,7 @@
|
||||
#include <errno.h>
|
||||
#include <dirent.h>
|
||||
#include <sys/stat.h>
|
||||
#include <unistd.h>
|
||||
|
||||
bool SDL_SYS_EnumerateDirectory(const char *path, const char *dirname, SDL_EnumerateDirectoryCallback cb, void *userdata)
|
||||
{
|
||||
@@ -185,5 +186,36 @@ bool SDL_SYS_GetPathInfo(const char *path, SDL_PathInfo *info)
|
||||
return true;
|
||||
}
|
||||
|
||||
// Note that this isn't actually part of filesystem, not fsops, but everything that uses posix fsops uses this implementation, even with separate filesystem code.
|
||||
char *SDL_SYS_GetCurrentDirectory(void)
|
||||
{
|
||||
size_t buflen = 64;
|
||||
char *buf = NULL;
|
||||
|
||||
while (true) {
|
||||
void *ptr = SDL_realloc(buf, buflen);
|
||||
if (!ptr) {
|
||||
SDL_free(buf);
|
||||
return NULL;
|
||||
}
|
||||
buf = (char *) ptr;
|
||||
|
||||
if (getcwd(buf, buflen) != NULL) {
|
||||
break; // we got it!
|
||||
}
|
||||
|
||||
if (errno == ERANGE) {
|
||||
buflen *= 2; // try again with a bigger buffer.
|
||||
continue;
|
||||
}
|
||||
|
||||
SDL_free(buf);
|
||||
SDL_SetError("getcwd failed: %s", strerror(errno));
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return buf;
|
||||
}
|
||||
|
||||
#endif // SDL_FSOPS_POSIX
|
||||
|
||||
|
@@ -345,4 +345,32 @@ done:
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
char *SDL_SYS_GetCurrentDirectory(void)
|
||||
{
|
||||
WCHAR *wstr = NULL;
|
||||
DWORD buflen = 0;
|
||||
while (true) {
|
||||
const DWORD bw = GetCurrentDirectoryW(buflen, wstr);
|
||||
if (bw == 0) {
|
||||
WIN_SetError("GetCurrentDirectoryW failed");
|
||||
return NULL;
|
||||
} else if (bw < buflen) {
|
||||
break; // we got it!
|
||||
}
|
||||
|
||||
void *ptr = SDL_realloc(wstr, bw * sizeof (WCHAR));
|
||||
if (!ptr) {
|
||||
SDL_free(wstr);
|
||||
return NULL;
|
||||
}
|
||||
wstr = (WCHAR *) ptr;
|
||||
buflen = bw;
|
||||
}
|
||||
|
||||
char *retval = WIN_StringToUTF8W(wstr);
|
||||
SDL_free(wstr);
|
||||
return retval;
|
||||
}
|
||||
|
||||
#endif // SDL_FILESYSTEM_WINDOWS
|
||||
|
Reference in New Issue
Block a user