SDL_rwops.h 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2020 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 "SDL_stdinc.h"
  27. #include "SDL_error.h"
  28. #include "begin_code.h"
  29. /* Set up for C function definitions, even when using C++ */
  30. #ifdef __cplusplus
  31. extern "C" {
  32. #endif
  33. /* RWops Types */
  34. #define SDL_RWOPS_UNKNOWN 0U /**< Unknown stream type */
  35. #define SDL_RWOPS_WINFILE 1U /**< Win32 file */
  36. #define SDL_RWOPS_STDFILE 2U /**< Stdio file */
  37. #define SDL_RWOPS_JNIFILE 3U /**< Android asset */
  38. #define SDL_RWOPS_MEMORY 4U /**< Memory stream */
  39. #define SDL_RWOPS_MEMORY_RO 5U /**< Read-Only memory stream */
  40. /**
  41. * This is the read/write operation structure -- very basic.
  42. */
  43. typedef struct SDL_RWops
  44. {
  45. /**
  46. * Return the size of the file in this rwops, or -1 if unknown
  47. */
  48. Sint64 (SDLCALL * size) (struct SDL_RWops * context);
  49. /**
  50. * Seek to \c offset relative to \c whence, one of stdio's whence values:
  51. * RW_SEEK_SET, RW_SEEK_CUR, RW_SEEK_END
  52. *
  53. * \return the final offset in the data stream, or -1 on error.
  54. */
  55. Sint64 (SDLCALL * seek) (struct SDL_RWops * context, Sint64 offset,
  56. int whence);
  57. /**
  58. * Read up to \c maxnum objects each of size \c size from the data
  59. * stream to the area pointed at by \c ptr.
  60. *
  61. * \return the number of objects read, or 0 at error or end of file.
  62. */
  63. size_t (SDLCALL * read) (struct SDL_RWops * context, void *ptr,
  64. size_t size, size_t maxnum);
  65. /**
  66. * Write exactly \c num objects each of size \c size from the area
  67. * pointed at by \c ptr to data stream.
  68. *
  69. * \return the number of objects written, or 0 at error or end of file.
  70. */
  71. size_t (SDLCALL * write) (struct SDL_RWops * context, const void *ptr,
  72. size_t size, size_t num);
  73. /**
  74. * Close and free an allocated SDL_RWops structure.
  75. *
  76. * \return 0 if successful or -1 on write error when flushing data.
  77. */
  78. int (SDLCALL * close) (struct SDL_RWops * context);
  79. Uint32 type;
  80. union
  81. {
  82. #if defined(__ANDROID__)
  83. struct
  84. {
  85. void *asset;
  86. } androidio;
  87. #elif defined(__WIN32__)
  88. struct
  89. {
  90. SDL_bool append;
  91. void *h;
  92. struct
  93. {
  94. void *data;
  95. size_t size;
  96. size_t left;
  97. } buffer;
  98. } windowsio;
  99. #endif
  100. #ifdef HAVE_STDIO_H
  101. struct
  102. {
  103. SDL_bool autoclose;
  104. FILE *fp;
  105. } stdio;
  106. #endif
  107. struct
  108. {
  109. Uint8 *base;
  110. Uint8 *here;
  111. Uint8 *stop;
  112. } mem;
  113. struct
  114. {
  115. void *data1;
  116. void *data2;
  117. } unknown;
  118. } hidden;
  119. } SDL_RWops;
  120. /**
  121. * \name RWFrom functions
  122. *
  123. * Functions to create SDL_RWops structures from various data streams.
  124. */
  125. /* @{ */
  126. extern DECLSPEC SDL_RWops *SDLCALL SDL_RWFromFile(const char *file,
  127. const char *mode);
  128. #ifdef HAVE_STDIO_H
  129. extern DECLSPEC SDL_RWops *SDLCALL SDL_RWFromFP(FILE * fp,
  130. SDL_bool autoclose);
  131. #else
  132. extern DECLSPEC SDL_RWops *SDLCALL SDL_RWFromFP(void * fp,
  133. SDL_bool autoclose);
  134. #endif
  135. extern DECLSPEC SDL_RWops *SDLCALL SDL_RWFromMem(void *mem, int size);
  136. extern DECLSPEC SDL_RWops *SDLCALL SDL_RWFromConstMem(const void *mem,
  137. int size);
  138. /* @} *//* RWFrom functions */
  139. extern DECLSPEC SDL_RWops *SDLCALL SDL_AllocRW(void);
  140. extern DECLSPEC void SDLCALL SDL_FreeRW(SDL_RWops * area);
  141. #define RW_SEEK_SET 0 /**< Seek from the beginning of data */
  142. #define RW_SEEK_CUR 1 /**< Seek relative to current read point */
  143. #define RW_SEEK_END 2 /**< Seek relative to the end of data */
  144. /**
  145. * Return the size of the file in this rwops, or -1 if unknown
  146. */
  147. extern DECLSPEC Sint64 SDLCALL SDL_RWsize(SDL_RWops *context);
  148. /**
  149. * Seek to \c offset relative to \c whence, one of stdio's whence values:
  150. * RW_SEEK_SET, RW_SEEK_CUR, RW_SEEK_END
  151. *
  152. * \return the final offset in the data stream, or -1 on error.
  153. */
  154. extern DECLSPEC Sint64 SDLCALL SDL_RWseek(SDL_RWops *context,
  155. Sint64 offset, int whence);
  156. /**
  157. * Return the current offset in the data stream, or -1 on error.
  158. */
  159. extern DECLSPEC Sint64 SDLCALL SDL_RWtell(SDL_RWops *context);
  160. /**
  161. * Read up to \c maxnum objects each of size \c size from the data
  162. * stream to the area pointed at by \c ptr.
  163. *
  164. * \return the number of objects read, or 0 at error or end of file.
  165. */
  166. extern DECLSPEC size_t SDLCALL SDL_RWread(SDL_RWops *context,
  167. void *ptr, size_t size, size_t maxnum);
  168. /**
  169. * Write exactly \c num objects each of size \c size from the area
  170. * pointed at by \c ptr to data stream.
  171. *
  172. * \return the number of objects written, or 0 at error or end of file.
  173. */
  174. extern DECLSPEC size_t SDLCALL SDL_RWwrite(SDL_RWops *context,
  175. const void *ptr, size_t size, size_t num);
  176. /**
  177. * Close and free an allocated SDL_RWops structure.
  178. *
  179. * \return 0 if successful or -1 on write error when flushing data.
  180. */
  181. extern DECLSPEC int SDLCALL SDL_RWclose(SDL_RWops *context);
  182. /**
  183. * Load all the data from an SDL data stream.
  184. *
  185. * The data is allocated with a zero byte at the end (null terminated)
  186. *
  187. * If \c datasize is not NULL, it is filled with the size of the data read.
  188. *
  189. * If \c freesrc is non-zero, the stream will be closed after being read.
  190. *
  191. * The data should be freed with SDL_free().
  192. *
  193. * \return the data, or NULL if there was an error.
  194. */
  195. extern DECLSPEC void *SDLCALL SDL_LoadFile_RW(SDL_RWops * src, size_t *datasize,
  196. int freesrc);
  197. /**
  198. * Load an entire file.
  199. *
  200. * The data is allocated with a zero byte at the end (null terminated)
  201. *
  202. * If \c datasize is not NULL, it is filled with the size of the data read.
  203. *
  204. * If \c freesrc is non-zero, the stream will be closed after being read.
  205. *
  206. * The data should be freed with SDL_free().
  207. *
  208. * \return the data, or NULL if there was an error.
  209. */
  210. extern DECLSPEC void *SDLCALL SDL_LoadFile(const char *file, size_t *datasize);
  211. /**
  212. * \name Read endian functions
  213. *
  214. * Read an item of the specified endianness and return in native format.
  215. */
  216. /* @{ */
  217. extern DECLSPEC Uint8 SDLCALL SDL_ReadU8(SDL_RWops * src);
  218. extern DECLSPEC Uint16 SDLCALL SDL_ReadLE16(SDL_RWops * src);
  219. extern DECLSPEC Uint16 SDLCALL SDL_ReadBE16(SDL_RWops * src);
  220. extern DECLSPEC Uint32 SDLCALL SDL_ReadLE32(SDL_RWops * src);
  221. extern DECLSPEC Uint32 SDLCALL SDL_ReadBE32(SDL_RWops * src);
  222. extern DECLSPEC Uint64 SDLCALL SDL_ReadLE64(SDL_RWops * src);
  223. extern DECLSPEC Uint64 SDLCALL SDL_ReadBE64(SDL_RWops * src);
  224. /* @} *//* Read endian functions */
  225. /**
  226. * \name Write endian functions
  227. *
  228. * Write an item of native format to the specified endianness.
  229. */
  230. /* @{ */
  231. extern DECLSPEC size_t SDLCALL SDL_WriteU8(SDL_RWops * dst, Uint8 value);
  232. extern DECLSPEC size_t SDLCALL SDL_WriteLE16(SDL_RWops * dst, Uint16 value);
  233. extern DECLSPEC size_t SDLCALL SDL_WriteBE16(SDL_RWops * dst, Uint16 value);
  234. extern DECLSPEC size_t SDLCALL SDL_WriteLE32(SDL_RWops * dst, Uint32 value);
  235. extern DECLSPEC size_t SDLCALL SDL_WriteBE32(SDL_RWops * dst, Uint32 value);
  236. extern DECLSPEC size_t SDLCALL SDL_WriteLE64(SDL_RWops * dst, Uint64 value);
  237. extern DECLSPEC size_t SDLCALL SDL_WriteBE64(SDL_RWops * dst, Uint64 value);
  238. /* @} *//* Write endian functions */
  239. /* Ends C function definitions when using C++ */
  240. #ifdef __cplusplus
  241. }
  242. #endif
  243. #include "close_code.h"
  244. #endif /* SDL_rwops_h_ */
  245. /* vi: set ts=4 sw=4 expandtab: */