SDL_storage.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2024 Sam Lantinga <[email protected]>
  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. /**
  19. * \file SDL_storage.h
  20. *
  21. * Include file for storage container SDL API functions
  22. */
  23. #ifndef SDL_storage_h_
  24. #define SDL_storage_h_
  25. #include <SDL3/SDL_stdinc.h>
  26. #include <SDL3/SDL_filesystem.h>
  27. #include <SDL3/SDL_properties.h>
  28. #include <SDL3/SDL_begin_code.h>
  29. /* Set up for C function definitions, even when using C++ */
  30. #ifdef __cplusplus
  31. extern "C" {
  32. #endif
  33. /* !!! FIXME: Don't let this ship without async R/W support!!! */
  34. typedef struct SDL_StorageInterface
  35. {
  36. /* Called when the storage is closed */
  37. int (SDLCALL *close)(void *userdata);
  38. /* Optional, returns whether the storage is currently ready for access */
  39. SDL_bool (SDLCALL *ready)(void *userdata);
  40. /* Enumerate a directory, optional for write-only storage */
  41. int (SDLCALL *enumerate)(void *userdata, const char *path, SDL_EnumerateDirectoryCallback callback, void *callback_userdata);
  42. /* Get path information, optional for write-only storage */
  43. int (SDLCALL *info)(void *userdata, const char *path, SDL_PathInfo *info);
  44. /* Read a file from storage, optional for write-only storage */
  45. int (SDLCALL *read_file)(void *userdata, const char *path, void *destination, Uint64 length);
  46. /* Write a file to storage, optional for read-only storage */
  47. int (SDLCALL *write_file)(void *userdata, const char *path, const void *source, Uint64 length);
  48. /* Create a directory, optional for read-only storage */
  49. int (SDLCALL *mkdir)(void *userdata, const char *path);
  50. /* Remove a file or empty directory, optional for read-only storage */
  51. int (SDLCALL *remove)(void *userdata, const char *path);
  52. /* Rename a path, optional for read-only storage */
  53. int (SDLCALL *rename)(void *userdata, const char *oldpath, const char *newpath);
  54. /* Get the space remaining, optional for read-only storage */
  55. Uint64 (SDLCALL *space_remaining)(void *userdata);
  56. } SDL_StorageInterface;
  57. typedef struct SDL_Storage SDL_Storage;
  58. /**
  59. * Opens up a read-only container for the application's filesystem.
  60. *
  61. * \param override a path to override the backend's default title root
  62. * \param props a property list that may contain backend-specific information
  63. * \returns a title storage container on success or NULL on failure; call
  64. * SDL_GetError() for more information.
  65. *
  66. * \since This function is available since SDL 3.0.0.
  67. *
  68. * \sa SDL_CloseStorage
  69. * \sa SDL_GetStorageFileSize
  70. * \sa SDL_OpenUserStorage
  71. * \sa SDL_ReadStorageFile
  72. */
  73. extern DECLSPEC SDL_Storage *SDLCALL SDL_OpenTitleStorage(const char *override, SDL_PropertiesID props);
  74. /**
  75. * Opens up a container for a user's unique read/write filesystem.
  76. *
  77. * While title storage can generally be kept open throughout runtime, user
  78. * storage should only be opened when the client is ready to read/write files.
  79. * This allows the backend to properly batch file operations and flush them
  80. * when the container has been closed; ensuring safe and optimal save I/O.
  81. *
  82. * \param org the name of your organization
  83. * \param app the name of your application
  84. * \param props a property list that may contain backend-specific information
  85. * \returns a user storage container on success or NULL on failure; call
  86. * SDL_GetError() for more information.
  87. *
  88. * \since This function is available since SDL 3.0.0.
  89. *
  90. * \sa SDL_CloseStorage
  91. * \sa SDL_GetStorageFileSize
  92. * \sa SDL_GetStorageSpaceRemaining
  93. * \sa SDL_OpenTitleStorage
  94. * \sa SDL_ReadStorageFile
  95. * \sa SDL_StorageReady
  96. * \sa SDL_WriteStorageFile
  97. */
  98. extern DECLSPEC SDL_Storage *SDLCALL SDL_OpenUserStorage(const char *org, const char *app, SDL_PropertiesID props);
  99. /**
  100. * Opens up a container for local filesystem storage.
  101. *
  102. * This is provided for development and tools. Portable applications should
  103. * use SDL_OpenTitleStorage() for access to game data and
  104. * SDL_OpenUserStorage() for access to user data.
  105. *
  106. * \param path the base path prepended to all storage paths, or NULL for no
  107. * base path
  108. * \returns a filesystem storage container on success or NULL on failure; call
  109. * SDL_GetError() for more information.
  110. *
  111. * \since This function is available since SDL 3.0.0.
  112. *
  113. * \sa SDL_CloseStorage
  114. * \sa SDL_GetStorageFileSize
  115. * \sa SDL_GetStorageSpaceRemaining
  116. * \sa SDL_OpenTitleStorage
  117. * \sa SDL_OpenUserStorage
  118. * \sa SDL_ReadStorageFile
  119. * \sa SDL_WriteStorageFile
  120. */
  121. extern DECLSPEC SDL_Storage *SDLCALL SDL_OpenFileStorage(const char *path);
  122. /**
  123. * Opens up a container using a client-provided storage interface.
  124. *
  125. * Applications do not need to use this function unless they are providing
  126. * their own SDL_Storage implementation. If you just need an SDL_Storage, you
  127. * should use the built-in implementations in SDL, like SDL_OpenTitleStorage()
  128. * or SDL_OpenUserStorage().
  129. *
  130. * \param iface the function table to be used by this container
  131. * \param userdata the pointer that will be passed to the store interface
  132. * \returns a storage container on success or NULL on failure; call
  133. * SDL_GetError() for more information.
  134. *
  135. * \since This function is available since SDL 3.0.0.
  136. *
  137. * \sa SDL_CloseStorage
  138. * \sa SDL_GetStorageFileSize
  139. * \sa SDL_GetStorageSpaceRemaining
  140. * \sa SDL_ReadStorageFile
  141. * \sa SDL_StorageReady
  142. * \sa SDL_WriteStorageFile
  143. */
  144. extern DECLSPEC SDL_Storage *SDLCALL SDL_OpenStorage(const SDL_StorageInterface *iface, void *userdata);
  145. /**
  146. * Closes and frees a storage container.
  147. *
  148. * \param storage a storage container to close
  149. * \returns 0 if the container was freed with no errors, a negative value
  150. * otherwise; call SDL_GetError() for more information. Even if the
  151. * function returns an error, the container data will be freed; the
  152. * error is only for informational purposes.
  153. *
  154. * \since This function is available since SDL 3.0.0.
  155. *
  156. * \sa SDL_OpenFileStorage
  157. * \sa SDL_OpenStorage
  158. * \sa SDL_OpenTitleStorage
  159. * \sa SDL_OpenUserStorage
  160. */
  161. extern DECLSPEC int SDLCALL SDL_CloseStorage(SDL_Storage *storage);
  162. /**
  163. * Checks if the storage container is ready to use.
  164. *
  165. * This function should be called in regular intervals until it returns
  166. * SDL_TRUE - however, it is not recommended to spinwait on this call, as the
  167. * backend may depend on a synchronous message loop.
  168. *
  169. * \param storage a storage container to query
  170. * \returns SDL_TRUE if the container is ready, SDL_FALSE otherwise
  171. *
  172. * \since This function is available since SDL 3.0.0.
  173. */
  174. extern DECLSPEC SDL_bool SDLCALL SDL_StorageReady(SDL_Storage *storage);
  175. /**
  176. * Query the size of a file within a storage container.
  177. *
  178. * \param storage a storage container to query
  179. * \param path the relative path of the file to query
  180. * \param length a pointer to be filled with the file's length
  181. * \returns 0 if the file could be queried, a negative value otherwise; call
  182. * SDL_GetError() for more information.
  183. *
  184. * \since This function is available since SDL 3.0.0.
  185. *
  186. * \sa SDL_ReadStorageFile
  187. * \sa SDL_StorageReady
  188. */
  189. extern DECLSPEC int SDLCALL SDL_GetStorageFileSize(SDL_Storage *storage, const char *path, Uint64 *length);
  190. /**
  191. * Synchronously read a file from a storage container into a client-provided
  192. * buffer.
  193. *
  194. * \param storage a storage container to read from
  195. * \param path the relative path of the file to read
  196. * \param destination a client-provided buffer to read the file into
  197. * \param length the length of the destination buffer
  198. * \returns 0 if the file was read, a negative value otherwise; call
  199. * SDL_GetError() for more information.
  200. *
  201. * \since This function is available since SDL 3.0.0.
  202. *
  203. * \sa SDL_GetStorageFileSize
  204. * \sa SDL_StorageReady
  205. * \sa SDL_WriteStorageFile
  206. */
  207. extern DECLSPEC int SDLCALL SDL_ReadStorageFile(SDL_Storage *storage, const char *path, void *destination, Uint64 length);
  208. /**
  209. * Synchronously write a file from client memory into a storage container.
  210. *
  211. * \param storage a storage container to write to
  212. * \param path the relative path of the file to write
  213. * \param source a client-provided buffer to write from
  214. * \param length the length of the source buffer
  215. * \returns 0 if the file was written, a negative value otherwise; call
  216. * SDL_GetError() for more information.
  217. *
  218. * \since This function is available since SDL 3.0.0.
  219. *
  220. * \sa SDL_GetStorageSpaceRemaining
  221. * \sa SDL_ReadStorageFile
  222. * \sa SDL_StorageReady
  223. */
  224. extern DECLSPEC int SDL_WriteStorageFile(SDL_Storage *storage, const char *path, const void *source, Uint64 length);
  225. /**
  226. * Create a directory in a writable storage container.
  227. *
  228. * \param storage a storage container
  229. * \param path the path of the directory to create
  230. * \returns 0 on success or a negative error code on failure; call
  231. * SDL_GetError() for more information.
  232. *
  233. * \since This function is available since SDL 3.0.0.
  234. *
  235. * \sa SDL_StorageReady
  236. */
  237. extern DECLSPEC int SDLCALL SDL_CreateStorageDirectory(SDL_Storage *storage, const char *path);
  238. /**
  239. * Enumerate a directory in a storage container.
  240. *
  241. * \param storage a storage container
  242. * \param path the path of the directory to enumerate
  243. * \param callback a function that is called for each entry in the directory
  244. * \param userdata a pointer that is passed to `callback`
  245. * \returns 0 on success or a negative error code on failure; call
  246. * SDL_GetError() for more information.
  247. *
  248. * \since This function is available since SDL 3.0.0.
  249. *
  250. * \sa SDL_StorageReady
  251. */
  252. extern DECLSPEC int SDLCALL SDL_EnumerateStorageDirectory(SDL_Storage *storage, const char *path, SDL_EnumerateDirectoryCallback callback, void *userdata);
  253. /**
  254. * Remove a file or an empty directory in a writable storage container.
  255. *
  256. * \param storage a storage container
  257. * \param path the path of the directory to enumerate
  258. * \returns 0 on success or a negative error code on failure; call
  259. * SDL_GetError() for more information.
  260. *
  261. * \since This function is available since SDL 3.0.0.
  262. *
  263. * \sa SDL_StorageReady
  264. */
  265. extern DECLSPEC int SDLCALL SDL_RemoveStoragePath(SDL_Storage *storage, const char *path);
  266. /**
  267. * Rename a file or directory in a writable storage container.
  268. *
  269. * \param storage a storage container
  270. * \param oldpath the old path
  271. * \param newpath the new path
  272. * \returns 0 on success or a negative error code on failure; call
  273. * SDL_GetError() for more information.
  274. *
  275. * \since This function is available since SDL 3.0.0.
  276. *
  277. * \sa SDL_StorageReady
  278. */
  279. extern DECLSPEC int SDLCALL SDL_RenameStoragePath(SDL_Storage *storage, const char *oldpath, const char *newpath);
  280. /**
  281. * Get information about a filesystem path in a storage container.
  282. *
  283. * \param storage a storage container
  284. * \param path the path to query
  285. * \param info a pointer filled in with information about the path
  286. * \returns 0 on success or a negative error code on failure; call
  287. * SDL_GetError() for more information.
  288. *
  289. * \since This function is available since SDL 3.0.0.
  290. *
  291. * \sa SDL_StorageReady
  292. */
  293. extern DECLSPEC int SDLCALL SDL_GetStoragePathInfo(SDL_Storage *storage, const char *path, SDL_PathInfo *info);
  294. /**
  295. * Queries the remaining space in a storage container.
  296. *
  297. * \param storage a storage container to query
  298. * \returns the amount of remaining space, in bytes
  299. *
  300. * \since This function is available since SDL 3.0.0.
  301. *
  302. * \sa SDL_StorageReady
  303. * \sa SDL_WriteStorageFile
  304. */
  305. extern DECLSPEC Uint64 SDLCALL SDL_GetStorageSpaceRemaining(SDL_Storage *storage);
  306. /* Ends C function definitions when using C++ */
  307. #ifdef __cplusplus
  308. }
  309. #endif
  310. #include <SDL3/SDL_close_code.h>
  311. #endif /* SDL_storage_h_ */