SDL Library

Dependents:   H261_decoder

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers SDL_rwops.h Source File

SDL_rwops.h

Go to the documentation of this file.
00001 /*
00002   Simple DirectMedia Layer
00003   Copyright (C) 1997-2014 Sam Lantinga <slouken@libsdl.org>
00004 
00005   This software is provided 'as-is', without any express or implied
00006   warranty.  In no event will the authors be held liable for any damages
00007   arising from the use of this software.
00008 
00009   Permission is granted to anyone to use this software for any purpose,
00010   including commercial applications, and to alter it and redistribute it
00011   freely, subject to the following restrictions:
00012 
00013   1. The origin of this software must not be misrepresented; you must not
00014      claim that you wrote the original software. If you use this software
00015      in a product, an acknowledgment in the product documentation would be
00016      appreciated but is not required.
00017   2. Altered source versions must be plainly marked as such, and must not be
00018      misrepresented as being the original software.
00019   3. This notice may not be removed or altered from any source distribution.
00020 */
00021 
00022 /**
00023  *  \file SDL_rwops.h
00024  *
00025  *  This file provides a general interface for SDL to read and write
00026  *  data streams.  It can easily be extended to files, memory, etc.
00027  */
00028 
00029 #ifndef _SDL_rwops_h
00030 #define _SDL_rwops_h
00031 
00032 #include "SDL_stdinc.h"
00033 #include "SDL_error.h"
00034 
00035 #include "begin_code.h"
00036 /* Set up for C function definitions, even when using C++ */
00037 #ifdef __cplusplus
00038 extern "C" {
00039 #endif
00040 
00041 /* RWops Types */
00042 #define SDL_RWOPS_UNKNOWN   0   /* Unknown stream type */
00043 #define SDL_RWOPS_WINFILE   1   /* Win32 file */
00044 #define SDL_RWOPS_STDFILE   2   /* Stdio file */
00045 #define SDL_RWOPS_JNIFILE   3   /* Android asset */
00046 #define SDL_RWOPS_MEMORY    4   /* Memory stream */
00047 #define SDL_RWOPS_MEMORY_RO 5   /* Read-Only memory stream */
00048 
00049 /**
00050  * This is the read/write operation structure -- very basic.
00051  */
00052 typedef struct SDL_RWops
00053 {
00054     /**
00055      *  Return the size of the file in this rwops, or -1 if unknown
00056      */
00057     Sint64 (SDLCALL * size) (struct SDL_RWops * context);
00058 
00059     /**
00060      *  Seek to \c offset relative to \c whence, one of stdio's whence values:
00061      *  RW_SEEK_SET, RW_SEEK_CUR, RW_SEEK_END
00062      *
00063      *  \return the final offset in the data stream, or -1 on error.
00064      */
00065     Sint64 (SDLCALL * seek) (struct SDL_RWops * context, Sint64 offset,
00066                              int whence);
00067 
00068     /**
00069      *  Read up to \c maxnum objects each of size \c size from the data
00070      *  stream to the area pointed at by \c ptr.
00071      *
00072      *  \return the number of objects read, or 0 at error or end of file.
00073      */
00074     size_t (SDLCALL * read) (struct SDL_RWops * context, void *ptr,
00075                              size_t size, size_t maxnum);
00076 
00077     /**
00078      *  Write exactly \c num objects each of size \c size from the area
00079      *  pointed at by \c ptr to data stream.
00080      *
00081      *  \return the number of objects written, or 0 at error or end of file.
00082      */
00083     size_t (SDLCALL * write) (struct SDL_RWops * context, const void *ptr,
00084                               size_t size, size_t num);
00085 
00086     /**
00087      *  Close and free an allocated SDL_RWops structure.
00088      *
00089      *  \return 0 if successful or -1 on write error when flushing data.
00090      */
00091     int (SDLCALL * close) (struct SDL_RWops * context);
00092 
00093     Uint32 type;
00094     union
00095     {
00096 #if defined(ANDROID)
00097         struct
00098         {
00099             void *fileNameRef;
00100             void *inputStreamRef;
00101             void *readableByteChannelRef;
00102             void *readMethod;
00103             void *assetFileDescriptorRef;
00104             long position;
00105             long size;
00106             long offset;
00107             int fd;
00108         } androidio;
00109 #elif defined(__WIN32__)
00110         struct
00111         {
00112             SDL_bool append;
00113             void *h;
00114             struct
00115             {
00116                 void *data;
00117                 size_t size;
00118                 size_t left;
00119             } buffer;
00120         } windowsio;
00121 #endif
00122 
00123 #ifdef HAVE_STDIO_H
00124         struct
00125         {
00126             SDL_bool autoclose;
00127             FILE *fp;
00128         } stdio;
00129 #endif
00130         struct
00131         {
00132             Uint8 *base;
00133             Uint8 *here;
00134             Uint8 *stop;
00135         } mem;
00136         struct
00137         {
00138             void *data1;
00139             void *data2;
00140         } unknown;
00141     } hidden;
00142 
00143 } SDL_RWops;
00144 
00145 
00146 /**
00147  *  \name RWFrom functions
00148  *
00149  *  Functions to create SDL_RWops structures from various data streams.
00150  */
00151 /* @{ */
00152 
00153 extern DECLSPEC SDL_RWops *SDLCALL SDL_RWFromFile(const char *file,
00154                                                   const char *mode);
00155 
00156 #ifdef HAVE_STDIO_H
00157 extern DECLSPEC SDL_RWops *SDLCALL SDL_RWFromFP(FILE * fp,
00158                                                 SDL_bool autoclose);
00159 #else
00160 extern DECLSPEC SDL_RWops *SDLCALL SDL_RWFromFP(void * fp,
00161                                                 SDL_bool autoclose);
00162 #endif
00163 
00164 extern DECLSPEC SDL_RWops *SDLCALL SDL_RWFromMem(void *mem, int size);
00165 extern DECLSPEC SDL_RWops *SDLCALL SDL_RWFromConstMem(const void *mem,
00166                                                       int size);
00167 
00168 /* @} *//* RWFrom functions */
00169 
00170 
00171 extern DECLSPEC SDL_RWops *SDLCALL SDL_AllocRW(void);
00172 extern DECLSPEC void SDLCALL SDL_FreeRW(SDL_RWops * area);
00173 
00174 #define RW_SEEK_SET 0       /**< Seek from the beginning of data */
00175 #define RW_SEEK_CUR 1       /**< Seek relative to current read point */
00176 #define RW_SEEK_END 2       /**< Seek relative to the end of data */
00177 
00178 /**
00179  *  \name Read/write macros
00180  *
00181  *  Macros to easily read and write from an SDL_RWops structure.
00182  */
00183 /* @{ */
00184 #define SDL_RWsize(ctx)         (ctx)->size(ctx)
00185 #define SDL_RWseek(ctx, offset, whence) (ctx)->seek(ctx, offset, whence)
00186 #define SDL_RWtell(ctx)         (ctx)->seek(ctx, 0, RW_SEEK_CUR)
00187 #define SDL_RWread(ctx, ptr, size, n)   (ctx)->read(ctx, ptr, size, n)
00188 #define SDL_RWwrite(ctx, ptr, size, n)  (ctx)->write(ctx, ptr, size, n)
00189 #define SDL_RWclose(ctx)        (ctx)->close(ctx)
00190 /* @} *//* Read/write macros */
00191 
00192 
00193 /**
00194  *  \name Read endian functions
00195  *
00196  *  Read an item of the specified endianness and return in native format.
00197  */
00198 /* @{ */
00199 extern DECLSPEC Uint8 SDLCALL SDL_ReadU8(SDL_RWops * src);
00200 extern DECLSPEC Uint16 SDLCALL SDL_ReadLE16(SDL_RWops * src);
00201 extern DECLSPEC Uint16 SDLCALL SDL_ReadBE16(SDL_RWops * src);
00202 extern DECLSPEC Uint32 SDLCALL SDL_ReadLE32(SDL_RWops * src);
00203 extern DECLSPEC Uint32 SDLCALL SDL_ReadBE32(SDL_RWops * src);
00204 extern DECLSPEC Uint64 SDLCALL SDL_ReadLE64(SDL_RWops * src);
00205 extern DECLSPEC Uint64 SDLCALL SDL_ReadBE64(SDL_RWops * src);
00206 /* @} *//* Read endian functions */
00207 
00208 /**
00209  *  \name Write endian functions
00210  *
00211  *  Write an item of native format to the specified endianness.
00212  */
00213 /* @{ */
00214 extern DECLSPEC size_t SDLCALL SDL_WriteU8(SDL_RWops * dst, Uint8 value);
00215 extern DECLSPEC size_t SDLCALL SDL_WriteLE16(SDL_RWops * dst, Uint16 value);
00216 extern DECLSPEC size_t SDLCALL SDL_WriteBE16(SDL_RWops * dst, Uint16 value);
00217 extern DECLSPEC size_t SDLCALL SDL_WriteLE32(SDL_RWops * dst, Uint32 value);
00218 extern DECLSPEC size_t SDLCALL SDL_WriteBE32(SDL_RWops * dst, Uint32 value);
00219 extern DECLSPEC size_t SDLCALL SDL_WriteLE64(SDL_RWops * dst, Uint64 value);
00220 extern DECLSPEC size_t SDLCALL SDL_WriteBE64(SDL_RWops * dst, Uint64 value);
00221 /* @} *//* Write endian functions */
00222 
00223 
00224 /* Ends C function definitions when using C++ */
00225 #ifdef __cplusplus
00226 }
00227 #endif
00228 #include "close_code.h"
00229 
00230 #endif /* _SDL_rwops_h */
00231 
00232 /* vi: set ts=4 sw=4 expandtab: */