From 43367c0c29b1e0d52e1041b39a9766db3897d9a6 Mon Sep 17 00:00:00 2001 From: "Ryan C. Gordon" Date: Wed, 21 Mar 2012 23:52:44 -0400 Subject: [PATCH] Fixed some brainfarts in the Windows version of PHYSFS_getPrefDir(). --- src/physfs.c | 4 +--- src/platform_windows.c | 28 +++++++++++++++++++++++----- 2 files changed, 24 insertions(+), 8 deletions(-) diff --git a/src/physfs.c b/src/physfs.c index 3687685..62ecdb3 100644 --- a/src/physfs.c +++ b/src/physfs.c @@ -1380,8 +1380,8 @@ void PHYSFS_getCdRomDirsCallback(PHYSFS_StringCallback callback, void *data) const char *PHYSFS_getPrefDir(const char *org, const char *app) { const char dirsep = __PHYSFS_platformDirSeparator; - char *ptr = NULL; PHYSFS_Stat statbuf; + char *ptr = NULL; int exists = 0; BAIL_IF_MACRO(!initialized, PHYSFS_ERR_NOT_INITIALIZED, 0); @@ -1394,7 +1394,6 @@ const char *PHYSFS_getPrefDir(const char *org, const char *app) prefDir = __PHYSFS_platformCalcPrefDir(org, app); BAIL_IF_MACRO(!prefDir, ERRPASS, NULL); - #if !PHYSFS_PLATFORM_WINDOWS /* Windows guarantees the dir exists here. */ if (__PHYSFS_platformStat(prefDir, &exists, &statbuf)) return prefDir; @@ -1410,7 +1409,6 @@ const char *PHYSFS_getPrefDir(const char *org, const char *app) allocator.Free(prefDir); prefDir = NULL; } /* if */ - #endif return prefDir; } /* PHYSFS_getPrefDir */ diff --git a/src/platform_windows.c b/src/platform_windows.c index 179bd36..f0780b9 100644 --- a/src/platform_windows.c +++ b/src/platform_windows.c @@ -443,17 +443,35 @@ char *__PHYSFS_platformCalcBaseDir(const char *argv0) char *__PHYSFS_platformCalcPrefDir(const char *org, const char *app) { - // Vista and later has a new API for this, but SHGetFolderPath works there, - // and apparently just wraps the new API. This is the new way to do it: - // SHGetKnownFolderPath(FOLDERID_RoamingAppData, KF_FLAG_CREATE, - // NULL, &wszPath); + /* + * Vista and later has a new API for this, but SHGetFolderPath works there, + * and apparently just wraps the new API. This is the new way to do it: + * + * SHGetKnownFolderPath(FOLDERID_RoamingAppData, KF_FLAG_CREATE, + * NULL, &wszPath); + */ WCHAR path[MAX_PATH]; + char *utf8 = NULL; + size_t len = 0; + char *retval = NULL; + if (!SUCCEEDED(SHGetFolderPathW(NULL, CSIDL_APPDATA | CSIDL_FLAG_CREATE, NULL, 0, path))) BAIL_MACRO(PHYSFS_ERR_OS_ERROR, NULL); - return unicodeToUtf8Heap(path); + utf8 = unicodeToUtf8Heap(path); + BAIL_IF_MACRO(!utf8, ERRPASS, NULL); + len = strlen(utf8) + strlen(org) + strlen(app) + 3; + retval = allocator.Malloc(len); + if (!retval) + { + allocator.Free(utf8); + BAIL_MACRO(PHYSFS_ERR_OUT_OF_MEMORY, NULL); + } /* if */ + + sprintf(retval, "%s\\%s\\%s", utf8, org, app); + return retval; } /* __PHYSFS_platformCalcPrefDir */