SDL_storage.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  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. * # 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. /* Copy a file, optional for read-only storage */
  68. int (SDLCALL *copy)(void *userdata, const char *oldpath, const char *newpath);
  69. /* Get the space remaining, optional for read-only storage */
  70. Uint64 (SDLCALL *space_remaining)(void *userdata);
  71. } SDL_StorageInterface;
  72. /**
  73. * An abstract interface for filesystem access.
  74. *
  75. * This is an opaque datatype. One can create this object using standard SDL
  76. * functions like SDL_OpenTitleStorage or SDL_OpenUserStorage, etc, or create
  77. * an object with a custom implementation using SDL_OpenStorage.
  78. *
  79. * \since This struct is available since SDL 3.0.0.
  80. */
  81. typedef struct SDL_Storage SDL_Storage;
  82. /**
  83. * Opens up a read-only container for the application's filesystem.
  84. *
  85. * \param override a path to override the backend's default title root.
  86. * \param props a property list that may contain backend-specific information.
  87. * \returns a title storage container on success or NULL on failure; call
  88. * SDL_GetError() for more information.
  89. *
  90. * \since This function is available since SDL 3.0.0.
  91. *
  92. * \sa SDL_CloseStorage
  93. * \sa SDL_GetStorageFileSize
  94. * \sa SDL_OpenUserStorage
  95. * \sa SDL_ReadStorageFile
  96. */
  97. extern SDL_DECLSPEC SDL_Storage * SDLCALL SDL_OpenTitleStorage(const char *override, SDL_PropertiesID props);
  98. /**
  99. * Opens up a container for a user's unique read/write filesystem.
  100. *
  101. * While title storage can generally be kept open throughout runtime, user
  102. * storage should only be opened when the client is ready to read/write files.
  103. * This allows the backend to properly batch file operations and flush them
  104. * when the container has been closed; ensuring safe and optimal save I/O.
  105. *
  106. * \param org the name of your organization.
  107. * \param app the name of your application.
  108. * \param props a property list that may contain backend-specific information.
  109. * \returns a user storage container on success or NULL on failure; call
  110. * SDL_GetError() for more information.
  111. *
  112. * \since This function is available since SDL 3.0.0.
  113. *
  114. * \sa SDL_CloseStorage
  115. * \sa SDL_GetStorageFileSize
  116. * \sa SDL_GetStorageSpaceRemaining
  117. * \sa SDL_OpenTitleStorage
  118. * \sa SDL_ReadStorageFile
  119. * \sa SDL_StorageReady
  120. * \sa SDL_WriteStorageFile
  121. */
  122. extern SDL_DECLSPEC SDL_Storage * SDLCALL SDL_OpenUserStorage(const char *org, const char *app, SDL_PropertiesID props);
  123. /**
  124. * Opens up a container for local filesystem storage.
  125. *
  126. * This is provided for development and tools. Portable applications should
  127. * use SDL_OpenTitleStorage() for access to game data and
  128. * SDL_OpenUserStorage() for access to user data.
  129. *
  130. * \param path the base path prepended to all storage paths, or NULL for no
  131. * base path.
  132. * \returns a filesystem 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_OpenTitleStorage
  141. * \sa SDL_OpenUserStorage
  142. * \sa SDL_ReadStorageFile
  143. * \sa SDL_WriteStorageFile
  144. */
  145. extern SDL_DECLSPEC SDL_Storage * SDLCALL SDL_OpenFileStorage(const char *path);
  146. /**
  147. * Opens up a container using a client-provided storage interface.
  148. *
  149. * Applications do not need to use this function unless they are providing
  150. * their own SDL_Storage implementation. If you just need an SDL_Storage, you
  151. * should use the built-in implementations in SDL, like SDL_OpenTitleStorage()
  152. * or SDL_OpenUserStorage().
  153. *
  154. * \param iface the function table to be used by this container.
  155. * \param userdata the pointer that will be passed to the store interface.
  156. * \returns a storage container on success or NULL on failure; call
  157. * SDL_GetError() for more information.
  158. *
  159. * \since This function is available since SDL 3.0.0.
  160. *
  161. * \sa SDL_CloseStorage
  162. * \sa SDL_GetStorageFileSize
  163. * \sa SDL_GetStorageSpaceRemaining
  164. * \sa SDL_ReadStorageFile
  165. * \sa SDL_StorageReady
  166. * \sa SDL_WriteStorageFile
  167. */
  168. extern SDL_DECLSPEC SDL_Storage * SDLCALL SDL_OpenStorage(const SDL_StorageInterface *iface, void *userdata);
  169. /**
  170. * Closes and frees a storage container.
  171. *
  172. * \param storage a storage container to close.
  173. * \returns 0 if the container was freed with no errors, a negative value
  174. * otherwise; call SDL_GetError() for more information. Even if the
  175. * function returns an error, the container data will be freed; the
  176. * error is only for informational purposes.
  177. *
  178. * \since This function is available since SDL 3.0.0.
  179. *
  180. * \sa SDL_OpenFileStorage
  181. * \sa SDL_OpenStorage
  182. * \sa SDL_OpenTitleStorage
  183. * \sa SDL_OpenUserStorage
  184. */
  185. extern SDL_DECLSPEC int SDLCALL SDL_CloseStorage(SDL_Storage *storage);
  186. /**
  187. * Checks if the storage container is ready to use.
  188. *
  189. * This function should be called in regular intervals until it returns
  190. * SDL_TRUE - however, it is not recommended to spinwait on this call, as the
  191. * backend may depend on a synchronous message loop.
  192. *
  193. * \param storage a storage container to query.
  194. * \returns SDL_TRUE if the container is ready, SDL_FALSE otherwise.
  195. *
  196. * \since This function is available since SDL 3.0.0.
  197. */
  198. extern SDL_DECLSPEC SDL_bool SDLCALL SDL_StorageReady(SDL_Storage *storage);
  199. /**
  200. * Query the size of a file within a storage container.
  201. *
  202. * \param storage a storage container to query.
  203. * \param path the relative path of the file to query.
  204. * \param length a pointer to be filled with the file's length.
  205. * \returns 0 if the file could be queried or a negative error code on
  206. * failure; call SDL_GetError() for more information.
  207. *
  208. * \since This function is available since SDL 3.0.0.
  209. *
  210. * \sa SDL_ReadStorageFile
  211. * \sa SDL_StorageReady
  212. */
  213. extern SDL_DECLSPEC int SDLCALL SDL_GetStorageFileSize(SDL_Storage *storage, const char *path, Uint64 *length);
  214. /**
  215. * Synchronously read a file from a storage container into a client-provided
  216. * buffer.
  217. *
  218. * \param storage a storage container to read from.
  219. * \param path the relative path of the file to read.
  220. * \param destination a client-provided buffer to read the file into.
  221. * \param length the length of the destination buffer.
  222. * \returns 0 if the file was read or a negative error code on failure; call
  223. * SDL_GetError() for more information.
  224. *
  225. * \since This function is available since SDL 3.0.0.
  226. *
  227. * \sa SDL_GetStorageFileSize
  228. * \sa SDL_StorageReady
  229. * \sa SDL_WriteStorageFile
  230. */
  231. extern SDL_DECLSPEC int SDLCALL SDL_ReadStorageFile(SDL_Storage *storage, const char *path, void *destination, Uint64 length);
  232. /**
  233. * Synchronously write a file from client memory into a storage container.
  234. *
  235. * \param storage a storage container to write to.
  236. * \param path the relative path of the file to write.
  237. * \param source a client-provided buffer to write from.
  238. * \param length the length of the source buffer.
  239. * \returns 0 if the file was written or a negative error code on failure;
  240. * call SDL_GetError() for more information.
  241. *
  242. * \since This function is available since SDL 3.0.0.
  243. *
  244. * \sa SDL_GetStorageSpaceRemaining
  245. * \sa SDL_ReadStorageFile
  246. * \sa SDL_StorageReady
  247. */
  248. extern SDL_DECLSPEC int SDLCALL SDL_WriteStorageFile(SDL_Storage *storage, const char *path, const void *source, Uint64 length);
  249. /**
  250. * Create a directory in a writable storage container.
  251. *
  252. * \param storage a storage container.
  253. * \param path the path of the directory to create.
  254. * \returns 0 on success or a negative error code on failure; call
  255. * SDL_GetError() for more information.
  256. *
  257. * \since This function is available since SDL 3.0.0.
  258. *
  259. * \sa SDL_StorageReady
  260. */
  261. extern SDL_DECLSPEC int SDLCALL SDL_CreateStorageDirectory(SDL_Storage *storage, const char *path);
  262. /**
  263. * Enumerate a directory in a storage container through a callback function.
  264. *
  265. * This function provides every directory entry through an app-provided
  266. * callback, called once for each directory entry, until all results have been
  267. * provided or the callback returns <= 0.
  268. *
  269. * \param storage a storage container.
  270. * \param path the path of the directory to enumerate.
  271. * \param callback a function that is called for each entry in the directory.
  272. * \param userdata a pointer that is passed to `callback`.
  273. * \returns 0 on success or a negative error code on failure; call
  274. * SDL_GetError() for more information.
  275. *
  276. * \since This function is available since SDL 3.0.0.
  277. *
  278. * \sa SDL_StorageReady
  279. */
  280. extern SDL_DECLSPEC int SDLCALL SDL_EnumerateStorageDirectory(SDL_Storage *storage, const char *path, SDL_EnumerateDirectoryCallback callback, void *userdata);
  281. /**
  282. * Remove a file or an empty directory in a writable storage container.
  283. *
  284. * \param storage a storage container.
  285. * \param path the path of the directory to enumerate.
  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 SDL_DECLSPEC int SDLCALL SDL_RemoveStoragePath(SDL_Storage *storage, const char *path);
  294. /**
  295. * Rename a file or directory in a writable storage container.
  296. *
  297. * \param storage a storage container.
  298. * \param oldpath the old path.
  299. * \param newpath the new path.
  300. * \returns 0 on success or a negative error code on failure; call
  301. * SDL_GetError() for more information.
  302. *
  303. * \since This function is available since SDL 3.0.0.
  304. *
  305. * \sa SDL_StorageReady
  306. */
  307. extern SDL_DECLSPEC int SDLCALL SDL_RenameStoragePath(SDL_Storage *storage, const char *oldpath, const char *newpath);
  308. /**
  309. * Copy a file in a writable storage container.
  310. *
  311. * \param storage a storage container.
  312. * \param oldpath the old path.
  313. * \param newpath the new path.
  314. * \returns 0 on success or a negative error code on failure; call
  315. * SDL_GetError() for more information.
  316. *
  317. * \since This function is available since SDL 3.0.0.
  318. *
  319. * \sa SDL_StorageReady
  320. */
  321. extern SDL_DECLSPEC int SDLCALL SDL_CopyStorageFile(SDL_Storage *storage, const char *oldpath, const char *newpath);
  322. /**
  323. * Get information about a filesystem path in a storage container.
  324. *
  325. * \param storage a storage container.
  326. * \param path the path to query.
  327. * \param info a pointer filled in with information about the path, or NULL to
  328. * check for the existence of a file.
  329. * \returns 0 on success or a negative error code if the file doesn't exist,
  330. * or another failure; call SDL_GetError() for more information.
  331. *
  332. * \since This function is available since SDL 3.0.0.
  333. *
  334. * \sa SDL_StorageReady
  335. */
  336. extern SDL_DECLSPEC int SDLCALL SDL_GetStoragePathInfo(SDL_Storage *storage, const char *path, SDL_PathInfo *info);
  337. /**
  338. * Queries the remaining space in a storage container.
  339. *
  340. * \param storage a storage container to query.
  341. * \returns the amount of remaining space, in bytes.
  342. *
  343. * \since This function is available since SDL 3.0.0.
  344. *
  345. * \sa SDL_StorageReady
  346. * \sa SDL_WriteStorageFile
  347. */
  348. extern SDL_DECLSPEC Uint64 SDLCALL SDL_GetStorageSpaceRemaining(SDL_Storage *storage);
  349. /**
  350. * Enumerate a directory tree, filtered by pattern, and return a list.
  351. *
  352. * Files are filtered out if they don't match the string in `pattern`, which
  353. * may contain wildcard characters '*' (match everything) and '?' (match one
  354. * character). If pattern is NULL, no filtering is done and all results are
  355. * returned. Subdirectories are permitted, and are specified with a path
  356. * separator of '/'. Wildcard characters '*' and '?' never match a path
  357. * separator.
  358. *
  359. * `flags` may be set to SDL_GLOB_CASEINSENSITIVE to make the pattern matching
  360. * case-insensitive.
  361. *
  362. * The returned array is always NULL-terminated, for your iterating
  363. * convenience, but if `count` is non-NULL, on return it will contain the
  364. * number of items in the array, not counting the NULL terminator.
  365. *
  366. * \param storage a storage container.
  367. * \param path the path of the directory to enumerate.
  368. * \param pattern the pattern that files in the directory must match. Can be
  369. * NULL.
  370. * \param flags `SDL_GLOB_*` bitflags that affect this search.
  371. * \param count on return, will be set to the number of items in the returned
  372. * array. Can be NULL.
  373. * \returns an array of strings on success or NULL on failure; call
  374. * SDL_GetError() for more information. The caller should pass the
  375. * returned pointer to SDL_free when done with it. This is a single allocation that should be freed with SDL_free() when it is no longer needed.
  376. *
  377. * \threadsafety It is safe to call this function from any thread, assuming
  378. * the `storage` object is thread-safe.
  379. *
  380. * \since This function is available since SDL 3.0.0.
  381. */
  382. extern SDL_DECLSPEC_FREE char ** SDLCALL SDL_GlobStorageDirectory(SDL_Storage *storage, const char *path, const char *pattern, SDL_GlobFlags flags, int *count);
  383. /* Ends C function definitions when using C++ */
  384. #ifdef __cplusplus
  385. }
  386. #endif
  387. #include <SDL3/SDL_close_code.h>
  388. #endif /* SDL_storage_h_ */