SDL_iostream.h 38 KB

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