SDL_iostream.h 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024
  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. /**
  48. * The function pointers that drive an SDL_IOStream.
  49. *
  50. * Applications can provide this struct to SDL_OpenIO() to create their own
  51. * implementation of SDL_IOStream. This is not necessarily required, as SDL
  52. * already offers several common types of I/O streams, via functions like
  53. * SDL_IOFromFile() and SDL_IOFromMem().
  54. *
  55. * \since This struct is available since SDL 3.0.0.
  56. */
  57. typedef struct SDL_IOStreamInterface
  58. {
  59. /**
  60. * Return the number of bytes in this SDL_IOStream
  61. *
  62. * \return the total size of the data stream, or -1 on error.
  63. */
  64. Sint64 (SDLCALL *size)(void *userdata);
  65. /**
  66. * Seek to `offset` relative to `whence`, one of stdio's whence values:
  67. * SDL_IO_SEEK_SET, SDL_IO_SEEK_CUR, SDL_IO_SEEK_END
  68. *
  69. * \return the final offset in the data stream, or -1 on error.
  70. */
  71. Sint64 (SDLCALL *seek)(void *userdata, Sint64 offset, int whence);
  72. /**
  73. * Read up to `size` bytes from the data stream to the area pointed
  74. * at by `ptr`.
  75. *
  76. * On an incomplete read, you should set `*status` to a value from the
  77. * SDL_IOStatus enum. You do not have to explicitly set this on
  78. * a complete, successful read.
  79. *
  80. * \return the number of bytes read
  81. */
  82. size_t (SDLCALL *read)(void *userdata, void *ptr, size_t size, SDL_IOStatus *status);
  83. /**
  84. * Write exactly `size` bytes from the area pointed at by `ptr`
  85. * to data stream.
  86. *
  87. * On an incomplete write, you should set `*status` to a value from the
  88. * SDL_IOStatus enum. You do not have to explicitly set this on
  89. * a complete, successful write.
  90. *
  91. * \return the number of bytes written
  92. */
  93. size_t (SDLCALL *write)(void *userdata, const void *ptr, size_t size, SDL_IOStatus *status);
  94. /**
  95. * Close and free any allocated resources.
  96. *
  97. * The SDL_IOStream is still destroyed even if this fails, so clean up anything
  98. * even if flushing to disk returns an error.
  99. *
  100. * \return 0 if successful or -1 on write error when flushing data.
  101. */
  102. int (SDLCALL *close)(void *userdata);
  103. } SDL_IOStreamInterface;
  104. /**
  105. * The read/write operation structure.
  106. *
  107. * This operates as an opaque handle. There are several APIs to create various
  108. * types of I/O streams, or an app can supply an SDL_IOStreamInterface to
  109. * SDL_OpenIO() to provide their own stream implementation behind this
  110. * struct's abstract interface.
  111. *
  112. * \since This struct is available since SDL 3.0.0.
  113. */
  114. typedef struct SDL_IOStream SDL_IOStream;
  115. /**
  116. * \name IOFrom functions
  117. *
  118. * Functions to create SDL_IOStream structures from various data streams.
  119. */
  120. /* @{ */
  121. /**
  122. * Use this function to create a new SDL_IOStream structure for reading from
  123. * and/or writing to a named file.
  124. *
  125. * The `mode` string is treated roughly the same as in a call to the C
  126. * library's fopen(), even if SDL doesn't happen to use fopen() behind the
  127. * scenes.
  128. *
  129. * Available `mode` strings:
  130. *
  131. * - "r": Open a file for reading. The file must exist.
  132. * - "w": Create an empty file for writing. If a file with the same name
  133. * already exists its content is erased and the file is treated as a new
  134. * empty file.
  135. * - "a": Append to a file. Writing operations append data at the end of the
  136. * file. The file is created if it does not exist.
  137. * - "r+": Open a file for update both reading and writing. The file must
  138. * exist.
  139. * - "w+": Create an empty file for both reading and writing. If a file with
  140. * the same name already exists its content is erased and the file is
  141. * treated as a new empty file.
  142. * - "a+": Open a file for reading and appending. All writing operations are
  143. * performed at the end of the file, protecting the previous content to be
  144. * overwritten. You can reposition (fseek, rewind) the internal pointer to
  145. * anywhere in the file for reading, but writing operations will move it
  146. * back to the end of file. The file is created if it does not exist.
  147. *
  148. * **NOTE**: In order to open a file as a binary file, a "b" character has to
  149. * be included in the `mode` string. This additional "b" character can either
  150. * be appended at the end of the string (thus making the following compound
  151. * modes: "rb", "wb", "ab", "r+b", "w+b", "a+b") or be inserted between the
  152. * letter and the "+" sign for the mixed modes ("rb+", "wb+", "ab+").
  153. * Additional characters may follow the sequence, although they should have no
  154. * effect. For example, "t" is sometimes appended to make explicit the file is
  155. * a text file.
  156. *
  157. * This function supports Unicode filenames, but they must be encoded in UTF-8
  158. * format, regardless of the underlying operating system.
  159. *
  160. * As a fallback, SDL_IOFromFile() will transparently open a matching filename
  161. * in an Android app's `assets`.
  162. *
  163. * Closing the SDL_IOStream will close SDL's internal file handle.
  164. *
  165. * The following properties may be set at creation time by SDL:
  166. *
  167. * - `SDL_PROP_IOSTREAM_WINDOWS_HANDLE_POINTER`: a pointer, that can be cast
  168. * to a win32 `HANDLE`, that this SDL_IOStream is using to access the
  169. * filesystem. If the program isn't running on Windows, or SDL used some
  170. * other method to access the filesystem, this property will not be set.
  171. * - `SDL_PROP_IOSTREAM_STDIO_FILE_POINTER`: a pointer, that can be cast to a
  172. * stdio `FILE *`, that this SDL_IOStream is using to access the filesystem.
  173. * If SDL used some other method to access the filesystem, this property
  174. * will not be set. PLEASE NOTE that if SDL is using a different C runtime
  175. * than your app, trying to use this pointer will almost certainly result in
  176. * a crash! This is mostly a problem on Windows; make sure you build SDL and
  177. * your app with the same compiler and settings to avoid it.
  178. * - `SDL_PROP_IOSTREAM_ANDROID_AASSET_POINTER`: a pointer, that can be cast
  179. * to an Android NDK `AAsset *`, that this SDL_IOStream is using to access
  180. * the filesystem. If SDL used some other method to access the filesystem,
  181. * this property will not be set.
  182. *
  183. * \param file a UTF-8 string representing the filename to open
  184. * \param mode an ASCII string representing the mode to be used for opening
  185. * the file.
  186. * \returns a pointer to the SDL_IOStream structure that is created, or NULL
  187. * on failure; call SDL_GetError() for more information.
  188. *
  189. * \since This function is available since SDL 3.0.0.
  190. *
  191. * \sa SDL_CloseIO
  192. * \sa SDL_ReadIO
  193. * \sa SDL_SeekIO
  194. * \sa SDL_TellIO
  195. * \sa SDL_WriteIO
  196. */
  197. extern DECLSPEC SDL_IOStream *SDLCALL SDL_IOFromFile(const char *file, const char *mode);
  198. #define SDL_PROP_IOSTREAM_WINDOWS_HANDLE_POINTER "SDL.iostream.windows.handle"
  199. #define SDL_PROP_IOSTREAM_STDIO_FILE_POINTER "SDL.iostream.stdio.file"
  200. #define SDL_PROP_IOSTREAM_ANDROID_AASSET_POINTER "SDL.iostream.android.aasset"
  201. /**
  202. * Use this function to prepare a read-write memory buffer for use with
  203. * SDL_IOStream.
  204. *
  205. * This function sets up an SDL_IOStream struct based on a memory area of a
  206. * certain size, for both read and write access.
  207. *
  208. * This memory buffer is not copied by the SDL_IOStream; the pointer you
  209. * provide must remain valid until you close the stream. Closing the stream
  210. * will not free the original buffer.
  211. *
  212. * If you need to make sure the SDL_IOStream never writes to the memory
  213. * buffer, you should use SDL_IOFromConstMem() with a read-only buffer of
  214. * memory instead.
  215. *
  216. * \param mem a pointer to a buffer to feed an SDL_IOStream stream
  217. * \param size the buffer size, in bytes
  218. * \returns a pointer to a new SDL_IOStream structure, or NULL if it fails;
  219. * call SDL_GetError() for more information.
  220. *
  221. * \since This function is available since SDL 3.0.0.
  222. *
  223. * \sa SDL_IOFromConstMem
  224. * \sa SDL_CloseIO
  225. * \sa SDL_ReadIO
  226. * \sa SDL_SeekIO
  227. * \sa SDL_TellIO
  228. * \sa SDL_WriteIO
  229. */
  230. extern DECLSPEC SDL_IOStream *SDLCALL SDL_IOFromMem(void *mem, size_t size);
  231. /**
  232. * Use this function to prepare a read-only memory buffer for use with
  233. * SDL_IOStream.
  234. *
  235. * This function sets up an SDL_IOStream struct based on a memory area of a
  236. * certain size. It assumes the memory area is not writable.
  237. *
  238. * Attempting to write to this SDL_IOStream stream will report an error
  239. * without writing to the memory buffer.
  240. *
  241. * This memory buffer is not copied by the SDL_IOStream; the pointer you
  242. * provide must remain valid until you close the stream. Closing the stream
  243. * will not free the original buffer.
  244. *
  245. * If you need to write to a memory buffer, you should use SDL_IOFromMem()
  246. * with a writable buffer of memory instead.
  247. *
  248. * \param mem a pointer to a read-only buffer to feed an SDL_IOStream stream
  249. * \param size the buffer size, in bytes
  250. * \returns a pointer to a new SDL_IOStream structure, or NULL if it fails;
  251. * call SDL_GetError() for more information.
  252. *
  253. * \since This function is available since SDL 3.0.0.
  254. *
  255. * \sa SDL_IOFromMem
  256. * \sa SDL_CloseIO
  257. * \sa SDL_ReadIO
  258. * \sa SDL_SeekIO
  259. * \sa SDL_TellIO
  260. */
  261. extern DECLSPEC SDL_IOStream *SDLCALL SDL_IOFromConstMem(const void *mem, size_t size);
  262. /**
  263. * Use this function to create an SDL_IOStream that is backed by dynamically
  264. * allocated memory.
  265. *
  266. * This supports the following properties to provide access to the memory and
  267. * control over allocations: - `SDL_PROP_IOSTREAM_DYNAMIC_MEMORY_POINTER`: a
  268. * pointer to the internal memory of the stream. This can be set to NULL to
  269. * transfer ownership of the memory to the application, which should free the
  270. * memory with SDL_free(). If this is done, the next operation on the stream
  271. * must be SDL_CloseIO(). - `SDL_PROP_IOSTREAM_DYNAMIC_CHUNKSIZE_NUMBER`:
  272. * memory will be allocated in multiples of this size, defaulting to 1024.
  273. *
  274. * \returns a pointer to a new SDL_IOStream structure, or NULL if it fails;
  275. * call SDL_GetError() for more information.
  276. *
  277. * \since This function is available since SDL 3.0.0.
  278. *
  279. * \sa SDL_CloseIO
  280. * \sa SDL_ReadIO
  281. * \sa SDL_SeekIO
  282. * \sa SDL_TellIO
  283. * \sa SDL_WriteIO
  284. */
  285. extern DECLSPEC SDL_IOStream *SDLCALL SDL_IOFromDynamicMem(void);
  286. #define SDL_PROP_IOSTREAM_DYNAMIC_MEMORY_POINTER "SDL.iostream.dynamic.memory"
  287. #define SDL_PROP_IOSTREAM_DYNAMIC_CHUNKSIZE_NUMBER "SDL.iostream.dynamic.chunksize"
  288. /* @} *//* IOFrom functions */
  289. /**
  290. * Create a custom SDL_IOStream.
  291. *
  292. * Applications do not need to use this function unless they are providing
  293. * their own SDL_IOStream implementation. If you just need an SDL_IOStream to
  294. * read/write a common data source, you should use the built-in
  295. * implementations in SDL, like SDL_IOFromFile() or SDL_IOFromMem(), etc.
  296. *
  297. * You must free the returned pointer with SDL_CloseIO().
  298. *
  299. * This function makes a copy of `iface` and the caller does not need to keep
  300. * this data around after this call.
  301. *
  302. * \param iface The function pointers that implement this SDL_IOStream.
  303. * \param userdata The app-controlled pointer that is passed to iface's
  304. * functions when called.
  305. * \returns a pointer to the allocated memory on success, or NULL on failure;
  306. * call SDL_GetError() for more information.
  307. *
  308. * \since This function is available since SDL 3.0.0.
  309. *
  310. * \sa SDL_CloseIO
  311. * \sa SDL_IOFromConstMem
  312. * \sa SDL_IOFromFile
  313. * \sa SDL_IOFromMem
  314. */
  315. extern DECLSPEC SDL_IOStream *SDLCALL SDL_OpenIO(const SDL_IOStreamInterface *iface, void *userdata);
  316. /**
  317. * Close and free an allocated SDL_IOStream structure.
  318. *
  319. * SDL_CloseIO() closes and cleans up the SDL_IOStream stream. It releases any
  320. * resources used by the stream and frees the SDL_IOStream itself. This
  321. * returns 0 on success, or -1 if the stream failed to flush to its output
  322. * (e.g. to disk).
  323. *
  324. * Note that if this fails to flush the stream to disk, this function reports
  325. * an error, but the SDL_IOStream is still invalid once this function returns.
  326. *
  327. * \param context SDL_IOStream structure to close
  328. * \returns 0 on success or a negative error code on failure; call
  329. * SDL_GetError() for more information.
  330. *
  331. * \since This function is available since SDL 3.0.0.
  332. *
  333. * \sa SDL_OpenIO
  334. */
  335. extern DECLSPEC int SDLCALL SDL_CloseIO(SDL_IOStream *context);
  336. /**
  337. * Get the properties associated with an SDL_IOStream.
  338. *
  339. * \param context a pointer to an SDL_IOStream structure
  340. * \returns a valid property ID on success or 0 on failure; call
  341. * SDL_GetError() for more information.
  342. *
  343. * \since This function is available since SDL 3.0.0.
  344. *
  345. * \sa SDL_GetProperty
  346. * \sa SDL_SetProperty
  347. */
  348. extern DECLSPEC SDL_PropertiesID SDLCALL SDL_GetIOProperties(SDL_IOStream *context);
  349. #define SDL_IO_SEEK_SET 0 /**< Seek from the beginning of data */
  350. #define SDL_IO_SEEK_CUR 1 /**< Seek relative to current read point */
  351. #define SDL_IO_SEEK_END 2 /**< Seek relative to the end of data */
  352. /**
  353. * Query the stream status of an SDL_IOStream.
  354. *
  355. * This information can be useful to decide if a short read or write was due
  356. * to an error, an EOF, or a non-blocking operation that isn't yet ready to
  357. * complete.
  358. *
  359. * An SDL_IOStream's status is only expected to change after a SDL_ReadIO or
  360. * SDL_WriteIO call; don't expect it to change if you just call this query
  361. * function in a tight loop.
  362. *
  363. * \param context the SDL_IOStream to query.
  364. * \returns an SDL_IOStatus enum with the current state.
  365. *
  366. * \threadsafety This function should not be called at the same time that
  367. * another thread is operating on the same SDL_IOStream.
  368. *
  369. * \since This function is available since SDL 3.0.0.
  370. */
  371. extern DECLSPEC SDL_IOStatus SDLCALL SDL_GetIOStatus(SDL_IOStream *context);
  372. /**
  373. * Use this function to get the size of the data stream in an SDL_IOStream.
  374. *
  375. * \param context the SDL_IOStream to get the size of the data stream from
  376. * \returns the size of the data stream in the SDL_IOStream on success or a
  377. * negative error code on failure; call SDL_GetError() for more
  378. * information.
  379. *
  380. * \since This function is available since SDL 3.0.0.
  381. */
  382. extern DECLSPEC Sint64 SDLCALL SDL_GetIOSize(SDL_IOStream *context);
  383. /**
  384. * Seek within an SDL_IOStream data stream.
  385. *
  386. * This function seeks to byte `offset`, relative to `whence`.
  387. *
  388. * `whence` may be any of the following values:
  389. *
  390. * - `SDL_IO_SEEK_SET`: seek from the beginning of data
  391. * - `SDL_IO_SEEK_CUR`: seek relative to current read point
  392. * - `SDL_IO_SEEK_END`: seek relative to the end of data
  393. *
  394. * If this stream can not seek, it will return -1.
  395. *
  396. * \param context a pointer to an SDL_IOStream structure
  397. * \param offset an offset in bytes, relative to **whence** location; can be
  398. * negative
  399. * \param whence any of `SDL_IO_SEEK_SET`, `SDL_IO_SEEK_CUR`,
  400. * `SDL_IO_SEEK_END`
  401. * \returns the final offset in the data stream after the seek or a negative
  402. * error code on failure; call SDL_GetError() for more information.
  403. *
  404. * \since This function is available since SDL 3.0.0.
  405. *
  406. * \sa SDL_TellIO
  407. */
  408. extern DECLSPEC Sint64 SDLCALL SDL_SeekIO(SDL_IOStream *context, Sint64 offset, int whence);
  409. /**
  410. * Determine the current read/write offset in an SDL_IOStream data stream.
  411. *
  412. * SDL_TellIO is actually a wrapper function that calls the SDL_IOStream's
  413. * `seek` method, with an offset of 0 bytes from `SDL_IO_SEEK_CUR`, to
  414. * simplify application development.
  415. *
  416. * \param context an SDL_IOStream data stream object from which to get the
  417. * current offset
  418. * \returns the current offset in the stream, or -1 if the information can not
  419. * be determined.
  420. *
  421. * \since This function is available since SDL 3.0.0.
  422. *
  423. * \sa SDL_SeekIO
  424. */
  425. extern DECLSPEC Sint64 SDLCALL SDL_TellIO(SDL_IOStream *context);
  426. /**
  427. * Read from a data source.
  428. *
  429. * This function reads up `size` bytes from the data source to the area
  430. * pointed at by `ptr`. This function may read less bytes than requested. It
  431. * will return zero when the data stream is completely read, or on error. To
  432. * determine if there was an error or all data was read, call
  433. * SDL_GetIOStatus().
  434. *
  435. * \param context a pointer to an SDL_IOStream structure
  436. * \param ptr a pointer to a buffer to read data into
  437. * \param size the number of bytes to read from the data source.
  438. * \returns the number of bytes read, or 0 on end of file or other error.
  439. *
  440. * \since This function is available since SDL 3.0.0.
  441. *
  442. * \sa SDL_WriteIO
  443. * \sa SDL_GetIOStatus
  444. */
  445. extern DECLSPEC size_t SDLCALL SDL_ReadIO(SDL_IOStream *context, void *ptr, size_t size);
  446. /**
  447. * Write to an SDL_IOStream data stream.
  448. *
  449. * This function writes exactly `size` bytes from the area pointed at by `ptr`
  450. * to the stream. If this fails for any reason, it'll return less than `size`
  451. * to demonstrate how far the write progressed. On success, it returns `num`.
  452. *
  453. * On error, this function still attempts to write as much as possible, so it
  454. * might return a positive value less than the requested write size.
  455. *
  456. * The caller can use SDL_GetIOStatus() to determine if the problem is
  457. * recoverable, such as a non-blocking write that can simply be retried later,
  458. * or a fatal error.
  459. *
  460. * \param context a pointer to an SDL_IOStream structure
  461. * \param ptr a pointer to a buffer containing data to write
  462. * \param size the number of bytes to write
  463. * \returns the number of bytes written, which will be less than `num` on
  464. * error; call SDL_GetError() for more information.
  465. *
  466. * \since This function is available since SDL 3.0.0.
  467. *
  468. * \sa SDL_IOprintf
  469. * \sa SDL_ReadIO
  470. * \sa SDL_SeekIO
  471. * \sa SDL_GetIOStatus
  472. */
  473. extern DECLSPEC size_t SDLCALL SDL_WriteIO(SDL_IOStream *context, const void *ptr, size_t size);
  474. /**
  475. * Print to an SDL_IOStream data stream.
  476. *
  477. * This function does formatted printing to the stream.
  478. *
  479. * \param context a pointer to an SDL_IOStream structure
  480. * \param fmt a printf() style format string
  481. * \param ... additional parameters matching % tokens in the `fmt` string, if
  482. * any
  483. * \returns the number of bytes written, or 0 on error; call SDL_GetError()
  484. * for more information.
  485. *
  486. * \since This function is available since SDL 3.0.0.
  487. *
  488. * \sa SDL_IOvprintf
  489. * \sa SDL_WriteIO
  490. */
  491. extern DECLSPEC size_t SDLCALL SDL_IOprintf(SDL_IOStream *context, SDL_PRINTF_FORMAT_STRING const char *fmt, ...) SDL_PRINTF_VARARG_FUNC(2);
  492. /**
  493. * Print to an SDL_IOStream data stream.
  494. *
  495. * This function does formatted printing to the stream.
  496. *
  497. * \param context a pointer to an SDL_IOStream structure
  498. * \param fmt a printf() style format string
  499. * \param ap a variable argument list
  500. * \returns the number of bytes written, or 0 on error; call SDL_GetError()
  501. * for more information.
  502. *
  503. * \since This function is available since SDL 3.0.0.
  504. *
  505. * \sa SDL_IOprintf
  506. * \sa SDL_WriteIO
  507. */
  508. 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);
  509. /**
  510. * Load all the data from an SDL data stream.
  511. *
  512. * The data is allocated with a zero byte at the end (null terminated) for
  513. * convenience. This extra byte is not included in the value reported via
  514. * `datasize`.
  515. *
  516. * The data should be freed with SDL_free().
  517. *
  518. * \param src the SDL_IOStream to read all available data from
  519. * \param datasize if not NULL, will store the number of bytes read
  520. * \param closeio if SDL_TRUE, calls SDL_CloseIO() on `src` before returning,
  521. * even in the case of an error
  522. * \returns the data, or NULL if there was an error.
  523. *
  524. * \since This function is available since SDL 3.0.0.
  525. *
  526. * \sa SDL_LoadFile
  527. */
  528. extern DECLSPEC void *SDLCALL SDL_LoadFile_IO(SDL_IOStream *src, size_t *datasize, SDL_bool closeio);
  529. /**
  530. * Load all the data from a file path.
  531. *
  532. * The data is allocated with a zero byte at the end (null terminated) for
  533. * convenience. This extra byte is not included in the value reported via
  534. * `datasize`.
  535. *
  536. * The data should be freed with SDL_free().
  537. *
  538. * \param file the path to read all available data from
  539. * \param datasize if not NULL, will store the number of bytes read
  540. * \returns the data, or NULL if there was an error.
  541. *
  542. * \since This function is available since SDL 3.0.0.
  543. *
  544. * \sa SDL_LoadFile_IO
  545. */
  546. extern DECLSPEC void *SDLCALL SDL_LoadFile(const char *file, size_t *datasize);
  547. /**
  548. * \name Read endian functions
  549. *
  550. * Read an item of the specified endianness and return in native format.
  551. */
  552. /* @{ */
  553. /**
  554. * Use this function to read a byte from an SDL_IOStream.
  555. *
  556. * \param src the SDL_IOStream to read from
  557. * \param value a pointer filled in with the data read
  558. * \returns SDL_TRUE on success or SDL_FALSE on failure; call SDL_GetError()
  559. * for more information.
  560. *
  561. * \since This function is available since SDL 3.0.0.
  562. */
  563. extern DECLSPEC SDL_bool SDLCALL SDL_ReadU8(SDL_IOStream *src, Uint8 *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_ReadU16LE(SDL_IOStream *src, Uint16 *value);
  579. /**
  580. * Use this function to read 16 bits of little-endian data from an
  581. * SDL_IOStream 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_ReadS16LE(SDL_IOStream *src, Sint16 *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_ReadU16BE(SDL_IOStream *src, Uint16 *value);
  609. /**
  610. * Use this function to read 16 bits of big-endian data from an SDL_IOStream
  611. * 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_ReadS16BE(SDL_IOStream *src, Sint16 *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_ReadU32LE(SDL_IOStream *src, Uint32 *value);
  639. /**
  640. * Use this function to read 32 bits of little-endian data from an
  641. * SDL_IOStream 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_ReadS32LE(SDL_IOStream *src, Sint32 *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_ReadU32BE(SDL_IOStream *src, Uint32 *value);
  669. /**
  670. * Use this function to read 32 bits of big-endian data from an SDL_IOStream
  671. * 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_ReadS32BE(SDL_IOStream *src, Sint32 *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_ReadU64LE(SDL_IOStream *src, Uint64 *value);
  699. /**
  700. * Use this function to read 64 bits of little-endian data from an
  701. * SDL_IOStream 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_ReadS64LE(SDL_IOStream *src, Sint64 *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_ReadU64BE(SDL_IOStream *src, Uint64 *value);
  729. /**
  730. * Use this function to read 64 bits of big-endian data from an SDL_IOStream
  731. * and return in native format.
  732. *
  733. * SDL byteswaps the data only if necessary, so the data returned will be in
  734. * the native byte order.
  735. *
  736. * \param src the stream from which to read data
  737. * \param value a pointer filled in with the data read
  738. * \returns SDL_TRUE on successful write, SDL_FALSE on failure; call
  739. * SDL_GetError() for more information.
  740. *
  741. * \since This function is available since SDL 3.0.0.
  742. */
  743. extern DECLSPEC SDL_bool SDLCALL SDL_ReadS64BE(SDL_IOStream *src, Sint64 *value);
  744. /* @} *//* Read endian functions */
  745. /**
  746. * \name Write endian functions
  747. *
  748. * Write an item of native format to the specified endianness.
  749. */
  750. /* @{ */
  751. /**
  752. * Use this function to write a byte to an SDL_IOStream.
  753. *
  754. * \param dst the SDL_IOStream to write to
  755. * \param value the byte value to write
  756. * \returns SDL_TRUE on successful write, SDL_FALSE on failure; call
  757. * SDL_GetError() for more information.
  758. *
  759. * \since This function is available since SDL 3.0.0.
  760. */
  761. extern DECLSPEC SDL_bool SDLCALL SDL_WriteU8(SDL_IOStream *dst, Uint8 value);
  762. /**
  763. * Use this function to write 16 bits in native format to an SDL_IOStream as
  764. * little-endian data.
  765. *
  766. * SDL byteswaps the data only if necessary, so the application always
  767. * specifies native format, and the data written will be in little-endian
  768. * format.
  769. *
  770. * \param dst the stream to which data will be written
  771. * \param value the data to be written, in native format
  772. * \returns SDL_TRUE on successful write, SDL_FALSE on failure; call
  773. * SDL_GetError() for more information.
  774. *
  775. * \since This function is available since SDL 3.0.0.
  776. */
  777. extern DECLSPEC SDL_bool SDLCALL SDL_WriteU16LE(SDL_IOStream *dst, Uint16 value);
  778. /**
  779. * Use this function to write 16 bits in native format to an SDL_IOStream as
  780. * little-endian data.
  781. *
  782. * SDL byteswaps the data only if necessary, so the application always
  783. * specifies native format, and the data written will be in little-endian
  784. * 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_WriteS16LE(SDL_IOStream *dst, Sint16 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_WriteU16BE(SDL_IOStream *dst, Uint16 value);
  809. /**
  810. * Use this function to write 16 bits in native format to an SDL_IOStream as
  811. * big-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 big-endian format.
  815. *
  816. * \param dst the stream to which data will be written
  817. * \param value the data to be written, in native format
  818. * \returns SDL_TRUE on successful write, SDL_FALSE on failure; call
  819. * SDL_GetError() for more information.
  820. *
  821. * \since This function is available since SDL 3.0.0.
  822. */
  823. extern DECLSPEC SDL_bool SDLCALL SDL_WriteS16BE(SDL_IOStream *dst, Sint16 value);
  824. /**
  825. * Use this function to write 32 bits in native format to an SDL_IOStream as
  826. * little-endian data.
  827. *
  828. * SDL byteswaps the data only if necessary, so the application always
  829. * specifies native format, and the data written will be in little-endian
  830. * format.
  831. *
  832. * \param dst the stream to which data will be written
  833. * \param value the data to be written, in native format
  834. * \returns SDL_TRUE on successful write, SDL_FALSE on failure; call
  835. * SDL_GetError() for more information.
  836. *
  837. * \since This function is available since SDL 3.0.0.
  838. */
  839. extern DECLSPEC SDL_bool SDLCALL SDL_WriteU32LE(SDL_IOStream *dst, Uint32 value);
  840. /**
  841. * Use this function to write 32 bits in native format to an SDL_IOStream as
  842. * little-endian data.
  843. *
  844. * SDL byteswaps the data only if necessary, so the application always
  845. * specifies native format, and the data written will be in little-endian
  846. * 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_WriteS32LE(SDL_IOStream *dst, Sint32 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_WriteU32BE(SDL_IOStream *dst, Uint32 value);
  871. /**
  872. * Use this function to write 32 bits in native format to an SDL_IOStream as
  873. * big-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 big-endian format.
  877. *
  878. * \param dst the stream to which data will be written
  879. * \param value the data to be written, in native format
  880. * \returns SDL_TRUE on successful write, SDL_FALSE on failure; call
  881. * SDL_GetError() for more information.
  882. *
  883. * \since This function is available since SDL 3.0.0.
  884. */
  885. extern DECLSPEC SDL_bool SDLCALL SDL_WriteS32BE(SDL_IOStream *dst, Sint32 value);
  886. /**
  887. * Use this function to write 64 bits in native format to an SDL_IOStream as
  888. * little-endian data.
  889. *
  890. * SDL byteswaps the data only if necessary, so the application always
  891. * specifies native format, and the data written will be in little-endian
  892. * format.
  893. *
  894. * \param dst the stream to which data will be written
  895. * \param value the data to be written, in native format
  896. * \returns SDL_TRUE on successful write, SDL_FALSE on failure; call
  897. * SDL_GetError() for more information.
  898. *
  899. * \since This function is available since SDL 3.0.0.
  900. */
  901. extern DECLSPEC SDL_bool SDLCALL SDL_WriteU64LE(SDL_IOStream *dst, Uint64 value);
  902. /**
  903. * Use this function to write 64 bits in native format to an SDL_IOStream as
  904. * little-endian data.
  905. *
  906. * SDL byteswaps the data only if necessary, so the application always
  907. * specifies native format, and the data written will be in little-endian
  908. * 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_WriteS64LE(SDL_IOStream *dst, Sint64 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_WriteU64BE(SDL_IOStream *dst, Uint64 value);
  933. /**
  934. * Use this function to write 64 bits in native format to an SDL_IOStream as
  935. * big-endian data.
  936. *
  937. * SDL byteswaps the data only if necessary, so the application always
  938. * specifies native format, and the data written will be in big-endian format.
  939. *
  940. * \param dst the stream to which data will be written
  941. * \param value the data to be written, in native format
  942. * \returns SDL_TRUE on successful write, SDL_FALSE on failure; call
  943. * SDL_GetError() for more information.
  944. *
  945. * \since This function is available since SDL 3.0.0.
  946. */
  947. extern DECLSPEC SDL_bool SDLCALL SDL_WriteS64BE(SDL_IOStream *dst, Sint64 value);
  948. /* @} *//* Write endian functions */
  949. /* Ends C function definitions when using C++ */
  950. #ifdef __cplusplus
  951. }
  952. #endif
  953. #include <SDL3/SDL_close_code.h>
  954. #endif /* SDL_iostream_h_ */