src/filesystem/winrt/SDL_sysfilesystem.cpp
author Sam Lantinga
Sun, 09 Mar 2014 11:06:11 -0700
changeset 8582 c3e9a2b93517
parent 8539 41885a5f550d
child 8600 092802455aed
permissions -rw-r--r--
Fixed line endings on WinRT source code
slouken@8582
     1
/* TODO, WinRT: include copyright info in SDL_winrtpaths.cpp
slouken@8582
     2
   TODO, WinRT: remove the need to compile this with C++/CX (/ZW) extensions, and if possible, without C++ at all
slouken@8582
     3
*/
slouken@8582
     4
slouken@8582
     5
#include "SDL_config.h"
slouken@8582
     6
slouken@8582
     7
#ifdef __WINRT__
slouken@8582
     8
slouken@8582
     9
extern "C" {
slouken@8582
    10
#include "SDL_filesystem.h"
slouken@8582
    11
#include "SDL_error.h"
slouken@8582
    12
#include "SDL_stdinc.h"
slouken@8582
    13
#include "SDL_system.h"
slouken@8582
    14
#include "../../core/windows/SDL_windows.h"
slouken@8582
    15
}
slouken@8582
    16
slouken@8582
    17
#include <string>
slouken@8582
    18
#include <unordered_map>
slouken@8582
    19
slouken@8582
    20
using namespace std;
slouken@8582
    21
using namespace Windows::Storage;
slouken@8582
    22
slouken@8582
    23
extern "C" const wchar_t *
slouken@8582
    24
SDL_WinRTGetFSPathUNICODE(SDL_WinRT_Path pathType)
slouken@8582
    25
{
slouken@8582
    26
    switch (pathType) {
slouken@8582
    27
        case SDL_WINRT_PATH_INSTALLED_LOCATION:
slouken@8582
    28
        {
slouken@8582
    29
            static wstring path;
slouken@8582
    30
            if (path.empty()) {
slouken@8582
    31
                path = Windows::ApplicationModel::Package::Current->InstalledLocation->Path->Data();
slouken@8582
    32
            }
slouken@8582
    33
            return path.c_str();
slouken@8582
    34
        }
slouken@8582
    35
slouken@8582
    36
        case SDL_WINRT_PATH_LOCAL_FOLDER:
slouken@8582
    37
        {
slouken@8582
    38
            static wstring path;
slouken@8582
    39
            if (path.empty()) {
slouken@8582
    40
                path = ApplicationData::Current->LocalFolder->Path->Data();
slouken@8582
    41
            }
slouken@8582
    42
            return path.c_str();
slouken@8582
    43
        }
slouken@8582
    44
slouken@8582
    45
#if WINAPI_FAMILY != WINAPI_FAMILY_PHONE_APP
slouken@8582
    46
        case SDL_WINRT_PATH_ROAMING_FOLDER:
slouken@8582
    47
        {
slouken@8582
    48
            static wstring path;
slouken@8582
    49
            if (path.empty()) {
slouken@8582
    50
                path = ApplicationData::Current->RoamingFolder->Path->Data();
slouken@8582
    51
            }
slouken@8582
    52
            return path.c_str();
slouken@8582
    53
        }
slouken@8582
    54
slouken@8582
    55
        case SDL_WINRT_PATH_TEMP_FOLDER:
slouken@8582
    56
        {
slouken@8582
    57
            static wstring path;
slouken@8582
    58
            if (path.empty()) {
slouken@8582
    59
                path = ApplicationData::Current->TemporaryFolder->Path->Data();
slouken@8582
    60
            }
slouken@8582
    61
            return path.c_str();
slouken@8582
    62
        }
slouken@8582
    63
#endif
slouken@8582
    64
slouken@8582
    65
        default:
slouken@8582
    66
            break;
slouken@8582
    67
    }
slouken@8582
    68
slouken@8582
    69
    SDL_Unsupported();
slouken@8582
    70
    return NULL;
slouken@8582
    71
}
slouken@8582
    72
slouken@8582
    73
extern "C" const char *
slouken@8582
    74
SDL_WinRTGetFSPathUTF8(SDL_WinRT_Path pathType)
slouken@8582
    75
{
slouken@8582
    76
    typedef unordered_map<SDL_WinRT_Path, string> UTF8PathMap;
slouken@8582
    77
    static UTF8PathMap utf8Paths;
slouken@8582
    78
slouken@8582
    79
    UTF8PathMap::iterator searchResult = utf8Paths.find(pathType);
slouken@8582
    80
    if (searchResult != utf8Paths.end()) {
slouken@8582
    81
        return searchResult->second.c_str();
slouken@8582
    82
    }
slouken@8582
    83
slouken@8582
    84
    const wchar_t * ucs2Path = SDL_WinRTGetFSPathUNICODE(pathType);
slouken@8582
    85
    if (!ucs2Path) {
slouken@8582
    86
        return NULL;
slouken@8582
    87
    }
slouken@8582
    88
slouken@8582
    89
    char * utf8Path = WIN_StringToUTF8(ucs2Path);
slouken@8582
    90
    utf8Paths[pathType] = utf8Path;
slouken@8582
    91
    SDL_free(utf8Path);
slouken@8582
    92
    return utf8Paths[pathType].c_str();
slouken@8582
    93
}
slouken@8582
    94
slouken@8582
    95
extern "C" char *
slouken@8582
    96
SDL_GetBasePath(void)
slouken@8582
    97
{
slouken@8582
    98
    const char * srcPath = SDL_WinRTGetFSPathUTF8(SDL_WINRT_PATH_INSTALLED_LOCATION);
slouken@8582
    99
    size_t destPathLen;
slouken@8582
   100
    char * destPath = NULL;
slouken@8582
   101
slouken@8582
   102
    if (!srcPath) {
slouken@8582
   103
        SDL_SetError("Couldn't locate our basepath: %s", SDL_GetError());
slouken@8582
   104
        return NULL;
slouken@8582
   105
    }
slouken@8582
   106
slouken@8582
   107
    destPathLen = SDL_strlen(srcPath) + 2;
slouken@8582
   108
    destPath = (char *) SDL_malloc(destPathLen);
slouken@8582
   109
    if (!destPath) {
slouken@8582
   110
        SDL_OutOfMemory();
slouken@8582
   111
        return NULL;
slouken@8582
   112
    }
slouken@8582
   113
slouken@8582
   114
    SDL_snprintf(destPath, destPathLen, "%s\\", srcPath);
slouken@8582
   115
    return destPath;
slouken@8582
   116
}
slouken@8582
   117
slouken@8582
   118
extern "C" char *
slouken@8582
   119
SDL_GetPrefPath(const char *org, const char *app)
slouken@8582
   120
{
slouken@8582
   121
    /* WinRT note: The 'SHGetFolderPath' API that is used in Windows 7 and
slouken@8582
   122
     * earlier is not available on WinRT or Windows Phone.  WinRT provides
slouken@8582
   123
     * a similar API, but SHGetFolderPath can't be called, at least not
slouken@8582
   124
     * without violating Microsoft's app-store requirements.
slouken@8582
   125
     */
slouken@8582
   126
slouken@8582
   127
#if WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP
slouken@8582
   128
    /* A 'Roaming' folder is not available in Windows Phone 8, however a 'Local' folder is. */
slouken@8582
   129
    const char * srcPath = SDL_WinRTGetFSPathUTF8(SDL_WINRT_PATH_LOCAL_FOLDER);
slouken@8582
   130
#else
slouken@8582
   131
    /* A 'Roaming' folder is available on Windows 8 and 8.1.  Use that. */
slouken@8582
   132
    const char * srcPath = SDL_WinRTGetFSPathUTF8(SDL_WINRT_PATH_ROAMING_FOLDER);
slouken@8582
   133
#endif
slouken@8582
   134
slouken@8582
   135
    size_t destPathLen;
slouken@8582
   136
    char * destPath = NULL;
slouken@8582
   137
slouken@8582
   138
    if (!srcPath) {
slouken@8582
   139
        SDL_SetError("Couldn't locate our basepath: %s", SDL_GetError());
slouken@8582
   140
        return NULL;
slouken@8582
   141
    }
slouken@8582
   142
slouken@8582
   143
    destPathLen = SDL_strlen(srcPath) + SDL_strlen(org) + SDL_strlen(app) + 4;
slouken@8582
   144
    destPath = (char *) SDL_malloc(destPathLen);
slouken@8582
   145
    if (!destPath) {
slouken@8582
   146
        SDL_OutOfMemory();
slouken@8582
   147
        return NULL;
slouken@8582
   148
    }
slouken@8582
   149
slouken@8582
   150
    SDL_snprintf(destPath, destPathLen, "%s\\%s\\%s\\", srcPath, org, app);
slouken@8582
   151
    return destPath;
slouken@8582
   152
}
slouken@8582
   153
slouken@8582
   154
#endif /* __WINRT__ */