SDL_rwops.h 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041
  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. /* RWops types */
  35. #define SDL_RWOPS_UNKNOWN 0 /**< Unknown stream type */
  36. #define SDL_RWOPS_WINFILE 1 /**< Win32 file */
  37. #define SDL_RWOPS_STDFILE 2 /**< Stdio file */
  38. #define SDL_RWOPS_JNIFILE 3 /**< Android asset */
  39. #define SDL_RWOPS_MEMORY 4 /**< Memory stream */
  40. #define SDL_RWOPS_MEMORY_RO 5 /**< Read-Only memory stream */
  41. /* RWops status, set by a read or write operation */
  42. #define SDL_RWOPS_STATUS_READY 0 /**< Everything is ready */
  43. #define SDL_RWOPS_STATUS_ERROR 1 /**< Read or write I/O error */
  44. #define SDL_RWOPS_STATUS_EOF 2 /**< End of file */
  45. #define SDL_RWOPS_STATUS_NOT_READY 3 /**< Non blocking I/O, not ready */
  46. #define SDL_RWOPS_STATUS_READONLY 4 /**< Tried to write a read-only buffer */
  47. #define SDL_RWOPS_STATUS_WRITEONLY 5 /**< Tried to read a write-only buffer */
  48. /**
  49. * This is the read/write operation structure -- very basic.
  50. */
  51. typedef struct SDL_RWops
  52. {
  53. /**
  54. * Return the number of bytes in this rwops
  55. *
  56. * \return the total size of the data stream, or -1 on error.
  57. */
  58. Sint64 (SDLCALL *size)(struct SDL_RWops *context);
  59. /**
  60. * Seek to \c offset relative to \c whence, one of stdio's whence values:
  61. * SDL_RW_SEEK_SET, SDL_RW_SEEK_CUR, SDL_RW_SEEK_END
  62. *
  63. * \return the final offset in the data stream, or -1 on error.
  64. */
  65. Sint64 (SDLCALL *seek)(struct SDL_RWops *context, Sint64 offset, int whence);
  66. /**
  67. * Read up to \c size bytes from the data stream to the area pointed
  68. * at by \c ptr.
  69. *
  70. * \return the number of bytes read
  71. */
  72. size_t (SDLCALL *read)(struct SDL_RWops *context, void *ptr, size_t size);
  73. /**
  74. * Write exactly \c size bytes from the area pointed at by \c ptr
  75. * to data stream.
  76. *
  77. * \return the number of bytes written
  78. */
  79. size_t (SDLCALL *write)(struct SDL_RWops *context, const void *ptr, size_t size);
  80. /**
  81. * Close and free an allocated SDL_RWops structure.
  82. *
  83. * \return 0 if successful or -1 on write error when flushing data.
  84. */
  85. int (SDLCALL *close)(struct SDL_RWops *context);
  86. Uint32 type;
  87. Uint32 status;
  88. SDL_PropertiesID props;
  89. union
  90. {
  91. #ifdef SDL_PLATFORM_ANDROID
  92. struct
  93. {
  94. void *asset;
  95. } androidio;
  96. #elif defined(SDL_PLATFORM_WIN32) || defined(SDL_PLATFORM_GDK) || defined(SDL_PLATFORM_WINRT)
  97. struct
  98. {
  99. SDL_bool append;
  100. void *h;
  101. struct
  102. {
  103. void *data;
  104. size_t size;
  105. size_t left;
  106. } buffer;
  107. } windowsio;
  108. #endif
  109. struct
  110. {
  111. SDL_bool autoclose;
  112. void *fp;
  113. } stdio;
  114. struct
  115. {
  116. Uint8 *base;
  117. Uint8 *here;
  118. Uint8 *stop;
  119. } mem;
  120. struct
  121. {
  122. void *data1;
  123. void *data2;
  124. } unknown;
  125. } hidden;
  126. } SDL_RWops;
  127. /**
  128. * \name RWFrom functions
  129. *
  130. * Functions to create SDL_RWops structures from various data streams.
  131. */
  132. /* @{ */
  133. /**
  134. * Use this function to create a new SDL_RWops structure for reading from
  135. * and/or writing to a named file.
  136. *
  137. * The `mode` string is treated roughly the same as in a call to the C
  138. * library's fopen(), even if SDL doesn't happen to use fopen() behind the
  139. * scenes.
  140. *
  141. * Available `mode` strings:
  142. *
  143. * - "r": Open a file for reading. The file must exist.
  144. * - "w": Create an empty file for writing. If a file with the same name
  145. * already exists its content is erased and the file is treated as a new
  146. * empty file.
  147. * - "a": Append to a file. Writing operations append data at the end of the
  148. * file. The file is created if it does not exist.
  149. * - "r+": Open a file for update both reading and writing. The file must
  150. * exist.
  151. * - "w+": Create an empty file for both reading and writing. If a file with
  152. * the same name already exists its content is erased and the file is
  153. * treated as a new empty file.
  154. * - "a+": Open a file for reading and appending. All writing operations are
  155. * performed at the end of the file, protecting the previous content to be
  156. * overwritten. You can reposition (fseek, rewind) the internal pointer to
  157. * anywhere in the file for reading, but writing operations will move it
  158. * back to the end of file. The file is created if it does not exist.
  159. *
  160. * **NOTE**: In order to open a file as a binary file, a "b" character has to
  161. * be included in the `mode` string. This additional "b" character can either
  162. * be appended at the end of the string (thus making the following compound
  163. * modes: "rb", "wb", "ab", "r+b", "w+b", "a+b") or be inserted between the
  164. * letter and the "+" sign for the mixed modes ("rb+", "wb+", "ab+").
  165. * Additional characters may follow the sequence, although they should have no
  166. * effect. For example, "t" is sometimes appended to make explicit the file is
  167. * a text file.
  168. *
  169. * This function supports Unicode filenames, but they must be encoded in UTF-8
  170. * format, regardless of the underlying operating system.
  171. *
  172. * As a fallback, SDL_RWFromFile() will transparently open a matching filename
  173. * in an Android app's `assets`.
  174. *
  175. * Closing the SDL_RWops will close the file handle SDL is holding internally.
  176. *
  177. * \param file a UTF-8 string representing the filename to open
  178. * \param mode an ASCII string representing the mode to be used for opening
  179. * the file.
  180. * \returns a pointer to the SDL_RWops structure that is created, or NULL on
  181. * failure; call SDL_GetError() for more information.
  182. *
  183. * \since This function is available since SDL 3.0.0.
  184. *
  185. * \sa SDL_RWclose
  186. * \sa SDL_RWFromConstMem
  187. * \sa SDL_RWFromMem
  188. * \sa SDL_RWread
  189. * \sa SDL_RWseek
  190. * \sa SDL_RWtell
  191. * \sa SDL_RWwrite
  192. */
  193. extern DECLSPEC SDL_RWops *SDLCALL SDL_RWFromFile(const char *file, const char *mode);
  194. /**
  195. * Use this function to prepare a read-write memory buffer for use with
  196. * SDL_RWops.
  197. *
  198. * This function sets up an SDL_RWops struct based on a memory area of a
  199. * certain size, for both read and write access.
  200. *
  201. * This memory buffer is not copied by the RWops; the pointer you provide must
  202. * remain valid until you close the stream. Closing the stream will not free
  203. * the original buffer.
  204. *
  205. * If you need to make sure the RWops never writes to the memory buffer, you
  206. * should use SDL_RWFromConstMem() with a read-only buffer of memory instead.
  207. *
  208. * \param mem a pointer to a buffer to feed an SDL_RWops stream
  209. * \param size the buffer size, in bytes
  210. * \returns a pointer to a new SDL_RWops structure, or NULL if it fails; call
  211. * SDL_GetError() for more information.
  212. *
  213. * \since This function is available since SDL 3.0.0.
  214. *
  215. * \sa SDL_RWclose
  216. * \sa SDL_RWFromConstMem
  217. * \sa SDL_RWFromFile
  218. * \sa SDL_RWFromMem
  219. * \sa SDL_RWread
  220. * \sa SDL_RWseek
  221. * \sa SDL_RWtell
  222. * \sa SDL_RWwrite
  223. */
  224. extern DECLSPEC SDL_RWops *SDLCALL SDL_RWFromMem(void *mem, size_t size);
  225. /**
  226. * Use this function to prepare a read-only memory buffer for use with RWops.
  227. *
  228. * This function sets up an SDL_RWops struct based on a memory area of a
  229. * certain size. It assumes the memory area is not writable.
  230. *
  231. * Attempting to write to this RWops stream will report an error without
  232. * writing to the memory buffer.
  233. *
  234. * This memory buffer is not copied by the RWops; the pointer you provide must
  235. * remain valid until you close the stream. Closing the stream will not free
  236. * the original buffer.
  237. *
  238. * If you need to write to a memory buffer, you should use SDL_RWFromMem()
  239. * with a writable buffer of memory instead.
  240. *
  241. * \param mem a pointer to a read-only buffer to feed an SDL_RWops stream
  242. * \param size the buffer size, in bytes
  243. * \returns a pointer to a new SDL_RWops structure, or NULL if it fails; call
  244. * SDL_GetError() for more information.
  245. *
  246. * \since This function is available since SDL 3.0.0.
  247. *
  248. * \sa SDL_RWclose
  249. * \sa SDL_RWFromConstMem
  250. * \sa SDL_RWFromFile
  251. * \sa SDL_RWFromMem
  252. * \sa SDL_RWread
  253. * \sa SDL_RWseek
  254. * \sa SDL_RWtell
  255. */
  256. extern DECLSPEC SDL_RWops *SDLCALL SDL_RWFromConstMem(const void *mem, size_t size);
  257. /* @} *//* RWFrom functions */
  258. /**
  259. * Use this function to allocate an empty, unpopulated SDL_RWops structure.
  260. *
  261. * Applications do not need to use this function unless they are providing
  262. * their own SDL_RWops implementation. If you just need an SDL_RWops to
  263. * read/write a common data source, you should use the built-in
  264. * implementations in SDL, like SDL_RWFromFile() or SDL_RWFromMem(), etc.
  265. *
  266. * You must free the returned pointer with SDL_DestroyRW(). Depending on your
  267. * operating system and compiler, there may be a difference between the
  268. * malloc() and free() your program uses and the versions SDL calls
  269. * internally. Trying to mix the two can cause crashing such as segmentation
  270. * faults. Since all SDL_RWops must free themselves when their **close**
  271. * method is called, all SDL_RWops must be allocated through this function, so
  272. * they can all be freed correctly with SDL_DestroyRW().
  273. *
  274. * \returns a pointer to the allocated memory on success, or NULL on failure;
  275. * call SDL_GetError() for more information.
  276. *
  277. * \since This function is available since SDL 3.0.0.
  278. *
  279. * \sa SDL_DestroyRW
  280. */
  281. extern DECLSPEC SDL_RWops *SDLCALL SDL_CreateRW(void);
  282. /**
  283. * Use this function to free an SDL_RWops structure allocated by
  284. * SDL_CreateRW().
  285. *
  286. * Applications do not need to use this function unless they are providing
  287. * their own SDL_RWops implementation. If you just need an SDL_RWops to
  288. * read/write a common data source, you should use the built-in
  289. * implementations in SDL, like SDL_RWFromFile() or SDL_RWFromMem(), etc, and
  290. * call the **close** method on those SDL_RWops pointers when you are done
  291. * with them.
  292. *
  293. * Only use SDL_DestroyRW() on pointers returned by SDL_CreateRW(). The
  294. * pointer is invalid as soon as this function returns. Any extra memory
  295. * allocated during creation of the SDL_RWops is not freed by SDL_DestroyRW();
  296. * the programmer must be responsible for managing that memory in their
  297. * **close** method.
  298. *
  299. * \param context the SDL_RWops structure to be freed
  300. *
  301. * \since This function is available since SDL 3.0.0.
  302. *
  303. * \sa SDL_CreateRW
  304. */
  305. extern DECLSPEC void SDLCALL SDL_DestroyRW(SDL_RWops *context);
  306. /**
  307. * Get the properties associated with an SDL_RWops.
  308. *
  309. * \param context a pointer to an SDL_RWops structure
  310. * \returns a valid property ID on success or 0 on failure; call
  311. * SDL_GetError() for more information.
  312. *
  313. * \since This function is available since SDL 3.0.0.
  314. *
  315. * \sa SDL_GetProperty
  316. * \sa SDL_SetProperty
  317. */
  318. extern DECLSPEC SDL_PropertiesID SDLCALL SDL_GetRWProperties(SDL_RWops *context);
  319. #define SDL_RW_SEEK_SET 0 /**< Seek from the beginning of data */
  320. #define SDL_RW_SEEK_CUR 1 /**< Seek relative to current read point */
  321. #define SDL_RW_SEEK_END 2 /**< Seek relative to the end of data */
  322. /**
  323. * Use this function to get the size of the data stream in an SDL_RWops.
  324. *
  325. * \param context the SDL_RWops to get the size of the data stream from
  326. * \returns the size of the data stream in the SDL_RWops on success or a
  327. * negative error code on failure; call SDL_GetError() for more
  328. * information.
  329. *
  330. * \since This function is available since SDL 3.0.0.
  331. */
  332. extern DECLSPEC Sint64 SDLCALL SDL_RWsize(SDL_RWops *context);
  333. /**
  334. * Seek within an SDL_RWops data stream.
  335. *
  336. * This function seeks to byte `offset`, relative to `whence`.
  337. *
  338. * `whence` may be any of the following values:
  339. *
  340. * - `SDL_RW_SEEK_SET`: seek from the beginning of data
  341. * - `SDL_RW_SEEK_CUR`: seek relative to current read point
  342. * - `SDL_RW_SEEK_END`: seek relative to the end of data
  343. *
  344. * If this stream can not seek, it will return -1.
  345. *
  346. * SDL_RWseek() is actually a wrapper function that calls the SDL_RWops's
  347. * `seek` method appropriately, to simplify application development.
  348. *
  349. * \param context a pointer to an SDL_RWops structure
  350. * \param offset an offset in bytes, relative to **whence** location; can be
  351. * negative
  352. * \param whence any of `SDL_RW_SEEK_SET`, `SDL_RW_SEEK_CUR`,
  353. * `SDL_RW_SEEK_END`
  354. * \returns the final offset in the data stream after the seek or a negative
  355. * error code on failure; call SDL_GetError() for more information.
  356. *
  357. * \since This function is available since SDL 3.0.0.
  358. *
  359. * \sa SDL_RWclose
  360. * \sa SDL_RWFromConstMem
  361. * \sa SDL_RWFromFile
  362. * \sa SDL_RWFromMem
  363. * \sa SDL_RWread
  364. * \sa SDL_RWtell
  365. * \sa SDL_RWwrite
  366. */
  367. extern DECLSPEC Sint64 SDLCALL SDL_RWseek(SDL_RWops *context, Sint64 offset, int whence);
  368. /**
  369. * Determine the current read/write offset in an SDL_RWops data stream.
  370. *
  371. * SDL_RWtell is actually a wrapper function that calls the SDL_RWops's `seek`
  372. * method, with an offset of 0 bytes from `SDL_RW_SEEK_CUR`, to simplify
  373. * application development.
  374. *
  375. * \param context an SDL_RWops 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_RWclose
  383. * \sa SDL_RWFromConstMem
  384. * \sa SDL_RWFromFile
  385. * \sa SDL_RWFromMem
  386. * \sa SDL_RWread
  387. * \sa SDL_RWseek
  388. * \sa SDL_RWwrite
  389. */
  390. extern DECLSPEC Sint64 SDLCALL SDL_RWtell(SDL_RWops *context);
  391. /**
  392. * Read from a data source.
  393. *
  394. * This function reads up `size` bytes from the data source to the area
  395. * pointed at by `ptr`. This function may read less bytes than requested. It
  396. * will return zero when the data stream is completely read, or -1 on error.
  397. * For streams that support non-blocking operation, if nothing was read
  398. * because it would require blocking, this function returns -2 to distinguish
  399. * that this is not an error or end-of-file, and the caller can try again
  400. * later.
  401. *
  402. * SDL_RWread() is actually a function wrapper that calls the SDL_RWops's
  403. * `read` method appropriately, to simplify application development.
  404. *
  405. * \param context a pointer to an SDL_RWops structure
  406. * \param ptr a pointer to a buffer to read data into
  407. * \param size the number of bytes to read from the data source.
  408. * \returns the number of bytes read, or 0 on end of file or other error.
  409. *
  410. * \since This function is available since SDL 3.0.0.
  411. *
  412. * \sa SDL_RWclose
  413. * \sa SDL_RWFromConstMem
  414. * \sa SDL_RWFromFile
  415. * \sa SDL_RWFromMem
  416. * \sa SDL_RWseek
  417. * \sa SDL_RWwrite
  418. */
  419. extern DECLSPEC size_t SDLCALL SDL_RWread(SDL_RWops *context, void *ptr, size_t size);
  420. /**
  421. * Write to an SDL_RWops data stream.
  422. *
  423. * This function writes exactly `size` bytes from the area pointed at by `ptr`
  424. * to the stream. If this fails for any reason, it'll return less than `size`
  425. * to demonstrate how far the write progressed. On success, it returns `num`.
  426. *
  427. * On error, this function still attempts to write as much as possible, so it
  428. * might return a positive value less than the requested write size. If the
  429. * function failed to write anything and there was an actual error, it will
  430. * return -1. For streams that support non-blocking operation, if nothing was
  431. * written because it would require blocking, this function returns -2 to
  432. * distinguish that this is not an error and the caller can try again later.
  433. *
  434. * SDL_RWwrite is actually a function wrapper that calls the SDL_RWops's
  435. * `write` method appropriately, to simplify application development.
  436. *
  437. * It is an error to specify a negative `size`, but this parameter is signed
  438. * so you definitely cannot overflow the return value on a successful run with
  439. * enormous amounts of data.
  440. *
  441. * \param context a pointer to an SDL_RWops structure
  442. * \param ptr a pointer to a buffer containing data to write
  443. * \param size the number of bytes to write
  444. * \returns the number of bytes written, which will be less than `num` on
  445. * error; call SDL_GetError() for more information.
  446. *
  447. * \since This function is available since SDL 3.0.0.
  448. *
  449. * \sa SDL_RWclose
  450. * \sa SDL_RWFromConstMem
  451. * \sa SDL_RWFromFile
  452. * \sa SDL_RWFromMem
  453. * \sa SDL_RWprint
  454. * \sa SDL_RWread
  455. * \sa SDL_RWseek
  456. */
  457. extern DECLSPEC size_t SDLCALL SDL_RWwrite(SDL_RWops *context, const void *ptr, size_t size);
  458. /**
  459. * Print to an SDL_RWops data stream.
  460. *
  461. * This function does formatted printing to the stream.
  462. *
  463. * \param context a pointer to an SDL_RWops structure
  464. * \param fmt a printf() style format string
  465. * \param ... additional parameters matching % tokens in the `fmt` string, if
  466. * any
  467. * \returns the number of bytes written, or 0 on error; call SDL_GetError()
  468. * for more information.
  469. *
  470. * \since This function is available since SDL 3.0.0.
  471. *
  472. * \sa SDL_RWclose
  473. * \sa SDL_RWFromConstMem
  474. * \sa SDL_RWFromFile
  475. * \sa SDL_RWFromMem
  476. * \sa SDL_RWread
  477. * \sa SDL_RWseek
  478. * \sa SDL_RWwrite
  479. */
  480. extern DECLSPEC size_t SDLCALL SDL_RWprintf(SDL_RWops *context, SDL_PRINTF_FORMAT_STRING const char *fmt, ...) SDL_PRINTF_VARARG_FUNC(2);
  481. /**
  482. * Print to an SDL_RWops data stream.
  483. *
  484. * This function does formatted printing to the stream.
  485. *
  486. * \param context a pointer to an SDL_RWops structure
  487. * \param fmt a printf() style format string
  488. * \param ap a variable argument list
  489. * \returns the number of bytes written, or 0 on error; call SDL_GetError()
  490. * for more information.
  491. *
  492. * \since This function is available since SDL 3.0.0.
  493. *
  494. * \sa SDL_RWclose
  495. * \sa SDL_RWFromConstMem
  496. * \sa SDL_RWFromFile
  497. * \sa SDL_RWFromMem
  498. * \sa SDL_RWread
  499. * \sa SDL_RWseek
  500. * \sa SDL_RWwrite
  501. */
  502. extern DECLSPEC size_t SDLCALL SDL_RWvprintf(SDL_RWops *context, SDL_PRINTF_FORMAT_STRING const char *fmt, va_list ap) SDL_PRINTF_VARARG_FUNCV(2);
  503. /**
  504. * Close and free an allocated SDL_RWops structure.
  505. *
  506. * SDL_RWclose() closes and cleans up the SDL_RWops stream. It releases any
  507. * resources used by the stream and frees the SDL_RWops itself with
  508. * SDL_DestroyRW(). This returns 0 on success, or -1 if the stream failed to
  509. * flush to its output (e.g. to disk).
  510. *
  511. * Note that if this fails to flush the stream to disk, this function reports
  512. * an error, but the SDL_RWops is still invalid once this function returns.
  513. *
  514. * \param context SDL_RWops structure to close
  515. * \returns 0 on success or a negative error code on failure; call
  516. * SDL_GetError() for more information.
  517. *
  518. * \since This function is available since SDL 3.0.0.
  519. *
  520. * \sa SDL_RWFromConstMem
  521. * \sa SDL_RWFromFile
  522. * \sa SDL_RWFromMem
  523. * \sa SDL_RWread
  524. * \sa SDL_RWseek
  525. * \sa SDL_RWwrite
  526. */
  527. extern DECLSPEC int SDLCALL SDL_RWclose(SDL_RWops *context);
  528. /**
  529. * Load all the data from an SDL data stream.
  530. *
  531. * The data is allocated with a zero byte at the end (null terminated) for
  532. * convenience. This extra byte is not included in the value reported via
  533. * `datasize`.
  534. *
  535. * The data should be freed with SDL_free().
  536. *
  537. * \param src the SDL_RWops to read all available data from
  538. * \param datasize if not NULL, will store the number of bytes read
  539. * \param freesrc if SDL_TRUE, calls SDL_RWclose() on `src` before returning,
  540. * even in the case of an error
  541. * \returns the data, or NULL if there was an error.
  542. *
  543. * \since This function is available since SDL 3.0.0.
  544. */
  545. extern DECLSPEC void *SDLCALL SDL_LoadFile_RW(SDL_RWops *src, size_t *datasize, SDL_bool freesrc);
  546. /**
  547. * Load all the data from a file path.
  548. *
  549. * The data is allocated with a zero byte at the end (null terminated) for
  550. * convenience. This extra byte is not included in the value reported via
  551. * `datasize`.
  552. *
  553. * The data should be freed with SDL_free().
  554. *
  555. * \param file the path to read all available data from
  556. * \param datasize if not NULL, will store the number of bytes read
  557. * \returns the data, or NULL if there was an error.
  558. *
  559. * \since This function is available since SDL 3.0.0.
  560. */
  561. extern DECLSPEC void *SDLCALL SDL_LoadFile(const char *file, size_t *datasize);
  562. /**
  563. * \name Read endian functions
  564. *
  565. * Read an item of the specified endianness and return in native format.
  566. */
  567. /* @{ */
  568. /**
  569. * Use this function to read a byte from an SDL_RWops.
  570. *
  571. * \param src the SDL_RWops to read from
  572. * \param value a pointer filled in with the data read
  573. * \returns SDL_TRUE on success or SDL_FALSE on failure; call SDL_GetError()
  574. * for more information.
  575. *
  576. * \since This function is available since SDL 3.0.0.
  577. */
  578. extern DECLSPEC SDL_bool SDLCALL SDL_ReadU8(SDL_RWops *src, Uint8 *value);
  579. /**
  580. * Use this function to read 16 bits of little-endian data from an SDL_RWops
  581. * and 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_ReadU16LE(SDL_RWops *src, Uint16 *value);
  594. /**
  595. * Use this function to read 16 bits of little-endian data from an SDL_RWops
  596. * and 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_ReadS16LE(SDL_RWops *src, Sint16 *value);
  609. /**
  610. * Use this function to read 16 bits of big-endian data from an SDL_RWops and
  611. * 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_ReadU16BE(SDL_RWops *src, Uint16 *value);
  624. /**
  625. * Use this function to read 16 bits of big-endian data from an SDL_RWops and
  626. * 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_ReadS16BE(SDL_RWops *src, Sint16 *value);
  639. /**
  640. * Use this function to read 32 bits of little-endian data from an SDL_RWops
  641. * and 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_ReadU32LE(SDL_RWops *src, Uint32 *value);
  654. /**
  655. * Use this function to read 32 bits of little-endian data from an SDL_RWops
  656. * and 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_ReadS32LE(SDL_RWops *src, Sint32 *value);
  669. /**
  670. * Use this function to read 32 bits of big-endian data from an SDL_RWops and
  671. * 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_ReadU32BE(SDL_RWops *src, Uint32 *value);
  684. /**
  685. * Use this function to read 32 bits of big-endian data from an SDL_RWops and
  686. * 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_ReadS32BE(SDL_RWops *src, Sint32 *value);
  699. /**
  700. * Use this function to read 64 bits of little-endian data from an SDL_RWops
  701. * and 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_ReadU64LE(SDL_RWops *src, Uint64 *value);
  714. /**
  715. * Use this function to read 64 bits of little-endian data from an SDL_RWops
  716. * and 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_ReadS64LE(SDL_RWops *src, Sint64 *value);
  729. /**
  730. * Use this function to read 64 bits of big-endian data from an SDL_RWops and
  731. * return in native format.
  732. *
  733. * SDL byteswaps the data only if necessary, so the data returned will be in
  734. * the native byte order.
  735. *
  736. * \param src the stream from which to read data
  737. * \param value a pointer filled in with the data read
  738. * \returns SDL_TRUE on successful write, SDL_FALSE on failure; call
  739. * SDL_GetError() for more information.
  740. *
  741. * \since This function is available since SDL 3.0.0.
  742. */
  743. extern DECLSPEC SDL_bool SDLCALL SDL_ReadU64BE(SDL_RWops *src, Uint64 *value);
  744. /**
  745. * Use this function to read 64 bits of big-endian data from an SDL_RWops and
  746. * return in native format.
  747. *
  748. * SDL byteswaps the data only if necessary, so the data returned will be in
  749. * the native byte order.
  750. *
  751. * \param src the stream from which to read data
  752. * \param value a pointer filled in with the data read
  753. * \returns SDL_TRUE on successful write, SDL_FALSE on failure; call
  754. * SDL_GetError() for more information.
  755. *
  756. * \since This function is available since SDL 3.0.0.
  757. */
  758. extern DECLSPEC SDL_bool SDLCALL SDL_ReadS64BE(SDL_RWops *src, Sint64 *value);
  759. /* @} *//* Read endian functions */
  760. /**
  761. * \name Write endian functions
  762. *
  763. * Write an item of native format to the specified endianness.
  764. */
  765. /* @{ */
  766. /**
  767. * Use this function to write a byte to an SDL_RWops.
  768. *
  769. * \param dst the SDL_RWops to write to
  770. * \param value the byte value to write
  771. * \returns SDL_TRUE on successful write, SDL_FALSE on failure; call
  772. * SDL_GetError() for more information.
  773. *
  774. * \since This function is available since SDL 3.0.0.
  775. */
  776. extern DECLSPEC SDL_bool SDLCALL SDL_WriteU8(SDL_RWops *dst, Uint8 value);
  777. /**
  778. * Use this function to write 16 bits in native format to an SDL_RWops as
  779. * little-endian data.
  780. *
  781. * SDL byteswaps the data only if necessary, so the application always
  782. * specifies native format, and the data written will be in little-endian
  783. * format.
  784. *
  785. * \param dst the stream to which data will be written
  786. * \param value the data to be written, in native format
  787. * \returns SDL_TRUE on successful write, SDL_FALSE on failure; call
  788. * SDL_GetError() for more information.
  789. *
  790. * \since This function is available since SDL 3.0.0.
  791. */
  792. extern DECLSPEC SDL_bool SDLCALL SDL_WriteU16LE(SDL_RWops *dst, Uint16 value);
  793. /**
  794. * Use this function to write 16 bits in native format to an SDL_RWops as
  795. * little-endian data.
  796. *
  797. * SDL byteswaps the data only if necessary, so the application always
  798. * specifies native format, and the data written will be in little-endian
  799. * 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_WriteS16LE(SDL_RWops *dst, Sint16 value);
  809. /**
  810. * Use this function to write 16 bits in native format to an SDL_RWops as
  811. * big-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 big-endian format.
  815. *
  816. * \param dst the stream to which data will be written
  817. * \param value the data to be written, in native format
  818. * \returns SDL_TRUE on successful write, SDL_FALSE on failure; call
  819. * SDL_GetError() for more information.
  820. *
  821. * \since This function is available since SDL 3.0.0.
  822. */
  823. extern DECLSPEC SDL_bool SDLCALL SDL_WriteU16BE(SDL_RWops *dst, Uint16 value);
  824. /**
  825. * Use this function to write 16 bits in native format to an SDL_RWops as
  826. * big-endian data.
  827. *
  828. * SDL byteswaps the data only if necessary, so the application always
  829. * specifies native format, and the data written will be in big-endian format.
  830. *
  831. * \param dst the stream to which data will be written
  832. * \param value the data to be written, in native format
  833. * \returns SDL_TRUE on successful write, SDL_FALSE on failure; call
  834. * SDL_GetError() for more information.
  835. *
  836. * \since This function is available since SDL 3.0.0.
  837. */
  838. extern DECLSPEC SDL_bool SDLCALL SDL_WriteS16BE(SDL_RWops *dst, Sint16 value);
  839. /**
  840. * Use this function to write 32 bits in native format to an SDL_RWops as
  841. * little-endian data.
  842. *
  843. * SDL byteswaps the data only if necessary, so the application always
  844. * specifies native format, and the data written will be in little-endian
  845. * format.
  846. *
  847. * \param dst the stream to which data will be written
  848. * \param value the data to be written, in native format
  849. * \returns SDL_TRUE on successful write, SDL_FALSE on failure; call
  850. * SDL_GetError() for more information.
  851. *
  852. * \since This function is available since SDL 3.0.0.
  853. */
  854. extern DECLSPEC SDL_bool SDLCALL SDL_WriteU32LE(SDL_RWops *dst, Uint32 value);
  855. /**
  856. * Use this function to write 32 bits in native format to an SDL_RWops as
  857. * little-endian data.
  858. *
  859. * SDL byteswaps the data only if necessary, so the application always
  860. * specifies native format, and the data written will be in little-endian
  861. * 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_WriteS32LE(SDL_RWops *dst, Sint32 value);
  871. /**
  872. * Use this function to write 32 bits in native format to an SDL_RWops as
  873. * big-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 big-endian format.
  877. *
  878. * \param dst the stream to which data will be written
  879. * \param value the data to be written, in native format
  880. * \returns SDL_TRUE on successful write, SDL_FALSE on failure; call
  881. * SDL_GetError() for more information.
  882. *
  883. * \since This function is available since SDL 3.0.0.
  884. */
  885. extern DECLSPEC SDL_bool SDLCALL SDL_WriteU32BE(SDL_RWops *dst, Uint32 value);
  886. /**
  887. * Use this function to write 32 bits in native format to an SDL_RWops as
  888. * big-endian data.
  889. *
  890. * SDL byteswaps the data only if necessary, so the application always
  891. * specifies native format, and the data written will be in big-endian format.
  892. *
  893. * \param dst the stream to which data will be written
  894. * \param value the data to be written, in native format
  895. * \returns SDL_TRUE on successful write, SDL_FALSE on failure; call
  896. * SDL_GetError() for more information.
  897. *
  898. * \since This function is available since SDL 3.0.0.
  899. */
  900. extern DECLSPEC SDL_bool SDLCALL SDL_WriteS32BE(SDL_RWops *dst, Sint32 value);
  901. /**
  902. * Use this function to write 64 bits in native format to an SDL_RWops as
  903. * little-endian data.
  904. *
  905. * SDL byteswaps the data only if necessary, so the application always
  906. * specifies native format, and the data written will be in little-endian
  907. * format.
  908. *
  909. * \param dst the stream to which data will be written
  910. * \param value the data to be written, in native format
  911. * \returns SDL_TRUE on successful write, SDL_FALSE on failure; call
  912. * SDL_GetError() for more information.
  913. *
  914. * \since This function is available since SDL 3.0.0.
  915. */
  916. extern DECLSPEC SDL_bool SDLCALL SDL_WriteU64LE(SDL_RWops *dst, Uint64 value);
  917. /**
  918. * Use this function to write 64 bits in native format to an SDL_RWops as
  919. * little-endian data.
  920. *
  921. * SDL byteswaps the data only if necessary, so the application always
  922. * specifies native format, and the data written will be in little-endian
  923. * 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_WriteS64LE(SDL_RWops *dst, Sint64 value);
  933. /**
  934. * Use this function to write 64 bits in native format to an SDL_RWops as
  935. * big-endian data.
  936. *
  937. * SDL byteswaps the data only if necessary, so the application always
  938. * specifies native format, and the data written will be in big-endian format.
  939. *
  940. * \param dst the stream to which data will be written
  941. * \param value the data to be written, in native format
  942. * \returns SDL_TRUE on successful write, SDL_FALSE on failure; call
  943. * SDL_GetError() for more information.
  944. *
  945. * \since This function is available since SDL 3.0.0.
  946. */
  947. extern DECLSPEC SDL_bool SDLCALL SDL_WriteU64BE(SDL_RWops *dst, Uint64 value);
  948. /**
  949. * Use this function to write 64 bits in native format to an SDL_RWops as
  950. * big-endian data.
  951. *
  952. * SDL byteswaps the data only if necessary, so the application always
  953. * specifies native format, and the data written will be in big-endian format.
  954. *
  955. * \param dst the stream to which data will be written
  956. * \param value the data to be written, in native format
  957. * \returns SDL_TRUE on successful write, SDL_FALSE on failure; call
  958. * SDL_GetError() for more information.
  959. *
  960. * \since This function is available since SDL 3.0.0.
  961. */
  962. extern DECLSPEC SDL_bool SDLCALL SDL_WriteS64BE(SDL_RWops *dst, Sint64 value);
  963. /* @} *//* Write endian functions */
  964. /* Ends C function definitions when using C++ */
  965. #ifdef __cplusplus
  966. }
  967. #endif
  968. #include <SDL3/SDL_close_code.h>
  969. #endif /* SDL_rwops_h_ */