Force Windows version of SDL_GetPrefPath() to Unicode, create missing dirs.
authorRyan C. Gordon
Sat, 01 Mar 2014 20:28:40 -0500
changeset 827539b104a55d19
parent 8274 8b1f8feb42a6
child 8276 9bd65b58278e
Force Windows version of SDL_GetPrefPath() to Unicode, create missing dirs.

Fixes Bugzilla #2273.
src/filesystem/windows/SDL_sysfilesystem.c
     1.1 --- a/src/filesystem/windows/SDL_sysfilesystem.c	Sat Mar 01 12:21:15 2014 -0800
     1.2 +++ b/src/filesystem/windows/SDL_sysfilesystem.c	Sat Mar 01 20:28:40 2014 -0500
     1.3 @@ -69,28 +69,70 @@
     1.4       *                          NULL, &wszPath);
     1.5       */
     1.6  
     1.7 -    TCHAR path[MAX_PATH];
     1.8 +    WCHAR path[MAX_PATH];
     1.9      char *utf8 = NULL;
    1.10      char *retval = NULL;
    1.11 +    WCHAR* worg = NULL;
    1.12 +    WCHAR* wapp = NULL;
    1.13 +    size_t new_wpath_len = 0;
    1.14 +    BOOL api_result = FALSE;
    1.15  
    1.16 -    if (!SUCCEEDED(SHGetFolderPath(NULL, CSIDL_APPDATA | CSIDL_FLAG_CREATE, NULL, 0, path))) {
    1.17 +    if (!SUCCEEDED(SHGetFolderPathW(NULL, CSIDL_APPDATA | CSIDL_FLAG_CREATE, NULL, 0, path))) {
    1.18          WIN_SetError("Couldn't locate our prefpath");
    1.19          return NULL;
    1.20      }
    1.21  
    1.22 -    utf8 = WIN_StringToUTF8(path);
    1.23 -    if (utf8) {
    1.24 -        const size_t len = SDL_strlen(utf8) + SDL_strlen(org) + SDL_strlen(app) + 4;
    1.25 -        retval = (char *) SDL_malloc(len);
    1.26 -        if (!retval) {
    1.27 -            SDL_free(utf8);
    1.28 -            SDL_OutOfMemory();
    1.29 +    worg = WIN_UTF8ToString(org);
    1.30 +    if (worg == NULL) {
    1.31 +        SDL_OutOfMemory();
    1.32 +        return NULL;
    1.33 +    }
    1.34 +
    1.35 +    wapp = WIN_UTF8ToString(app);
    1.36 +    if (wapp == NULL) {
    1.37 +        SDL_free(worg);
    1.38 +        SDL_OutOfMemory();
    1.39 +        return NULL;
    1.40 +    }
    1.41 +
    1.42 +    new_wpath_len = lstrlenW(worg) + lstrlenW(wapp) + lstrlenW(path) + 3;
    1.43 +
    1.44 +    if ((new_wpath_len + 1) > MAX_PATH) {
    1.45 +        SDL_free(worg);
    1.46 +        SDL_free(wapp);
    1.47 +        WIN_SetError("Path too long.");
    1.48 +        return NULL;
    1.49 +    }
    1.50 +
    1.51 +    lstrcatW(path, L"\\");
    1.52 +    lstrcatW(path, worg);
    1.53 +    SDL_free(worg);
    1.54 +
    1.55 +    api_result = CreateDirectoryW(path, NULL);
    1.56 +    if (api_result == FALSE) {
    1.57 +        if (GetLastError() != ERROR_ALREADY_EXISTS) {
    1.58 +            SDL_free(wapp);
    1.59 +            WIN_SetError("Couldn't create a prefpath.");
    1.60              return NULL;
    1.61          }
    1.62 -        SDL_snprintf(retval, len, "%s\\%s\\%s\\", utf8, org, app);
    1.63 -        SDL_free(utf8);
    1.64      }
    1.65  
    1.66 +    lstrcatW(path, L"\\");
    1.67 +    lstrcatW(path, wapp);
    1.68 +    SDL_free(wapp);
    1.69 +
    1.70 +    api_result = CreateDirectoryW(path, NULL);
    1.71 +    if (api_result == FALSE) {
    1.72 +        if (GetLastError() != ERROR_ALREADY_EXISTS) {
    1.73 +            WIN_SetError("Couldn't create a prefpath.");
    1.74 +            return NULL;
    1.75 +        }
    1.76 +    }
    1.77 +
    1.78 +    lstrcatW(path, L"\\");
    1.79 +
    1.80 +    retval = WIN_StringToUTF8(path);
    1.81 +
    1.82      return retval;
    1.83  }
    1.84