SDL_iostream.h 36 KB

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