SDL_rwops.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /*
  2. SDL - Simple DirectMedia Layer
  3. Copyright (C) 1997-2010 Sam Lantinga
  4. This library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. This library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with this library; if not, write to the Free Software
  14. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  15. Sam Lantinga
  16. [email protected]
  17. */
  18. /**
  19. * \file SDL_rwops.h
  20. *
  21. * This file provides a general interface for SDL to read and write
  22. * data sources. 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. /* *INDENT-OFF* */
  32. extern "C" {
  33. /* *INDENT-ON* */
  34. #endif
  35. /**
  36. * This is the read/write operation structure -- very basic.
  37. */
  38. typedef struct SDL_RWops
  39. {
  40. /**
  41. * Seek to \c offset relative to \c whence, one of stdio's whence values:
  42. * RW_SEEK_SET, RW_SEEK_CUR, RW_SEEK_END
  43. *
  44. * \return the final offset in the data source.
  45. */
  46. long (SDLCALL * seek) (struct SDL_RWops * context, long offset,
  47. int whence);
  48. /**
  49. * Read up to \c maxnum objects each of size \c size from the data
  50. * source to the area pointed at by \c ptr.
  51. *
  52. * \return the number of objects read, or 0 at error or end of file.
  53. */
  54. size_t(SDLCALL * read) (struct SDL_RWops * context, void *ptr,
  55. size_t size, size_t maxnum);
  56. /**
  57. * Write exactly \c num objects each of size \c size from the area
  58. * pointed at by \c ptr to data source.
  59. *
  60. * \return the number of objects written, or 0 at error or end of file.
  61. */
  62. size_t(SDLCALL * write) (struct SDL_RWops * context, const void *ptr,
  63. size_t size, size_t num);
  64. /**
  65. * Close and free an allocated SDL_RWops structure.
  66. *
  67. * \return 0 if successful or -1 on write error when flushing data.
  68. */
  69. int (SDLCALL * close) (struct SDL_RWops * context);
  70. Uint32 type;
  71. union
  72. {
  73. #ifdef __WIN32__
  74. struct
  75. {
  76. SDL_bool append;
  77. void *h;
  78. struct
  79. {
  80. void *data;
  81. size_t size;
  82. size_t left;
  83. } buffer;
  84. } win32io;
  85. #endif
  86. #ifdef HAVE_STDIO_H
  87. struct
  88. {
  89. SDL_bool autoclose;
  90. FILE *fp;
  91. } stdio;
  92. #endif
  93. struct
  94. {
  95. Uint8 *base;
  96. Uint8 *here;
  97. Uint8 *stop;
  98. } mem;
  99. struct
  100. {
  101. void *data1;
  102. } unknown;
  103. } hidden;
  104. } SDL_RWops;
  105. /**
  106. * \name RWFrom functions
  107. *
  108. * Functions to create SDL_RWops structures from various data sources.
  109. */
  110. /*@{*/
  111. extern DECLSPEC SDL_RWops *SDLCALL SDL_RWFromFile(const char *file,
  112. const char *mode);
  113. #ifdef HAVE_STDIO_H
  114. extern DECLSPEC SDL_RWops *SDLCALL SDL_RWFromFP(FILE * fp,
  115. SDL_bool autoclose);
  116. #else
  117. extern DECLSPEC SDL_RWops *SDLCALL SDL_RWFromFP(void * fp,
  118. SDL_bool autoclose);
  119. #endif
  120. extern DECLSPEC SDL_RWops *SDLCALL SDL_RWFromMem(void *mem, int size);
  121. extern DECLSPEC SDL_RWops *SDLCALL SDL_RWFromConstMem(const void *mem,
  122. int size);
  123. /*@}*//*RWFrom functions*/
  124. extern DECLSPEC SDL_RWops *SDLCALL SDL_AllocRW(void);
  125. extern DECLSPEC void SDLCALL SDL_FreeRW(SDL_RWops * area);
  126. #define RW_SEEK_SET 0 /**< Seek from the beginning of data */
  127. #define RW_SEEK_CUR 1 /**< Seek relative to current read point */
  128. #define RW_SEEK_END 2 /**< Seek relative to the end of data */
  129. /**
  130. * \name Read/write macros
  131. *
  132. * Macros to easily read and write from an SDL_RWops structure.
  133. */
  134. /*@{*/
  135. #define SDL_RWseek(ctx, offset, whence) (ctx)->seek(ctx, offset, whence)
  136. #define SDL_RWtell(ctx) (ctx)->seek(ctx, 0, RW_SEEK_CUR)
  137. #define SDL_RWread(ctx, ptr, size, n) (ctx)->read(ctx, ptr, size, n)
  138. #define SDL_RWwrite(ctx, ptr, size, n) (ctx)->write(ctx, ptr, size, n)
  139. #define SDL_RWclose(ctx) (ctx)->close(ctx)
  140. /*@}*//*Read/write macros*/
  141. /**
  142. * \name Read endian functions
  143. *
  144. * Read an item of the specified endianness and return in native format.
  145. */
  146. /*@{*/
  147. extern DECLSPEC Uint16 SDLCALL SDL_ReadLE16(SDL_RWops * src);
  148. extern DECLSPEC Uint16 SDLCALL SDL_ReadBE16(SDL_RWops * src);
  149. extern DECLSPEC Uint32 SDLCALL SDL_ReadLE32(SDL_RWops * src);
  150. extern DECLSPEC Uint32 SDLCALL SDL_ReadBE32(SDL_RWops * src);
  151. extern DECLSPEC Uint64 SDLCALL SDL_ReadLE64(SDL_RWops * src);
  152. extern DECLSPEC Uint64 SDLCALL SDL_ReadBE64(SDL_RWops * src);
  153. /*@}*//*Read endian functions*/
  154. /**
  155. * \name Write endian functions
  156. *
  157. * Write an item of native format to the specified endianness.
  158. */
  159. /*@{*/
  160. extern DECLSPEC size_t SDLCALL SDL_WriteLE16(SDL_RWops * dst, Uint16 value);
  161. extern DECLSPEC size_t SDLCALL SDL_WriteBE16(SDL_RWops * dst, Uint16 value);
  162. extern DECLSPEC size_t SDLCALL SDL_WriteLE32(SDL_RWops * dst, Uint32 value);
  163. extern DECLSPEC size_t SDLCALL SDL_WriteBE32(SDL_RWops * dst, Uint32 value);
  164. extern DECLSPEC size_t SDLCALL SDL_WriteLE64(SDL_RWops * dst, Uint64 value);
  165. extern DECLSPEC size_t SDLCALL SDL_WriteBE64(SDL_RWops * dst, Uint64 value);
  166. /*@}*//*Write endian functions*/
  167. /* Ends C function definitions when using C++ */
  168. #ifdef __cplusplus
  169. /* *INDENT-OFF* */
  170. }
  171. /* *INDENT-ON* */
  172. #endif
  173. #include "close_code.h"
  174. #endif /* _SDL_rwops_h */
  175. /* vi: set ts=4 sw=4 expandtab: */