SDL_iostream.h 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2024 Sam Lantinga <slouken@libsdl.org>
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any damages
  6. arising from the use of this software.
  7. Permission is granted to anyone to use this software for any purpose,
  8. including commercial applications, and to alter it and redistribute it
  9. freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you must not
  11. claim that you wrote the original software. If you use this software
  12. in a product, an acknowledgment in the product documentation would be
  13. appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and must not be
  15. misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source distribution.
  17. */
  18. /**
  19. * \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_HANDLE_POINTER`: a pointer, that can be cast to
  155. * a stdio `FILE *`, that this SDL_IOStream is using to access the
  156. * filesystem. If SDL used some other method to access the filesystem, this
  157. * property will not be set. PLEASE NOTE that if SDL is using a different C
  158. * runtime than your app, trying to use this pointer will almost certainly
  159. * result in a crash! This is mostly a problem on Windows; make sure you
  160. * build SDL and 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_IOFromConstMem
  175. * \sa SDL_IOFromMem
  176. * \sa SDL_CloseIO
  177. * \sa SDL_ReadIO
  178. * \sa SDL_SeekIO
  179. * \sa SDL_TellIO
  180. * \sa SDL_WriteIO
  181. */
  182. extern DECLSPEC SDL_IOStream *SDLCALL SDL_IOFromFile(const char *file, const char *mode);
  183. #define SDL_PROP_IOSTREAM_WINDOWS_HANDLE_POINTER "SDL.iostream.windows.handle"
  184. #define SDL_PROP_IOSTREAM_STDIO_HANDLE_POINTER "SDL.iostream.stdio.handle"
  185. #define SDL_PROP_IOSTREAM_ANDROID_AASSET_POINTER "SDL.opstream.android.aasset"
  186. /**
  187. * Use this function to prepare a read-write memory buffer for use with
  188. * SDL_IOStream.
  189. *
  190. * This function sets up an SDL_IOStream struct based on a memory area of a
  191. * certain size, for both read and write access.
  192. *
  193. * This memory buffer is not copied by the SDL_IOStream; the pointer you
  194. * provide must remain valid until you close the stream. Closing the stream
  195. * will not free the original buffer.
  196. *
  197. * If you need to make sure the SDL_IOStream never writes to the memory
  198. * buffer, you should use SDL_IOFromConstMem() with a read-only buffer of
  199. * memory instead.
  200. *
  201. * \param mem a pointer to a buffer to feed an SDL_IOStream stream
  202. * \param size the buffer size, in bytes
  203. * \returns a pointer to a new SDL_IOStream structure, or NULL if it fails;
  204. * call SDL_GetError() for more information.
  205. *
  206. * \since This function is available since SDL 3.0.0.
  207. *
  208. * \sa SDL_IOFromConstMem
  209. * \sa SDL_IOFromFile
  210. * \sa SDL_IOFromMem
  211. * \sa SDL_CloseIO
  212. * \sa SDL_ReadIO
  213. * \sa SDL_SeekIO
  214. * \sa SDL_TellIO
  215. * \sa SDL_WriteIO
  216. */
  217. extern DECLSPEC SDL_IOStream *SDLCALL SDL_IOFromMem(void *mem, size_t size);
  218. /**
  219. * Use this function to prepare a read-only memory buffer for use with
  220. * SDL_IOStream.
  221. *
  222. * This function sets up an SDL_IOStream struct based on a memory area of a
  223. * certain size. It assumes the memory area is not writable.
  224. *
  225. * Attempting to write to this SDL_IOStream stream will report an error
  226. * without writing to the memory buffer.
  227. *
  228. * This memory buffer is not copied by the SDL_IOStream; the pointer you
  229. * provide must remain valid until you close the stream. Closing the stream
  230. * will not free the original buffer.
  231. *
  232. * If you need to write to a memory buffer, you should use SDL_IOFromMem()
  233. * with a writable buffer of memory instead.
  234. *
  235. * \param mem a pointer to a read-only buffer to feed an SDL_IOStream stream
  236. * \param size the buffer size, in bytes
  237. * \returns a pointer to a new SDL_IOStream structure, or NULL if it fails;
  238. * call SDL_GetError() for more information.
  239. *
  240. * \since This function is available since SDL 3.0.0.
  241. *
  242. * \sa SDL_IOFromConstMem
  243. * \sa SDL_IOFromFile
  244. * \sa SDL_IOFromMem
  245. * \sa SDL_CloseIO
  246. * \sa SDL_ReadIO
  247. * \sa SDL_SeekIO
  248. * \sa SDL_TellIO
  249. */
  250. extern DECLSPEC SDL_IOStream *SDLCALL SDL_IOFromConstMem(const void *mem, size_t size);
  251. /* @} *//* IOFrom functions */
  252. /**
  253. * Create a custom SDL_IOStream.
  254. *
  255. * Applications do not need to use this function unless they are providing
  256. * their own SDL_IOStream implementation. If you just need an SDL_IOStream to
  257. * read/write a common data source, you should use the built-in
  258. * implementations in SDL, like SDL_IOFromFile() or SDL_IOFromMem(), etc.
  259. *
  260. * You must free the returned pointer with SDL_CloseIO().
  261. *
  262. * \param iface The function pointers that implement this SDL_IOStream.
  263. * \param userdata The app-controlled pointer that is passed to iface's
  264. * functions when called.
  265. * \returns a pointer to the allocated memory on success, or NULL on failure;
  266. * call SDL_GetError() for more information.
  267. *
  268. * \since This function is available since SDL 3.0.0.
  269. *
  270. * \sa SDL_CloseIO
  271. */
  272. extern DECLSPEC SDL_IOStream *SDLCALL SDL_OpenIO(const SDL_IOStreamInterface *iface, void *userdata);
  273. /**
  274. * Close and free an allocated SDL_IOStream structure.
  275. *
  276. * SDL_CloseIO() closes and cleans up the SDL_IOStream stream. It releases any
  277. * resources used by the stream and frees the SDL_IOStream itself. This
  278. * returns 0 on success, or -1 if the stream failed to flush to its output
  279. * (e.g. to disk).
  280. *
  281. * Note that if this fails to flush the stream to disk, this function reports
  282. * an error, but the SDL_IOStream is still invalid once this function returns.
  283. *
  284. * \param context SDL_IOStream structure to close
  285. * \returns 0 on success or a negative error code on failure; call
  286. * SDL_GetError() for more information.
  287. *
  288. * \since This function is available since SDL 3.0.0.
  289. *
  290. * \sa SDL_IOFromConstMem
  291. * \sa SDL_IOFromFile
  292. * \sa SDL_IOFromMem
  293. * \sa SDL_ReadIO
  294. * \sa SDL_SeekIO
  295. * \sa SDL_WriteIO
  296. */
  297. extern DECLSPEC int SDLCALL SDL_CloseIO(SDL_IOStream *context);
  298. /**
  299. * Get the properties associated with an SDL_IOStream.
  300. *
  301. * \param context a pointer to an SDL_IOStream structure
  302. * \returns a valid property ID on success or 0 on failure; call
  303. * SDL_GetError() for more information.
  304. *
  305. * \since This function is available since SDL 3.0.0.
  306. *
  307. * \sa SDL_GetProperty
  308. * \sa SDL_SetProperty
  309. */
  310. extern DECLSPEC SDL_PropertiesID SDLCALL SDL_GetIOProperties(SDL_IOStream *context);
  311. #define SDL_IO_SEEK_SET 0 /**< Seek from the beginning of data */
  312. #define SDL_IO_SEEK_CUR 1 /**< Seek relative to current read point */
  313. #define SDL_IO_SEEK_END 2 /**< Seek relative to the end of data */
  314. /**
  315. * Query the stream status of an SDL_IOStream.
  316. *
  317. * This information can be useful to decide if a short read or write was due
  318. * to an error, an EOF, or a non-blocking operation that isn't yet ready to
  319. * complete.
  320. *
  321. * An SDL_IOStream's status is only expected to change after a SDL_ReadIO or
  322. * SDL_WriteIO call; don't expect it to change if you just call this query
  323. * function in a tight loop.
  324. *
  325. * \param context the SDL_IOStream to query.
  326. * \returns an SDL_IOStatus enum with the current state.
  327. *
  328. * \threadsafety This function should not be called at the same time that
  329. * another thread is operating on the same SDL_IOStream.
  330. *
  331. * \since This function is available since SDL 3.0.0.
  332. */
  333. extern DECLSPEC SDL_IOStatus SDLCALL SDL_GetIOStatus(SDL_IOStream *context);
  334. /**
  335. * Use this function to get the size of the data stream in an SDL_IOStream.
  336. *
  337. * \param context the SDL_IOStream to get the size of the data stream from
  338. * \returns the size of the data stream in the SDL_IOStream on success or a
  339. * negative error code on failure; call SDL_GetError() for more
  340. * information.
  341. *
  342. * \since This function is available since SDL 3.0.0.
  343. */
  344. extern DECLSPEC Sint64 SDLCALL SDL_SizeIO(SDL_IOStream *context);
  345. /**
  346. * Seek within an SDL_IOStream data stream.
  347. *
  348. * This function seeks to byte `offset`, relative to `whence`.
  349. *
  350. * `whence` may be any of the following values:
  351. *
  352. * - `SDL_IO_SEEK_SET`: seek from the beginning of data
  353. * - `SDL_IO_SEEK_CUR`: seek relative to current read point
  354. * - `SDL_IO_SEEK_END`: seek relative to the end of data
  355. *
  356. * If this stream can not seek, it will return -1.
  357. *
  358. * \param context a pointer to an SDL_IOStream structure
  359. * \param offset an offset in bytes, relative to **whence** location; can be
  360. * negative
  361. * \param whence any of `SDL_IO_SEEK_SET`, `SDL_IO_SEEK_CUR`,
  362. * `SDL_IO_SEEK_END`
  363. * \returns the final offset in the data stream after the seek or a negative
  364. * error code on failure; call SDL_GetError() for more information.
  365. *
  366. * \since This function is available since SDL 3.0.0.
  367. *
  368. * \sa SDL_IOFromConstMem
  369. * \sa SDL_IOFromFile
  370. * \sa SDL_IOFromMem
  371. * \sa SDL_ReadIO
  372. * \sa SDL_TellIO
  373. * \sa SDL_WriteIO
  374. */
  375. extern DECLSPEC Sint64 SDLCALL SDL_SeekIO(SDL_IOStream *context, Sint64 offset, int whence);
  376. /**
  377. * Determine the current read/write offset in an SDL_IOStream data stream.
  378. *
  379. * SDL_TellIO is actually a wrapper function that calls the SDL_IOStream's
  380. * `seek` method, with an offset of 0 bytes from `SDL_IO_SEEK_CUR`, to
  381. * simplify application development.
  382. *
  383. * \param context an SDL_IOStream data stream object from which to get the
  384. * current offset
  385. * \returns the current offset in the stream, or -1 if the information can not
  386. * be determined.
  387. *
  388. * \since This function is available since SDL 3.0.0.
  389. *
  390. * \sa SDL_IOFromConstMem
  391. * \sa SDL_IOFromFile
  392. * \sa SDL_IOFromMem
  393. * \sa SDL_ReadIO
  394. * \sa SDL_SeekIO
  395. * \sa SDL_WriteIO
  396. */
  397. extern DECLSPEC Sint64 SDLCALL SDL_TellIO(SDL_IOStream *context);
  398. /**
  399. * Read from a data source.
  400. *
  401. * This function reads up `size` bytes from the data source to the area
  402. * pointed at by `ptr`. This function may read less bytes than requested. It
  403. * will return zero when the data stream is completely read, or -1 on error.
  404. * For streams that support non-blocking operation, if nothing was read
  405. * because it would require blocking, this function returns -2 to distinguish
  406. * that this is not an error or end-of-file, and the caller can try again
  407. * later.
  408. *
  409. * \param context a pointer to an SDL_IOStream structure
  410. * \param ptr a pointer to a buffer to read data into
  411. * \param size the number of bytes to read from the data source.
  412. * \returns the number of bytes read, or 0 on end of file or other error.
  413. *
  414. * \since This function is available since SDL 3.0.0.
  415. *
  416. * \sa SDL_IOFromConstMem
  417. * \sa SDL_IOFromFile
  418. * \sa SDL_IOFromMem
  419. * \sa SDL_SeekIO
  420. * \sa SDL_WriteIO
  421. */
  422. extern DECLSPEC size_t SDLCALL SDL_ReadIO(SDL_IOStream *context, void *ptr, size_t size);
  423. /**
  424. * Write to an SDL_IOStream data stream.
  425. *
  426. * This function writes exactly `size` bytes from the area pointed at by `ptr`
  427. * to the stream. If this fails for any reason, it'll return less than `size`
  428. * to demonstrate how far the write progressed. On success, it returns `num`.
  429. *
  430. * On error, this function still attempts to write as much as possible, so it
  431. * might return a positive value less than the requested write size. If the
  432. * function failed to write anything and there was an actual error, it will
  433. * return -1. For streams that support non-blocking operation, if nothing was
  434. * written because it would require blocking, this function returns -2 to
  435. * distinguish that this is not an error and the caller can try again later.
  436. *
  437. * It is an error to specify a negative `size`, but this parameter is signed
  438. * so you definitely cannot overflow the return value on a successful run with
  439. * enormous amounts of data.
  440. *
  441. * \param context a pointer to an SDL_IOStream structure
  442. * \param ptr a pointer to a buffer containing data to write
  443. * \param size the number of bytes to write
  444. * \returns the number of bytes written, which will be less than `num` on
  445. * error; call SDL_GetError() for more information.
  446. *
  447. * \since This function is available since SDL 3.0.0.
  448. *
  449. * \sa SDL_IOFromConstMem
  450. * \sa SDL_IOFromFile
  451. * \sa SDL_IOFromMem
  452. * \sa SDL_IOprintf
  453. * \sa SDL_ReadIO
  454. * \sa SDL_SeekIO
  455. */
  456. extern DECLSPEC size_t SDLCALL SDL_WriteIO(SDL_IOStream *context, const void *ptr, size_t size);
  457. /**
  458. * Print to an SDL_IOStream data stream.
  459. *
  460. * This function does formatted printing to the stream.
  461. *
  462. * \param context a pointer to an SDL_IOStream structure
  463. * \param fmt a printf() style format string
  464. * \param ... additional parameters matching % tokens in the `fmt` string, if
  465. * any
  466. * \returns the number of bytes written, or 0 on error; call SDL_GetError()
  467. * for more information.
  468. *
  469. * \since This function is available since SDL 3.0.0.
  470. *
  471. * \sa SDL_IOFromConstMem
  472. * \sa SDL_IOFromFile
  473. * \sa SDL_IOFromMem
  474. * \sa SDL_ReadIO
  475. * \sa SDL_SeekIO
  476. * \sa SDL_WriteIO
  477. */
  478. extern DECLSPEC size_t SDLCALL SDL_IOprintf(SDL_IOStream *context, SDL_PRINTF_FORMAT_STRING const char *fmt, ...) SDL_PRINTF_VARARG_FUNC(2);
  479. /**
  480. * Print to an SDL_IOStream data stream.
  481. *
  482. * This function does formatted printing to the stream.
  483. *
  484. * \param context a pointer to an SDL_IOStream structure
  485. * \param fmt a printf() style format string
  486. * \param ap a variable argument list
  487. * \returns the number of bytes written, or 0 on error; call SDL_GetError()
  488. * for more information.
  489. *
  490. * \since This function is available since SDL 3.0.0.
  491. *
  492. * \sa SDL_IOFromConstMem
  493. * \sa SDL_IOFromFile
  494. * \sa SDL_IOFromMem
  495. * \sa SDL_ReadIO
  496. * \sa SDL_SeekIO
  497. * \sa SDL_WriteIO
  498. */
  499. 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);
  500. /**
  501. * Load all the data from an SDL data stream.
  502. *
  503. * The data is allocated with a zero byte at the end (null terminated) for
  504. * convenience. This extra byte is not included in the value reported via
  505. * `datasize`.
  506. *
  507. * The data should be freed with SDL_free().
  508. *
  509. * \param src the SDL_IOStream to read all available data from
  510. * \param datasize if not NULL, will store the number of bytes read
  511. * \param closeio if SDL_TRUE, calls SDL_CloseIO() on `src` before returning,
  512. * even in the case of an error
  513. * \returns the data, or NULL if there was an error.
  514. *
  515. * \since This function is available since SDL 3.0.0.
  516. */
  517. extern DECLSPEC void *SDLCALL SDL_LoadFile_IO(SDL_IOStream *src, size_t *datasize, SDL_bool closeio);
  518. /**
  519. * Load all the data from a file path.
  520. *
  521. * The data is allocated with a zero byte at the end (null terminated) for
  522. * convenience. This extra byte is not included in the value reported via
  523. * `datasize`.
  524. *
  525. * The data should be freed with SDL_free().
  526. *
  527. * \param file the path to read all available data from
  528. * \param datasize if not NULL, will store the number of bytes read
  529. * \returns the data, or NULL if there was an error.
  530. *
  531. * \since This function is available since SDL 3.0.0.
  532. */
  533. extern DECLSPEC void *SDLCALL SDL_LoadFile(const char *file, size_t *datasize);
  534. /**
  535. * \name Read endian functions
  536. *
  537. * Read an item of the specified endianness and return in native format.
  538. */
  539. /* @{ */
  540. /**
  541. * Use this function to read a byte from an SDL_IOStream.
  542. *
  543. * \param src the SDL_IOStream to read from
  544. * \param value a pointer filled in with the data read
  545. * \returns SDL_TRUE on success or SDL_FALSE on failure; call SDL_GetError()
  546. * for more information.
  547. *
  548. * \since This function is available since SDL 3.0.0.
  549. */
  550. extern DECLSPEC SDL_bool SDLCALL SDL_ReadU8(SDL_IOStream *src, Uint8 *value);
  551. /**
  552. * Use this function to read 16 bits of little-endian data from an
  553. * SDL_IOStream and return in native format.
  554. *
  555. * SDL byteswaps the data only if necessary, so the data returned will be in
  556. * the native byte order.
  557. *
  558. * \param src the stream from which to read data
  559. * \param value a pointer filled in with the data read
  560. * \returns SDL_TRUE on successful write, SDL_FALSE on failure; call
  561. * SDL_GetError() for more information.
  562. *
  563. * \since This function is available since SDL 3.0.0.
  564. */
  565. extern DECLSPEC SDL_bool SDLCALL SDL_ReadU16LE(SDL_IOStream *src, Uint16 *value);
  566. /**
  567. * Use this function to read 16 bits of little-endian data from an
  568. * SDL_IOStream and return in native format.
  569. *
  570. * SDL byteswaps the data only if necessary, so the data returned will be in
  571. * the native byte order.
  572. *
  573. * \param src the stream from which to read data
  574. * \param value a pointer filled in with the data read
  575. * \returns SDL_TRUE on successful write, SDL_FALSE on failure; call
  576. * SDL_GetError() for more information.
  577. *
  578. * \since This function is available since SDL 3.0.0.
  579. */
  580. extern DECLSPEC SDL_bool SDLCALL SDL_ReadS16LE(SDL_IOStream *src, Sint16 *value);
  581. /**
  582. * Use this function to read 16 bits of big-endian data from an SDL_IOStream
  583. * and return in native format.
  584. *
  585. * SDL byteswaps the data only if necessary, so the data returned will be in
  586. * the native byte order.
  587. *
  588. * \param src the stream from which to read data
  589. * \param value a pointer filled in with the data read
  590. * \returns SDL_TRUE on successful write, SDL_FALSE on failure; call
  591. * SDL_GetError() for more information.
  592. *
  593. * \since This function is available since SDL 3.0.0.
  594. */
  595. extern DECLSPEC SDL_bool SDLCALL SDL_ReadU16BE(SDL_IOStream *src, Uint16 *value);
  596. /**
  597. * Use this function to read 16 bits of big-endian data from an SDL_IOStream
  598. * and return in native format.
  599. *
  600. * SDL byteswaps the data only if necessary, so the data returned will be in
  601. * the native byte order.
  602. *
  603. * \param src the stream from which to read data
  604. * \param value a pointer filled in with the data read
  605. * \returns SDL_TRUE on successful write, SDL_FALSE on failure; call
  606. * SDL_GetError() for more information.
  607. *
  608. * \since This function is available since SDL 3.0.0.
  609. */
  610. extern DECLSPEC SDL_bool SDLCALL SDL_ReadS16BE(SDL_IOStream *src, Sint16 *value);
  611. /**
  612. * Use this function to read 32 bits of little-endian data from an
  613. * SDL_IOStream and return in native format.
  614. *
  615. * SDL byteswaps the data only if necessary, so the data returned will be in
  616. * the native byte order.
  617. *
  618. * \param src the stream from which to read data
  619. * \param value a pointer filled in with the data read
  620. * \returns SDL_TRUE on successful write, SDL_FALSE on failure; call
  621. * SDL_GetError() for more information.
  622. *
  623. * \since This function is available since SDL 3.0.0.
  624. */
  625. extern DECLSPEC SDL_bool SDLCALL SDL_ReadU32LE(SDL_IOStream *src, Uint32 *value);
  626. /**
  627. * Use this function to read 32 bits of little-endian data from an
  628. * SDL_IOStream and return in native format.
  629. *
  630. * SDL byteswaps the data only if necessary, so the data returned will be in
  631. * the native byte order.
  632. *
  633. * \param src the stream from which to read data
  634. * \param value a pointer filled in with the data read
  635. * \returns SDL_TRUE on successful write, SDL_FALSE on failure; call
  636. * SDL_GetError() for more information.
  637. *
  638. * \since This function is available since SDL 3.0.0.
  639. */
  640. extern DECLSPEC SDL_bool SDLCALL SDL_ReadS32LE(SDL_IOStream *src, Sint32 *value);
  641. /**
  642. * Use this function to read 32 bits of big-endian data from an SDL_IOStream
  643. * and return in native format.
  644. *
  645. * SDL byteswaps the data only if necessary, so the data returned will be in
  646. * the native byte order.
  647. *
  648. * \param src the stream from which to read data
  649. * \param value a pointer filled in with the data read
  650. * \returns SDL_TRUE on successful write, SDL_FALSE on failure; call
  651. * SDL_GetError() for more information.
  652. *
  653. * \since This function is available since SDL 3.0.0.
  654. */
  655. extern DECLSPEC SDL_bool SDLCALL SDL_ReadU32BE(SDL_IOStream *src, Uint32 *value);
  656. /**
  657. * Use this function to read 32 bits of big-endian data from an SDL_IOStream
  658. * and return in native format.
  659. *
  660. * SDL byteswaps the data only if necessary, so the data returned will be in
  661. * the native byte order.
  662. *
  663. * \param src the stream from which to read data
  664. * \param value a pointer filled in with the data read
  665. * \returns SDL_TRUE on successful write, SDL_FALSE on failure; call
  666. * SDL_GetError() for more information.
  667. *
  668. * \since This function is available since SDL 3.0.0.
  669. */
  670. extern DECLSPEC SDL_bool SDLCALL SDL_ReadS32BE(SDL_IOStream *src, Sint32 *value);
  671. /**
  672. * Use this function to read 64 bits of little-endian data from an
  673. * SDL_IOStream and return in native format.
  674. *
  675. * SDL byteswaps the data only if necessary, so the data returned will be in
  676. * the native byte order.
  677. *
  678. * \param src the stream from which to read data
  679. * \param value a pointer filled in with the data read
  680. * \returns SDL_TRUE on successful write, SDL_FALSE on failure; call
  681. * SDL_GetError() for more information.
  682. *
  683. * \since This function is available since SDL 3.0.0.
  684. */
  685. extern DECLSPEC SDL_bool SDLCALL SDL_ReadU64LE(SDL_IOStream *src, Uint64 *value);
  686. /**
  687. * Use this function to read 64 bits of little-endian data from an
  688. * SDL_IOStream and return in native format.
  689. *
  690. * SDL byteswaps the data only if necessary, so the data returned will be in
  691. * the native byte order.
  692. *
  693. * \param src the stream from which to read data
  694. * \param value a pointer filled in with the data read
  695. * \returns SDL_TRUE on successful write, SDL_FALSE on failure; call
  696. * SDL_GetError() for more information.
  697. *
  698. * \since This function is available since SDL 3.0.0.
  699. */
  700. extern DECLSPEC SDL_bool SDLCALL SDL_ReadS64LE(SDL_IOStream *src, Sint64 *value);
  701. /**
  702. * Use this function to read 64 bits of big-endian data from an SDL_IOStream
  703. * and return in native format.
  704. *
  705. * SDL byteswaps the data only if necessary, so the data returned will be in
  706. * the native byte order.
  707. *
  708. * \param src the stream from which to read data
  709. * \param value a pointer filled in with the data read
  710. * \returns SDL_TRUE on successful write, SDL_FALSE on failure; call
  711. * SDL_GetError() for more information.
  712. *
  713. * \since This function is available since SDL 3.0.0.
  714. */
  715. extern DECLSPEC SDL_bool SDLCALL SDL_ReadU64BE(SDL_IOStream *src, Uint64 *value);
  716. /**
  717. * Use this function to read 64 bits of big-endian data from an SDL_IOStream
  718. * and return in native format.
  719. *
  720. * SDL byteswaps the data only if necessary, so the data returned will be in
  721. * the native byte order.
  722. *
  723. * \param src the stream from which to read data
  724. * \param value a pointer filled in with the data read
  725. * \returns SDL_TRUE on successful write, SDL_FALSE on failure; call
  726. * SDL_GetError() for more information.
  727. *
  728. * \since This function is available since SDL 3.0.0.
  729. */
  730. extern DECLSPEC SDL_bool SDLCALL SDL_ReadS64BE(SDL_IOStream *src, Sint64 *value);
  731. /* @} *//* Read endian functions */
  732. /**
  733. * \name Write endian functions
  734. *
  735. * Write an item of native format to the specified endianness.
  736. */
  737. /* @{ */
  738. /**
  739. * Use this function to write a byte to an SDL_IOStream.
  740. *
  741. * \param dst the SDL_IOStream to write to
  742. * \param value the byte value to write
  743. * \returns SDL_TRUE on successful write, SDL_FALSE on failure; call
  744. * SDL_GetError() for more information.
  745. *
  746. * \since This function is available since SDL 3.0.0.
  747. */
  748. extern DECLSPEC SDL_bool SDLCALL SDL_WriteU8(SDL_IOStream *dst, Uint8 value);
  749. /**
  750. * Use this function to write 16 bits in native format to an SDL_IOStream as
  751. * little-endian data.
  752. *
  753. * SDL byteswaps the data only if necessary, so the application always
  754. * specifies native format, and the data written will be in little-endian
  755. * format.
  756. *
  757. * \param dst the stream to which data will be written
  758. * \param value the data to be written, in native format
  759. * \returns SDL_TRUE on successful write, SDL_FALSE on failure; call
  760. * SDL_GetError() for more information.
  761. *
  762. * \since This function is available since SDL 3.0.0.
  763. */
  764. extern DECLSPEC SDL_bool SDLCALL SDL_WriteU16LE(SDL_IOStream *dst, Uint16 value);
  765. /**
  766. * Use this function to write 16 bits in native format to an SDL_IOStream as
  767. * little-endian data.
  768. *
  769. * SDL byteswaps the data only if necessary, so the application always
  770. * specifies native format, and the data written will be in little-endian
  771. * format.
  772. *
  773. * \param dst the stream to which data will be written
  774. * \param value the data to be written, in native format
  775. * \returns SDL_TRUE on successful write, SDL_FALSE on failure; call
  776. * SDL_GetError() for more information.
  777. *
  778. * \since This function is available since SDL 3.0.0.
  779. */
  780. extern DECLSPEC SDL_bool SDLCALL SDL_WriteS16LE(SDL_IOStream *dst, Sint16 value);
  781. /**
  782. * Use this function to write 16 bits in native format to an SDL_IOStream as
  783. * big-endian data.
  784. *
  785. * SDL byteswaps the data only if necessary, so the application always
  786. * specifies native format, and the data written will be in big-endian format.
  787. *
  788. * \param dst the stream to which data will be written
  789. * \param value the data to be written, in native format
  790. * \returns SDL_TRUE on successful write, SDL_FALSE on failure; call
  791. * SDL_GetError() for more information.
  792. *
  793. * \since This function is available since SDL 3.0.0.
  794. */
  795. extern DECLSPEC SDL_bool SDLCALL SDL_WriteU16BE(SDL_IOStream *dst, Uint16 value);
  796. /**
  797. * Use this function to write 16 bits in native format to an SDL_IOStream as
  798. * big-endian data.
  799. *
  800. * SDL byteswaps the data only if necessary, so the application always
  801. * specifies native format, and the data written will be in big-endian format.
  802. *
  803. * \param dst the stream to which data will be written
  804. * \param value the data to be written, in native format
  805. * \returns SDL_TRUE on successful write, SDL_FALSE on failure; call
  806. * SDL_GetError() for more information.
  807. *
  808. * \since This function is available since SDL 3.0.0.
  809. */
  810. extern DECLSPEC SDL_bool SDLCALL SDL_WriteS16BE(SDL_IOStream *dst, Sint16 value);
  811. /**
  812. * Use this function to write 32 bits in native format to an SDL_IOStream as
  813. * little-endian data.
  814. *
  815. * SDL byteswaps the data only if necessary, so the application always
  816. * specifies native format, and the data written will be in little-endian
  817. * format.
  818. *
  819. * \param dst the stream to which data will be written
  820. * \param value the data to be written, in native format
  821. * \returns SDL_TRUE on successful write, SDL_FALSE on failure; call
  822. * SDL_GetError() for more information.
  823. *
  824. * \since This function is available since SDL 3.0.0.
  825. */
  826. extern DECLSPEC SDL_bool SDLCALL SDL_WriteU32LE(SDL_IOStream *dst, Uint32 value);
  827. /**
  828. * Use this function to write 32 bits in native format to an SDL_IOStream as
  829. * little-endian data.
  830. *
  831. * SDL byteswaps the data only if necessary, so the application always
  832. * specifies native format, and the data written will be in little-endian
  833. * format.
  834. *
  835. * \param dst the stream to which data will be written
  836. * \param value the data to be written, in native format
  837. * \returns SDL_TRUE on successful write, SDL_FALSE on failure; call
  838. * SDL_GetError() for more information.
  839. *
  840. * \since This function is available since SDL 3.0.0.
  841. */
  842. extern DECLSPEC SDL_bool SDLCALL SDL_WriteS32LE(SDL_IOStream *dst, Sint32 value);
  843. /**
  844. * Use this function to write 32 bits in native format to an SDL_IOStream as
  845. * big-endian data.
  846. *
  847. * SDL byteswaps the data only if necessary, so the application always
  848. * specifies native format, and the data written will be in big-endian format.
  849. *
  850. * \param dst the stream to which data will be written
  851. * \param value the data to be written, in native format
  852. * \returns SDL_TRUE on successful write, SDL_FALSE on failure; call
  853. * SDL_GetError() for more information.
  854. *
  855. * \since This function is available since SDL 3.0.0.
  856. */
  857. extern DECLSPEC SDL_bool SDLCALL SDL_WriteU32BE(SDL_IOStream *dst, Uint32 value);
  858. /**
  859. * Use this function to write 32 bits in native format to an SDL_IOStream as
  860. * big-endian data.
  861. *
  862. * SDL byteswaps the data only if necessary, so the application always
  863. * specifies native format, and the data written will be in big-endian format.
  864. *
  865. * \param dst the stream to which data will be written
  866. * \param value the data to be written, in native format
  867. * \returns SDL_TRUE on successful write, SDL_FALSE on failure; call
  868. * SDL_GetError() for more information.
  869. *
  870. * \since This function is available since SDL 3.0.0.
  871. */
  872. extern DECLSPEC SDL_bool SDLCALL SDL_WriteS32BE(SDL_IOStream *dst, Sint32 value);
  873. /**
  874. * Use this function to write 64 bits in native format to an SDL_IOStream as
  875. * little-endian data.
  876. *
  877. * SDL byteswaps the data only if necessary, so the application always
  878. * specifies native format, and the data written will be in little-endian
  879. * format.
  880. *
  881. * \param dst the stream to which data will be written
  882. * \param value the data to be written, in native format
  883. * \returns SDL_TRUE on successful write, SDL_FALSE on failure; call
  884. * SDL_GetError() for more information.
  885. *
  886. * \since This function is available since SDL 3.0.0.
  887. */
  888. extern DECLSPEC SDL_bool SDLCALL SDL_WriteU64LE(SDL_IOStream *dst, Uint64 value);
  889. /**
  890. * Use this function to write 64 bits in native format to an SDL_IOStream as
  891. * little-endian data.
  892. *
  893. * SDL byteswaps the data only if necessary, so the application always
  894. * specifies native format, and the data written will be in little-endian
  895. * format.
  896. *
  897. * \param dst the stream to which data will be written
  898. * \param value the data to be written, in native format
  899. * \returns SDL_TRUE on successful write, SDL_FALSE on failure; call
  900. * SDL_GetError() for more information.
  901. *
  902. * \since This function is available since SDL 3.0.0.
  903. */
  904. extern DECLSPEC SDL_bool SDLCALL SDL_WriteS64LE(SDL_IOStream *dst, Sint64 value);
  905. /**
  906. * Use this function to write 64 bits in native format to an SDL_IOStream as
  907. * big-endian data.
  908. *
  909. * SDL byteswaps the data only if necessary, so the application always
  910. * specifies native format, and the data written will be in big-endian format.
  911. *
  912. * \param dst the stream to which data will be written
  913. * \param value the data to be written, in native format
  914. * \returns SDL_TRUE on successful write, SDL_FALSE on failure; call
  915. * SDL_GetError() for more information.
  916. *
  917. * \since This function is available since SDL 3.0.0.
  918. */
  919. extern DECLSPEC SDL_bool SDLCALL SDL_WriteU64BE(SDL_IOStream *dst, Uint64 value);
  920. /**
  921. * Use this function to write 64 bits in native format to an SDL_IOStream as
  922. * big-endian data.
  923. *
  924. * SDL byteswaps the data only if necessary, so the application always
  925. * specifies native format, and the data written will be in big-endian format.
  926. *
  927. * \param dst the stream to which data will be written
  928. * \param value the data to be written, in native format
  929. * \returns SDL_TRUE on successful write, SDL_FALSE on failure; call
  930. * SDL_GetError() for more information.
  931. *
  932. * \since This function is available since SDL 3.0.0.
  933. */
  934. extern DECLSPEC SDL_bool SDLCALL SDL_WriteS64BE(SDL_IOStream *dst, Sint64 value);
  935. /* @} *//* Write endian functions */
  936. /* Ends C function definitions when using C++ */
  937. #ifdef __cplusplus
  938. }
  939. #endif
  940. #include <SDL3/SDL_close_code.h>
  941. #endif /* SDL_iostream_h_ */