SDL_sysfilesystem.cpp 5.0 KB

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