SDL_storage.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  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. * # CategoryStorage
  20. *
  21. * SDL storage container management.
  22. */
  23. #ifndef SDL_storage_h_
  24. #define SDL_storage_h_
  25. #include <SDL3/SDL_stdinc.h>
  26. #include <SDL3/SDL_error.h>
  27. #include <SDL3/SDL_filesystem.h>
  28. #include <SDL3/SDL_properties.h>
  29. #include <SDL3/SDL_begin_code.h>
  30. /* Set up for C function definitions, even when using C++ */
  31. #ifdef __cplusplus
  32. extern "C" {
  33. #endif
  34. /* !!! FIXME: Don't let this ship without async R/W support!!! */
  35. /**
  36. * Function interface for SDL_Storage.
  37. *
  38. * Apps that want to supply a custom implementation of SDL_Storage will fill
  39. * in all the functions in this struct, and then pass it to SDL_OpenStorage to
  40. * create a custom SDL_Storage object.
  41. *
  42. * It is not usually necessary to do this; SDL provides standard
  43. * implementations for many things you might expect to do with an SDL_Storage.
  44. *
  45. * \since This struct is available since SDL 3.0.0.
  46. */
  47. typedef struct SDL_StorageInterface
  48. {
  49. /* Called when the storage is closed */
  50. int (SDLCALL *close)(void *userdata);
  51. /* Optional, returns whether the storage is currently ready for access */
  52. SDL_bool (SDLCALL *ready)(void *userdata);
  53. /* Enumerate a directory, optional for write-only storage */
  54. int (SDLCALL *enumerate)(void *userdata, const char *path, SDL_EnumerateDirectoryCallback callback, void *callback_userdata);
  55. /* Get path information, optional for write-only storage */
  56. int (SDLCALL *info)(void *userdata, const char *path, SDL_PathInfo *info);
  57. /* Read a file from storage, optional for write-only storage */
  58. int (SDLCALL *read_file)(void *userdata, const char *path, void *destination, Uint64 length);
  59. /* Write a file to storage, optional for read-only storage */
  60. int (SDLCALL *write_file)(void *userdata, const char *path, const void *source, Uint64 length);
  61. /* Create a directory, optional for read-only storage */
  62. int (SDLCALL *mkdir)(void *userdata, const char *path);
  63. /* Remove a file or empty directory, optional for read-only storage */
  64. int (SDLCALL *remove)(void *userdata, const char *path);
  65. /* Rename a path, optional for read-only storage */
  66. int (SDLCALL *rename)(void *userdata, const char *oldpath, const char *newpath);
  67. /* Get the space remaining, optional for read-only storage */
  68. Uint64 (SDLCALL *space_remaining)(void *userdata);
  69. } SDL_StorageInterface;
  70. /**
  71. * An abstract interface for filesystem access.
  72. *
  73. * This is an opaque datatype. One can create this object using standard SDL
  74. * functions like SDL_OpenTitleStorage or SDL_OpenUserStorage, etc, or create
  75. * an object with a custom implementation using SDL_OpenStorage.
  76. *
  77. * \since This struct is available since SDL 3.0.0.
  78. */
  79. typedef struct SDL_Storage SDL_Storage;
  80. /**
  81. * Opens up a read-only container for the application's filesystem.
  82. *
  83. * \param override a path to override the backend's default title root.
  84. * \param props a property list that may contain backend-specific information.
  85. * \returns a title 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_OpenUserStorage
  93. * \sa SDL_ReadStorageFile
  94. */
  95. extern SDL_DECLSPEC SDL_Storage *SDLCALL SDL_OpenTitleStorage(const char *override, SDL_PropertiesID props);
  96. /**
  97. * Opens up a container for a user's unique read/write filesystem.
  98. *
  99. * While title storage can generally be kept open throughout runtime, user
  100. * storage should only be opened when the client is ready to read/write files.
  101. * This allows the backend to properly batch file operations and flush them
  102. * when the container has been closed; ensuring safe and optimal save I/O.
  103. *
  104. * \param org the name of your organization.
  105. * \param app the name of your application.
  106. * \param props a property list that may contain backend-specific information.
  107. * \returns a user storage container on success or NULL on failure; call
  108. * SDL_GetError() for more information.
  109. *
  110. * \since This function is available since SDL 3.0.0.
  111. *
  112. * \sa SDL_CloseStorage
  113. * \sa SDL_GetStorageFileSize
  114. * \sa SDL_GetStorageSpaceRemaining
  115. * \sa SDL_OpenTitleStorage
  116. * \sa SDL_ReadStorageFile
  117. * \sa SDL_StorageReady
  118. * \sa SDL_WriteStorageFile
  119. */
  120. extern SDL_DECLSPEC SDL_Storage *SDLCALL SDL_OpenUserStorage(const char *org, const char *app, SDL_PropertiesID props);
  121. /**
  122. * Opens up a container for local filesystem storage.
  123. *
  124. * This is provided for development and tools. Portable applications should
  125. * use SDL_OpenTitleStorage() for access to game data and
  126. * SDL_OpenUserStorage() for access to user data.
  127. *
  128. * \param path the base path prepended to all storage paths, or NULL for no
  129. * base path.
  130. * \returns a filesystem storage container on success or NULL on failure; call
  131. * SDL_GetError() for more information.
  132. *
  133. * \since This function is available since SDL 3.0.0.
  134. *
  135. * \sa SDL_CloseStorage
  136. * \sa SDL_GetStorageFileSize
  137. * \sa SDL_GetStorageSpaceRemaining
  138. * \sa SDL_OpenTitleStorage
  139. * \sa SDL_OpenUserStorage
  140. * \sa SDL_ReadStorageFile
  141. * \sa SDL_WriteStorageFile
  142. */
  143. extern SDL_DECLSPEC SDL_Storage *SDLCALL SDL_OpenFileStorage(const char *path);
  144. /**
  145. * Opens up a container using a client-provided storage interface.
  146. *
  147. * Applications do not need to use this function unless they are providing
  148. * their own SDL_Storage implementation. If you just need an SDL_Storage, you
  149. * should use the built-in implementations in SDL, like SDL_OpenTitleStorage()
  150. * or SDL_OpenUserStorage().
  151. *
  152. * \param iface the function table to be used by this container.
  153. * \param userdata the pointer that will be passed to the store interface.
  154. * \returns a storage container on success or NULL on failure; call
  155. * SDL_GetError() for more information.
  156. *
  157. * \since This function is available since SDL 3.0.0.
  158. *
  159. * \sa SDL_CloseStorage
  160. * \sa SDL_GetStorageFileSize
  161. * \sa SDL_GetStorageSpaceRemaining
  162. * \sa SDL_ReadStorageFile
  163. * \sa SDL_StorageReady
  164. * \sa SDL_WriteStorageFile
  165. */
  166. extern SDL_DECLSPEC SDL_Storage *SDLCALL SDL_OpenStorage(const SDL_StorageInterface *iface, void *userdata);
  167. /**
  168. * Closes and frees a storage container.
  169. *
  170. * \param storage a storage container to close.
  171. * \returns 0 if the container was freed with no errors, a negative value
  172. * otherwise; call SDL_GetError() for more information. Even if the
  173. * function returns an error, the container data will be freed; the
  174. * error is only for informational purposes.
  175. *
  176. * \since This function is available since SDL 3.0.0.
  177. *
  178. * \sa SDL_OpenFileStorage
  179. * \sa SDL_OpenStorage
  180. * \sa SDL_OpenTitleStorage
  181. * \sa SDL_OpenUserStorage
  182. */
  183. extern SDL_DECLSPEC int SDLCALL SDL_CloseStorage(SDL_Storage *storage);
  184. /**
  185. * Checks if the storage container is ready to use.
  186. *
  187. * This function should be called in regular intervals until it returns
  188. * SDL_TRUE - however, it is not recommended to spinwait on this call, as the
  189. * backend may depend on a synchronous message loop.
  190. *
  191. * \param storage a storage container to query.
  192. * \returns SDL_TRUE if the container is ready, SDL_FALSE otherwise.
  193. *
  194. * \since This function is available since SDL 3.0.0.
  195. */
  196. extern SDL_DECLSPEC SDL_bool SDLCALL SDL_StorageReady(SDL_Storage *storage);
  197. /**
  198. * Query the size of a file within a storage container.
  199. *
  200. * \param storage a storage container to query.
  201. * \param path the relative path of the file to query.
  202. * \param length a pointer to be filled with the file's length.
  203. * \returns 0 if the file could be queried, a negative value otherwise; call
  204. * SDL_GetError() for more information.
  205. *
  206. * \since This function is available since SDL 3.0.0.
  207. *
  208. * \sa SDL_ReadStorageFile
  209. * \sa SDL_StorageReady
  210. */
  211. extern SDL_DECLSPEC int SDLCALL SDL_GetStorageFileSize(SDL_Storage *storage, const char *path, Uint64 *length);
  212. /**
  213. * Synchronously read a file from a storage container into a client-provided
  214. * buffer.
  215. *
  216. * \param storage a storage container to read from.
  217. * \param path the relative path of the file to read.
  218. * \param destination a client-provided buffer to read the file into.
  219. * \param length the length of the destination buffer.
  220. * \returns 0 if the file was read, a negative value otherwise; call
  221. * SDL_GetError() for more information.
  222. *
  223. * \since This function is available since SDL 3.0.0.
  224. *
  225. * \sa SDL_GetStorageFileSize
  226. * \sa SDL_StorageReady
  227. * \sa SDL_WriteStorageFile
  228. */
  229. extern SDL_DECLSPEC int SDLCALL SDL_ReadStorageFile(SDL_Storage *storage, const char *path, void *destination, Uint64 length);
  230. /**
  231. * Synchronously write a file from client memory into a storage container.
  232. *
  233. * \param storage a storage container to write to.
  234. * \param path the relative path of the file to write.
  235. * \param source a client-provided buffer to write from.
  236. * \param length the length of the source buffer.
  237. * \returns 0 if the file was written, a negative value otherwise; call
  238. * SDL_GetError() for more information.
  239. *
  240. * \since This function is available since SDL 3.0.0.
  241. *
  242. * \sa SDL_GetStorageSpaceRemaining
  243. * \sa SDL_ReadStorageFile
  244. * \sa SDL_StorageReady
  245. */
  246. extern SDL_DECLSPEC int SDLCALL SDL_WriteStorageFile(SDL_Storage *storage, const char *path, const void *source, Uint64 length);
  247. /**
  248. * Create a directory in a writable storage container.
  249. *
  250. * \param storage a storage container.
  251. * \param path the path of the directory to create.
  252. * \returns 0 on success or a negative error code on failure; call
  253. * SDL_GetError() for more information.
  254. *
  255. * \since This function is available since SDL 3.0.0.
  256. *
  257. * \sa SDL_StorageReady
  258. */
  259. extern SDL_DECLSPEC int SDLCALL SDL_CreateStorageDirectory(SDL_Storage *storage, const char *path);
  260. /**
  261. * Enumerate a directory in a storage container through a callback function.
  262. *
  263. * This function provides every directory entry through an app-provided
  264. * callback, called once for each directory entry, until all results have been
  265. * provided or the callback returns <= 0.
  266. *
  267. * \param storage a storage container.
  268. * \param path the path of the directory to enumerate.
  269. * \param callback a function that is called for each entry in the directory.
  270. * \param userdata a pointer that is passed to `callback`.
  271. * \returns 0 on success or a negative error code on failure; call
  272. * SDL_GetError() for more information.
  273. *
  274. * \since This function is available since SDL 3.0.0.
  275. *
  276. * \sa SDL_StorageReady
  277. */
  278. extern SDL_DECLSPEC int SDLCALL SDL_EnumerateStorageDirectory(SDL_Storage *storage, const char *path, SDL_EnumerateDirectoryCallback callback, void *userdata);
  279. /**
  280. * Remove a file or an empty directory in a writable storage container.
  281. *
  282. * \param storage a storage container.
  283. * \param path the path of the directory to enumerate.
  284. * \returns 0 on success or a negative error code on failure; call
  285. * SDL_GetError() for more information.
  286. *
  287. * \since This function is available since SDL 3.0.0.
  288. *
  289. * \sa SDL_StorageReady
  290. */
  291. extern SDL_DECLSPEC int SDLCALL SDL_RemoveStoragePath(SDL_Storage *storage, const char *path);
  292. /**
  293. * Rename a file or directory in a writable storage container.
  294. *
  295. * \param storage a storage container.
  296. * \param oldpath the old path.
  297. * \param newpath the new path.
  298. * \returns 0 on success or a negative error code on failure; call
  299. * SDL_GetError() for more information.
  300. *
  301. * \since This function is available since SDL 3.0.0.
  302. *
  303. * \sa SDL_StorageReady
  304. */
  305. extern SDL_DECLSPEC int SDLCALL SDL_RenameStoragePath(SDL_Storage *storage, const char *oldpath, const char *newpath);
  306. /**
  307. * Get information about a filesystem path in a storage container.
  308. *
  309. * \param storage a storage container.
  310. * \param path the path to query.
  311. * \param info a pointer filled in with information about the path, or NULL to
  312. * check for the existence of a file.
  313. * \returns 0 on success or a negative error code if the file doesn't exist,
  314. * or another failure; call SDL_GetError() for more information.
  315. *
  316. * \since This function is available since SDL 3.0.0.
  317. *
  318. * \sa SDL_StorageReady
  319. */
  320. extern SDL_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_StorageReady
  330. * \sa SDL_WriteStorageFile
  331. */
  332. extern SDL_DECLSPEC Uint64 SDLCALL SDL_GetStorageSpaceRemaining(SDL_Storage *storage);
  333. /**
  334. * Enumerate a directory tree, filtered by pattern, and return a list.
  335. *
  336. * Files are filtered out if they don't match the string in `pattern`, which
  337. * may contain wildcard characters '*' (match everything) and '?' (match one
  338. * character). If pattern is NULL, no filtering is done and all results are
  339. * returned. Subdirectories are permitted, and are specified with a path
  340. * separator of '/'. Wildcard characters '*' and '?' never match a path
  341. * separator.
  342. *
  343. * `flags` may be set to SDL_GLOB_CASEINSENSITIVE to make the pattern matching
  344. * case-insensitive.
  345. *
  346. * The returned array is always NULL-terminated, for your iterating
  347. * convenience, but if `count` is non-NULL, on return it will contain the
  348. * number of items in the array, not counting the NULL terminator.
  349. *
  350. * You must free the returned pointer with SDL_free() when done with it.
  351. *
  352. * \param storage a storage container.
  353. * \param path the path of the directory to enumerate.
  354. * \param pattern the pattern that files in the directory must match. Can be
  355. * NULL.
  356. * \param flags `SDL_GLOB_*` bitflags that affect this search.
  357. * \param count on return, will be set to the number of items in the returned
  358. * array. Can be NULL.
  359. * \returns an array of strings on success or NULL on failure; call
  360. * SDL_GetError() for more information. The caller should pass the
  361. * returned pointer to SDL_free when done with it.
  362. *
  363. * \threadsafety It is safe to call this function from any thread, assuming
  364. * the `storage` object is thread-safe.
  365. *
  366. * \since This function is available since SDL 3.0.0.
  367. */
  368. extern SDL_DECLSPEC char **SDLCALL SDL_GlobStorageDirectory(SDL_Storage *storage, const char *path, const char *pattern, SDL_GlobFlags flags, int *count);
  369. /* Ends C function definitions when using C++ */
  370. #ifdef __cplusplus
  371. }
  372. #endif
  373. #include <SDL3/SDL_close_code.h>
  374. #endif /* SDL_storage_h_ */