include/SDL_rwops.h
author Gabriel Jacobo <gabomdq@gmail.com>
Mon, 01 Apr 2013 13:20:22 -0300
changeset 7039 f69b305b053d
parent 6999 681820ca0e78
child 7063 bea100d73d13
permissions -rw-r--r--
Backed out changeset: 9cef1005df5f
     1 /*
     2   Simple DirectMedia Layer
     3   Copyright (C) 1997-2013 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 /* RWops Types */
    44 #define SDL_RWOPS_UNKNOWN	0	/* Unknown stream type */
    45 #define SDL_RWOPS_WINFILE	1	/* Win32 file */
    46 #define SDL_RWOPS_STDFILE	2	/* Stdio file */
    47 #define SDL_RWOPS_JNIFILE	3	/* Android asset */
    48 #define SDL_RWOPS_MEMORY	4	/* Memory stream */
    49 #define SDL_RWOPS_MEMORY_RO	5	/* Read-Only memory stream */
    50 
    51 /**
    52  * This is the read/write operation structure -- very basic.
    53  */
    54 typedef struct SDL_RWops
    55 {
    56     /**
    57      *  Return the size of the file in this rwops, or -1 if unknown
    58      */
    59     Sint64 (SDLCALL * size) (struct SDL_RWops * context);
    60 
    61     /**
    62      *  Seek to \c offset relative to \c whence, one of stdio's whence values:
    63      *  RW_SEEK_SET, RW_SEEK_CUR, RW_SEEK_END
    64      *  
    65      *  \return the final offset in the data stream, or -1 on error.
    66      */
    67     Sint64 (SDLCALL * seek) (struct SDL_RWops * context, Sint64 offset,
    68                              int whence);
    69 
    70     /**
    71      *  Read up to \c maxnum objects each of size \c size from the data
    72      *  stream to the area pointed at by \c ptr.
    73      *  
    74      *  \return the number of objects read, or 0 at error or end of file.
    75      */
    76     size_t (SDLCALL * read) (struct SDL_RWops * context, void *ptr,
    77                              size_t size, size_t maxnum);
    78 
    79     /**
    80      *  Write exactly \c num objects each of size \c size from the area
    81      *  pointed at by \c ptr to data stream.
    82      *  
    83      *  \return the number of objects written, or 0 at error or end of file.
    84      */
    85     size_t (SDLCALL * write) (struct SDL_RWops * context, const void *ptr,
    86                               size_t size, size_t num);
    87 
    88     /**
    89      *  Close and free an allocated SDL_RWops structure.
    90      *  
    91      *  \return 0 if successful or -1 on write error when flushing data.
    92      */
    93     int (SDLCALL * close) (struct SDL_RWops * context);
    94 
    95     Uint32 type;
    96     union
    97     {
    98 #if defined(ANDROID)
    99         struct
   100         {
   101             void *fileNameRef;
   102             void *inputStreamRef;
   103             void *readableByteChannelRef;
   104             void *readMethod;
   105             void *assetFileDescriptorRef;
   106             long position;
   107             long size;
   108             long offset;
   109             int fd;
   110         } androidio;
   111 #elif defined(__WIN32__)
   112         struct
   113         {
   114             SDL_bool append;
   115             void *h;
   116             struct
   117             {
   118                 void *data;
   119                 size_t size;
   120                 size_t left;
   121             } buffer;
   122         } windowsio;
   123 #endif
   124 
   125 #ifdef HAVE_STDIO_H
   126         struct
   127         {
   128             SDL_bool autoclose;
   129             FILE *fp;
   130         } stdio;
   131 #endif
   132         struct
   133         {
   134             Uint8 *base;
   135             Uint8 *here;
   136             Uint8 *stop;
   137         } mem;
   138         struct
   139         {
   140             void *data1;
   141         } unknown;
   142     } hidden;
   143 
   144 } SDL_RWops;
   145 
   146 
   147 /**
   148  *  \name RWFrom functions
   149  *  
   150  *  Functions to create SDL_RWops structures from various data streams.
   151  */
   152 /*@{*/
   153 
   154 extern DECLSPEC SDL_RWops *SDLCALL SDL_RWFromFile(const char *file,
   155                                                   const char *mode);
   156 
   157 #ifdef HAVE_STDIO_H
   158 extern DECLSPEC SDL_RWops *SDLCALL SDL_RWFromFP(FILE * fp,
   159                                                 SDL_bool autoclose);
   160 #else
   161 extern DECLSPEC SDL_RWops *SDLCALL SDL_RWFromFP(void * fp,
   162                                                 SDL_bool autoclose);
   163 #endif
   164 
   165 extern DECLSPEC SDL_RWops *SDLCALL SDL_RWFromMem(void *mem, int size);
   166 extern DECLSPEC SDL_RWops *SDLCALL SDL_RWFromConstMem(const void *mem,
   167                                                       int size);
   168 
   169 /*@}*//*RWFrom functions*/
   170 
   171 
   172 extern DECLSPEC SDL_RWops *SDLCALL SDL_AllocRW(void);
   173 extern DECLSPEC void SDLCALL SDL_FreeRW(SDL_RWops * area);
   174 
   175 #define RW_SEEK_SET	0       /**< Seek from the beginning of data */
   176 #define RW_SEEK_CUR	1       /**< Seek relative to current read point */
   177 #define RW_SEEK_END	2       /**< Seek relative to the end of data */
   178 
   179 /**
   180  *  \name Read/write macros
   181  *  
   182  *  Macros to easily read and write from an SDL_RWops structure.
   183  */
   184 /*@{*/
   185 #define SDL_RWsize(ctx)	        (ctx)->size(ctx)
   186 #define SDL_RWseek(ctx, offset, whence)	(ctx)->seek(ctx, offset, whence)
   187 #define SDL_RWtell(ctx)			(ctx)->seek(ctx, 0, RW_SEEK_CUR)
   188 #define SDL_RWread(ctx, ptr, size, n)	(ctx)->read(ctx, ptr, size, n)
   189 #define SDL_RWwrite(ctx, ptr, size, n)	(ctx)->write(ctx, ptr, size, n)
   190 #define SDL_RWclose(ctx)		(ctx)->close(ctx)
   191 /*@}*//*Read/write macros*/
   192 
   193 
   194 /** 
   195  *  \name Read endian functions
   196  *  
   197  *  Read an item of the specified endianness and return in native format.
   198  */
   199 /*@{*/
   200 extern DECLSPEC Uint8 SDLCALL SDL_ReadU8(SDL_RWops * src);
   201 extern DECLSPEC Uint16 SDLCALL SDL_ReadLE16(SDL_RWops * src);
   202 extern DECLSPEC Uint16 SDLCALL SDL_ReadBE16(SDL_RWops * src);
   203 extern DECLSPEC Uint32 SDLCALL SDL_ReadLE32(SDL_RWops * src);
   204 extern DECLSPEC Uint32 SDLCALL SDL_ReadBE32(SDL_RWops * src);
   205 extern DECLSPEC Uint64 SDLCALL SDL_ReadLE64(SDL_RWops * src);
   206 extern DECLSPEC Uint64 SDLCALL SDL_ReadBE64(SDL_RWops * src);
   207 /*@}*//*Read endian functions*/
   208 
   209 /** 
   210  *  \name Write endian functions
   211  *  
   212  *  Write an item of native format to the specified endianness.
   213  */
   214 /*@{*/
   215 extern DECLSPEC size_t SDLCALL SDL_WriteU8(SDL_RWops * dst, Uint8 value);
   216 extern DECLSPEC size_t SDLCALL SDL_WriteLE16(SDL_RWops * dst, Uint16 value);
   217 extern DECLSPEC size_t SDLCALL SDL_WriteBE16(SDL_RWops * dst, Uint16 value);
   218 extern DECLSPEC size_t SDLCALL SDL_WriteLE32(SDL_RWops * dst, Uint32 value);
   219 extern DECLSPEC size_t SDLCALL SDL_WriteBE32(SDL_RWops * dst, Uint32 value);
   220 extern DECLSPEC size_t SDLCALL SDL_WriteLE64(SDL_RWops * dst, Uint64 value);
   221 extern DECLSPEC size_t SDLCALL SDL_WriteBE64(SDL_RWops * dst, Uint64 value);
   222 /*@}*//*Write endian functions*/
   223 
   224 
   225 /* Ends C function definitions when using C++ */
   226 #ifdef __cplusplus
   227 /* *INDENT-OFF* */
   228 }
   229 /* *INDENT-ON* */
   230 #endif
   231 #include "close_code.h"
   232 
   233 #endif /* _SDL_rwops_h */
   234 
   235 /* vi: set ts=4 sw=4 expandtab: */