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