SDL_iostream.h 37 KB

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