🛠️🐜 Antkeeper superbuild with dependencies included https://antkeeper.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

291 lines
9.0 KiB

  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2019 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 *fileNameRef;
  86. void *inputStreamRef;
  87. void *readableByteChannelRef;
  88. void *readMethod;
  89. void *assetFileDescriptorRef;
  90. long position;
  91. long size;
  92. long offset;
  93. int fd;
  94. } androidio;
  95. #elif defined(__WIN32__)
  96. struct
  97. {
  98. SDL_bool append;
  99. void *h;
  100. struct
  101. {
  102. void *data;
  103. size_t size;
  104. size_t left;
  105. } buffer;
  106. } windowsio;
  107. #endif
  108. #ifdef HAVE_STDIO_H
  109. struct
  110. {
  111. SDL_bool autoclose;
  112. FILE *fp;
  113. } stdio;
  114. #endif
  115. struct
  116. {
  117. Uint8 *base;
  118. Uint8 *here;
  119. Uint8 *stop;
  120. } mem;
  121. struct
  122. {
  123. void *data1;
  124. void *data2;
  125. } unknown;
  126. } hidden;
  127. } SDL_RWops;
  128. /**
  129. * \name RWFrom functions
  130. *
  131. * Functions to create SDL_RWops structures from various data streams.
  132. */
  133. /* @{ */
  134. extern DECLSPEC SDL_RWops *SDLCALL SDL_RWFromFile(const char *file,
  135. const char *mode);
  136. #ifdef HAVE_STDIO_H
  137. extern DECLSPEC SDL_RWops *SDLCALL SDL_RWFromFP(FILE * fp,
  138. SDL_bool autoclose);
  139. #else
  140. extern DECLSPEC SDL_RWops *SDLCALL SDL_RWFromFP(void * fp,
  141. SDL_bool autoclose);
  142. #endif
  143. extern DECLSPEC SDL_RWops *SDLCALL SDL_RWFromMem(void *mem, int size);
  144. extern DECLSPEC SDL_RWops *SDLCALL SDL_RWFromConstMem(const void *mem,
  145. int size);
  146. /* @} *//* RWFrom functions */
  147. extern DECLSPEC SDL_RWops *SDLCALL SDL_AllocRW(void);
  148. extern DECLSPEC void SDLCALL SDL_FreeRW(SDL_RWops * area);
  149. #define RW_SEEK_SET 0 /**< Seek from the beginning of data */
  150. #define RW_SEEK_CUR 1 /**< Seek relative to current read point */
  151. #define RW_SEEK_END 2 /**< Seek relative to the end of data */
  152. /**
  153. * Return the size of the file in this rwops, or -1 if unknown
  154. */
  155. extern DECLSPEC Sint64 SDLCALL SDL_RWsize(SDL_RWops *context);
  156. /**
  157. * Seek to \c offset relative to \c whence, one of stdio's whence values:
  158. * RW_SEEK_SET, RW_SEEK_CUR, RW_SEEK_END
  159. *
  160. * \return the final offset in the data stream, or -1 on error.
  161. */
  162. extern DECLSPEC Sint64 SDLCALL SDL_RWseek(SDL_RWops *context,
  163. Sint64 offset, int whence);
  164. /**
  165. * Return the current offset in the data stream, or -1 on error.
  166. */
  167. extern DECLSPEC Sint64 SDLCALL SDL_RWtell(SDL_RWops *context);
  168. /**
  169. * Read up to \c maxnum objects each of size \c size from the data
  170. * stream to the area pointed at by \c ptr.
  171. *
  172. * \return the number of objects read, or 0 at error or end of file.
  173. */
  174. extern DECLSPEC size_t SDLCALL SDL_RWread(SDL_RWops *context,
  175. void *ptr, size_t size, size_t maxnum);
  176. /**
  177. * Write exactly \c num objects each of size \c size from the area
  178. * pointed at by \c ptr to data stream.
  179. *
  180. * \return the number of objects written, or 0 at error or end of file.
  181. */
  182. extern DECLSPEC size_t SDLCALL SDL_RWwrite(SDL_RWops *context,
  183. const void *ptr, size_t size, size_t num);
  184. /**
  185. * Close and free an allocated SDL_RWops structure.
  186. *
  187. * \return 0 if successful or -1 on write error when flushing data.
  188. */
  189. extern DECLSPEC int SDLCALL SDL_RWclose(SDL_RWops *context);
  190. /**
  191. * Load all the data from an SDL data stream.
  192. *
  193. * The data is allocated with a zero byte at the end (null terminated)
  194. *
  195. * If \c datasize is not NULL, it is filled with the size of the data read.
  196. *
  197. * If \c freesrc is non-zero, the stream will be closed after being read.
  198. *
  199. * The data should be freed with SDL_free().
  200. *
  201. * \return the data, or NULL if there was an error.
  202. */
  203. extern DECLSPEC void *SDLCALL SDL_LoadFile_RW(SDL_RWops * src, size_t *datasize,
  204. int freesrc);
  205. /**
  206. * Load an entire file.
  207. *
  208. * The data is allocated with a zero byte at the end (null terminated)
  209. *
  210. * If \c datasize is not NULL, it is filled with the size of the data read.
  211. *
  212. * If \c freesrc is non-zero, the stream will be closed after being read.
  213. *
  214. * The data should be freed with SDL_free().
  215. *
  216. * \return the data, or NULL if there was an error.
  217. */
  218. extern DECLSPEC void *SDLCALL SDL_LoadFile(const char *file, size_t *datasize);
  219. /**
  220. * \name Read endian functions
  221. *
  222. * Read an item of the specified endianness and return in native format.
  223. */
  224. /* @{ */
  225. extern DECLSPEC Uint8 SDLCALL SDL_ReadU8(SDL_RWops * src);
  226. extern DECLSPEC Uint16 SDLCALL SDL_ReadLE16(SDL_RWops * src);
  227. extern DECLSPEC Uint16 SDLCALL SDL_ReadBE16(SDL_RWops * src);
  228. extern DECLSPEC Uint32 SDLCALL SDL_ReadLE32(SDL_RWops * src);
  229. extern DECLSPEC Uint32 SDLCALL SDL_ReadBE32(SDL_RWops * src);
  230. extern DECLSPEC Uint64 SDLCALL SDL_ReadLE64(SDL_RWops * src);
  231. extern DECLSPEC Uint64 SDLCALL SDL_ReadBE64(SDL_RWops * src);
  232. /* @} *//* Read endian functions */
  233. /**
  234. * \name Write endian functions
  235. *
  236. * Write an item of native format to the specified endianness.
  237. */
  238. /* @{ */
  239. extern DECLSPEC size_t SDLCALL SDL_WriteU8(SDL_RWops * dst, Uint8 value);
  240. extern DECLSPEC size_t SDLCALL SDL_WriteLE16(SDL_RWops * dst, Uint16 value);
  241. extern DECLSPEC size_t SDLCALL SDL_WriteBE16(SDL_RWops * dst, Uint16 value);
  242. extern DECLSPEC size_t SDLCALL SDL_WriteLE32(SDL_RWops * dst, Uint32 value);
  243. extern DECLSPEC size_t SDLCALL SDL_WriteBE32(SDL_RWops * dst, Uint32 value);
  244. extern DECLSPEC size_t SDLCALL SDL_WriteLE64(SDL_RWops * dst, Uint64 value);
  245. extern DECLSPEC size_t SDLCALL SDL_WriteBE64(SDL_RWops * dst, Uint64 value);
  246. /* @} *//* Write endian functions */
  247. /* Ends C function definitions when using C++ */
  248. #ifdef __cplusplus
  249. }
  250. #endif
  251. #include "close_code.h"
  252. #endif /* SDL_rwops_h_ */
  253. /* vi: set ts=4 sw=4 expandtab: */