SDL_storage.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  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. /**
  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_OpenUserStorage
  69. * \sa SDL_OpenStorage
  70. * \sa SDL_TitleStorageReady
  71. * \sa SDL_CloseStorage
  72. * \sa SDL_StorageReady
  73. * \sa SDL_GetStorageFileSize
  74. * \sa SDL_ReadStorageFile
  75. * \sa SDL_WriteStorageFile
  76. * \sa SDL_GetStorageSpaceRemaining
  77. */
  78. extern DECLSPEC SDL_Storage *SDLCALL SDL_OpenTitleStorage(const char *override, SDL_PropertiesID props);
  79. /**
  80. * Opens up a container for a user's unique read/write filesystem.
  81. *
  82. * While title storage can generally be kept open throughout runtime, user
  83. * storage should only be opened when the client is ready to read/write files.
  84. * This allows the backend to properly batch file operations and flush them
  85. * when the container has been closed; ensuring safe and optimal save I/O.
  86. *
  87. * \param org the name of your organization
  88. * \param app the name of your application
  89. * \param props a property list that may contain backend-specific information
  90. * \returns a user storage container on success or NULL on failure; call
  91. * SDL_GetError() for more information.
  92. *
  93. * \since This function is available since SDL 3.0.0.
  94. *
  95. * \sa SDL_OpenTitleStorage
  96. * \sa SDL_OpenStorage
  97. * \sa SDL_CloseStorage
  98. * \sa SDL_StorageReady
  99. * \sa SDL_GetStorageFileSize
  100. * \sa SDL_ReadStorageFile
  101. * \sa SDL_WriteStorageFile
  102. * \sa SDL_GetStorageSpaceRemaining
  103. */
  104. extern DECLSPEC SDL_Storage *SDLCALL SDL_OpenUserStorage(const char *org, const char *app, SDL_PropertiesID props);
  105. /**
  106. * Opens up a container for local filesystem storage.
  107. *
  108. * This is provided for development and tools. Portable applications should
  109. * use SDL_OpenTitleStorage() for access to game data and
  110. * SDL_OpenUserStorage() for access to user data.
  111. *
  112. * \param path the base path prepended to all storage paths, or NULL for no
  113. * base path
  114. * \returns a filesystem storage container on success or NULL on failure; call
  115. * SDL_GetError() for more information.
  116. *
  117. * \since This function is available since SDL 3.0.0.
  118. *
  119. * \sa SDL_OpenStorage
  120. * \sa SDL_CloseStorage
  121. * \sa SDL_StorageReady
  122. * \sa SDL_GetStorageFileSize
  123. * \sa SDL_ReadStorageFile
  124. * \sa SDL_WriteStorageFile
  125. * \sa SDL_GetStorageSpaceRemaining
  126. */
  127. extern DECLSPEC SDL_Storage *SDLCALL SDL_OpenFileStorage(const char *path);
  128. /**
  129. * Opens up a container using a client-provided storage interface.
  130. *
  131. * Applications do not need to use this function unless they are providing
  132. * their own SDL_Storage implementation. If you just need an SDL_Storage, you
  133. * should use the built-in implementations in SDL, like SDL_OpenTitleStorage()
  134. * or SDL_OpenUserStorage().
  135. *
  136. * \param iface the function table to be used by this container
  137. * \param userdata the pointer that will be passed to the store interface
  138. * \returns a storage container on success or NULL on failure; call
  139. * SDL_GetError() for more information.
  140. *
  141. * \since This function is available since SDL 3.0.0.
  142. *
  143. * \sa SDL_OpenTitleStorage
  144. * \sa SDL_OpenUserStorage
  145. * \sa SDL_CloseStorage
  146. * \sa SDL_StorageReady
  147. * \sa SDL_GetStorageFileSize
  148. * \sa SDL_ReadStorageFile
  149. * \sa SDL_WriteStorageFile
  150. * \sa SDL_GetStorageSpaceRemaining
  151. */
  152. extern DECLSPEC SDL_Storage *SDLCALL SDL_OpenStorage(const SDL_StorageInterface *iface, void *userdata);
  153. /**
  154. * Closes and frees a storage container.
  155. *
  156. * \param storage a storage container to close
  157. * \returns 0 if the container was freed with no errors, a negative value
  158. * otherwise; call SDL_GetError() for more information. Even if the
  159. * function returns an error, the container data will be freed; the
  160. * error is only for informational purposes.
  161. *
  162. * \since This function is available since SDL 3.0.0.
  163. *
  164. * \sa SDL_OpenTitleStorage
  165. * \sa SDL_OpenUserStorage
  166. * \sa SDL_OpenStorage
  167. * \sa SDL_StorageReady
  168. * \sa SDL_GetStorageFileSize
  169. * \sa SDL_ReadStorageFile
  170. * \sa SDL_WriteStorageFile
  171. * \sa SDL_GetStorageSpaceRemaining
  172. */
  173. extern DECLSPEC int SDLCALL SDL_CloseStorage(SDL_Storage *storage);
  174. /**
  175. * Checks if the storage container is ready to use.
  176. *
  177. * This function should be called in regular intervals until it returns
  178. * SDL_TRUE - however, it is not recommended to spinwait on this call, as the
  179. * backend may depend on a synchronous message loop.
  180. *
  181. * \param storage a storage container to query
  182. * \returns SDL_TRUE if the container is ready, SDL_FALSE otherwise
  183. *
  184. * \since This function is available since SDL 3.0.0.
  185. *
  186. * \sa SDL_OpenTitleStorage
  187. * \sa SDL_OpenUserStorage
  188. * \sa SDL_OpenStorage
  189. * \sa SDL_CloseStorage
  190. * \sa SDL_GetStorageFileSize
  191. * \sa SDL_ReadStorageFile
  192. * \sa SDL_WriteStorageFile
  193. * \sa SDL_GetStorageSpaceRemaining
  194. */
  195. extern DECLSPEC SDL_bool SDLCALL SDL_StorageReady(SDL_Storage *storage);
  196. /**
  197. * Query the size of a file within a storage container.
  198. *
  199. * \param storage a storage container to query
  200. * \param path the relative path of the file to query
  201. * \param length a pointer to be filled with the file's length
  202. * \returns 0 if the file could be queried, a negative value otherwise; call
  203. * SDL_GetError() for more information.
  204. *
  205. * \since This function is available since SDL 3.0.0.
  206. *
  207. * \sa SDL_OpenTitleStorage
  208. * \sa SDL_OpenUserStorage
  209. * \sa SDL_OpenStorage
  210. * \sa SDL_CloseStorage
  211. * \sa SDL_StorageReady
  212. * \sa SDL_ReadStorageFile
  213. * \sa SDL_WriteStorageFile
  214. * \sa SDL_GetStorageSpaceRemaining
  215. */
  216. extern DECLSPEC int SDLCALL SDL_GetStorageFileSize(SDL_Storage *storage, const char *path, Uint64 *length);
  217. /**
  218. * Synchronously read a file from a storage container into a client-provided
  219. * buffer.
  220. *
  221. * \param storage a storage container to read from
  222. * \param path the relative path of the file to read
  223. * \param destination a client-provided buffer to read the file into
  224. * \param length the length of the destination buffer
  225. * \returns 0 if the file was read, a negative value otherwise; call
  226. * SDL_GetError() for more information.
  227. *
  228. * \since This function is available since SDL 3.0.0.
  229. *
  230. * \sa SDL_OpenTitleStorage
  231. * \sa SDL_OpenUserStorage
  232. * \sa SDL_OpenStorage
  233. * \sa SDL_CloseStorage
  234. * \sa SDL_StorageReady
  235. * \sa SDL_GetStorageFileSize
  236. * \sa SDL_WriteStorageFile
  237. * \sa SDL_GetStorageSpaceRemaining
  238. */
  239. extern DECLSPEC int SDLCALL SDL_ReadStorageFile(SDL_Storage *storage, const char *path, void *destination, Uint64 length);
  240. /**
  241. * Synchronously write a file from client memory into a storage container.
  242. *
  243. * \param storage a storage container to write to
  244. * \param path the relative path of the file to write
  245. * \param source a client-provided buffer to write from
  246. * \param length the length of the source buffer
  247. * \returns 0 if the file was written, a negative value otherwise; call
  248. * SDL_GetError() for more information.
  249. *
  250. * \since This function is available since SDL 3.0.0.
  251. *
  252. * \sa SDL_OpenTitleStorage
  253. * \sa SDL_OpenUserStorage
  254. * \sa SDL_OpenStorage
  255. * \sa SDL_CloseStorage
  256. * \sa SDL_StorageReady
  257. * \sa SDL_GetStorageFileSize
  258. * \sa SDL_ReadStorageFile
  259. * \sa SDL_GetStorageSpaceRemaining
  260. */
  261. extern DECLSPEC int SDL_WriteStorageFile(SDL_Storage *storage, const char *path, const void *source, Uint64 length);
  262. /**
  263. * Create a directory in a writable storage container.
  264. *
  265. * \param storage a storage container
  266. * \param path the path of the directory to create
  267. * \returns 0 on success or a negative error code on failure; call
  268. * SDL_GetError() for more information.
  269. *
  270. * \since This function is available since SDL 3.0.0.
  271. */
  272. extern DECLSPEC int SDLCALL SDL_CreateStorageDirectory(SDL_Storage *storage, const char *path);
  273. /**
  274. * Enumerate a directory in a storage container.
  275. *
  276. * \param storage a storage container
  277. * \param path the path of the directory to enumerate
  278. * \param callback a function that is called for each entry in the directory
  279. * \param userdata a pointer that is passed to `callback`
  280. * \returns 0 on success or a negative error code on failure; call
  281. * SDL_GetError() for more information.
  282. *
  283. * \since This function is available since SDL 3.0.0.
  284. */
  285. extern DECLSPEC int SDLCALL SDL_EnumerateStorageDirectory(SDL_Storage *storage, const char *path, SDL_EnumerateDirectoryCallback callback, void *userdata);
  286. /**
  287. * Remove a file or an empty directory in a writable storage container.
  288. *
  289. * \param storage a storage container
  290. * \param path the path of the directory to enumerate
  291. * \returns 0 on success or a negative error code on failure; call
  292. * SDL_GetError() for more information.
  293. *
  294. * \since This function is available since SDL 3.0.0.
  295. */
  296. extern DECLSPEC int SDLCALL SDL_RemoveStoragePath(SDL_Storage *storage, const char *path);
  297. /**
  298. * Rename a file or directory in a writable storage container.
  299. *
  300. * \param storage a storage container
  301. * \param oldpath the old path
  302. * \param newpath the new path
  303. * \returns 0 on success or a negative error code on failure; call
  304. * SDL_GetError() for more information.
  305. *
  306. * \since This function is available since SDL 3.0.0.
  307. */
  308. extern DECLSPEC int SDLCALL SDL_RenameStoragePath(SDL_Storage *storage, const char *oldpath, const char *newpath);
  309. /**
  310. * Get information about a filesystem path in a storage container.
  311. *
  312. * \param storage a storage container
  313. * \param path the path to query
  314. * \param info a pointer filled in with information about the path
  315. * \returns 0 on success or a negative error code on failure; call
  316. * SDL_GetError() for more information.
  317. *
  318. * \since This function is available since SDL 3.0.0.
  319. */
  320. extern DECLSPEC int SDLCALL SDL_GetStoragePathInfo(SDL_Storage *storage, const char *path, SDL_PathInfo *info);
  321. /**
  322. * Queries the remaining space in a storage container.
  323. *
  324. * \param storage a storage container to query
  325. * \returns the amount of remaining space, in bytes
  326. *
  327. * \since This function is available since SDL 3.0.0.
  328. *
  329. * \sa SDL_OpenTitleStorage
  330. * \sa SDL_OpenUserStorage
  331. * \sa SDL_OpenStorage
  332. * \sa SDL_CloseStorage
  333. * \sa SDL_StorageReady
  334. * \sa SDL_GetStorageFileSize
  335. * \sa SDL_ReadStorageFile
  336. * \sa SDL_WriteStorageFile
  337. */
  338. extern DECLSPEC Uint64 SDLCALL SDL_GetStorageSpaceRemaining(SDL_Storage *storage);
  339. /* Ends C function definitions when using C++ */
  340. #ifdef __cplusplus
  341. }
  342. #endif
  343. #include <SDL3/SDL_close_code.h>
  344. #endif /* SDL_storage_h_ */