src/filesystem/windows/SDL_sysfilesystem.c
author Alex Szpakowski <slime73@gmail.com>
Tue, 15 Jul 2014 02:01:43 -0300
branchiOS-improvements
changeset 9489 6cd0275146b3
parent 8589 65d0979cdf08
child 9619 b94b6d0bff0f
permissions -rw-r--r--
iOS now respects SDL_HINT_ACCELEROMETER_AS_JOYSTICK.
icculus@7667
     1
/*
icculus@7667
     2
  Simple DirectMedia Layer
slouken@8149
     3
  Copyright (C) 1997-2014 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@8093
    21
#include "../../SDL_internal.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@7671
    28
#include "../../core/windows/SDL_windows.h"
icculus@7671
    29
#include <shlobj.h>
icculus@7671
    30
icculus@7671
    31
#include "SDL_assert.h"
icculus@7667
    32
#include "SDL_error.h"
icculus@7667
    33
#include "SDL_stdinc.h"
icculus@7667
    34
#include "SDL_filesystem.h"
icculus@7667
    35
icculus@7667
    36
char *
icculus@7667
    37
SDL_GetBasePath(void)
icculus@7667
    38
{
icculus@7667
    39
    TCHAR path[MAX_PATH];
icculus@7667
    40
    const DWORD len = GetModuleFileName(NULL, path, SDL_arraysize(path));
icculus@7667
    41
    size_t i;
icculus@7667
    42
icculus@7667
    43
    SDL_assert(len < SDL_arraysize(path));
icculus@7667
    44
icculus@7667
    45
    if (len == 0) {
icculus@7667
    46
        WIN_SetError("Couldn't locate our .exe");
icculus@7667
    47
        return NULL;
icculus@7667
    48
    }
icculus@7667
    49
icculus@7667
    50
    for (i = len-1; i > 0; i--) {
icculus@7667
    51
        if (path[i] == '\\') {
icculus@7667
    52
            break;
icculus@7667
    53
        }
icculus@7667
    54
    }
icculus@7667
    55
icculus@7667
    56
    SDL_assert(i > 0); /* Should have been an absolute path. */
icculus@7667
    57
    path[i+1] = '\0';  /* chop off filename. */
icculus@7667
    58
    return WIN_StringToUTF8(path);
icculus@7667
    59
}
icculus@7667
    60
icculus@7667
    61
char *
icculus@7667
    62
SDL_GetPrefPath(const char *org, const char *app)
icculus@7667
    63
{
icculus@7667
    64
    /*
icculus@7667
    65
     * Vista and later has a new API for this, but SHGetFolderPath works there,
icculus@7667
    66
     *  and apparently just wraps the new API. This is the new way to do it:
icculus@7667
    67
     *
icculus@7667
    68
     *     SHGetKnownFolderPath(FOLDERID_RoamingAppData, KF_FLAG_CREATE,
icculus@7667
    69
     *                          NULL, &wszPath);
icculus@7667
    70
     */
icculus@7667
    71
icculus@8275
    72
    WCHAR path[MAX_PATH];
icculus@7667
    73
    char *retval = NULL;
icculus@8275
    74
    WCHAR* worg = NULL;
icculus@8275
    75
    WCHAR* wapp = NULL;
icculus@8275
    76
    size_t new_wpath_len = 0;
icculus@8275
    77
    BOOL api_result = FALSE;
icculus@7667
    78
icculus@8275
    79
    if (!SUCCEEDED(SHGetFolderPathW(NULL, CSIDL_APPDATA | CSIDL_FLAG_CREATE, NULL, 0, path))) {
icculus@7667
    80
        WIN_SetError("Couldn't locate our prefpath");
icculus@7667
    81
        return NULL;
icculus@7667
    82
    }
icculus@7667
    83
icculus@8275
    84
    worg = WIN_UTF8ToString(org);
icculus@8275
    85
    if (worg == NULL) {
icculus@8275
    86
        SDL_OutOfMemory();
icculus@8275
    87
        return NULL;
icculus@8275
    88
    }
icculus@8275
    89
icculus@8275
    90
    wapp = WIN_UTF8ToString(app);
icculus@8275
    91
    if (wapp == NULL) {
icculus@8275
    92
        SDL_free(worg);
icculus@8275
    93
        SDL_OutOfMemory();
icculus@8275
    94
        return NULL;
icculus@8275
    95
    }
icculus@8275
    96
icculus@8275
    97
    new_wpath_len = lstrlenW(worg) + lstrlenW(wapp) + lstrlenW(path) + 3;
icculus@8275
    98
icculus@8275
    99
    if ((new_wpath_len + 1) > MAX_PATH) {
icculus@8275
   100
        SDL_free(worg);
icculus@8275
   101
        SDL_free(wapp);
icculus@8275
   102
        WIN_SetError("Path too long.");
icculus@8275
   103
        return NULL;
icculus@8275
   104
    }
icculus@8275
   105
icculus@8275
   106
    lstrcatW(path, L"\\");
icculus@8275
   107
    lstrcatW(path, worg);
icculus@8275
   108
    SDL_free(worg);
icculus@8275
   109
icculus@8275
   110
    api_result = CreateDirectoryW(path, NULL);
icculus@8275
   111
    if (api_result == FALSE) {
icculus@8275
   112
        if (GetLastError() != ERROR_ALREADY_EXISTS) {
icculus@8275
   113
            SDL_free(wapp);
icculus@8275
   114
            WIN_SetError("Couldn't create a prefpath.");
icculus@7667
   115
            return NULL;
icculus@7667
   116
        }
icculus@7667
   117
    }
icculus@7667
   118
icculus@8275
   119
    lstrcatW(path, L"\\");
icculus@8275
   120
    lstrcatW(path, wapp);
icculus@8275
   121
    SDL_free(wapp);
icculus@8275
   122
icculus@8275
   123
    api_result = CreateDirectoryW(path, NULL);
icculus@8275
   124
    if (api_result == FALSE) {
icculus@8275
   125
        if (GetLastError() != ERROR_ALREADY_EXISTS) {
icculus@8275
   126
            WIN_SetError("Couldn't create a prefpath.");
icculus@8275
   127
            return NULL;
icculus@8275
   128
        }
icculus@8275
   129
    }
icculus@8275
   130
icculus@8275
   131
    lstrcatW(path, L"\\");
icculus@8275
   132
icculus@8275
   133
    retval = WIN_StringToUTF8(path);
icculus@8275
   134
icculus@7667
   135
    return retval;
icculus@7667
   136
}
icculus@7667
   137
icculus@7667
   138
#endif /* SDL_FILESYSTEM_WINDOWS */
icculus@7667
   139
icculus@7667
   140
/* vi: set ts=4 sw=4 expandtab: */