SDL_genericstorage.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2024 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. #include "../SDL_sysstorage.h"
  20. static char *GENERIC_INTERNAL_CreateFullPath(const char *base, const char *relative)
  21. {
  22. size_t len = 0;
  23. if (base) {
  24. len += SDL_strlen(base);
  25. }
  26. len += SDL_strlen(relative) + 1;
  27. char *result = (char*) SDL_malloc(len);
  28. if (result != NULL) {
  29. SDL_snprintf(result, len, "%s%s", base, relative);
  30. }
  31. return result;
  32. }
  33. static int GENERIC_StorageClose(void *userdata)
  34. {
  35. SDL_free(userdata);
  36. return 0;
  37. }
  38. static SDL_bool GENERIC_StorageReady(void *userdata)
  39. {
  40. return SDL_TRUE;
  41. }
  42. static int GENERIC_StorageFileSize(void *userdata, const char *path, Uint64 *length)
  43. {
  44. SDL_IOStream *stream;
  45. Sint64 result;
  46. char *fullpath = GENERIC_INTERNAL_CreateFullPath((char*) userdata, path);
  47. if (fullpath == NULL) {
  48. return SDL_OutOfMemory();
  49. }
  50. stream = SDL_IOFromFile(fullpath, "rb");
  51. SDL_free(fullpath);
  52. result = SDL_SizeIO(stream);
  53. SDL_CloseIO(stream);
  54. if (result < 0) {
  55. return result;
  56. }
  57. /* FIXME: Should SDL_SizeIO use u64 now...? */
  58. *length = (Uint64) result;
  59. return 0;
  60. }
  61. static int GENERIC_StorageReadFile(void *userdata, const char *path, void *destination, Uint64 length)
  62. {
  63. SDL_IOStream *stream;
  64. char *fullpath;
  65. int fullread;
  66. if (length > SDL_SIZE_MAX) {
  67. return SDL_SetError("Read size exceeds SDL_SIZE_MAX");
  68. }
  69. fullpath = GENERIC_INTERNAL_CreateFullPath((char*) userdata, path);
  70. if (fullpath == NULL) {
  71. return SDL_OutOfMemory();
  72. }
  73. stream = SDL_IOFromFile(fullpath, "rb");
  74. SDL_free(fullpath);
  75. /* FIXME: Should SDL_ReadIO use u64 now...? */
  76. fullread = (SDL_ReadIO(stream, destination, (size_t) length) == length);
  77. SDL_CloseIO(stream);
  78. return fullread - 1;
  79. }
  80. static int GENERIC_StorageWriteFile(void *userdata, const char *path, const void *source, Uint64 length)
  81. {
  82. /* TODO: Recursively create subdirectories with SDL_mkdir */
  83. SDL_IOStream *stream;
  84. char *fullpath;
  85. int fullwrite;
  86. if (length > SDL_SIZE_MAX) {
  87. return SDL_SetError("Write size exceeds SDL_SIZE_MAX");
  88. }
  89. fullpath = GENERIC_INTERNAL_CreateFullPath((char*) userdata, path);
  90. if (fullpath == NULL) {
  91. return SDL_OutOfMemory();
  92. }
  93. stream = SDL_IOFromFile(fullpath, "wb");
  94. SDL_free(fullpath);
  95. /* FIXME: Should SDL_WriteIO use u64 now...? */
  96. fullwrite = (SDL_WriteIO(stream, source, (size_t) length) == length);
  97. SDL_CloseIO(stream);
  98. return fullwrite - 1;
  99. }
  100. static Uint64 GENERIC_StorageSpaceRemaining(void *userdata)
  101. {
  102. /* TODO: There's totally a way to query a folder root's quota... */
  103. return SDL_MAX_UINT64;
  104. }
  105. static const SDL_StorageInterface GENERIC_title_iface = {
  106. GENERIC_StorageClose,
  107. GENERIC_StorageReady,
  108. GENERIC_StorageFileSize,
  109. GENERIC_StorageReadFile,
  110. NULL,
  111. NULL
  112. };
  113. static SDL_Storage *GENERIC_Title_Create(const char *override, SDL_PropertiesID props)
  114. {
  115. SDL_Storage *result;
  116. char *basepath;
  117. if (override != NULL) {
  118. basepath = SDL_strdup(override);
  119. } else {
  120. basepath = SDL_GetBasePath();
  121. }
  122. if (basepath == NULL) {
  123. return NULL;
  124. }
  125. result = SDL_OpenStorage(&GENERIC_title_iface, basepath);
  126. if (result == NULL) {
  127. SDL_free(basepath);
  128. }
  129. return result;
  130. }
  131. TitleStorageBootStrap GENERIC_titlebootstrap = {
  132. "generic",
  133. "SDL generic title storage driver",
  134. GENERIC_Title_Create
  135. };
  136. static const SDL_StorageInterface GENERIC_user_iface = {
  137. GENERIC_StorageClose,
  138. GENERIC_StorageReady,
  139. GENERIC_StorageFileSize,
  140. GENERIC_StorageReadFile,
  141. GENERIC_StorageWriteFile,
  142. GENERIC_StorageSpaceRemaining
  143. };
  144. static SDL_Storage *GENERIC_User_Create(const char *org, const char *app, SDL_PropertiesID props)
  145. {
  146. SDL_Storage *result;
  147. char *prefpath = SDL_GetPrefPath(org, app);
  148. if (prefpath == NULL) {
  149. return NULL;
  150. }
  151. result = SDL_OpenStorage(&GENERIC_user_iface, prefpath);
  152. if (result == NULL) {
  153. SDL_free(prefpath);
  154. }
  155. return result;
  156. }
  157. UserStorageBootStrap GENERIC_userbootstrap = {
  158. "generic",
  159. "SDL generic user storage driver",
  160. GENERIC_User_Create
  161. };
  162. static const SDL_StorageInterface GENERIC_file_iface = {
  163. GENERIC_StorageClose,
  164. GENERIC_StorageReady,
  165. GENERIC_StorageFileSize,
  166. GENERIC_StorageReadFile,
  167. GENERIC_StorageWriteFile,
  168. GENERIC_StorageSpaceRemaining
  169. };
  170. SDL_Storage *GENERIC_OpenFileStorage(const char *path)
  171. {
  172. SDL_Storage *result;
  173. size_t len = 0;
  174. char *basepath = NULL;
  175. if (path) {
  176. len += SDL_strlen(path);
  177. }
  178. if (len > 0) {
  179. if (path[len-1] == '/') {
  180. basepath = SDL_strdup(path);
  181. if (!basepath) {
  182. return NULL;
  183. }
  184. } else {
  185. if (SDL_asprintf(&basepath, "%s/", path) < 0) {
  186. return NULL;
  187. }
  188. }
  189. }
  190. result = SDL_OpenStorage(&GENERIC_file_iface, basepath);
  191. if (result == NULL) {
  192. SDL_free(basepath);
  193. }
  194. return result;
  195. }