src/filesystem/winrt/SDL_sysfilesystem.cpp
author Sam Lantinga
Thu, 13 Mar 2014 21:21:26 -0700
changeset 8616 ec011c16e2fd
parent 8600 092802455aed
child 9228 9ecf7c9f49e5
permissions -rw-r--r--
Added missing copyright notices
slouken@8616
     1
/*
slouken@8616
     2
  Simple DirectMedia Layer
slouken@8616
     3
  Copyright (C) 1997-2014 Sam Lantinga <slouken@libsdl.org>
slouken@8616
     4
slouken@8616
     5
  This software is provided 'as-is', without any express or implied
slouken@8616
     6
  warranty.  In no event will the authors be held liable for any damages
slouken@8616
     7
  arising from the use of this software.
slouken@8616
     8
slouken@8616
     9
  Permission is granted to anyone to use this software for any purpose,
slouken@8616
    10
  including commercial applications, and to alter it and redistribute it
slouken@8616
    11
  freely, subject to the following restrictions:
slouken@8616
    12
slouken@8616
    13
  1. The origin of this software must not be misrepresented; you must not
slouken@8616
    14
     claim that you wrote the original software. If you use this software
slouken@8616
    15
     in a product, an acknowledgment in the product documentation would be
slouken@8616
    16
     appreciated but is not required.
slouken@8616
    17
  2. Altered source versions must be plainly marked as such, and must not be
slouken@8616
    18
     misrepresented as being the original software.
slouken@8616
    19
  3. This notice may not be removed or altered from any source distribution.
slouken@8616
    20
*/
slouken@8616
    21
#include "../../SDL_internal.h"
slouken@8616
    22
slouken@8582
    23
/* TODO, WinRT: include copyright info in SDL_winrtpaths.cpp
slouken@8582
    24
   TODO, WinRT: remove the need to compile this with C++/CX (/ZW) extensions, and if possible, without C++ at all
slouken@8582
    25
*/
slouken@8582
    26
slouken@8582
    27
#ifdef __WINRT__
slouken@8582
    28
slouken@8582
    29
extern "C" {
slouken@8582
    30
#include "SDL_filesystem.h"
slouken@8582
    31
#include "SDL_error.h"
slouken@8582
    32
#include "SDL_stdinc.h"
slouken@8582
    33
#include "SDL_system.h"
slouken@8582
    34
#include "../../core/windows/SDL_windows.h"
slouken@8582
    35
}
slouken@8582
    36
slouken@8582
    37
#include <string>
slouken@8582
    38
#include <unordered_map>
slouken@8582
    39
slouken@8582
    40
using namespace std;
slouken@8582
    41
using namespace Windows::Storage;
slouken@8582
    42
slouken@8582
    43
extern "C" const wchar_t *
slouken@8582
    44
SDL_WinRTGetFSPathUNICODE(SDL_WinRT_Path pathType)
slouken@8582
    45
{
slouken@8582
    46
    switch (pathType) {
slouken@8582
    47
        case SDL_WINRT_PATH_INSTALLED_LOCATION:
slouken@8582
    48
        {
slouken@8582
    49
            static wstring path;
slouken@8582
    50
            if (path.empty()) {
slouken@8582
    51
                path = Windows::ApplicationModel::Package::Current->InstalledLocation->Path->Data();
slouken@8582
    52
            }
slouken@8582
    53
            return path.c_str();
slouken@8582
    54
        }
slouken@8582
    55
slouken@8582
    56
        case SDL_WINRT_PATH_LOCAL_FOLDER:
slouken@8582
    57
        {
slouken@8582
    58
            static wstring path;
slouken@8582
    59
            if (path.empty()) {
slouken@8582
    60
                path = ApplicationData::Current->LocalFolder->Path->Data();
slouken@8582
    61
            }
slouken@8582
    62
            return path.c_str();
slouken@8582
    63
        }
slouken@8582
    64
slouken@8582
    65
#if WINAPI_FAMILY != WINAPI_FAMILY_PHONE_APP
slouken@8582
    66
        case SDL_WINRT_PATH_ROAMING_FOLDER:
slouken@8582
    67
        {
slouken@8582
    68
            static wstring path;
slouken@8582
    69
            if (path.empty()) {
slouken@8582
    70
                path = ApplicationData::Current->RoamingFolder->Path->Data();
slouken@8582
    71
            }
slouken@8582
    72
            return path.c_str();
slouken@8582
    73
        }
slouken@8582
    74
slouken@8582
    75
        case SDL_WINRT_PATH_TEMP_FOLDER:
slouken@8582
    76
        {
slouken@8582
    77
            static wstring path;
slouken@8582
    78
            if (path.empty()) {
slouken@8582
    79
                path = ApplicationData::Current->TemporaryFolder->Path->Data();
slouken@8582
    80
            }
slouken@8582
    81
            return path.c_str();
slouken@8582
    82
        }
slouken@8582
    83
#endif
slouken@8582
    84
slouken@8582
    85
        default:
slouken@8582
    86
            break;
slouken@8582
    87
    }
slouken@8582
    88
slouken@8582
    89
    SDL_Unsupported();
slouken@8582
    90
    return NULL;
slouken@8582
    91
}
slouken@8582
    92
slouken@8582
    93
extern "C" const char *
slouken@8582
    94
SDL_WinRTGetFSPathUTF8(SDL_WinRT_Path pathType)
slouken@8582
    95
{
slouken@8582
    96
    typedef unordered_map<SDL_WinRT_Path, string> UTF8PathMap;
slouken@8582
    97
    static UTF8PathMap utf8Paths;
slouken@8582
    98
slouken@8582
    99
    UTF8PathMap::iterator searchResult = utf8Paths.find(pathType);
slouken@8582
   100
    if (searchResult != utf8Paths.end()) {
slouken@8582
   101
        return searchResult->second.c_str();
slouken@8582
   102
    }
slouken@8582
   103
slouken@8582
   104
    const wchar_t * ucs2Path = SDL_WinRTGetFSPathUNICODE(pathType);
slouken@8582
   105
    if (!ucs2Path) {
slouken@8582
   106
        return NULL;
slouken@8582
   107
    }
slouken@8582
   108
slouken@8582
   109
    char * utf8Path = WIN_StringToUTF8(ucs2Path);
slouken@8582
   110
    utf8Paths[pathType] = utf8Path;
slouken@8582
   111
    SDL_free(utf8Path);
slouken@8582
   112
    return utf8Paths[pathType].c_str();
slouken@8582
   113
}
slouken@8582
   114
slouken@8582
   115
extern "C" char *
slouken@8582
   116
SDL_GetBasePath(void)
slouken@8582
   117
{
slouken@8582
   118
    const char * srcPath = SDL_WinRTGetFSPathUTF8(SDL_WINRT_PATH_INSTALLED_LOCATION);
slouken@8582
   119
    size_t destPathLen;
slouken@8582
   120
    char * destPath = NULL;
slouken@8582
   121
slouken@8582
   122
    if (!srcPath) {
slouken@8582
   123
        SDL_SetError("Couldn't locate our basepath: %s", SDL_GetError());
slouken@8582
   124
        return NULL;
slouken@8582
   125
    }
slouken@8582
   126
slouken@8582
   127
    destPathLen = SDL_strlen(srcPath) + 2;
slouken@8582
   128
    destPath = (char *) SDL_malloc(destPathLen);
slouken@8582
   129
    if (!destPath) {
slouken@8582
   130
        SDL_OutOfMemory();
slouken@8582
   131
        return NULL;
slouken@8582
   132
    }
slouken@8582
   133
slouken@8582
   134
    SDL_snprintf(destPath, destPathLen, "%s\\", srcPath);
slouken@8582
   135
    return destPath;
slouken@8582
   136
}
slouken@8582
   137
slouken@8582
   138
extern "C" char *
slouken@8582
   139
SDL_GetPrefPath(const char *org, const char *app)
slouken@8582
   140
{
slouken@8582
   141
    /* WinRT note: The 'SHGetFolderPath' API that is used in Windows 7 and
slouken@8582
   142
     * earlier is not available on WinRT or Windows Phone.  WinRT provides
slouken@8582
   143
     * a similar API, but SHGetFolderPath can't be called, at least not
slouken@8582
   144
     * without violating Microsoft's app-store requirements.
slouken@8582
   145
     */
slouken@8582
   146
slouken@8582
   147
#if WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP
slouken@8582
   148
    /* A 'Roaming' folder is not available in Windows Phone 8, however a 'Local' folder is. */
slouken@8582
   149
    const char * srcPath = SDL_WinRTGetFSPathUTF8(SDL_WINRT_PATH_LOCAL_FOLDER);
slouken@8582
   150
#else
slouken@8582
   151
    /* A 'Roaming' folder is available on Windows 8 and 8.1.  Use that. */
slouken@8582
   152
    const char * srcPath = SDL_WinRTGetFSPathUTF8(SDL_WINRT_PATH_ROAMING_FOLDER);
slouken@8582
   153
#endif
slouken@8582
   154
slouken@8582
   155
    size_t destPathLen;
slouken@8582
   156
    char * destPath = NULL;
slouken@8582
   157
slouken@8582
   158
    if (!srcPath) {
slouken@8582
   159
        SDL_SetError("Couldn't locate our basepath: %s", SDL_GetError());
slouken@8582
   160
        return NULL;
slouken@8582
   161
    }
slouken@8582
   162
slouken@8582
   163
    destPathLen = SDL_strlen(srcPath) + SDL_strlen(org) + SDL_strlen(app) + 4;
slouken@8582
   164
    destPath = (char *) SDL_malloc(destPathLen);
slouken@8582
   165
    if (!destPath) {
slouken@8582
   166
        SDL_OutOfMemory();
slouken@8582
   167
        return NULL;
slouken@8582
   168
    }
slouken@8582
   169
slouken@8582
   170
    SDL_snprintf(destPath, destPathLen, "%s\\%s\\%s\\", srcPath, org, app);
slouken@8582
   171
    return destPath;
slouken@8582
   172
}
slouken@8582
   173
slouken@8582
   174
#endif /* __WINRT__ */