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.
 
 
 
 
 
 

255 line
7.8 KiB

  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2018 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. * \name Read/write macros
  154. *
  155. * Macros to easily read and write from an SDL_RWops structure.
  156. */
  157. /* @{ */
  158. #define SDL_RWsize(ctx) (ctx)->size(ctx)
  159. #define SDL_RWseek(ctx, offset, whence) (ctx)->seek(ctx, offset, whence)
  160. #define SDL_RWtell(ctx) (ctx)->seek(ctx, 0, RW_SEEK_CUR)
  161. #define SDL_RWread(ctx, ptr, size, n) (ctx)->read(ctx, ptr, size, n)
  162. #define SDL_RWwrite(ctx, ptr, size, n) (ctx)->write(ctx, ptr, size, n)
  163. #define SDL_RWclose(ctx) (ctx)->close(ctx)
  164. /* @} *//* Read/write macros */
  165. /**
  166. * Load all the data from an SDL data stream.
  167. *
  168. * The data is allocated with a zero byte at the end (null terminated)
  169. *
  170. * If \c datasize is not NULL, it is filled with the size of the data read.
  171. *
  172. * If \c freesrc is non-zero, the stream will be closed after being read.
  173. *
  174. * The data should be freed with SDL_free().
  175. *
  176. * \return the data, or NULL if there was an error.
  177. */
  178. extern DECLSPEC void *SDLCALL SDL_LoadFile_RW(SDL_RWops * src, size_t *datasize,
  179. int freesrc);
  180. /**
  181. * Load an entire file.
  182. *
  183. * Convenience macro.
  184. */
  185. #define SDL_LoadFile(file, datasize) SDL_LoadFile_RW(SDL_RWFromFile(file, "rb"), datasize, 1)
  186. /**
  187. * \name Read endian functions
  188. *
  189. * Read an item of the specified endianness and return in native format.
  190. */
  191. /* @{ */
  192. extern DECLSPEC Uint8 SDLCALL SDL_ReadU8(SDL_RWops * src);
  193. extern DECLSPEC Uint16 SDLCALL SDL_ReadLE16(SDL_RWops * src);
  194. extern DECLSPEC Uint16 SDLCALL SDL_ReadBE16(SDL_RWops * src);
  195. extern DECLSPEC Uint32 SDLCALL SDL_ReadLE32(SDL_RWops * src);
  196. extern DECLSPEC Uint32 SDLCALL SDL_ReadBE32(SDL_RWops * src);
  197. extern DECLSPEC Uint64 SDLCALL SDL_ReadLE64(SDL_RWops * src);
  198. extern DECLSPEC Uint64 SDLCALL SDL_ReadBE64(SDL_RWops * src);
  199. /* @} *//* Read endian functions */
  200. /**
  201. * \name Write endian functions
  202. *
  203. * Write an item of native format to the specified endianness.
  204. */
  205. /* @{ */
  206. extern DECLSPEC size_t SDLCALL SDL_WriteU8(SDL_RWops * dst, Uint8 value);
  207. extern DECLSPEC size_t SDLCALL SDL_WriteLE16(SDL_RWops * dst, Uint16 value);
  208. extern DECLSPEC size_t SDLCALL SDL_WriteBE16(SDL_RWops * dst, Uint16 value);
  209. extern DECLSPEC size_t SDLCALL SDL_WriteLE32(SDL_RWops * dst, Uint32 value);
  210. extern DECLSPEC size_t SDLCALL SDL_WriteBE32(SDL_RWops * dst, Uint32 value);
  211. extern DECLSPEC size_t SDLCALL SDL_WriteLE64(SDL_RWops * dst, Uint64 value);
  212. extern DECLSPEC size_t SDLCALL SDL_WriteBE64(SDL_RWops * dst, Uint64 value);
  213. /* @} *//* Write endian functions */
  214. /* Ends C function definitions when using C++ */
  215. #ifdef __cplusplus
  216. }
  217. #endif
  218. #include "close_code.h"
  219. #endif /* SDL_rwops_h_ */
  220. /* vi: set ts=4 sw=4 expandtab: */