SDL_iostream.h 39 KB

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