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