SDL_iostream.h 38 KB

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