SDL_rwops.h 37 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_rwops.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. #ifndef SDL_rwops_h_
  25. #define SDL_rwops_h_
  26. #include <SDL3/SDL_stdinc.h>
  27. #include <SDL3/SDL_error.h>
  28. #include <SDL3/SDL_properties.h>
  29. #include <SDL3/SDL_begin_code.h>
  30. /* Set up for C function definitions, even when using C++ */
  31. #ifdef __cplusplus
  32. extern "C" {
  33. #endif
  34. /* SDL_IOStream status, set by a read or write operation */
  35. typedef enum SDL_IOStatus
  36. {
  37. SDL_IO_STATUS_READY, /**< Everything is ready */
  38. SDL_IO_STATUS_ERROR, /**< Read or write I/O error */
  39. SDL_IO_STATUS_EOF, /**< End of file */
  40. SDL_IO_STATUS_NOT_READY, /**< Non blocking I/O, not ready */
  41. SDL_IO_STATUS_READONLY, /**< Tried to write a read-only buffer */
  42. SDL_IO_STATUS_WRITEONLY /**< Tried to read a write-only buffer */
  43. } SDL_IOStatus;
  44. typedef struct SDL_IOStreamInterface
  45. {
  46. /**
  47. * Return the number of bytes in this SDL_IOStream
  48. *
  49. * \return the total size of the data stream, or -1 on error.
  50. */
  51. Sint64 (SDLCALL *size)(void *userdata);
  52. /**
  53. * Seek to \c offset relative to \c whence, one of stdio's whence values:
  54. * SDL_IO_SEEK_SET, SDL_IO_SEEK_CUR, SDL_IO_SEEK_END
  55. *
  56. * \return the final offset in the data stream, or -1 on error.
  57. */
  58. Sint64 (SDLCALL *seek)(void *userdata, Sint64 offset, int whence);
  59. /**
  60. * Read up to \c size bytes from the data stream to the area pointed
  61. * at by \c ptr.
  62. *
  63. * On an incomplete read, you should set `*status` to a value from the
  64. * SDL_IOStatus enum. You do not have to explicitly set this on
  65. * a complete, successful read.
  66. *
  67. * \return the number of bytes read
  68. */
  69. size_t (SDLCALL *read)(void *userdata, void *ptr, size_t size, SDL_IOStatus *status);
  70. /**
  71. * Write exactly \c size bytes from the area pointed at by \c ptr
  72. * to data stream.
  73. *
  74. * On an incomplete write, you should set `*status` to a value from the
  75. * SDL_IOStatus enum. You do not have to explicitly set this on
  76. * a complete, successful write.
  77. *
  78. * \return the number of bytes written
  79. */
  80. size_t (SDLCALL *write)(void *userdata, const void *ptr, size_t size, SDL_IOStatus *status);
  81. /**
  82. * Close and free any allocated resources.
  83. *
  84. * The SDL_IOStream is still destroyed even if this fails, so clean up anything
  85. * even if flushing to disk returns an error.
  86. *
  87. * \return 0 if successful or -1 on write error when flushing data.
  88. */
  89. int (SDLCALL *close)(void *userdata);
  90. } SDL_IOStreamInterface;
  91. /**
  92. * This is the read/write operation structure -- opaque, as of SDL3!
  93. */
  94. typedef struct SDL_IOStream SDL_IOStream;
  95. /**
  96. * \name IOFrom functions
  97. *
  98. * Functions to create SDL_IOStream structures from various data streams.
  99. */
  100. /* @{ */
  101. /**
  102. * Use this function to create a new SDL_IOStream structure for reading from
  103. * and/or writing to a named file.
  104. *
  105. * The `mode` string is treated roughly the same as in a call to the C
  106. * library's fopen(), even if SDL doesn't happen to use fopen() behind the
  107. * scenes.
  108. *
  109. * Available `mode` strings:
  110. *
  111. * - "r": Open a file for reading. The file must exist.
  112. * - "w": Create an empty file for writing. If a file with the same name
  113. * already exists its content is erased and the file is treated as a new
  114. * empty file.
  115. * - "a": Append to a file. Writing operations append data at the end of the
  116. * file. The file is created if it does not exist.
  117. * - "r+": Open a file for update both reading and writing. The file must
  118. * exist.
  119. * - "w+": Create an empty file for both reading and writing. If a file with
  120. * the same name already exists its content is erased and the file is
  121. * treated as a new empty file.
  122. * - "a+": Open a file for reading and appending. All writing operations are
  123. * performed at the end of the file, protecting the previous content to be
  124. * overwritten. You can reposition (fseek, rewind) the internal pointer to
  125. * anywhere in the file for reading, but writing operations will move it
  126. * back to the end of file. The file is created if it does not exist.
  127. *
  128. * **NOTE**: In order to open a file as a binary file, a "b" character has to
  129. * be included in the `mode` string. This additional "b" character can either
  130. * be appended at the end of the string (thus making the following compound
  131. * modes: "rb", "wb", "ab", "r+b", "w+b", "a+b") or be inserted between the
  132. * letter and the "+" sign for the mixed modes ("rb+", "wb+", "ab+").
  133. * Additional characters may follow the sequence, although they should have no
  134. * effect. For example, "t" is sometimes appended to make explicit the file is
  135. * a text file.
  136. *
  137. * This function supports Unicode filenames, but they must be encoded in UTF-8
  138. * format, regardless of the underlying operating system.
  139. *
  140. * As a fallback, SDL_IOFromFile() will transparently open a matching filename
  141. * in an Android app's `assets`.
  142. *
  143. * Destroying the SDL_IOStream will close the file handle SDL is holding internally.
  144. *
  145. * The following properties may be set at creation time by SDL:
  146. *
  147. * - `SDL_PROP_IOSTREAM_WINDOWS_HANDLE_POINTER`: a pointer, that can be cast
  148. * to a win32 `HANDLE`, that this SDL_IOStream is using to access the filesystem.
  149. * If the program isn't running on Windows, or SDL used some other method
  150. * to access the filesystem, this property will not be set.
  151. * - `SDL_PROP_IOSTREAM_STDIO_HANDLE_POINTER`: a pointer, that can be cast
  152. * to a stdio `FILE *`, that this SDL_IOStream is using to access the filesystem.
  153. * If SDL used some other method to access the filesystem, this property
  154. * will not be set. PLEASE NOTE that if SDL is using a different C runtime
  155. * than your app, trying to use this pointer will almost certainly result
  156. * in a crash! This is mostly a problem on Windows; make sure you build SDL
  157. * and your app with the same compiler and settings to avoid it.
  158. * - `SDL_PROP_IOSTREAM_ANDROID_AASSET_POINTER`: a pointer, that can be cast
  159. * to an Android NDK `AAsset *`, that this SDL_IOStream is using to access the
  160. * filesystem. If SDL used some other method to access the filesystem, this
  161. * property will not be set.
  162. *
  163. * \param file a UTF-8 string representing the filename to open
  164. * \param mode an ASCII string representing the mode to be used for opening
  165. * the file.
  166. * \returns a pointer to the SDL_IOStream structure that is created, or NULL on
  167. * failure; call SDL_GetError() for more information.
  168. *
  169. * \since This function is available since SDL 3.0.0.
  170. *
  171. * \sa SDL_IOFromConstMem
  172. * \sa SDL_IOFromMem
  173. * \sa SDL_ReadIO
  174. * \sa SDL_SeekIO
  175. * \sa SDL_TellIO
  176. * \sa SDL_WriteIO
  177. */
  178. extern DECLSPEC SDL_IOStream *SDLCALL SDL_IOFromFile(const char *file, const char *mode);
  179. #define SDL_PROP_IOSTREAM_WINDOWS_HANDLE_POINTER "SDL.iostream.windows.handle"
  180. #define SDL_PROP_IOSTREAM_STDIO_HANDLE_POINTER "SDL.iostream.stdio.handle"
  181. #define SDL_PROP_IOSTREAM_ANDROID_AASSET_POINTER "SDL.opstream.android.aasset"
  182. /**
  183. * Use this function to prepare a read-write memory buffer for use with
  184. * SDL_IOStream.
  185. *
  186. * This function sets up an SDL_IOStream struct based on a memory area of a
  187. * certain size, for both read and write access.
  188. *
  189. * This memory buffer is not copied by the SDL_IOStream; the pointer you provide must
  190. * remain valid until you close the stream. Closing the stream will not free
  191. * the original buffer.
  192. *
  193. * If you need to make sure the SDL_IOStream never writes to the memory buffer, you
  194. * should use SDL_IOFromConstMem() with a read-only buffer of memory instead.
  195. *
  196. * \param mem a pointer to a buffer to feed an SDL_IOStream stream
  197. * \param size the buffer size, in bytes
  198. * \returns a pointer to a new SDL_IOStream structure, or NULL if it fails; call
  199. * SDL_GetError() for more information.
  200. *
  201. * \since This function is available since SDL 3.0.0.
  202. *
  203. * \sa SDL_IOFromConstMem
  204. * \sa SDL_IOFromFile
  205. * \sa SDL_IOFromMem
  206. * \sa SDL_ReadIO
  207. * \sa SDL_SeekIO
  208. * \sa SDL_TellIO
  209. * \sa SDL_WriteIO
  210. */
  211. extern DECLSPEC SDL_IOStream *SDLCALL SDL_IOFromMem(void *mem, size_t size);
  212. /**
  213. * Use this function to prepare a read-only memory buffer for use with SDL_IOStream.
  214. *
  215. * This function sets up an SDL_IOStream struct based on a memory area of a
  216. * certain size. It assumes the memory area is not writable.
  217. *
  218. * Attempting to write to this SDL_IOStream stream will report an error without
  219. * writing to the memory buffer.
  220. *
  221. * This memory buffer is not copied by the SDL_IOStream; the pointer you provide must
  222. * remain valid until you close the stream. Closing the stream will not free
  223. * the original buffer.
  224. *
  225. * If you need to write to a memory buffer, you should use SDL_IOFromMem()
  226. * with a writable buffer of memory instead.
  227. *
  228. * \param mem a pointer to a read-only buffer to feed an SDL_IOStream stream
  229. * \param size the buffer size, in bytes
  230. * \returns a pointer to a new SDL_IOStream structure, or NULL if it fails; call
  231. * SDL_GetError() for more information.
  232. *
  233. * \since This function is available since SDL 3.0.0.
  234. *
  235. * \sa SDL_IOFromConstMem
  236. * \sa SDL_IOFromFile
  237. * \sa SDL_IOFromMem
  238. * \sa SDL_ReadIO
  239. * \sa SDL_SeekIO
  240. * \sa SDL_TellIO
  241. */
  242. extern DECLSPEC SDL_IOStream *SDLCALL SDL_IOFromConstMem(const void *mem, size_t size);
  243. /* @} *//* IOFrom functions */
  244. /**
  245. * Create a custom SDL_IOStream.
  246. *
  247. * Applications do not need to use this function unless they are providing
  248. * their own SDL_IOStream implementation. If you just need an SDL_IOStream to
  249. * read/write a common data source, you should use the built-in
  250. * implementations in SDL, like SDL_IOFromFile() or SDL_IOFromMem(), etc.
  251. *
  252. * You must free the returned pointer with SDL_CloseIO().
  253. *
  254. *
  255. * \param iface The function pointers that implement this SDL_IOStream.
  256. * \param userdata The app-controlled pointer that is passed to iface's functions when called.
  257. * \returns a pointer to the allocated memory on success, or NULL on failure;
  258. * call SDL_GetError() for more information.
  259. *
  260. * \since This function is available since SDL 3.0.0.
  261. *
  262. * \sa SDL_CloseIO
  263. */
  264. extern DECLSPEC SDL_IOStream *SDLCALL SDL_OpenIO(const SDL_IOStreamInterface *iface, void *userdata);
  265. /**
  266. * Close and free an allocated SDL_IOStream structure.
  267. *
  268. * SDL_CloseIO() closes and cleans up the SDL_IOStream stream. It releases any
  269. * resources used by the stream and frees the SDL_IOStream itself with
  270. * SDL_CloseIO(). This returns 0 on success, or -1 if the stream failed to
  271. * flush to its output (e.g. to disk).
  272. *
  273. * Note that if this fails to flush the stream to disk, this function reports
  274. * an error, but the SDL_IOStream is still invalid once this function returns.
  275. *
  276. * \param context SDL_IOStream structure to close
  277. * \returns 0 on success or a negative error code on failure; call
  278. * SDL_GetError() for more information.
  279. *
  280. * \since This function is available since SDL 3.0.0.
  281. *
  282. * \sa SDL_IOFromConstMem
  283. * \sa SDL_IOFromFile
  284. * \sa SDL_IOFromMem
  285. * \sa SDL_ReadIO
  286. * \sa SDL_SeekIO
  287. * \sa SDL_WriteIO
  288. */
  289. extern DECLSPEC int SDLCALL SDL_CloseIO(SDL_IOStream *context);
  290. /**
  291. * Get the properties associated with an SDL_IOStream.
  292. *
  293. * \param context a pointer to an SDL_IOStream structure
  294. * \returns a valid property ID on success or 0 on failure; call
  295. * SDL_GetError() for more information.
  296. *
  297. * \since This function is available since SDL 3.0.0.
  298. *
  299. * \sa SDL_GetProperty
  300. * \sa SDL_SetProperty
  301. */
  302. extern DECLSPEC SDL_PropertiesID SDLCALL SDL_GetRWProperties(SDL_IOStream *context);
  303. #define SDL_IO_SEEK_SET 0 /**< Seek from the beginning of data */
  304. #define SDL_IO_SEEK_CUR 1 /**< Seek relative to current read point */
  305. #define SDL_IO_SEEK_END 2 /**< Seek relative to the end of data */
  306. /**
  307. * Query the stream status of an SDL_IOStream.
  308. *
  309. * This information can be useful to decide if a short read or write was
  310. * due to an error, an EOF, or a non-blocking operation that isn't yet
  311. * ready to complete.
  312. *
  313. * An SDL_IOStream's status is only expected to change after a SDL_ReadIO or
  314. * SDL_WriteIO call; don't expect it to change if you just call this
  315. * query function in a tight loop.
  316. *
  317. * \param context the SDL_IOStream to query.
  318. * \returns an SDL_IOStatus enum with the current state.
  319. *
  320. * \threadsafety This function should not be called at the same time that
  321. * another thread is operating on the same SDL_IOStream.
  322. *
  323. * \since This function is available since SDL 3.0.0.
  324. */
  325. extern DECLSPEC SDL_IOStatus SDLCALL SDL_GetRWStatus(SDL_IOStream *context);
  326. /**
  327. * Use this function to get the size of the data stream in an SDL_IOStream.
  328. *
  329. * \param context the SDL_IOStream to get the size of the data stream from
  330. * \returns the size of the data stream in the SDL_IOStream on success or a
  331. * negative error code on failure; call SDL_GetError() for more
  332. * information.
  333. *
  334. * \since This function is available since SDL 3.0.0.
  335. */
  336. extern DECLSPEC Sint64 SDLCALL SDL_SizeIO(SDL_IOStream *context);
  337. /**
  338. * Seek within an SDL_IOStream data stream.
  339. *
  340. * This function seeks to byte `offset`, relative to `whence`.
  341. *
  342. * `whence` may be any of the following values:
  343. *
  344. * - `SDL_IO_SEEK_SET`: seek from the beginning of data
  345. * - `SDL_IO_SEEK_CUR`: seek relative to current read point
  346. * - `SDL_IO_SEEK_END`: seek relative to the end of data
  347. *
  348. * If this stream can not seek, it will return -1.
  349. *
  350. * \param context a pointer to an SDL_IOStream structure
  351. * \param offset an offset in bytes, relative to **whence** location; can be
  352. * negative
  353. * \param whence any of `SDL_IO_SEEK_SET`, `SDL_IO_SEEK_CUR`,
  354. * `SDL_IO_SEEK_END`
  355. * \returns the final offset in the data stream after the seek or a negative
  356. * error code on failure; call SDL_GetError() for more information.
  357. *
  358. * \since This function is available since SDL 3.0.0.
  359. *
  360. * \sa SDL_IOFromConstMem
  361. * \sa SDL_IOFromFile
  362. * \sa SDL_IOFromMem
  363. * \sa SDL_ReadIO
  364. * \sa SDL_TellIO
  365. * \sa SDL_WriteIO
  366. */
  367. extern DECLSPEC Sint64 SDLCALL SDL_SeekIO(SDL_IOStream *context, Sint64 offset, int whence);
  368. /**
  369. * Determine the current read/write offset in an SDL_IOStream data stream.
  370. *
  371. * SDL_TellIO is actually a wrapper function that calls the SDL_IOStream's `seek`
  372. * method, with an offset of 0 bytes from `SDL_IO_SEEK_CUR`, to simplify
  373. * application development.
  374. *
  375. * \param context an SDL_IOStream data stream object from which to get the
  376. * current offset
  377. * \returns the current offset in the stream, or -1 if the information can not
  378. * be determined.
  379. *
  380. * \since This function is available since SDL 3.0.0.
  381. *
  382. * \sa SDL_IOFromConstMem
  383. * \sa SDL_IOFromFile
  384. * \sa SDL_IOFromMem
  385. * \sa SDL_ReadIO
  386. * \sa SDL_SeekIO
  387. * \sa SDL_WriteIO
  388. */
  389. extern DECLSPEC Sint64 SDLCALL SDL_TellIO(SDL_IOStream *context);
  390. /**
  391. * Read from a data source.
  392. *
  393. * This function reads up `size` bytes from the data source to the area
  394. * pointed at by `ptr`. This function may read less bytes than requested. It
  395. * will return zero when the data stream is completely read, or -1 on error.
  396. * For streams that support non-blocking operation, if nothing was read
  397. * because it would require blocking, this function returns -2 to distinguish
  398. * that this is not an error or end-of-file, and the caller can try again
  399. * later.
  400. *
  401. * SDL_ReadIO() is actually a function wrapper that calls the SDL_IOStream's
  402. * `read` method appropriately, to simplify application development.
  403. *
  404. * \param context a pointer to an SDL_IOStream structure
  405. * \param ptr a pointer to a buffer to read data into
  406. * \param size the number of bytes to read from the data source.
  407. * \returns the number of bytes read, or 0 on end of file or other error.
  408. *
  409. * \since This function is available since SDL 3.0.0.
  410. *
  411. * \sa SDL_IOFromConstMem
  412. * \sa SDL_IOFromFile
  413. * \sa SDL_IOFromMem
  414. * \sa SDL_SeekIO
  415. * \sa SDL_WriteIO
  416. */
  417. extern DECLSPEC size_t SDLCALL SDL_ReadIO(SDL_IOStream *context, void *ptr, size_t size);
  418. /**
  419. * Write to an SDL_IOStream data stream.
  420. *
  421. * This function writes exactly `size` bytes from the area pointed at by `ptr`
  422. * to the stream. If this fails for any reason, it'll return less than `size`
  423. * to demonstrate how far the write progressed. On success, it returns `num`.
  424. *
  425. * On error, this function still attempts to write as much as possible, so it
  426. * might return a positive value less than the requested write size. If the
  427. * function failed to write anything and there was an actual error, it will
  428. * return -1. For streams that support non-blocking operation, if nothing was
  429. * written because it would require blocking, this function returns -2 to
  430. * distinguish that this is not an error and the caller can try again later.
  431. *
  432. * SDL_WriteIO is actually a function wrapper that calls the SDL_IOStream's
  433. * `write` method appropriately, to simplify application development.
  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 freesrc 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_RW(SDL_IOStream *src, size_t *datasize, SDL_bool freesrc);
  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_rwops_h_ */