SDL_iostream.h 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009
  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_iostream.h
  20. *
  21. * This file provides a general interface for SDL to read and write
  22. * data streams. It can easily be extended to files, memory, etc.
  23. *
  24. * SDL_IOStream is not related to the standard C++ iostream class, other
  25. * than both are abstract interfaces to read/write data.
  26. */
  27. #ifndef SDL_iostream_h_
  28. #define SDL_iostream_h_
  29. #include <SDL3/SDL_stdinc.h>
  30. #include <SDL3/SDL_error.h>
  31. #include <SDL3/SDL_properties.h>
  32. #include <SDL3/SDL_begin_code.h>
  33. /* Set up for C function definitions, even when using C++ */
  34. #ifdef __cplusplus
  35. extern "C" {
  36. #endif
  37. /* SDL_IOStream status, set by a read or write operation */
  38. typedef enum SDL_IOStatus
  39. {
  40. SDL_IO_STATUS_READY, /**< Everything is ready */
  41. SDL_IO_STATUS_ERROR, /**< Read or write I/O error */
  42. SDL_IO_STATUS_EOF, /**< End of file */
  43. SDL_IO_STATUS_NOT_READY, /**< Non blocking I/O, not ready */
  44. SDL_IO_STATUS_READONLY, /**< Tried to write a read-only buffer */
  45. SDL_IO_STATUS_WRITEONLY /**< Tried to read a write-only buffer */
  46. } SDL_IOStatus;
  47. typedef struct SDL_IOStreamInterface
  48. {
  49. /**
  50. * Return the number of bytes in this SDL_IOStream
  51. *
  52. * \return the total size of the data stream, or -1 on error.
  53. */
  54. Sint64 (SDLCALL *size)(void *userdata);
  55. /**
  56. * Seek to \c offset relative to \c whence, one of stdio's whence values:
  57. * SDL_IO_SEEK_SET, SDL_IO_SEEK_CUR, SDL_IO_SEEK_END
  58. *
  59. * \return the final offset in the data stream, or -1 on error.
  60. */
  61. Sint64 (SDLCALL *seek)(void *userdata, Sint64 offset, int whence);
  62. /**
  63. * Read up to \c size bytes from the data stream to the area pointed
  64. * at by \c ptr.
  65. *
  66. * On an incomplete read, you should set `*status` to a value from the
  67. * SDL_IOStatus enum. You do not have to explicitly set this on
  68. * a complete, successful read.
  69. *
  70. * \return the number of bytes read
  71. */
  72. size_t (SDLCALL *read)(void *userdata, void *ptr, size_t size, SDL_IOStatus *status);
  73. /**
  74. * Write exactly \c size bytes from the area pointed at by \c ptr
  75. * to data stream.
  76. *
  77. * On an incomplete write, you should set `*status` to a value from the
  78. * SDL_IOStatus enum. You do not have to explicitly set this on
  79. * a complete, successful write.
  80. *
  81. * \return the number of bytes written
  82. */
  83. size_t (SDLCALL *write)(void *userdata, const void *ptr, size_t size, SDL_IOStatus *status);
  84. /**
  85. * Close and free any allocated resources.
  86. *
  87. * The SDL_IOStream is still destroyed even if this fails, so clean up anything
  88. * even if flushing to disk returns an error.
  89. *
  90. * \return 0 if successful or -1 on write error when flushing data.
  91. */
  92. int (SDLCALL *close)(void *userdata);
  93. } SDL_IOStreamInterface;
  94. /**
  95. * This is the read/write operation structure -- opaque, as of SDL3!
  96. */
  97. typedef struct SDL_IOStream SDL_IOStream;
  98. /**
  99. * \name IOFrom functions
  100. *
  101. * Functions to create SDL_IOStream structures from various data streams.
  102. */
  103. /* @{ */
  104. /**
  105. * Use this function to create a new SDL_IOStream structure for reading from
  106. * and/or writing to a named file.
  107. *
  108. * The `mode` string is treated roughly the same as in a call to the C
  109. * library's fopen(), even if SDL doesn't happen to use fopen() behind the
  110. * scenes.
  111. *
  112. * Available `mode` strings:
  113. *
  114. * - "r": Open a file for reading. The file must exist.
  115. * - "w": Create an empty file for writing. If a file with the same name
  116. * already exists its content is erased and the file is treated as a new
  117. * empty file.
  118. * - "a": Append to a file. Writing operations append data at the end of the
  119. * file. The file is created if it does not exist.
  120. * - "r+": Open a file for update both reading and writing. The file must
  121. * exist.
  122. * - "w+": Create an empty file for both reading and writing. If a file with
  123. * the same name already exists its content is erased and the file is
  124. * treated as a new empty file.
  125. * - "a+": Open a file for reading and appending. All writing operations are
  126. * performed at the end of the file, protecting the previous content to be
  127. * overwritten. You can reposition (fseek, rewind) the internal pointer to
  128. * anywhere in the file for reading, but writing operations will move it
  129. * back to the end of file. The file is created if it does not exist.
  130. *
  131. * **NOTE**: In order to open a file as a binary file, a "b" character has to
  132. * be included in the `mode` string. This additional "b" character can either
  133. * be appended at the end of the string (thus making the following compound
  134. * modes: "rb", "wb", "ab", "r+b", "w+b", "a+b") or be inserted between the
  135. * letter and the "+" sign for the mixed modes ("rb+", "wb+", "ab+").
  136. * Additional characters may follow the sequence, although they should have no
  137. * effect. For example, "t" is sometimes appended to make explicit the file is
  138. * a text file.
  139. *
  140. * This function supports Unicode filenames, but they must be encoded in UTF-8
  141. * format, regardless of the underlying operating system.
  142. *
  143. * As a fallback, SDL_IOFromFile() will transparently open a matching filename
  144. * in an Android app's `assets`.
  145. *
  146. * Closing the SDL_IOStream will close SDL's internal file handle.
  147. *
  148. * The following properties may be set at creation time by SDL:
  149. *
  150. * - `SDL_PROP_IOSTREAM_WINDOWS_HANDLE_POINTER`: a pointer, that can be cast
  151. * to a win32 `HANDLE`, that this SDL_IOStream is using to access the
  152. * filesystem. If the program isn't running on Windows, or SDL used some
  153. * other method to access the filesystem, this property will not be set.
  154. * - `SDL_PROP_IOSTREAM_STDIO_FILE_POINTER`: a pointer, that can be cast to a
  155. * stdio `FILE *`, that this SDL_IOStream is using to access the filesystem.
  156. * If SDL used some other method to access the filesystem, this property
  157. * will not be set. PLEASE NOTE that if SDL is using a different C runtime
  158. * than your app, trying to use this pointer will almost certainly result in
  159. * a crash! This is mostly a problem on Windows; make sure you build SDL and
  160. * your app with the same compiler and settings to avoid it.
  161. * - `SDL_PROP_IOSTREAM_ANDROID_AASSET_POINTER`: a pointer, that can be cast
  162. * to an Android NDK `AAsset *`, that this SDL_IOStream is using to access
  163. * the filesystem. If SDL used some other method to access the filesystem,
  164. * this property will not be set.
  165. *
  166. * \param file a UTF-8 string representing the filename to open
  167. * \param mode an ASCII string representing the mode to be used for opening
  168. * the file.
  169. * \returns a pointer to the SDL_IOStream structure that is created, or NULL
  170. * on failure; call SDL_GetError() for more information.
  171. *
  172. * \since This function is available since SDL 3.0.0.
  173. *
  174. * \sa SDL_CloseIO
  175. * \sa SDL_ReadIO
  176. * \sa SDL_SeekIO
  177. * \sa SDL_TellIO
  178. * \sa SDL_WriteIO
  179. */
  180. extern DECLSPEC SDL_IOStream *SDLCALL SDL_IOFromFile(const char *file, const char *mode);
  181. #define SDL_PROP_IOSTREAM_WINDOWS_HANDLE_POINTER "SDL.iostream.windows.handle"
  182. #define SDL_PROP_IOSTREAM_STDIO_FILE_POINTER "SDL.iostream.stdio.file"
  183. #define SDL_PROP_IOSTREAM_ANDROID_AASSET_POINTER "SDL.iostream.android.aasset"
  184. /**
  185. * Use this function to prepare a read-write memory buffer for use with
  186. * SDL_IOStream.
  187. *
  188. * This function sets up an SDL_IOStream struct based on a memory area of a
  189. * certain size, for both read and write access.
  190. *
  191. * This memory buffer is not copied by the SDL_IOStream; the pointer you
  192. * provide must remain valid until you close the stream. Closing the stream
  193. * will not free the original buffer.
  194. *
  195. * If you need to make sure the SDL_IOStream never writes to the memory
  196. * buffer, you should use SDL_IOFromConstMem() with a read-only buffer of
  197. * memory instead.
  198. *
  199. * \param mem a pointer to a buffer to feed an SDL_IOStream stream
  200. * \param size the buffer size, in bytes
  201. * \returns a pointer to a new SDL_IOStream structure, or NULL if it fails;
  202. * call SDL_GetError() for more information.
  203. *
  204. * \since This function is available since SDL 3.0.0.
  205. *
  206. * \sa SDL_IOFromConstMem
  207. * \sa SDL_CloseIO
  208. * \sa SDL_ReadIO
  209. * \sa SDL_SeekIO
  210. * \sa SDL_TellIO
  211. * \sa SDL_WriteIO
  212. */
  213. extern DECLSPEC SDL_IOStream *SDLCALL SDL_IOFromMem(void *mem, size_t size);
  214. /**
  215. * Use this function to prepare a read-only memory buffer for use with
  216. * SDL_IOStream.
  217. *
  218. * This function sets up an SDL_IOStream struct based on a memory area of a
  219. * certain size. It assumes the memory area is not writable.
  220. *
  221. * Attempting to write to this SDL_IOStream stream will report an error
  222. * without writing to the memory buffer.
  223. *
  224. * This memory buffer is not copied by the SDL_IOStream; the pointer you
  225. * provide must remain valid until you close the stream. Closing the stream
  226. * will not free the original buffer.
  227. *
  228. * If you need to write to a memory buffer, you should use SDL_IOFromMem()
  229. * with a writable buffer of memory instead.
  230. *
  231. * \param mem a pointer to a read-only buffer to feed an SDL_IOStream stream
  232. * \param size the buffer size, in bytes
  233. * \returns a pointer to a new SDL_IOStream structure, or NULL if it fails;
  234. * call SDL_GetError() for more information.
  235. *
  236. * \since This function is available since SDL 3.0.0.
  237. *
  238. * \sa SDL_IOFromMem
  239. * \sa SDL_CloseIO
  240. * \sa SDL_ReadIO
  241. * \sa SDL_SeekIO
  242. * \sa SDL_TellIO
  243. */
  244. extern DECLSPEC SDL_IOStream *SDLCALL SDL_IOFromConstMem(const void *mem, size_t size);
  245. /**
  246. * Use this function to create an SDL_IOStream that is backed by dynamically
  247. * allocated memory.
  248. *
  249. * This supports the following properties to provide access to the memory and
  250. * control over allocations: - `SDL_PROP_IOSTREAM_DYNAMIC_MEMORY_POINTER`: a
  251. * pointer to the internal memory of the stream. This can be set to NULL to
  252. * transfer ownership of the memory to the application, which should free the
  253. * memory with SDL_free(). If this is done, the next operation on the stream
  254. * must be SDL_CloseIO(). - `SDL_PROP_IOSTREAM_DYNAMIC_CHUNKSIZE_NUMBER`:
  255. * memory will be allocated in multiples of this size, defaulting to 1024.
  256. *
  257. * \returns a pointer to a new SDL_IOStream structure, or NULL if it fails;
  258. * call SDL_GetError() for more information.
  259. *
  260. * \since This function is available since SDL 3.0.0.
  261. *
  262. * \sa SDL_CloseIO
  263. * \sa SDL_ReadIO
  264. * \sa SDL_SeekIO
  265. * \sa SDL_TellIO
  266. * \sa SDL_WriteIO
  267. */
  268. extern DECLSPEC SDL_IOStream *SDLCALL SDL_IOFromDynamicMem(void);
  269. #define SDL_PROP_IOSTREAM_DYNAMIC_MEMORY_POINTER "SDL.iostream.dynamic.memory"
  270. #define SDL_PROP_IOSTREAM_DYNAMIC_CHUNKSIZE_NUMBER "SDL.iostream.dynamic.chunksize"
  271. /* @} *//* IOFrom functions */
  272. /**
  273. * Create a custom SDL_IOStream.
  274. *
  275. * Applications do not need to use this function unless they are providing
  276. * their own SDL_IOStream implementation. If you just need an SDL_IOStream to
  277. * read/write a common data source, you should use the built-in
  278. * implementations in SDL, like SDL_IOFromFile() or SDL_IOFromMem(), etc.
  279. *
  280. * You must free the returned pointer with SDL_CloseIO().
  281. *
  282. * \param iface The function pointers that implement this SDL_IOStream.
  283. * \param userdata The app-controlled pointer that is passed to iface's
  284. * functions when called.
  285. * \returns a pointer to the allocated memory on success, or NULL on failure;
  286. * call SDL_GetError() for more information.
  287. *
  288. * \since This function is available since SDL 3.0.0.
  289. *
  290. * \sa SDL_CloseIO
  291. * \sa SDL_IOFromConstMem
  292. * \sa SDL_IOFromFile
  293. * \sa SDL_IOFromMem
  294. */
  295. extern DECLSPEC SDL_IOStream *SDLCALL SDL_OpenIO(const SDL_IOStreamInterface *iface, void *userdata);
  296. /**
  297. * Close and free an allocated SDL_IOStream structure.
  298. *
  299. * SDL_CloseIO() closes and cleans up the SDL_IOStream stream. It releases any
  300. * resources used by the stream and frees the SDL_IOStream itself. This
  301. * returns 0 on success, or -1 if the stream failed to flush to its output
  302. * (e.g. to disk).
  303. *
  304. * Note that if this fails to flush the stream to disk, this function reports
  305. * an error, but the SDL_IOStream is still invalid once this function returns.
  306. *
  307. * \param context SDL_IOStream structure to close
  308. * \returns 0 on success or a negative error code on failure; call
  309. * SDL_GetError() for more information.
  310. *
  311. * \since This function is available since SDL 3.0.0.
  312. *
  313. * \sa SDL_OpenIO
  314. */
  315. extern DECLSPEC int SDLCALL SDL_CloseIO(SDL_IOStream *context);
  316. /**
  317. * Get the properties associated with an SDL_IOStream.
  318. *
  319. * \param context a pointer to an SDL_IOStream structure
  320. * \returns a valid property ID on success or 0 on failure; call
  321. * SDL_GetError() for more information.
  322. *
  323. * \since This function is available since SDL 3.0.0.
  324. *
  325. * \sa SDL_GetProperty
  326. * \sa SDL_SetProperty
  327. */
  328. extern DECLSPEC SDL_PropertiesID SDLCALL SDL_GetIOProperties(SDL_IOStream *context);
  329. #define SDL_IO_SEEK_SET 0 /**< Seek from the beginning of data */
  330. #define SDL_IO_SEEK_CUR 1 /**< Seek relative to current read point */
  331. #define SDL_IO_SEEK_END 2 /**< Seek relative to the end of data */
  332. /**
  333. * Query the stream status of an SDL_IOStream.
  334. *
  335. * This information can be useful to decide if a short read or write was due
  336. * to an error, an EOF, or a non-blocking operation that isn't yet ready to
  337. * complete.
  338. *
  339. * An SDL_IOStream's status is only expected to change after a SDL_ReadIO or
  340. * SDL_WriteIO call; don't expect it to change if you just call this query
  341. * function in a tight loop.
  342. *
  343. * \param context the SDL_IOStream to query.
  344. * \returns an SDL_IOStatus enum with the current state.
  345. *
  346. * \threadsafety This function should not be called at the same time that
  347. * another thread is operating on the same SDL_IOStream.
  348. *
  349. * \since This function is available since SDL 3.0.0.
  350. */
  351. extern DECLSPEC SDL_IOStatus SDLCALL SDL_GetIOStatus(SDL_IOStream *context);
  352. /**
  353. * Use this function to get the size of the data stream in an SDL_IOStream.
  354. *
  355. * \param context the SDL_IOStream to get the size of the data stream from
  356. * \returns the size of the data stream in the SDL_IOStream on success or a
  357. * negative error code on failure; call SDL_GetError() for more
  358. * information.
  359. *
  360. * \since This function is available since SDL 3.0.0.
  361. */
  362. extern DECLSPEC Sint64 SDLCALL SDL_GetIOSize(SDL_IOStream *context);
  363. /**
  364. * Seek within an SDL_IOStream data stream.
  365. *
  366. * This function seeks to byte `offset`, relative to `whence`.
  367. *
  368. * `whence` may be any of the following values:
  369. *
  370. * - `SDL_IO_SEEK_SET`: seek from the beginning of data
  371. * - `SDL_IO_SEEK_CUR`: seek relative to current read point
  372. * - `SDL_IO_SEEK_END`: seek relative to the end of data
  373. *
  374. * If this stream can not seek, it will return -1.
  375. *
  376. * \param context a pointer to an SDL_IOStream structure
  377. * \param offset an offset in bytes, relative to **whence** location; can be
  378. * negative
  379. * \param whence any of `SDL_IO_SEEK_SET`, `SDL_IO_SEEK_CUR`,
  380. * `SDL_IO_SEEK_END`
  381. * \returns the final offset in the data stream after the seek or a negative
  382. * error code on failure; call SDL_GetError() for more information.
  383. *
  384. * \since This function is available since SDL 3.0.0.
  385. *
  386. * \sa SDL_TellIO
  387. */
  388. extern DECLSPEC Sint64 SDLCALL SDL_SeekIO(SDL_IOStream *context, Sint64 offset, int whence);
  389. /**
  390. * Determine the current read/write offset in an SDL_IOStream data stream.
  391. *
  392. * SDL_TellIO is actually a wrapper function that calls the SDL_IOStream's
  393. * `seek` method, with an offset of 0 bytes from `SDL_IO_SEEK_CUR`, to
  394. * simplify application development.
  395. *
  396. * \param context an SDL_IOStream data stream object from which to get the
  397. * current offset
  398. * \returns the current offset in the stream, or -1 if the information can not
  399. * be determined.
  400. *
  401. * \since This function is available since SDL 3.0.0.
  402. *
  403. * \sa SDL_SeekIO
  404. */
  405. extern DECLSPEC Sint64 SDLCALL SDL_TellIO(SDL_IOStream *context);
  406. /**
  407. * Read from a data source.
  408. *
  409. * This function reads up `size` bytes from the data source to the area
  410. * pointed at by `ptr`. This function may read less bytes than requested. It
  411. * will return zero when the data stream is completely read, or -1 on error.
  412. * For streams that support non-blocking operation, if nothing was read
  413. * because it would require blocking, this function returns -2 to distinguish
  414. * that this is not an error or end-of-file, and the caller can try again
  415. * later.
  416. *
  417. * \param context a pointer to an SDL_IOStream structure
  418. * \param ptr a pointer to a buffer to read data into
  419. * \param size the number of bytes to read from the data source.
  420. * \returns the number of bytes read, or 0 on end of file or other error.
  421. *
  422. * \since This function is available since SDL 3.0.0.
  423. *
  424. * \sa SDL_SeekIO
  425. * \sa SDL_WriteIO
  426. */
  427. extern DECLSPEC size_t SDLCALL SDL_ReadIO(SDL_IOStream *context, void *ptr, size_t size);
  428. /**
  429. * Write to an SDL_IOStream data stream.
  430. *
  431. * This function writes exactly `size` bytes from the area pointed at by `ptr`
  432. * to the stream. If this fails for any reason, it'll return less than `size`
  433. * to demonstrate how far the write progressed. On success, it returns `num`.
  434. *
  435. * On error, this function still attempts to write as much as possible, so it
  436. * might return a positive value less than the requested write size. If the
  437. * function failed to write anything and there was an actual error, it will
  438. * return -1. For streams that support non-blocking operation, if nothing was
  439. * written because it would require blocking, this function returns -2 to
  440. * distinguish that this is not an error and the caller can try again later.
  441. *
  442. * It is an error to specify a negative `size`, but this parameter is signed
  443. * so you definitely cannot overflow the return value on a successful run with
  444. * enormous amounts of data.
  445. *
  446. * \param context a pointer to an SDL_IOStream structure
  447. * \param ptr a pointer to a buffer containing data to write
  448. * \param size the number of bytes to write
  449. * \returns the number of bytes written, which will be less than `num` on
  450. * error; call SDL_GetError() for more information.
  451. *
  452. * \since This function is available since SDL 3.0.0.
  453. *
  454. * \sa SDL_IOprintf
  455. * \sa SDL_ReadIO
  456. * \sa SDL_SeekIO
  457. */
  458. extern DECLSPEC size_t SDLCALL SDL_WriteIO(SDL_IOStream *context, const void *ptr, size_t size);
  459. /**
  460. * Print to an SDL_IOStream data stream.
  461. *
  462. * This function does formatted printing to the stream.
  463. *
  464. * \param context a pointer to an SDL_IOStream structure
  465. * \param fmt a printf() style format string
  466. * \param ... additional parameters matching % tokens in the `fmt` string, if
  467. * any
  468. * \returns the number of bytes written, or 0 on error; call SDL_GetError()
  469. * for more information.
  470. *
  471. * \since This function is available since SDL 3.0.0.
  472. *
  473. * \sa SDL_IOvprintf
  474. * \sa SDL_WriteIO
  475. */
  476. extern DECLSPEC size_t SDLCALL SDL_IOprintf(SDL_IOStream *context, SDL_PRINTF_FORMAT_STRING const char *fmt, ...) SDL_PRINTF_VARARG_FUNC(2);
  477. /**
  478. * Print to an SDL_IOStream data stream.
  479. *
  480. * This function does formatted printing to the stream.
  481. *
  482. * \param context a pointer to an SDL_IOStream structure
  483. * \param fmt a printf() style format string
  484. * \param ap a variable argument list
  485. * \returns the number of bytes written, or 0 on error; call SDL_GetError()
  486. * for more information.
  487. *
  488. * \since This function is available since SDL 3.0.0.
  489. *
  490. * \sa SDL_IOprintf
  491. * \sa SDL_WriteIO
  492. */
  493. extern DECLSPEC size_t SDLCALL SDL_IOvprintf(SDL_IOStream *context, SDL_PRINTF_FORMAT_STRING const char *fmt, va_list ap) SDL_PRINTF_VARARG_FUNCV(2);
  494. /**
  495. * Load all the data from an SDL data stream.
  496. *
  497. * The data is allocated with a zero byte at the end (null terminated) for
  498. * convenience. This extra byte is not included in the value reported via
  499. * `datasize`.
  500. *
  501. * The data should be freed with SDL_free().
  502. *
  503. * \param src the SDL_IOStream to read all available data from
  504. * \param datasize if not NULL, will store the number of bytes read
  505. * \param closeio if SDL_TRUE, calls SDL_CloseIO() on `src` before returning,
  506. * even in the case of an error
  507. * \returns the data, or NULL if there was an error.
  508. *
  509. * \since This function is available since SDL 3.0.0.
  510. *
  511. * \sa SDL_LoadFile
  512. */
  513. extern DECLSPEC void *SDLCALL SDL_LoadFile_IO(SDL_IOStream *src, size_t *datasize, SDL_bool closeio);
  514. /**
  515. * Load all the data from a file path.
  516. *
  517. * The data is allocated with a zero byte at the end (null terminated) for
  518. * convenience. This extra byte is not included in the value reported via
  519. * `datasize`.
  520. *
  521. * The data should be freed with SDL_free().
  522. *
  523. * \param file the path to read all available data from
  524. * \param datasize if not NULL, will store the number of bytes read
  525. * \returns the data, or NULL if there was an error.
  526. *
  527. * \since This function is available since SDL 3.0.0.
  528. *
  529. * \sa SDL_LoadFile_IO
  530. */
  531. extern DECLSPEC void *SDLCALL SDL_LoadFile(const char *file, size_t *datasize);
  532. /**
  533. * \name Read endian functions
  534. *
  535. * Read an item of the specified endianness and return in native format.
  536. */
  537. /* @{ */
  538. /**
  539. * Use this function to read a byte from an SDL_IOStream.
  540. *
  541. * \param src the SDL_IOStream to read from
  542. * \param value a pointer filled in with the data read
  543. * \returns SDL_TRUE on success or SDL_FALSE on failure; call SDL_GetError()
  544. * for more information.
  545. *
  546. * \since This function is available since SDL 3.0.0.
  547. */
  548. extern DECLSPEC SDL_bool SDLCALL SDL_ReadU8(SDL_IOStream *src, Uint8 *value);
  549. /**
  550. * Use this function to read 16 bits of little-endian data from an
  551. * SDL_IOStream and return in native format.
  552. *
  553. * SDL byteswaps the data only if necessary, so the data returned will be in
  554. * the native byte order.
  555. *
  556. * \param src the stream from which to read data
  557. * \param value a pointer filled in with the data read
  558. * \returns SDL_TRUE on successful write, SDL_FALSE on failure; call
  559. * SDL_GetError() for more information.
  560. *
  561. * \since This function is available since SDL 3.0.0.
  562. */
  563. extern DECLSPEC SDL_bool SDLCALL SDL_ReadU16LE(SDL_IOStream *src, Uint16 *value);
  564. /**
  565. * Use this function to read 16 bits of little-endian data from an
  566. * SDL_IOStream and return in native format.
  567. *
  568. * SDL byteswaps the data only if necessary, so the data returned will be in
  569. * the native byte order.
  570. *
  571. * \param src the stream from which to read data
  572. * \param value a pointer filled in with the data read
  573. * \returns SDL_TRUE on successful write, SDL_FALSE on failure; call
  574. * SDL_GetError() for more information.
  575. *
  576. * \since This function is available since SDL 3.0.0.
  577. */
  578. extern DECLSPEC SDL_bool SDLCALL SDL_ReadS16LE(SDL_IOStream *src, Sint16 *value);
  579. /**
  580. * Use this function to read 16 bits of big-endian data from an SDL_IOStream
  581. * and return in native format.
  582. *
  583. * SDL byteswaps the data only if necessary, so the data returned will be in
  584. * the native byte order.
  585. *
  586. * \param src the stream from which to read data
  587. * \param value a pointer filled in with the data read
  588. * \returns SDL_TRUE on successful write, SDL_FALSE on failure; call
  589. * SDL_GetError() for more information.
  590. *
  591. * \since This function is available since SDL 3.0.0.
  592. */
  593. extern DECLSPEC SDL_bool SDLCALL SDL_ReadU16BE(SDL_IOStream *src, Uint16 *value);
  594. /**
  595. * Use this function to read 16 bits of big-endian data from an SDL_IOStream
  596. * and return in native format.
  597. *
  598. * SDL byteswaps the data only if necessary, so the data returned will be in
  599. * the native byte order.
  600. *
  601. * \param src the stream from which to read data
  602. * \param value a pointer filled in with the data read
  603. * \returns SDL_TRUE on successful write, SDL_FALSE on failure; call
  604. * SDL_GetError() for more information.
  605. *
  606. * \since This function is available since SDL 3.0.0.
  607. */
  608. extern DECLSPEC SDL_bool SDLCALL SDL_ReadS16BE(SDL_IOStream *src, Sint16 *value);
  609. /**
  610. * Use this function to read 32 bits of little-endian data from an
  611. * SDL_IOStream and return in native format.
  612. *
  613. * SDL byteswaps the data only if necessary, so the data returned will be in
  614. * the native byte order.
  615. *
  616. * \param src the stream from which to read data
  617. * \param value a pointer filled in with the data read
  618. * \returns SDL_TRUE on successful write, SDL_FALSE on failure; call
  619. * SDL_GetError() for more information.
  620. *
  621. * \since This function is available since SDL 3.0.0.
  622. */
  623. extern DECLSPEC SDL_bool SDLCALL SDL_ReadU32LE(SDL_IOStream *src, Uint32 *value);
  624. /**
  625. * Use this function to read 32 bits of little-endian data from an
  626. * SDL_IOStream and return in native format.
  627. *
  628. * SDL byteswaps the data only if necessary, so the data returned will be in
  629. * the native byte order.
  630. *
  631. * \param src the stream from which to read data
  632. * \param value a pointer filled in with the data read
  633. * \returns SDL_TRUE on successful write, SDL_FALSE on failure; call
  634. * SDL_GetError() for more information.
  635. *
  636. * \since This function is available since SDL 3.0.0.
  637. */
  638. extern DECLSPEC SDL_bool SDLCALL SDL_ReadS32LE(SDL_IOStream *src, Sint32 *value);
  639. /**
  640. * Use this function to read 32 bits of big-endian data from an SDL_IOStream
  641. * and return in native format.
  642. *
  643. * SDL byteswaps the data only if necessary, so the data returned will be in
  644. * the native byte order.
  645. *
  646. * \param src the stream from which to read data
  647. * \param value a pointer filled in with the data read
  648. * \returns SDL_TRUE on successful write, SDL_FALSE on failure; call
  649. * SDL_GetError() for more information.
  650. *
  651. * \since This function is available since SDL 3.0.0.
  652. */
  653. extern DECLSPEC SDL_bool SDLCALL SDL_ReadU32BE(SDL_IOStream *src, Uint32 *value);
  654. /**
  655. * Use this function to read 32 bits of big-endian data from an SDL_IOStream
  656. * and return in native format.
  657. *
  658. * SDL byteswaps the data only if necessary, so the data returned will be in
  659. * the native byte order.
  660. *
  661. * \param src the stream from which to read data
  662. * \param value a pointer filled in with the data read
  663. * \returns SDL_TRUE on successful write, SDL_FALSE on failure; call
  664. * SDL_GetError() for more information.
  665. *
  666. * \since This function is available since SDL 3.0.0.
  667. */
  668. extern DECLSPEC SDL_bool SDLCALL SDL_ReadS32BE(SDL_IOStream *src, Sint32 *value);
  669. /**
  670. * Use this function to read 64 bits of little-endian data from an
  671. * SDL_IOStream and return in native format.
  672. *
  673. * SDL byteswaps the data only if necessary, so the data returned will be in
  674. * the native byte order.
  675. *
  676. * \param src the stream from which to read data
  677. * \param value a pointer filled in with the data read
  678. * \returns SDL_TRUE on successful write, SDL_FALSE on failure; call
  679. * SDL_GetError() for more information.
  680. *
  681. * \since This function is available since SDL 3.0.0.
  682. */
  683. extern DECLSPEC SDL_bool SDLCALL SDL_ReadU64LE(SDL_IOStream *src, Uint64 *value);
  684. /**
  685. * Use this function to read 64 bits of little-endian data from an
  686. * SDL_IOStream and return in native format.
  687. *
  688. * SDL byteswaps the data only if necessary, so the data returned will be in
  689. * the native byte order.
  690. *
  691. * \param src the stream from which to read data
  692. * \param value a pointer filled in with the data read
  693. * \returns SDL_TRUE on successful write, SDL_FALSE on failure; call
  694. * SDL_GetError() for more information.
  695. *
  696. * \since This function is available since SDL 3.0.0.
  697. */
  698. extern DECLSPEC SDL_bool SDLCALL SDL_ReadS64LE(SDL_IOStream *src, Sint64 *value);
  699. /**
  700. * Use this function to read 64 bits of big-endian data from an SDL_IOStream
  701. * and return in native format.
  702. *
  703. * SDL byteswaps the data only if necessary, so the data returned will be in
  704. * the native byte order.
  705. *
  706. * \param src the stream from which to read data
  707. * \param value a pointer filled in with the data read
  708. * \returns SDL_TRUE on successful write, SDL_FALSE on failure; call
  709. * SDL_GetError() for more information.
  710. *
  711. * \since This function is available since SDL 3.0.0.
  712. */
  713. extern DECLSPEC SDL_bool SDLCALL SDL_ReadU64BE(SDL_IOStream *src, Uint64 *value);
  714. /**
  715. * Use this function to read 64 bits of big-endian data from an SDL_IOStream
  716. * and return in native format.
  717. *
  718. * SDL byteswaps the data only if necessary, so the data returned will be in
  719. * the native byte order.
  720. *
  721. * \param src the stream from which to read data
  722. * \param value a pointer filled in with the data read
  723. * \returns SDL_TRUE on successful write, SDL_FALSE on failure; call
  724. * SDL_GetError() for more information.
  725. *
  726. * \since This function is available since SDL 3.0.0.
  727. */
  728. extern DECLSPEC SDL_bool SDLCALL SDL_ReadS64BE(SDL_IOStream *src, Sint64 *value);
  729. /* @} *//* Read endian functions */
  730. /**
  731. * \name Write endian functions
  732. *
  733. * Write an item of native format to the specified endianness.
  734. */
  735. /* @{ */
  736. /**
  737. * Use this function to write a byte to an SDL_IOStream.
  738. *
  739. * \param dst the SDL_IOStream to write to
  740. * \param value the byte value to write
  741. * \returns SDL_TRUE on successful write, SDL_FALSE on failure; call
  742. * SDL_GetError() for more information.
  743. *
  744. * \since This function is available since SDL 3.0.0.
  745. */
  746. extern DECLSPEC SDL_bool SDLCALL SDL_WriteU8(SDL_IOStream *dst, Uint8 value);
  747. /**
  748. * Use this function to write 16 bits in native format to an SDL_IOStream as
  749. * little-endian data.
  750. *
  751. * SDL byteswaps the data only if necessary, so the application always
  752. * specifies native format, and the data written will be in little-endian
  753. * format.
  754. *
  755. * \param dst the stream to which data will be written
  756. * \param value the data to be written, in native format
  757. * \returns SDL_TRUE on successful write, SDL_FALSE on failure; call
  758. * SDL_GetError() for more information.
  759. *
  760. * \since This function is available since SDL 3.0.0.
  761. */
  762. extern DECLSPEC SDL_bool SDLCALL SDL_WriteU16LE(SDL_IOStream *dst, Uint16 value);
  763. /**
  764. * Use this function to write 16 bits in native format to an SDL_IOStream as
  765. * little-endian data.
  766. *
  767. * SDL byteswaps the data only if necessary, so the application always
  768. * specifies native format, and the data written will be in little-endian
  769. * format.
  770. *
  771. * \param dst the stream to which data will be written
  772. * \param value the data to be written, in native format
  773. * \returns SDL_TRUE on successful write, SDL_FALSE on failure; call
  774. * SDL_GetError() for more information.
  775. *
  776. * \since This function is available since SDL 3.0.0.
  777. */
  778. extern DECLSPEC SDL_bool SDLCALL SDL_WriteS16LE(SDL_IOStream *dst, Sint16 value);
  779. /**
  780. * Use this function to write 16 bits in native format to an SDL_IOStream as
  781. * big-endian data.
  782. *
  783. * SDL byteswaps the data only if necessary, so the application always
  784. * specifies native format, and the data written will be in big-endian format.
  785. *
  786. * \param dst the stream to which data will be written
  787. * \param value the data to be written, in native format
  788. * \returns SDL_TRUE on successful write, SDL_FALSE on failure; call
  789. * SDL_GetError() for more information.
  790. *
  791. * \since This function is available since SDL 3.0.0.
  792. */
  793. extern DECLSPEC SDL_bool SDLCALL SDL_WriteU16BE(SDL_IOStream *dst, Uint16 value);
  794. /**
  795. * Use this function to write 16 bits in native format to an SDL_IOStream as
  796. * big-endian data.
  797. *
  798. * SDL byteswaps the data only if necessary, so the application always
  799. * specifies native format, and the data written will be in big-endian format.
  800. *
  801. * \param dst the stream to which data will be written
  802. * \param value the data to be written, in native format
  803. * \returns SDL_TRUE on successful write, SDL_FALSE on failure; call
  804. * SDL_GetError() for more information.
  805. *
  806. * \since This function is available since SDL 3.0.0.
  807. */
  808. extern DECLSPEC SDL_bool SDLCALL SDL_WriteS16BE(SDL_IOStream *dst, Sint16 value);
  809. /**
  810. * Use this function to write 32 bits in native format to an SDL_IOStream as
  811. * little-endian data.
  812. *
  813. * SDL byteswaps the data only if necessary, so the application always
  814. * specifies native format, and the data written will be in little-endian
  815. * format.
  816. *
  817. * \param dst the stream to which data will be written
  818. * \param value the data to be written, in native format
  819. * \returns SDL_TRUE on successful write, SDL_FALSE on failure; call
  820. * SDL_GetError() for more information.
  821. *
  822. * \since This function is available since SDL 3.0.0.
  823. */
  824. extern DECLSPEC SDL_bool SDLCALL SDL_WriteU32LE(SDL_IOStream *dst, Uint32 value);
  825. /**
  826. * Use this function to write 32 bits in native format to an SDL_IOStream as
  827. * little-endian data.
  828. *
  829. * SDL byteswaps the data only if necessary, so the application always
  830. * specifies native format, and the data written will be in little-endian
  831. * format.
  832. *
  833. * \param dst the stream to which data will be written
  834. * \param value the data to be written, in native format
  835. * \returns SDL_TRUE on successful write, SDL_FALSE on failure; call
  836. * SDL_GetError() for more information.
  837. *
  838. * \since This function is available since SDL 3.0.0.
  839. */
  840. extern DECLSPEC SDL_bool SDLCALL SDL_WriteS32LE(SDL_IOStream *dst, Sint32 value);
  841. /**
  842. * Use this function to write 32 bits in native format to an SDL_IOStream as
  843. * big-endian data.
  844. *
  845. * SDL byteswaps the data only if necessary, so the application always
  846. * specifies native format, and the data written will be in big-endian format.
  847. *
  848. * \param dst the stream to which data will be written
  849. * \param value the data to be written, in native format
  850. * \returns SDL_TRUE on successful write, SDL_FALSE on failure; call
  851. * SDL_GetError() for more information.
  852. *
  853. * \since This function is available since SDL 3.0.0.
  854. */
  855. extern DECLSPEC SDL_bool SDLCALL SDL_WriteU32BE(SDL_IOStream *dst, Uint32 value);
  856. /**
  857. * Use this function to write 32 bits in native format to an SDL_IOStream as
  858. * big-endian data.
  859. *
  860. * SDL byteswaps the data only if necessary, so the application always
  861. * specifies native format, and the data written will be in big-endian format.
  862. *
  863. * \param dst the stream to which data will be written
  864. * \param value the data to be written, in native format
  865. * \returns SDL_TRUE on successful write, SDL_FALSE on failure; call
  866. * SDL_GetError() for more information.
  867. *
  868. * \since This function is available since SDL 3.0.0.
  869. */
  870. extern DECLSPEC SDL_bool SDLCALL SDL_WriteS32BE(SDL_IOStream *dst, Sint32 value);
  871. /**
  872. * Use this function to write 64 bits in native format to an SDL_IOStream as
  873. * little-endian data.
  874. *
  875. * SDL byteswaps the data only if necessary, so the application always
  876. * specifies native format, and the data written will be in little-endian
  877. * format.
  878. *
  879. * \param dst the stream to which data will be written
  880. * \param value the data to be written, in native format
  881. * \returns SDL_TRUE on successful write, SDL_FALSE on failure; call
  882. * SDL_GetError() for more information.
  883. *
  884. * \since This function is available since SDL 3.0.0.
  885. */
  886. extern DECLSPEC SDL_bool SDLCALL SDL_WriteU64LE(SDL_IOStream *dst, Uint64 value);
  887. /**
  888. * Use this function to write 64 bits in native format to an SDL_IOStream as
  889. * little-endian data.
  890. *
  891. * SDL byteswaps the data only if necessary, so the application always
  892. * specifies native format, and the data written will be in little-endian
  893. * format.
  894. *
  895. * \param dst the stream to which data will be written
  896. * \param value the data to be written, in native format
  897. * \returns SDL_TRUE on successful write, SDL_FALSE on failure; call
  898. * SDL_GetError() for more information.
  899. *
  900. * \since This function is available since SDL 3.0.0.
  901. */
  902. extern DECLSPEC SDL_bool SDLCALL SDL_WriteS64LE(SDL_IOStream *dst, Sint64 value);
  903. /**
  904. * Use this function to write 64 bits in native format to an SDL_IOStream as
  905. * big-endian data.
  906. *
  907. * SDL byteswaps the data only if necessary, so the application always
  908. * specifies native format, and the data written will be in big-endian format.
  909. *
  910. * \param dst the stream to which data will be written
  911. * \param value the data to be written, in native format
  912. * \returns SDL_TRUE on successful write, SDL_FALSE on failure; call
  913. * SDL_GetError() for more information.
  914. *
  915. * \since This function is available since SDL 3.0.0.
  916. */
  917. extern DECLSPEC SDL_bool SDLCALL SDL_WriteU64BE(SDL_IOStream *dst, Uint64 value);
  918. /**
  919. * Use this function to write 64 bits in native format to an SDL_IOStream as
  920. * big-endian data.
  921. *
  922. * SDL byteswaps the data only if necessary, so the application always
  923. * specifies native format, and the data written will be in big-endian format.
  924. *
  925. * \param dst the stream to which data will be written
  926. * \param value the data to be written, in native format
  927. * \returns SDL_TRUE on successful write, SDL_FALSE on failure; call
  928. * SDL_GetError() for more information.
  929. *
  930. * \since This function is available since SDL 3.0.0.
  931. */
  932. extern DECLSPEC SDL_bool SDLCALL SDL_WriteS64BE(SDL_IOStream *dst, Sint64 value);
  933. /* @} *//* Write endian functions */
  934. /* Ends C function definitions when using C++ */
  935. #ifdef __cplusplus
  936. }
  937. #endif
  938. #include <SDL3/SDL_close_code.h>
  939. #endif /* SDL_iostream_h_ */