SDL standard library
SDL_rwops.h@0:dda4f4550403, 2016-09-07 (annotated)
- Committer:
- miruga27
- Date:
- Wed Sep 07 18:46:53 2016 +0000
- Revision:
- 0:dda4f4550403
7/09/2016;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
miruga27 | 0:dda4f4550403 | 1 | /* |
miruga27 | 0:dda4f4550403 | 2 | Simple DirectMedia Layer |
miruga27 | 0:dda4f4550403 | 3 | Copyright (C) 1997-2014 Sam Lantinga <slouken@libsdl.org> |
miruga27 | 0:dda4f4550403 | 4 | |
miruga27 | 0:dda4f4550403 | 5 | This software is provided 'as-is', without any express or implied |
miruga27 | 0:dda4f4550403 | 6 | warranty. In no event will the authors be held liable for any damages |
miruga27 | 0:dda4f4550403 | 7 | arising from the use of this software. |
miruga27 | 0:dda4f4550403 | 8 | |
miruga27 | 0:dda4f4550403 | 9 | Permission is granted to anyone to use this software for any purpose, |
miruga27 | 0:dda4f4550403 | 10 | including commercial applications, and to alter it and redistribute it |
miruga27 | 0:dda4f4550403 | 11 | freely, subject to the following restrictions: |
miruga27 | 0:dda4f4550403 | 12 | |
miruga27 | 0:dda4f4550403 | 13 | 1. The origin of this software must not be misrepresented; you must not |
miruga27 | 0:dda4f4550403 | 14 | claim that you wrote the original software. If you use this software |
miruga27 | 0:dda4f4550403 | 15 | in a product, an acknowledgment in the product documentation would be |
miruga27 | 0:dda4f4550403 | 16 | appreciated but is not required. |
miruga27 | 0:dda4f4550403 | 17 | 2. Altered source versions must be plainly marked as such, and must not be |
miruga27 | 0:dda4f4550403 | 18 | misrepresented as being the original software. |
miruga27 | 0:dda4f4550403 | 19 | 3. This notice may not be removed or altered from any source distribution. |
miruga27 | 0:dda4f4550403 | 20 | */ |
miruga27 | 0:dda4f4550403 | 21 | |
miruga27 | 0:dda4f4550403 | 22 | /** |
miruga27 | 0:dda4f4550403 | 23 | * \file SDL_rwops.h |
miruga27 | 0:dda4f4550403 | 24 | * |
miruga27 | 0:dda4f4550403 | 25 | * This file provides a general interface for SDL to read and write |
miruga27 | 0:dda4f4550403 | 26 | * data streams. It can easily be extended to files, memory, etc. |
miruga27 | 0:dda4f4550403 | 27 | */ |
miruga27 | 0:dda4f4550403 | 28 | |
miruga27 | 0:dda4f4550403 | 29 | #ifndef _SDL_rwops_h |
miruga27 | 0:dda4f4550403 | 30 | #define _SDL_rwops_h |
miruga27 | 0:dda4f4550403 | 31 | |
miruga27 | 0:dda4f4550403 | 32 | #include "SDL_stdinc.h" |
miruga27 | 0:dda4f4550403 | 33 | #include "SDL_error.h" |
miruga27 | 0:dda4f4550403 | 34 | |
miruga27 | 0:dda4f4550403 | 35 | #include "begin_code.h" |
miruga27 | 0:dda4f4550403 | 36 | /* Set up for C function definitions, even when using C++ */ |
miruga27 | 0:dda4f4550403 | 37 | #ifdef __cplusplus |
miruga27 | 0:dda4f4550403 | 38 | extern "C" { |
miruga27 | 0:dda4f4550403 | 39 | #endif |
miruga27 | 0:dda4f4550403 | 40 | |
miruga27 | 0:dda4f4550403 | 41 | /* RWops Types */ |
miruga27 | 0:dda4f4550403 | 42 | #define SDL_RWOPS_UNKNOWN 0 /* Unknown stream type */ |
miruga27 | 0:dda4f4550403 | 43 | #define SDL_RWOPS_WINFILE 1 /* Win32 file */ |
miruga27 | 0:dda4f4550403 | 44 | #define SDL_RWOPS_STDFILE 2 /* Stdio file */ |
miruga27 | 0:dda4f4550403 | 45 | #define SDL_RWOPS_JNIFILE 3 /* Android asset */ |
miruga27 | 0:dda4f4550403 | 46 | #define SDL_RWOPS_MEMORY 4 /* Memory stream */ |
miruga27 | 0:dda4f4550403 | 47 | #define SDL_RWOPS_MEMORY_RO 5 /* Read-Only memory stream */ |
miruga27 | 0:dda4f4550403 | 48 | |
miruga27 | 0:dda4f4550403 | 49 | /** |
miruga27 | 0:dda4f4550403 | 50 | * This is the read/write operation structure -- very basic. |
miruga27 | 0:dda4f4550403 | 51 | */ |
miruga27 | 0:dda4f4550403 | 52 | typedef struct SDL_RWops |
miruga27 | 0:dda4f4550403 | 53 | { |
miruga27 | 0:dda4f4550403 | 54 | /** |
miruga27 | 0:dda4f4550403 | 55 | * Return the size of the file in this rwops, or -1 if unknown |
miruga27 | 0:dda4f4550403 | 56 | */ |
miruga27 | 0:dda4f4550403 | 57 | Sint64 (SDLCALL * size) (struct SDL_RWops * context); |
miruga27 | 0:dda4f4550403 | 58 | |
miruga27 | 0:dda4f4550403 | 59 | /** |
miruga27 | 0:dda4f4550403 | 60 | * Seek to \c offset relative to \c whence, one of stdio's whence values: |
miruga27 | 0:dda4f4550403 | 61 | * RW_SEEK_SET, RW_SEEK_CUR, RW_SEEK_END |
miruga27 | 0:dda4f4550403 | 62 | * |
miruga27 | 0:dda4f4550403 | 63 | * \return the final offset in the data stream, or -1 on error. |
miruga27 | 0:dda4f4550403 | 64 | */ |
miruga27 | 0:dda4f4550403 | 65 | Sint64 (SDLCALL * seek) (struct SDL_RWops * context, Sint64 offset, |
miruga27 | 0:dda4f4550403 | 66 | int whence); |
miruga27 | 0:dda4f4550403 | 67 | |
miruga27 | 0:dda4f4550403 | 68 | /** |
miruga27 | 0:dda4f4550403 | 69 | * Read up to \c maxnum objects each of size \c size from the data |
miruga27 | 0:dda4f4550403 | 70 | * stream to the area pointed at by \c ptr. |
miruga27 | 0:dda4f4550403 | 71 | * |
miruga27 | 0:dda4f4550403 | 72 | * \return the number of objects read, or 0 at error or end of file. |
miruga27 | 0:dda4f4550403 | 73 | */ |
miruga27 | 0:dda4f4550403 | 74 | size_t (SDLCALL * read) (struct SDL_RWops * context, void *ptr, |
miruga27 | 0:dda4f4550403 | 75 | size_t size, size_t maxnum); |
miruga27 | 0:dda4f4550403 | 76 | |
miruga27 | 0:dda4f4550403 | 77 | /** |
miruga27 | 0:dda4f4550403 | 78 | * Write exactly \c num objects each of size \c size from the area |
miruga27 | 0:dda4f4550403 | 79 | * pointed at by \c ptr to data stream. |
miruga27 | 0:dda4f4550403 | 80 | * |
miruga27 | 0:dda4f4550403 | 81 | * \return the number of objects written, or 0 at error or end of file. |
miruga27 | 0:dda4f4550403 | 82 | */ |
miruga27 | 0:dda4f4550403 | 83 | size_t (SDLCALL * write) (struct SDL_RWops * context, const void *ptr, |
miruga27 | 0:dda4f4550403 | 84 | size_t size, size_t num); |
miruga27 | 0:dda4f4550403 | 85 | |
miruga27 | 0:dda4f4550403 | 86 | /** |
miruga27 | 0:dda4f4550403 | 87 | * Close and free an allocated SDL_RWops structure. |
miruga27 | 0:dda4f4550403 | 88 | * |
miruga27 | 0:dda4f4550403 | 89 | * \return 0 if successful or -1 on write error when flushing data. |
miruga27 | 0:dda4f4550403 | 90 | */ |
miruga27 | 0:dda4f4550403 | 91 | int (SDLCALL * close) (struct SDL_RWops * context); |
miruga27 | 0:dda4f4550403 | 92 | |
miruga27 | 0:dda4f4550403 | 93 | Uint32 type; |
miruga27 | 0:dda4f4550403 | 94 | union |
miruga27 | 0:dda4f4550403 | 95 | { |
miruga27 | 0:dda4f4550403 | 96 | #if defined(ANDROID) |
miruga27 | 0:dda4f4550403 | 97 | struct |
miruga27 | 0:dda4f4550403 | 98 | { |
miruga27 | 0:dda4f4550403 | 99 | void *fileNameRef; |
miruga27 | 0:dda4f4550403 | 100 | void *inputStreamRef; |
miruga27 | 0:dda4f4550403 | 101 | void *readableByteChannelRef; |
miruga27 | 0:dda4f4550403 | 102 | void *readMethod; |
miruga27 | 0:dda4f4550403 | 103 | void *assetFileDescriptorRef; |
miruga27 | 0:dda4f4550403 | 104 | long position; |
miruga27 | 0:dda4f4550403 | 105 | long size; |
miruga27 | 0:dda4f4550403 | 106 | long offset; |
miruga27 | 0:dda4f4550403 | 107 | int fd; |
miruga27 | 0:dda4f4550403 | 108 | } androidio; |
miruga27 | 0:dda4f4550403 | 109 | #elif defined(__WIN32__) |
miruga27 | 0:dda4f4550403 | 110 | struct |
miruga27 | 0:dda4f4550403 | 111 | { |
miruga27 | 0:dda4f4550403 | 112 | SDL_bool append; |
miruga27 | 0:dda4f4550403 | 113 | void *h; |
miruga27 | 0:dda4f4550403 | 114 | struct |
miruga27 | 0:dda4f4550403 | 115 | { |
miruga27 | 0:dda4f4550403 | 116 | void *data; |
miruga27 | 0:dda4f4550403 | 117 | size_t size; |
miruga27 | 0:dda4f4550403 | 118 | size_t left; |
miruga27 | 0:dda4f4550403 | 119 | } buffer; |
miruga27 | 0:dda4f4550403 | 120 | } windowsio; |
miruga27 | 0:dda4f4550403 | 121 | #endif |
miruga27 | 0:dda4f4550403 | 122 | |
miruga27 | 0:dda4f4550403 | 123 | #ifdef HAVE_STDIO_H |
miruga27 | 0:dda4f4550403 | 124 | struct |
miruga27 | 0:dda4f4550403 | 125 | { |
miruga27 | 0:dda4f4550403 | 126 | SDL_bool autoclose; |
miruga27 | 0:dda4f4550403 | 127 | FILE *fp; |
miruga27 | 0:dda4f4550403 | 128 | } stdio; |
miruga27 | 0:dda4f4550403 | 129 | #endif |
miruga27 | 0:dda4f4550403 | 130 | struct |
miruga27 | 0:dda4f4550403 | 131 | { |
miruga27 | 0:dda4f4550403 | 132 | Uint8 *base; |
miruga27 | 0:dda4f4550403 | 133 | Uint8 *here; |
miruga27 | 0:dda4f4550403 | 134 | Uint8 *stop; |
miruga27 | 0:dda4f4550403 | 135 | } mem; |
miruga27 | 0:dda4f4550403 | 136 | struct |
miruga27 | 0:dda4f4550403 | 137 | { |
miruga27 | 0:dda4f4550403 | 138 | void *data1; |
miruga27 | 0:dda4f4550403 | 139 | void *data2; |
miruga27 | 0:dda4f4550403 | 140 | } unknown; |
miruga27 | 0:dda4f4550403 | 141 | } hidden; |
miruga27 | 0:dda4f4550403 | 142 | |
miruga27 | 0:dda4f4550403 | 143 | } SDL_RWops; |
miruga27 | 0:dda4f4550403 | 144 | |
miruga27 | 0:dda4f4550403 | 145 | |
miruga27 | 0:dda4f4550403 | 146 | /** |
miruga27 | 0:dda4f4550403 | 147 | * \name RWFrom functions |
miruga27 | 0:dda4f4550403 | 148 | * |
miruga27 | 0:dda4f4550403 | 149 | * Functions to create SDL_RWops structures from various data streams. |
miruga27 | 0:dda4f4550403 | 150 | */ |
miruga27 | 0:dda4f4550403 | 151 | /* @{ */ |
miruga27 | 0:dda4f4550403 | 152 | |
miruga27 | 0:dda4f4550403 | 153 | extern DECLSPEC SDL_RWops *SDLCALL SDL_RWFromFile(const char *file, |
miruga27 | 0:dda4f4550403 | 154 | const char *mode); |
miruga27 | 0:dda4f4550403 | 155 | |
miruga27 | 0:dda4f4550403 | 156 | #ifdef HAVE_STDIO_H |
miruga27 | 0:dda4f4550403 | 157 | extern DECLSPEC SDL_RWops *SDLCALL SDL_RWFromFP(FILE * fp, |
miruga27 | 0:dda4f4550403 | 158 | SDL_bool autoclose); |
miruga27 | 0:dda4f4550403 | 159 | #else |
miruga27 | 0:dda4f4550403 | 160 | extern DECLSPEC SDL_RWops *SDLCALL SDL_RWFromFP(void * fp, |
miruga27 | 0:dda4f4550403 | 161 | SDL_bool autoclose); |
miruga27 | 0:dda4f4550403 | 162 | #endif |
miruga27 | 0:dda4f4550403 | 163 | |
miruga27 | 0:dda4f4550403 | 164 | extern DECLSPEC SDL_RWops *SDLCALL SDL_RWFromMem(void *mem, int size); |
miruga27 | 0:dda4f4550403 | 165 | extern DECLSPEC SDL_RWops *SDLCALL SDL_RWFromConstMem(const void *mem, |
miruga27 | 0:dda4f4550403 | 166 | int size); |
miruga27 | 0:dda4f4550403 | 167 | |
miruga27 | 0:dda4f4550403 | 168 | /* @} *//* RWFrom functions */ |
miruga27 | 0:dda4f4550403 | 169 | |
miruga27 | 0:dda4f4550403 | 170 | |
miruga27 | 0:dda4f4550403 | 171 | extern DECLSPEC SDL_RWops *SDLCALL SDL_AllocRW(void); |
miruga27 | 0:dda4f4550403 | 172 | extern DECLSPEC void SDLCALL SDL_FreeRW(SDL_RWops * area); |
miruga27 | 0:dda4f4550403 | 173 | |
miruga27 | 0:dda4f4550403 | 174 | #define RW_SEEK_SET 0 /**< Seek from the beginning of data */ |
miruga27 | 0:dda4f4550403 | 175 | #define RW_SEEK_CUR 1 /**< Seek relative to current read point */ |
miruga27 | 0:dda4f4550403 | 176 | #define RW_SEEK_END 2 /**< Seek relative to the end of data */ |
miruga27 | 0:dda4f4550403 | 177 | |
miruga27 | 0:dda4f4550403 | 178 | /** |
miruga27 | 0:dda4f4550403 | 179 | * \name Read/write macros |
miruga27 | 0:dda4f4550403 | 180 | * |
miruga27 | 0:dda4f4550403 | 181 | * Macros to easily read and write from an SDL_RWops structure. |
miruga27 | 0:dda4f4550403 | 182 | */ |
miruga27 | 0:dda4f4550403 | 183 | /* @{ */ |
miruga27 | 0:dda4f4550403 | 184 | #define SDL_RWsize(ctx) (ctx)->size(ctx) |
miruga27 | 0:dda4f4550403 | 185 | #define SDL_RWseek(ctx, offset, whence) (ctx)->seek(ctx, offset, whence) |
miruga27 | 0:dda4f4550403 | 186 | #define SDL_RWtell(ctx) (ctx)->seek(ctx, 0, RW_SEEK_CUR) |
miruga27 | 0:dda4f4550403 | 187 | #define SDL_RWread(ctx, ptr, size, n) (ctx)->read(ctx, ptr, size, n) |
miruga27 | 0:dda4f4550403 | 188 | #define SDL_RWwrite(ctx, ptr, size, n) (ctx)->write(ctx, ptr, size, n) |
miruga27 | 0:dda4f4550403 | 189 | #define SDL_RWclose(ctx) (ctx)->close(ctx) |
miruga27 | 0:dda4f4550403 | 190 | /* @} *//* Read/write macros */ |
miruga27 | 0:dda4f4550403 | 191 | |
miruga27 | 0:dda4f4550403 | 192 | |
miruga27 | 0:dda4f4550403 | 193 | /** |
miruga27 | 0:dda4f4550403 | 194 | * \name Read endian functions |
miruga27 | 0:dda4f4550403 | 195 | * |
miruga27 | 0:dda4f4550403 | 196 | * Read an item of the specified endianness and return in native format. |
miruga27 | 0:dda4f4550403 | 197 | */ |
miruga27 | 0:dda4f4550403 | 198 | /* @{ */ |
miruga27 | 0:dda4f4550403 | 199 | extern DECLSPEC Uint8 SDLCALL SDL_ReadU8(SDL_RWops * src); |
miruga27 | 0:dda4f4550403 | 200 | extern DECLSPEC Uint16 SDLCALL SDL_ReadLE16(SDL_RWops * src); |
miruga27 | 0:dda4f4550403 | 201 | extern DECLSPEC Uint16 SDLCALL SDL_ReadBE16(SDL_RWops * src); |
miruga27 | 0:dda4f4550403 | 202 | extern DECLSPEC Uint32 SDLCALL SDL_ReadLE32(SDL_RWops * src); |
miruga27 | 0:dda4f4550403 | 203 | extern DECLSPEC Uint32 SDLCALL SDL_ReadBE32(SDL_RWops * src); |
miruga27 | 0:dda4f4550403 | 204 | extern DECLSPEC Uint64 SDLCALL SDL_ReadLE64(SDL_RWops * src); |
miruga27 | 0:dda4f4550403 | 205 | extern DECLSPEC Uint64 SDLCALL SDL_ReadBE64(SDL_RWops * src); |
miruga27 | 0:dda4f4550403 | 206 | /* @} *//* Read endian functions */ |
miruga27 | 0:dda4f4550403 | 207 | |
miruga27 | 0:dda4f4550403 | 208 | /** |
miruga27 | 0:dda4f4550403 | 209 | * \name Write endian functions |
miruga27 | 0:dda4f4550403 | 210 | * |
miruga27 | 0:dda4f4550403 | 211 | * Write an item of native format to the specified endianness. |
miruga27 | 0:dda4f4550403 | 212 | */ |
miruga27 | 0:dda4f4550403 | 213 | /* @{ */ |
miruga27 | 0:dda4f4550403 | 214 | extern DECLSPEC size_t SDLCALL SDL_WriteU8(SDL_RWops * dst, Uint8 value); |
miruga27 | 0:dda4f4550403 | 215 | extern DECLSPEC size_t SDLCALL SDL_WriteLE16(SDL_RWops * dst, Uint16 value); |
miruga27 | 0:dda4f4550403 | 216 | extern DECLSPEC size_t SDLCALL SDL_WriteBE16(SDL_RWops * dst, Uint16 value); |
miruga27 | 0:dda4f4550403 | 217 | extern DECLSPEC size_t SDLCALL SDL_WriteLE32(SDL_RWops * dst, Uint32 value); |
miruga27 | 0:dda4f4550403 | 218 | extern DECLSPEC size_t SDLCALL SDL_WriteBE32(SDL_RWops * dst, Uint32 value); |
miruga27 | 0:dda4f4550403 | 219 | extern DECLSPEC size_t SDLCALL SDL_WriteLE64(SDL_RWops * dst, Uint64 value); |
miruga27 | 0:dda4f4550403 | 220 | extern DECLSPEC size_t SDLCALL SDL_WriteBE64(SDL_RWops * dst, Uint64 value); |
miruga27 | 0:dda4f4550403 | 221 | /* @} *//* Write endian functions */ |
miruga27 | 0:dda4f4550403 | 222 | |
miruga27 | 0:dda4f4550403 | 223 | |
miruga27 | 0:dda4f4550403 | 224 | /* Ends C function definitions when using C++ */ |
miruga27 | 0:dda4f4550403 | 225 | #ifdef __cplusplus |
miruga27 | 0:dda4f4550403 | 226 | } |
miruga27 | 0:dda4f4550403 | 227 | #endif |
miruga27 | 0:dda4f4550403 | 228 | #include "close_code.h" |
miruga27 | 0:dda4f4550403 | 229 | |
miruga27 | 0:dda4f4550403 | 230 | #endif /* _SDL_rwops_h */ |
miruga27 | 0:dda4f4550403 | 231 | |
miruga27 | 0:dda4f4550403 | 232 | /* vi: set ts=4 sw=4 expandtab: */ |