SDL_iostream.h 38 KB

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