SDL_rwops.h 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2025 Sam Lantinga <slouken@libsdl.org>
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any damages
  6. arising from the use of this software.
  7. Permission is granted to anyone to use this software for any purpose,
  8. including commercial applications, and to alter it and redistribute it
  9. freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you must not
  11. claim that you wrote the original software. If you use this software
  12. in a product, an acknowledgment in the product documentation would be
  13. appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and must not be
  15. misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source distribution.
  17. */
  18. /* WIKI CATEGORY: RWOPS */
  19. /**
  20. * # CategoryRWOPS
  21. *
  22. * This file provides a general interface for SDL to read and write data
  23. * streams. It can easily be extended to files, memory, etc.
  24. */
  25. #ifndef SDL_rwops_h_
  26. #define SDL_rwops_h_
  27. #include "SDL_stdinc.h"
  28. #include "SDL_error.h"
  29. #include "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 0U /**< Unknown stream type */
  36. #define SDL_RWOPS_WINFILE 1U /**< Win32 file */
  37. #define SDL_RWOPS_STDFILE 2U /**< Stdio file */
  38. #define SDL_RWOPS_JNIFILE 3U /**< Android asset */
  39. #define SDL_RWOPS_MEMORY 4U /**< Memory stream */
  40. #define SDL_RWOPS_MEMORY_RO 5U /**< Read-Only memory stream */
  41. /**
  42. * This is the read/write operation structure -- very basic.
  43. */
  44. typedef struct SDL_RWops
  45. {
  46. /**
  47. * Return the size of the file in this rwops, or -1 if unknown
  48. */
  49. Sint64 (SDLCALL * size) (struct SDL_RWops * context);
  50. /**
  51. * Seek to `offset` relative to `whence`, one of stdio's whence values:
  52. * RW_SEEK_SET, RW_SEEK_CUR, RW_SEEK_END
  53. *
  54. * \return the final offset in the data stream, or -1 on error.
  55. */
  56. Sint64 (SDLCALL * seek) (struct SDL_RWops * context, Sint64 offset,
  57. int whence);
  58. /**
  59. * Read up to `maxnum` objects each of size `size` from the data
  60. * stream to the area pointed at by `ptr`.
  61. *
  62. * \return the number of objects read, or 0 at error or end of file.
  63. */
  64. size_t (SDLCALL * read) (struct SDL_RWops * context, void *ptr,
  65. size_t size, size_t maxnum);
  66. /**
  67. * Write exactly `num` objects each of size `size` from the area
  68. * pointed at by `ptr` to data stream.
  69. *
  70. * \return the number of objects written, or 0 at error or end of file.
  71. */
  72. size_t (SDLCALL * write) (struct SDL_RWops * context, const void *ptr,
  73. size_t size, size_t num);
  74. /**
  75. * Close and free an allocated SDL_RWops structure.
  76. *
  77. * \return 0 if successful or -1 on write error when flushing data.
  78. */
  79. int (SDLCALL * close) (struct SDL_RWops * context);
  80. Uint32 type;
  81. union
  82. {
  83. #if defined(__ANDROID__)
  84. struct
  85. {
  86. void *asset;
  87. } androidio;
  88. #elif defined(__WIN32__) || defined(__GDK__)
  89. struct
  90. {
  91. SDL_bool append;
  92. void *h;
  93. struct
  94. {
  95. void *data;
  96. size_t size;
  97. size_t left;
  98. } buffer;
  99. } windowsio;
  100. #endif
  101. #ifdef HAVE_STDIO_H
  102. struct
  103. {
  104. SDL_bool autoclose;
  105. FILE *fp;
  106. } stdio;
  107. #endif
  108. struct
  109. {
  110. Uint8 *base;
  111. Uint8 *here;
  112. Uint8 *stop;
  113. } mem;
  114. struct
  115. {
  116. void *data1;
  117. void *data2;
  118. } unknown;
  119. } hidden;
  120. } SDL_RWops;
  121. /**
  122. * \name RWFrom functions
  123. *
  124. * Functions to create SDL_RWops structures from various data streams.
  125. */
  126. /* @{ */
  127. /**
  128. * Use this function to create a new SDL_RWops structure for reading from
  129. * and/or writing to a named file.
  130. *
  131. * The `mode` string is treated roughly the same as in a call to the C
  132. * library's fopen(), even if SDL doesn't happen to use fopen() behind the
  133. * scenes.
  134. *
  135. * Available `mode` strings:
  136. *
  137. * - "r": Open a file for reading. The file must exist.
  138. * - "w": Create an empty file for writing. If a file with the same name
  139. * already exists its content is erased and the file is treated as a new
  140. * empty file.
  141. * - "a": Append to a file. Writing operations append data at the end of the
  142. * file. The file is created if it does not exist.
  143. * - "r+": Open a file for update both reading and writing. The file must
  144. * exist.
  145. * - "w+": Create an empty file for both reading and writing. If a file with
  146. * the same name already exists its content is erased and the file is
  147. * treated as a new empty file.
  148. * - "a+": Open a file for reading and appending. All writing operations are
  149. * performed at the end of the file, protecting the previous content to be
  150. * overwritten. You can reposition (fseek, rewind) the internal pointer to
  151. * anywhere in the file for reading, but writing operations will move it
  152. * back to the end of file. The file is created if it does not exist.
  153. *
  154. * **NOTE**: In order to open a file as a binary file, a "b" character has to
  155. * be included in the `mode` string. This additional "b" character can either
  156. * be appended at the end of the string (thus making the following compound
  157. * modes: "rb", "wb", "ab", "r+b", "w+b", "a+b") or be inserted between the
  158. * letter and the "+" sign for the mixed modes ("rb+", "wb+", "ab+").
  159. * Additional characters may follow the sequence, although they should have no
  160. * effect. For example, "t" is sometimes appended to make explicit the file is
  161. * a text file.
  162. *
  163. * This function supports Unicode filenames, but they must be encoded in UTF-8
  164. * format, regardless of the underlying operating system.
  165. *
  166. * As a fallback, SDL_RWFromFile() will transparently open a matching filename
  167. * in an Android app's `assets`.
  168. *
  169. * Closing the SDL_RWops will close the file handle SDL is holding internally.
  170. *
  171. * \param file a UTF-8 string representing the filename to open.
  172. * \param mode an ASCII string representing the mode to be used for opening
  173. * the file.
  174. * \returns a pointer to the SDL_RWops structure that is created, or NULL on
  175. * failure; call SDL_GetError() for more information.
  176. *
  177. * \since This function is available since SDL 2.0.0.
  178. *
  179. * \sa SDL_RWclose
  180. * \sa SDL_RWFromConstMem
  181. * \sa SDL_RWFromFP
  182. * \sa SDL_RWFromMem
  183. * \sa SDL_RWread
  184. * \sa SDL_RWseek
  185. * \sa SDL_RWtell
  186. * \sa SDL_RWwrite
  187. */
  188. extern DECLSPEC SDL_RWops *SDLCALL SDL_RWFromFile(const char *file,
  189. const char *mode);
  190. #ifdef HAVE_STDIO_H
  191. extern DECLSPEC SDL_RWops *SDLCALL SDL_RWFromFP(FILE * fp, SDL_bool autoclose);
  192. #else
  193. /**
  194. * Use this function to create an SDL_RWops structure from a standard I/O file
  195. * pointer (stdio.h's `FILE*`).
  196. *
  197. * This function is not available on Windows, since files opened in an
  198. * application on that platform cannot be used by a dynamically linked
  199. * library.
  200. *
  201. * On some platforms, the first parameter is a `void*`, on others, it's a
  202. * `FILE*`, depending on what system headers are available to SDL. It is
  203. * always intended to be the `FILE*` type from the C runtime's stdio.h.
  204. *
  205. * \param fp the `FILE*` that feeds the SDL_RWops stream.
  206. * \param autoclose SDL_TRUE to close the `FILE*` when closing the SDL_RWops,
  207. * SDL_FALSE to leave the `FILE*` open when the RWops is
  208. * closed.
  209. * \returns a pointer to the SDL_RWops structure that is created, or NULL on
  210. * failure; call SDL_GetError() for more information.
  211. *
  212. * \since This function is available since SDL 2.0.0.
  213. *
  214. * \sa SDL_RWclose
  215. * \sa SDL_RWFromConstMem
  216. * \sa SDL_RWFromFile
  217. * \sa SDL_RWFromMem
  218. * \sa SDL_RWread
  219. * \sa SDL_RWseek
  220. * \sa SDL_RWtell
  221. * \sa SDL_RWwrite
  222. */
  223. extern DECLSPEC SDL_RWops *SDLCALL SDL_RWFromFP(void * fp,
  224. SDL_bool autoclose);
  225. #endif
  226. /**
  227. * Use this function to prepare a read-write memory buffer for use with
  228. * SDL_RWops.
  229. *
  230. * This function sets up an SDL_RWops struct based on a memory area of a
  231. * certain size, for both read and write access.
  232. *
  233. * This memory buffer is not copied by the RWops; the pointer you provide must
  234. * remain valid until you close the stream. Closing the stream will not free
  235. * the original buffer.
  236. *
  237. * If you need to make sure the RWops never writes to the memory buffer, you
  238. * should use SDL_RWFromConstMem() with a read-only buffer of memory instead.
  239. *
  240. * \param mem a pointer to a buffer to feed an SDL_RWops stream.
  241. * \param size the buffer size, in bytes.
  242. * \returns a pointer to a new SDL_RWops structure, or NULL if it fails; call
  243. * SDL_GetError() for more information.
  244. *
  245. * \since This function is available since SDL 2.0.0.
  246. *
  247. * \sa SDL_RWclose
  248. * \sa SDL_RWFromConstMem
  249. * \sa SDL_RWFromFile
  250. * \sa SDL_RWFromFP
  251. * \sa SDL_RWFromMem
  252. * \sa SDL_RWread
  253. * \sa SDL_RWseek
  254. * \sa SDL_RWtell
  255. * \sa SDL_RWwrite
  256. */
  257. extern DECLSPEC SDL_RWops *SDLCALL SDL_RWFromMem(void *mem, int size);
  258. /**
  259. * Use this function to prepare a read-only memory buffer for use with RWops.
  260. *
  261. * This function sets up an SDL_RWops struct based on a memory area of a
  262. * certain size. It assumes the memory area is not writable.
  263. *
  264. * Attempting to write to this RWops stream will report an error without
  265. * writing to the memory buffer.
  266. *
  267. * This memory buffer is not copied by the RWops; the pointer you provide must
  268. * remain valid until you close the stream. Closing the stream will not free
  269. * the original buffer.
  270. *
  271. * If you need to write to a memory buffer, you should use SDL_RWFromMem()
  272. * with a writable buffer of memory instead.
  273. *
  274. * \param mem a pointer to a read-only buffer to feed an SDL_RWops stream.
  275. * \param size the buffer size, in bytes.
  276. * \returns a pointer to a new SDL_RWops structure, or NULL if it fails; call
  277. * SDL_GetError() for more information.
  278. *
  279. * \since This function is available since SDL 2.0.0.
  280. *
  281. * \sa SDL_RWclose
  282. * \sa SDL_RWFromConstMem
  283. * \sa SDL_RWFromFile
  284. * \sa SDL_RWFromFP
  285. * \sa SDL_RWFromMem
  286. * \sa SDL_RWread
  287. * \sa SDL_RWseek
  288. * \sa SDL_RWtell
  289. */
  290. extern DECLSPEC SDL_RWops *SDLCALL SDL_RWFromConstMem(const void *mem,
  291. int size);
  292. /* @} *//* RWFrom functions */
  293. /**
  294. * Use this function to allocate an empty, unpopulated SDL_RWops structure.
  295. *
  296. * Applications do not need to use this function unless they are providing
  297. * their own SDL_RWops implementation. If you just need a SDL_RWops to
  298. * read/write a common data source, you should use the built-in
  299. * implementations in SDL, like SDL_RWFromFile() or SDL_RWFromMem(), etc.
  300. *
  301. * You must free the returned pointer with SDL_FreeRW(). Depending on your
  302. * operating system and compiler, there may be a difference between the
  303. * malloc() and free() your program uses and the versions SDL calls
  304. * internally. Trying to mix the two can cause crashing such as segmentation
  305. * faults. Since all SDL_RWops must free themselves when their **close**
  306. * method is called, all SDL_RWops must be allocated through this function, so
  307. * they can all be freed correctly with SDL_FreeRW().
  308. *
  309. * \returns a pointer to the allocated memory on success, or NULL on failure;
  310. * call SDL_GetError() for more information.
  311. *
  312. * \since This function is available since SDL 2.0.0.
  313. *
  314. * \sa SDL_FreeRW
  315. */
  316. extern DECLSPEC SDL_RWops *SDLCALL SDL_AllocRW(void);
  317. /**
  318. * Use this function to free an SDL_RWops structure allocated by
  319. * SDL_AllocRW().
  320. *
  321. * Applications do not need to use this function unless they are providing
  322. * their own SDL_RWops implementation. If you just need a SDL_RWops to
  323. * read/write a common data source, you should use the built-in
  324. * implementations in SDL, like SDL_RWFromFile() or SDL_RWFromMem(), etc, and
  325. * call the **close** method on those SDL_RWops pointers when you are done
  326. * with them.
  327. *
  328. * Only use SDL_FreeRW() on pointers returned by SDL_AllocRW(). The pointer is
  329. * invalid as soon as this function returns. Any extra memory allocated during
  330. * creation of the SDL_RWops is not freed by SDL_FreeRW(); the programmer must
  331. * be responsible for managing that memory in their **close** method.
  332. *
  333. * \param area the SDL_RWops structure to be freed.
  334. *
  335. * \since This function is available since SDL 2.0.0.
  336. *
  337. * \sa SDL_AllocRW
  338. */
  339. extern DECLSPEC void SDLCALL SDL_FreeRW(SDL_RWops * area);
  340. /* Possible `whence` values for SDL_RWops seeking... */
  341. #define RW_SEEK_SET 0 /**< Seek from the beginning of data */
  342. #define RW_SEEK_CUR 1 /**< Seek relative to current read point */
  343. #define RW_SEEK_END 2 /**< Seek relative to the end of data */
  344. /**
  345. * Use this function to get the size of the data stream in an SDL_RWops.
  346. *
  347. * Prior to SDL 2.0.10, this function was a macro.
  348. *
  349. * \param context the SDL_RWops to get the size of the data stream from.
  350. * \returns the size of the data stream in the SDL_RWops on success, -1 if
  351. * unknown or a negative error code on failure; call SDL_GetError()
  352. * for more information.
  353. *
  354. * \since This function is available since SDL 2.0.10.
  355. */
  356. extern DECLSPEC Sint64 SDLCALL SDL_RWsize(SDL_RWops *context);
  357. /**
  358. * Seek within an SDL_RWops data stream.
  359. *
  360. * This function seeks to byte `offset`, relative to `whence`.
  361. *
  362. * `whence` may be any of the following values:
  363. *
  364. * - `RW_SEEK_SET`: seek from the beginning of data
  365. * - `RW_SEEK_CUR`: seek relative to current read point
  366. * - `RW_SEEK_END`: seek relative to the end of data
  367. *
  368. * If this stream can not seek, it will return -1.
  369. *
  370. * SDL_RWseek() is actually a wrapper function that calls the SDL_RWops's
  371. * `seek` method appropriately, to simplify application development.
  372. *
  373. * Prior to SDL 2.0.10, this function was a macro.
  374. *
  375. * \param context a pointer to an SDL_RWops structure.
  376. * \param offset an offset in bytes, relative to **whence** location; can be
  377. * negative.
  378. * \param whence any of `RW_SEEK_SET`, `RW_SEEK_CUR`, `RW_SEEK_END`.
  379. * \returns the final offset in the data stream after the seek or -1 on error.
  380. *
  381. * \since This function is available since SDL 2.0.10.
  382. *
  383. * \sa SDL_RWclose
  384. * \sa SDL_RWFromConstMem
  385. * \sa SDL_RWFromFile
  386. * \sa SDL_RWFromFP
  387. * \sa SDL_RWFromMem
  388. * \sa SDL_RWread
  389. * \sa SDL_RWtell
  390. * \sa SDL_RWwrite
  391. */
  392. extern DECLSPEC Sint64 SDLCALL SDL_RWseek(SDL_RWops *context,
  393. Sint64 offset, int whence);
  394. /**
  395. * Determine the current read/write offset in an SDL_RWops data stream.
  396. *
  397. * SDL_RWtell is actually a wrapper function that calls the SDL_RWops's `seek`
  398. * method, with an offset of 0 bytes from `RW_SEEK_CUR`, to simplify
  399. * application development.
  400. *
  401. * Prior to SDL 2.0.10, this function was a macro.
  402. *
  403. * \param context a SDL_RWops data stream object from which to get the current
  404. * offset.
  405. * \returns the current offset in the stream, or -1 if the information can not
  406. * be determined.
  407. *
  408. * \since This function is available since SDL 2.0.10.
  409. *
  410. * \sa SDL_RWclose
  411. * \sa SDL_RWFromConstMem
  412. * \sa SDL_RWFromFile
  413. * \sa SDL_RWFromFP
  414. * \sa SDL_RWFromMem
  415. * \sa SDL_RWread
  416. * \sa SDL_RWseek
  417. * \sa SDL_RWwrite
  418. */
  419. extern DECLSPEC Sint64 SDLCALL SDL_RWtell(SDL_RWops *context);
  420. /**
  421. * Read from a data source.
  422. *
  423. * This function reads up to `maxnum` objects each of size `size` from the
  424. * data source to the area pointed at by `ptr`. This function may read less
  425. * objects than requested. It will return zero when there has been an error or
  426. * the data stream is completely read.
  427. *
  428. * SDL_RWread() is actually a function wrapper that calls the SDL_RWops's
  429. * `read` method appropriately, to simplify application development.
  430. *
  431. * Prior to SDL 2.0.10, this function was a macro.
  432. *
  433. * \param context a pointer to an SDL_RWops structure.
  434. * \param ptr a pointer to a buffer to read data into.
  435. * \param size the size of each object to read, in bytes.
  436. * \param maxnum the maximum number of objects to be read.
  437. * \returns the number of objects read, or 0 at error or end of file; call
  438. * SDL_GetError() for more information.
  439. *
  440. * \since This function is available since SDL 2.0.10.
  441. *
  442. * \sa SDL_RWclose
  443. * \sa SDL_RWFromConstMem
  444. * \sa SDL_RWFromFile
  445. * \sa SDL_RWFromFP
  446. * \sa SDL_RWFromMem
  447. * \sa SDL_RWseek
  448. * \sa SDL_RWwrite
  449. */
  450. extern DECLSPEC size_t SDLCALL SDL_RWread(SDL_RWops *context,
  451. void *ptr, size_t size,
  452. size_t maxnum);
  453. /**
  454. * Write to an SDL_RWops data stream.
  455. *
  456. * This function writes exactly `num` objects each of size `size` from the
  457. * area pointed at by `ptr` to the stream. If this fails for any reason, it'll
  458. * return less than `num` to demonstrate how far the write progressed. On
  459. * success, it returns `num`.
  460. *
  461. * SDL_RWwrite is actually a function wrapper that calls the SDL_RWops's
  462. * `write` method appropriately, to simplify application development.
  463. *
  464. * Prior to SDL 2.0.10, this function was a macro.
  465. *
  466. * \param context a pointer to an SDL_RWops structure.
  467. * \param ptr a pointer to a buffer containing data to write.
  468. * \param size the size of an object to write, in bytes.
  469. * \param num the number of objects to write.
  470. * \returns the number of objects written, which will be less than **num** on
  471. * error; call SDL_GetError() for more information.
  472. *
  473. * \since This function is available since SDL 2.0.10.
  474. *
  475. * \sa SDL_RWclose
  476. * \sa SDL_RWFromConstMem
  477. * \sa SDL_RWFromFile
  478. * \sa SDL_RWFromFP
  479. * \sa SDL_RWFromMem
  480. * \sa SDL_RWread
  481. * \sa SDL_RWseek
  482. */
  483. extern DECLSPEC size_t SDLCALL SDL_RWwrite(SDL_RWops *context,
  484. const void *ptr, size_t size,
  485. size_t num);
  486. /**
  487. * Close and free an allocated SDL_RWops structure.
  488. *
  489. * SDL_RWclose() closes and cleans up the SDL_RWops stream. It releases any
  490. * resources used by the stream and frees the SDL_RWops itself with
  491. * SDL_FreeRW(). This returns 0 on success, or -1 if the stream failed to
  492. * flush to its output (e.g. to disk).
  493. *
  494. * Note that if this fails to flush the stream to disk, this function reports
  495. * an error, but the SDL_RWops is still invalid once this function returns.
  496. *
  497. * Prior to SDL 2.0.10, this function was a macro.
  498. *
  499. * \param context SDL_RWops structure to close.
  500. * \returns 0 on success or a negative error code on failure; call
  501. * SDL_GetError() for more information.
  502. *
  503. * \since This function is available since SDL 2.0.10.
  504. *
  505. * \sa SDL_RWFromConstMem
  506. * \sa SDL_RWFromFile
  507. * \sa SDL_RWFromFP
  508. * \sa SDL_RWFromMem
  509. * \sa SDL_RWread
  510. * \sa SDL_RWseek
  511. * \sa SDL_RWwrite
  512. */
  513. extern DECLSPEC int SDLCALL SDL_RWclose(SDL_RWops *context);
  514. /**
  515. * Load all the data from an SDL data stream.
  516. *
  517. * The data is allocated with a zero byte at the end (null terminated) for
  518. * convenience. This extra byte is not included in the value reported via
  519. * `datasize`.
  520. *
  521. * The data should be freed with SDL_free().
  522. *
  523. * \param src the SDL_RWops to read all available data from.
  524. * \param datasize if not NULL, will store the number of bytes read.
  525. * \param freesrc if non-zero, calls SDL_RWclose() on `src` before returning.
  526. * \returns the data, or NULL if there was an error.
  527. *
  528. * \since This function is available since SDL 2.0.6.
  529. */
  530. extern DECLSPEC void *SDLCALL SDL_LoadFile_RW(SDL_RWops *src,
  531. size_t *datasize,
  532. int freesrc);
  533. /**
  534. * Load all the data from a file path.
  535. *
  536. * The data is allocated with a zero byte at the end (null terminated) for
  537. * convenience. This extra byte is not included in the value reported via
  538. * `datasize`.
  539. *
  540. * The data should be freed with SDL_free().
  541. *
  542. * Prior to SDL 2.0.10, this function was a macro wrapping around
  543. * SDL_LoadFile_RW.
  544. *
  545. * \param file the path to read all available data from.
  546. * \param datasize if not NULL, will store the number of bytes read.
  547. * \returns the data, or NULL if there was an error.
  548. *
  549. * \since This function is available since SDL 2.0.10.
  550. */
  551. extern DECLSPEC void *SDLCALL SDL_LoadFile(const char *file, size_t *datasize);
  552. /**
  553. * \name Read endian functions
  554. *
  555. * Read an item of the specified endianness and return in native format.
  556. */
  557. /* @{ */
  558. /**
  559. * Use this function to read a byte from an SDL_RWops.
  560. *
  561. * \param src the SDL_RWops to read from.
  562. * \returns the read byte on success or 0 on failure; call SDL_GetError() for
  563. * more information.
  564. *
  565. * \since This function is available since SDL 2.0.0.
  566. *
  567. * \sa SDL_WriteU8
  568. */
  569. extern DECLSPEC Uint8 SDLCALL SDL_ReadU8(SDL_RWops * src);
  570. /**
  571. * Use this function to read 16 bits of little-endian data from an SDL_RWops
  572. * and return in native format.
  573. *
  574. * SDL byteswaps the data only if necessary, so the data returned will be in
  575. * the native byte order.
  576. *
  577. * \param src the stream from which to read data.
  578. * \returns 16 bits of data in the native byte order of the platform.
  579. *
  580. * \since This function is available since SDL 2.0.0.
  581. *
  582. * \sa SDL_ReadBE16
  583. */
  584. extern DECLSPEC Uint16 SDLCALL SDL_ReadLE16(SDL_RWops * src);
  585. /**
  586. * Use this function to read 16 bits of big-endian data from an SDL_RWops and
  587. * return in native format.
  588. *
  589. * SDL byteswaps the data only if necessary, so the data returned will be in
  590. * the native byte order.
  591. *
  592. * \param src the stream from which to read data.
  593. * \returns 16 bits of data in the native byte order of the platform.
  594. *
  595. * \since This function is available since SDL 2.0.0.
  596. *
  597. * \sa SDL_ReadLE16
  598. */
  599. extern DECLSPEC Uint16 SDLCALL SDL_ReadBE16(SDL_RWops * src);
  600. /**
  601. * Use this function to read 32 bits of little-endian data from an SDL_RWops
  602. * and return in native format.
  603. *
  604. * SDL byteswaps the data only if necessary, so the data returned will be in
  605. * the native byte order.
  606. *
  607. * \param src the stream from which to read data.
  608. * \returns 32 bits of data in the native byte order of the platform.
  609. *
  610. * \since This function is available since SDL 2.0.0.
  611. *
  612. * \sa SDL_ReadBE32
  613. */
  614. extern DECLSPEC Uint32 SDLCALL SDL_ReadLE32(SDL_RWops * src);
  615. /**
  616. * Use this function to read 32 bits of big-endian data from an SDL_RWops and
  617. * return in native format.
  618. *
  619. * SDL byteswaps the data only if necessary, so the data returned will be in
  620. * the native byte order.
  621. *
  622. * \param src the stream from which to read data.
  623. * \returns 32 bits of data in the native byte order of the platform.
  624. *
  625. * \since This function is available since SDL 2.0.0.
  626. *
  627. * \sa SDL_ReadLE32
  628. */
  629. extern DECLSPEC Uint32 SDLCALL SDL_ReadBE32(SDL_RWops * src);
  630. /**
  631. * Use this function to read 64 bits of little-endian data from an SDL_RWops
  632. * and return in native format.
  633. *
  634. * SDL byteswaps the data only if necessary, so the data returned will be in
  635. * the native byte order.
  636. *
  637. * \param src the stream from which to read data.
  638. * \returns 64 bits of data in the native byte order of the platform.
  639. *
  640. * \since This function is available since SDL 2.0.0.
  641. *
  642. * \sa SDL_ReadBE64
  643. */
  644. extern DECLSPEC Uint64 SDLCALL SDL_ReadLE64(SDL_RWops * src);
  645. /**
  646. * Use this function to read 64 bits of big-endian data from an SDL_RWops and
  647. * return in native format.
  648. *
  649. * SDL byteswaps the data only if necessary, so the data returned will be in
  650. * the native byte order.
  651. *
  652. * \param src the stream from which to read data.
  653. * \returns 64 bits of data in the native byte order of the platform.
  654. *
  655. * \since This function is available since SDL 2.0.0.
  656. *
  657. * \sa SDL_ReadLE64
  658. */
  659. extern DECLSPEC Uint64 SDLCALL SDL_ReadBE64(SDL_RWops * src);
  660. /* @} *//* Read endian functions */
  661. /**
  662. * \name Write endian functions
  663. *
  664. * Write an item of native format to the specified endianness.
  665. */
  666. /* @{ */
  667. /**
  668. * Use this function to write a byte to an SDL_RWops.
  669. *
  670. * \param dst the SDL_RWops to write to.
  671. * \param value the byte value to write.
  672. * \returns 1 on success or 0 on failure; call SDL_GetError() for more
  673. * information.
  674. *
  675. * \since This function is available since SDL 2.0.0.
  676. *
  677. * \sa SDL_ReadU8
  678. */
  679. extern DECLSPEC size_t SDLCALL SDL_WriteU8(SDL_RWops * dst, Uint8 value);
  680. /**
  681. * Use this function to write 16 bits in native format to a SDL_RWops as
  682. * little-endian data.
  683. *
  684. * SDL byteswaps the data only if necessary, so the application always
  685. * specifies native format, and the data written will be in little-endian
  686. * format.
  687. *
  688. * \param dst the stream to which data will be written.
  689. * \param value the data to be written, in native format.
  690. * \returns 1 on successful write, 0 on error.
  691. *
  692. * \since This function is available since SDL 2.0.0.
  693. *
  694. * \sa SDL_WriteBE16
  695. */
  696. extern DECLSPEC size_t SDLCALL SDL_WriteLE16(SDL_RWops * dst, Uint16 value);
  697. /**
  698. * Use this function to write 16 bits in native format to a SDL_RWops as
  699. * big-endian data.
  700. *
  701. * SDL byteswaps the data only if necessary, so the application always
  702. * specifies native format, and the data written will be in big-endian format.
  703. *
  704. * \param dst the stream to which data will be written.
  705. * \param value the data to be written, in native format.
  706. * \returns 1 on successful write, 0 on error.
  707. *
  708. * \since This function is available since SDL 2.0.0.
  709. *
  710. * \sa SDL_WriteLE16
  711. */
  712. extern DECLSPEC size_t SDLCALL SDL_WriteBE16(SDL_RWops * dst, Uint16 value);
  713. /**
  714. * Use this function to write 32 bits in native format to a SDL_RWops as
  715. * little-endian data.
  716. *
  717. * SDL byteswaps the data only if necessary, so the application always
  718. * specifies native format, and the data written will be in little-endian
  719. * format.
  720. *
  721. * \param dst the stream to which data will be written.
  722. * \param value the data to be written, in native format.
  723. * \returns 1 on successful write, 0 on error.
  724. *
  725. * \since This function is available since SDL 2.0.0.
  726. *
  727. * \sa SDL_WriteBE32
  728. */
  729. extern DECLSPEC size_t SDLCALL SDL_WriteLE32(SDL_RWops * dst, Uint32 value);
  730. /**
  731. * Use this function to write 32 bits in native format to a SDL_RWops as
  732. * big-endian data.
  733. *
  734. * SDL byteswaps the data only if necessary, so the application always
  735. * specifies native format, and the data written will be in big-endian format.
  736. *
  737. * \param dst the stream to which data will be written.
  738. * \param value the data to be written, in native format.
  739. * \returns 1 on successful write, 0 on error.
  740. *
  741. * \since This function is available since SDL 2.0.0.
  742. *
  743. * \sa SDL_WriteLE32
  744. */
  745. extern DECLSPEC size_t SDLCALL SDL_WriteBE32(SDL_RWops * dst, Uint32 value);
  746. /**
  747. * Use this function to write 64 bits in native format to a SDL_RWops as
  748. * little-endian data.
  749. *
  750. * SDL byteswaps the data only if necessary, so the application always
  751. * specifies native format, and the data written will be in little-endian
  752. * format.
  753. *
  754. * \param dst the stream to which data will be written.
  755. * \param value the data to be written, in native format.
  756. * \returns 1 on successful write, 0 on error.
  757. *
  758. * \since This function is available since SDL 2.0.0.
  759. *
  760. * \sa SDL_WriteBE64
  761. */
  762. extern DECLSPEC size_t SDLCALL SDL_WriteLE64(SDL_RWops * dst, Uint64 value);
  763. /**
  764. * Use this function to write 64 bits in native format to a SDL_RWops as
  765. * big-endian data.
  766. *
  767. * SDL byteswaps the data only if necessary, so the application always
  768. * specifies native format, and the data written will be in big-endian format.
  769. *
  770. * \param dst the stream to which data will be written.
  771. * \param value the data to be written, in native format.
  772. * \returns 1 on successful write, 0 on error.
  773. *
  774. * \since This function is available since SDL 2.0.0.
  775. *
  776. * \sa SDL_WriteLE64
  777. */
  778. extern DECLSPEC size_t SDLCALL SDL_WriteBE64(SDL_RWops * dst, Uint64 value);
  779. /* @} *//* Write endian functions */
  780. /* Ends C function definitions when using C++ */
  781. #ifdef __cplusplus
  782. }
  783. #endif
  784. #include "close_code.h"
  785. #endif /* SDL_rwops_h_ */
  786. /* vi: set ts=4 sw=4 expandtab: */