SDL_rwops.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2014 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 0 /* Unknown stream type */
  35. #define SDL_RWOPS_WINFILE 1 /* Win32 file */
  36. #define SDL_RWOPS_STDFILE 2 /* Stdio file */
  37. #define SDL_RWOPS_JNIFILE 3 /* Android asset */
  38. #define SDL_RWOPS_MEMORY 4 /* Memory stream */
  39. #define SDL_RWOPS_MEMORY_RO 5 /* 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. * \name Read endian functions
  167. *
  168. * Read an item of the specified endianness and return in native format.
  169. */
  170. /* @{ */
  171. extern DECLSPEC Uint8 SDLCALL SDL_ReadU8(SDL_RWops * src);
  172. extern DECLSPEC Uint16 SDLCALL SDL_ReadLE16(SDL_RWops * src);
  173. extern DECLSPEC Uint16 SDLCALL SDL_ReadBE16(SDL_RWops * src);
  174. extern DECLSPEC Uint32 SDLCALL SDL_ReadLE32(SDL_RWops * src);
  175. extern DECLSPEC Uint32 SDLCALL SDL_ReadBE32(SDL_RWops * src);
  176. extern DECLSPEC Uint64 SDLCALL SDL_ReadLE64(SDL_RWops * src);
  177. extern DECLSPEC Uint64 SDLCALL SDL_ReadBE64(SDL_RWops * src);
  178. /* @} *//* Read endian functions */
  179. /**
  180. * \name Write endian functions
  181. *
  182. * Write an item of native format to the specified endianness.
  183. */
  184. /* @{ */
  185. extern DECLSPEC size_t SDLCALL SDL_WriteU8(SDL_RWops * dst, Uint8 value);
  186. extern DECLSPEC size_t SDLCALL SDL_WriteLE16(SDL_RWops * dst, Uint16 value);
  187. extern DECLSPEC size_t SDLCALL SDL_WriteBE16(SDL_RWops * dst, Uint16 value);
  188. extern DECLSPEC size_t SDLCALL SDL_WriteLE32(SDL_RWops * dst, Uint32 value);
  189. extern DECLSPEC size_t SDLCALL SDL_WriteBE32(SDL_RWops * dst, Uint32 value);
  190. extern DECLSPEC size_t SDLCALL SDL_WriteLE64(SDL_RWops * dst, Uint64 value);
  191. extern DECLSPEC size_t SDLCALL SDL_WriteBE64(SDL_RWops * dst, Uint64 value);
  192. /* @} *//* Write endian functions */
  193. /* Ends C function definitions when using C++ */
  194. #ifdef __cplusplus
  195. }
  196. #endif
  197. #include "close_code.h"
  198. #endif /* _SDL_rwops_h */
  199. /* vi: set ts=4 sw=4 expandtab: */