SDL_iostream.h 37 KB

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