include/SDL_rwops.h
author David Ludwig <dludwig@pobox.com>
Mon, 01 Apr 2013 21:33:06 -0400
changeset 8453 499aa167ea79
parent 6885 700f1b25f77f
child 6996 bc0fd5bced25
permissions -rw-r--r--
WinRT: made WinRT path retrieval be available in both UCS-2 and UTF-8 flavors
     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 /**
    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             void *assetFileDescriptorRef;
    98             long position;
    99             long size;
   100             long offset;
   101             int fd;
   102         } androidio;
   103 #elif defined(__WIN32__)
   104         struct
   105         {
   106             SDL_bool append;
   107             void *h;
   108             struct
   109             {
   110                 void *data;
   111                 size_t size;
   112                 size_t left;
   113             } buffer;
   114         } windowsio;
   115 #endif
   116 
   117 #ifdef HAVE_STDIO_H
   118         struct
   119         {
   120             SDL_bool autoclose;
   121             FILE *fp;
   122         } stdio;
   123 #endif
   124         struct
   125         {
   126             Uint8 *base;
   127             Uint8 *here;
   128             Uint8 *stop;
   129         } mem;
   130         struct
   131         {
   132             void *data1;
   133         } unknown;
   134     } hidden;
   135 
   136 } SDL_RWops;
   137 
   138 
   139 /**
   140  *  \name RWFrom functions
   141  *  
   142  *  Functions to create SDL_RWops structures from various data streams.
   143  */
   144 /*@{*/
   145 
   146 extern DECLSPEC SDL_RWops *SDLCALL SDL_RWFromFile(const char *file,
   147                                                   const char *mode);
   148 
   149 #ifdef HAVE_STDIO_H
   150 extern DECLSPEC SDL_RWops *SDLCALL SDL_RWFromFP(FILE * fp,
   151                                                 SDL_bool autoclose);
   152 #else
   153 extern DECLSPEC SDL_RWops *SDLCALL SDL_RWFromFP(void * fp,
   154                                                 SDL_bool autoclose);
   155 #endif
   156 
   157 extern DECLSPEC SDL_RWops *SDLCALL SDL_RWFromMem(void *mem, int size);
   158 extern DECLSPEC SDL_RWops *SDLCALL SDL_RWFromConstMem(const void *mem,
   159                                                       int size);
   160 
   161 /*@}*//*RWFrom functions*/
   162 
   163 
   164 extern DECLSPEC SDL_RWops *SDLCALL SDL_AllocRW(void);
   165 extern DECLSPEC void SDLCALL SDL_FreeRW(SDL_RWops * area);
   166 
   167 #define RW_SEEK_SET	0       /**< Seek from the beginning of data */
   168 #define RW_SEEK_CUR	1       /**< Seek relative to current read point */
   169 #define RW_SEEK_END	2       /**< Seek relative to the end of data */
   170 
   171 /**
   172  *  \name Read/write macros
   173  *  
   174  *  Macros to easily read and write from an SDL_RWops structure.
   175  */
   176 /*@{*/
   177 #define SDL_RWsize(ctx)	        (ctx)->size(ctx)
   178 #define SDL_RWseek(ctx, offset, whence)	(ctx)->seek(ctx, offset, whence)
   179 #define SDL_RWtell(ctx)			(ctx)->seek(ctx, 0, RW_SEEK_CUR)
   180 #define SDL_RWread(ctx, ptr, size, n)	(ctx)->read(ctx, ptr, size, n)
   181 #define SDL_RWwrite(ctx, ptr, size, n)	(ctx)->write(ctx, ptr, size, n)
   182 #define SDL_RWclose(ctx)		(ctx)->close(ctx)
   183 /*@}*//*Read/write macros*/
   184 
   185 
   186 /** 
   187  *  \name Read endian functions
   188  *  
   189  *  Read an item of the specified endianness and return in native format.
   190  */
   191 /*@{*/
   192 extern DECLSPEC Uint8 SDLCALL SDL_ReadU8(SDL_RWops * src);
   193 extern DECLSPEC Uint16 SDLCALL SDL_ReadLE16(SDL_RWops * src);
   194 extern DECLSPEC Uint16 SDLCALL SDL_ReadBE16(SDL_RWops * src);
   195 extern DECLSPEC Uint32 SDLCALL SDL_ReadLE32(SDL_RWops * src);
   196 extern DECLSPEC Uint32 SDLCALL SDL_ReadBE32(SDL_RWops * src);
   197 extern DECLSPEC Uint64 SDLCALL SDL_ReadLE64(SDL_RWops * src);
   198 extern DECLSPEC Uint64 SDLCALL SDL_ReadBE64(SDL_RWops * src);
   199 /*@}*//*Read endian functions*/
   200 
   201 /** 
   202  *  \name Write endian functions
   203  *  
   204  *  Write an item of native format to the specified endianness.
   205  */
   206 /*@{*/
   207 extern DECLSPEC size_t SDLCALL SDL_WriteU8(SDL_RWops * dst, Uint8 value);
   208 extern DECLSPEC size_t SDLCALL SDL_WriteLE16(SDL_RWops * dst, Uint16 value);
   209 extern DECLSPEC size_t SDLCALL SDL_WriteBE16(SDL_RWops * dst, Uint16 value);
   210 extern DECLSPEC size_t SDLCALL SDL_WriteLE32(SDL_RWops * dst, Uint32 value);
   211 extern DECLSPEC size_t SDLCALL SDL_WriteBE32(SDL_RWops * dst, Uint32 value);
   212 extern DECLSPEC size_t SDLCALL SDL_WriteLE64(SDL_RWops * dst, Uint64 value);
   213 extern DECLSPEC size_t SDLCALL SDL_WriteBE64(SDL_RWops * dst, Uint64 value);
   214 /*@}*//*Write endian functions*/
   215 
   216 
   217 /* Ends C function definitions when using C++ */
   218 #ifdef __cplusplus
   219 /* *INDENT-OFF* */
   220 }
   221 /* *INDENT-ON* */
   222 #endif
   223 #include "close_code.h"
   224 
   225 #endif /* _SDL_rwops_h */
   226 
   227 /* vi: set ts=4 sw=4 expandtab: */