src/filesystem/windows/SDL_sysfilesystem.c
changeset 7667 be1cc6f55840
child 7668 66da4242ce53
equal deleted inserted replaced
7666:603fb699946c 7667:be1cc6f55840
       
     1 /*
       
     2   Simple DirectMedia Layer
       
     3   Copyright (C) 1997-2013 Sam Lantinga <slouken@libsdl.org>
       
     4 
       
     5   This software is provided 'as-is', without any express or implied
       
     6   warranty.  In no event will the authors be held liable for any damages
       
     7   arising from the use of this software.
       
     8 
       
     9   Permission is granted to anyone to use this software for any purpose,
       
    10   including commercial applications, and to alter it and redistribute it
       
    11   freely, subject to the following restrictions:
       
    12 
       
    13   1. The origin of this software must not be misrepresented; you must not
       
    14      claim that you wrote the original software. If you use this software
       
    15      in a product, an acknowledgment in the product documentation would be
       
    16      appreciated but is not required.
       
    17   2. Altered source versions must be plainly marked as such, and must not be
       
    18      misrepresented as being the original software.
       
    19   3. This notice may not be removed or altered from any source distribution.
       
    20 */
       
    21 #include "SDL_config.h"
       
    22 
       
    23 #ifdef SDL_FILESYSTEM_WINDOWS
       
    24 
       
    25 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
       
    26 /* System dependent filesystem routines                                */
       
    27 
       
    28 #include "SDL_error.h"
       
    29 #include "SDL_windows.h"
       
    30 #include "SDL_stdinc.h"
       
    31 #include "SDL_filesystem.h"
       
    32 
       
    33 char *
       
    34 SDL_GetBasePath(void)
       
    35 {
       
    36     TCHAR path[MAX_PATH];
       
    37     const DWORD len = GetModuleFileName(NULL, path, SDL_arraysize(path));
       
    38     size_t i;
       
    39 
       
    40     SDL_assert(len < SDL_arraysize(path));
       
    41 
       
    42     if (len == 0) {
       
    43         WIN_SetError("Couldn't locate our .exe");
       
    44         return NULL;
       
    45     }
       
    46 
       
    47     for (i = len-1; i > 0; i--) {
       
    48         if (path[i] == '\\') {
       
    49             break;
       
    50         }
       
    51     }
       
    52 
       
    53     SDL_assert(i > 0); /* Should have been an absolute path. */
       
    54     path[i+1] = '\0';  /* chop off filename. */
       
    55     return WIN_StringToUTF8(path);
       
    56 }
       
    57 
       
    58 char *
       
    59 SDL_GetPrefPath(const char *org, const char *app)
       
    60 {
       
    61     /*
       
    62      * Vista and later has a new API for this, but SHGetFolderPath works there,
       
    63      *  and apparently just wraps the new API. This is the new way to do it:
       
    64      *
       
    65      *     SHGetKnownFolderPath(FOLDERID_RoamingAppData, KF_FLAG_CREATE,
       
    66      *                          NULL, &wszPath);
       
    67      */
       
    68 
       
    69     TCHAR path[MAX_PATH];
       
    70     char *utf8 = NULL;
       
    71     char *retval = NULL;
       
    72 
       
    73     if (!SUCCEEDED(SHGetFolderPath(NULL, CSIDL_APPDATA | CSIDL_FLAG_CREATE, NULL, 0, path))) {
       
    74         WIN_SetError("Couldn't locate our prefpath");
       
    75         return NULL;
       
    76     }
       
    77 
       
    78     utf8 = WIN_StringToUTF8(path);
       
    79     if (utf8) {
       
    80         const size_t len = SDL_strlen(utf8) + SDL_strlen(org) + SDL_strlen(app) + 4;
       
    81         retval = (char *) SDL_malloc(len);
       
    82         if (!retval) {
       
    83             SDL_free(utf8);
       
    84             SDL_OutOfMemory();
       
    85             return NULL;
       
    86         }
       
    87         SDL_snprintf(retval, len, "%s\\%s\\%s\\", utf8, org, app);
       
    88         SDL_free(utf8);
       
    89     }
       
    90 
       
    91     return retval;
       
    92 }
       
    93 
       
    94 #endif /* SDL_FILESYSTEM_WINDOWS */
       
    95 
       
    96 /* vi: set ts=4 sw=4 expandtab: */