SDL_iostream.h 41 KB

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