SDL_sysfilesystem.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2026 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. #ifdef SDL_FILESYSTEM_RISCOS
  20. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
  21. // System dependent filesystem routines
  22. #include "../SDL_sysfilesystem.h"
  23. #include <kernel.h>
  24. #include <swis.h>
  25. #include <unixlib/local.h>
  26. // Wrapper around __unixify_std that uses SDL's memory allocators
  27. static char *SDL_unixify_std(const char *ro_path, char *buffer, size_t buf_len, int filetype)
  28. {
  29. const char *const in_buf = buffer; // = NULL if we allocate the buffer.
  30. if (!buffer) {
  31. /* This matches the logic in __unixify, with an additional byte for the
  32. * extra path separator.
  33. */
  34. buf_len = SDL_strlen(ro_path) + 14 + 1;
  35. buffer = SDL_malloc(buf_len);
  36. if (!buffer) {
  37. return NULL;
  38. }
  39. }
  40. if (!__unixify_std(ro_path, buffer, buf_len, filetype)) {
  41. if (!in_buf) {
  42. SDL_free(buffer);
  43. }
  44. SDL_SetError("Could not convert '%s' to a Unix-style path", ro_path);
  45. return NULL;
  46. }
  47. /* HACK: It's necessary to add an extra path separator here since SDL's API
  48. * requires it, however paths with trailing separators aren't normally valid
  49. * on RISC OS.
  50. */
  51. if (__get_riscosify_control() & __RISCOSIFY_NO_PROCESS)
  52. SDL_strlcat(buffer, ".", buf_len);
  53. else
  54. SDL_strlcat(buffer, "/", buf_len);
  55. return buffer;
  56. }
  57. static char *canonicalisePath(const char *path, const char *pathVar)
  58. {
  59. _kernel_oserror *error;
  60. _kernel_swi_regs regs;
  61. char *buf;
  62. regs.r[0] = 37;
  63. regs.r[1] = (int)path;
  64. regs.r[2] = 0;
  65. regs.r[3] = (int)pathVar;
  66. regs.r[4] = 0;
  67. regs.r[5] = 0;
  68. error = _kernel_swi(OS_FSControl, &regs, &regs);
  69. if (error) {
  70. SDL_SetError("Couldn't canonicalise path: %s", error->errmess);
  71. return NULL;
  72. }
  73. regs.r[5] = 1 - regs.r[5];
  74. buf = SDL_malloc(regs.r[5]);
  75. if (!buf) {
  76. return NULL;
  77. }
  78. regs.r[2] = (int)buf;
  79. error = _kernel_swi(OS_FSControl, &regs, &regs);
  80. if (error) {
  81. SDL_SetError("Couldn't canonicalise path: %s", error->errmess);
  82. SDL_free(buf);
  83. return NULL;
  84. }
  85. return buf;
  86. }
  87. static _kernel_oserror *createDirectoryRecursive(char *path)
  88. {
  89. char *ptr = NULL;
  90. _kernel_oserror *error;
  91. _kernel_swi_regs regs;
  92. regs.r[0] = 8;
  93. regs.r[1] = (int)path;
  94. regs.r[2] = 0;
  95. for (ptr = path + 1; *ptr; ptr++) {
  96. if (*ptr == '.') {
  97. *ptr = '\0';
  98. error = _kernel_swi(OS_File, &regs, &regs);
  99. *ptr = '.';
  100. if (error) {
  101. return error;
  102. }
  103. }
  104. }
  105. return _kernel_swi(OS_File, &regs, &regs);
  106. }
  107. char *SDL_SYS_GetBasePath(void)
  108. {
  109. _kernel_swi_regs regs;
  110. _kernel_oserror *error;
  111. char *canon, *ptr, *result;
  112. error = _kernel_swi(OS_GetEnv, &regs, &regs);
  113. if (error) {
  114. return NULL;
  115. }
  116. canon = canonicalisePath((const char *)regs.r[0], "Run$Path");
  117. if (!canon) {
  118. return NULL;
  119. }
  120. // chop off filename.
  121. ptr = SDL_strrchr(canon, '.');
  122. if (ptr) {
  123. *ptr = '\0';
  124. }
  125. result = SDL_unixify_std(canon, NULL, 0, __RISCOSIFY_FILETYPE_NOTSPECIFIED);
  126. SDL_free(canon);
  127. return result;
  128. }
  129. char *SDL_SYS_GetExeName(void)
  130. {
  131. _kernel_swi_regs regs;
  132. _kernel_oserror *error;
  133. char *canon, *ptr, *retval;
  134. error = _kernel_swi(OS_GetEnv, &regs, &regs);
  135. if (error) {
  136. return NULL;
  137. }
  138. canon = canonicalisePath((const char *)regs.r[0], "Run$Path");
  139. if (!canon) {
  140. return NULL;
  141. }
  142. // find filename.
  143. ptr = SDL_strrchr(canon, '.');
  144. retval = SDL_strdup(ptr ? ptr + 1 : canon);
  145. SDL_free(canon);
  146. return retval;
  147. }
  148. char *SDL_SYS_GetPrefPath(const char *org, const char *app)
  149. {
  150. char *canon, *dir, *result;
  151. _kernel_oserror *error;
  152. canon = canonicalisePath("<Choices$Write>", "Run$Path");
  153. if (!canon) {
  154. return NULL;
  155. }
  156. const size_t len = SDL_strlen(canon) + SDL_strlen(org) + SDL_strlen(app) + 4;
  157. dir = (char *)SDL_malloc(len);
  158. if (!dir) {
  159. SDL_free(canon);
  160. return NULL;
  161. }
  162. if (*org) {
  163. SDL_snprintf(dir, len, "%s.%s.%s", canon, org, app);
  164. } else {
  165. SDL_snprintf(dir, len, "%s.%s", canon, app);
  166. }
  167. SDL_free(canon);
  168. error = createDirectoryRecursive(dir);
  169. if (error) {
  170. SDL_SetError("Couldn't create directory: %s", error->errmess);
  171. SDL_free(dir);
  172. return NULL;
  173. }
  174. result = SDL_unixify_std(dir, NULL, 0, __RISCOSIFY_FILETYPE_NOTSPECIFIED);
  175. SDL_free(dir);
  176. return result;
  177. }
  178. // TODO
  179. char *SDL_SYS_GetUserFolder(SDL_Folder folder)
  180. {
  181. SDL_Unsupported();
  182. return NULL;
  183. }
  184. #endif // SDL_FILESYSTEM_RISCOS