SDL_iostream.h 37 KB

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