SDL_storage.h 14 KB

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