SDL_iostream.h 36 KB

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