SDL_iostream.h 40 KB

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