include/SDL_rwops.h
author Bob Pendleton <bob@pendleton.com>
Thu, 09 Jul 2009 21:31:27 +0000
changeset 3216 48a80f2a7ff2
parent 2859 99210400e8b9
child 3253 5d7ef5970073
permissions -rw-r--r--
volitile... duh, yeah the variable need to be labeled volitile
     1 /*
     2     SDL - Simple DirectMedia Layer
     3     Copyright (C) 1997-2009 Sam Lantinga
     4 
     5     This library is free software; you can redistribute it and/or
     6     modify it under the terms of the GNU Lesser General Public
     7     License as published by the Free Software Foundation; either
     8     version 2.1 of the License, or (at your option) any later version.
     9 
    10     This library is distributed in the hope that it will be useful,
    11     but WITHOUT ANY WARRANTY; without even the implied warranty of
    12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    13     Lesser General Public License for more details.
    14 
    15     You should have received a copy of the GNU Lesser General Public
    16     License along with this library; if not, write to the Free Software
    17     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
    18 
    19     Sam Lantinga
    20     slouken@libsdl.org
    21 */
    22 
    23 /**
    24  * \file SDL_rwops.h
    25  *
    26  * This file provides a general interface for SDL to read and write
    27  * data sources.  It can easily be extended to files, memory, etc.
    28  */
    29 
    30 #ifndef _SDL_rwops_h
    31 #define _SDL_rwops_h
    32 
    33 #include "SDL_stdinc.h"
    34 #include "SDL_error.h"
    35 
    36 #include "begin_code.h"
    37 /* Set up for C function definitions, even when using C++ */
    38 #ifdef __cplusplus
    39 /* *INDENT-OFF* */
    40 extern "C" {
    41 /* *INDENT-ON* */
    42 #endif
    43 
    44 /* This is the read/write operation structure -- very basic */
    45 
    46 typedef struct SDL_RWops
    47 {
    48     /* Seek to 'offset' relative to whence, one of stdio's whence values:
    49        RW_SEEK_SET, RW_SEEK_CUR, RW_SEEK_END
    50        Returns the final offset in the data source.
    51      */
    52     long (SDLCALL * seek) (struct SDL_RWops * context, long offset,
    53                            int whence);
    54 
    55     /* Read up to 'num' objects each of size 'objsize' from the data
    56        source to the area pointed at by 'ptr'.
    57        Returns the number of objects read, or 0 at error or end of file.
    58      */
    59       size_t(SDLCALL * read) (struct SDL_RWops * context, void *ptr,
    60                               size_t size, size_t maxnum);
    61 
    62     /* Write exactly 'num' objects each of size 'objsize' from the area
    63        pointed at by 'ptr' to data source.
    64        Returns the number of objects written, or 0 at error or end of file.
    65      */
    66       size_t(SDLCALL * write) (struct SDL_RWops * context, const void *ptr,
    67                                size_t size, size_t num);
    68 
    69     /* Close and free an allocated SDL_RWops structure.
    70        Returns 0 if successful or -1 on write error when flushing data.
    71      */
    72     int (SDLCALL * close) (struct SDL_RWops * context);
    73 
    74     Uint32 type;
    75     union
    76     {
    77 #ifdef __WIN32__
    78         struct
    79         {
    80             SDL_bool append;
    81             void *h;
    82             struct
    83             {
    84                 void *data;
    85                 int size;
    86                 int left;
    87             } buffer;
    88         } win32io;
    89 #endif
    90 #ifdef HAVE_STDIO_H
    91         struct
    92         {
    93             SDL_bool autoclose;
    94             FILE *fp;
    95         } stdio;
    96 #endif
    97         struct
    98         {
    99             Uint8 *base;
   100             Uint8 *here;
   101             Uint8 *stop;
   102         } mem;
   103         struct
   104         {
   105             void *data1;
   106         } unknown;
   107     } hidden;
   108 
   109 } SDL_RWops;
   110 
   111 
   112 /* Functions to create SDL_RWops structures from various data sources */
   113 
   114 extern DECLSPEC SDL_RWops *SDLCALL SDL_RWFromFile(const char *file,
   115                                                   const char *mode);
   116 
   117 #ifdef HAVE_STDIO_H
   118 extern DECLSPEC SDL_RWops *SDLCALL SDL_RWFromFP(FILE * fp,
   119                                                 SDL_bool autoclose);
   120 #endif
   121 
   122 extern DECLSPEC SDL_RWops *SDLCALL SDL_RWFromMem(void *mem, int size);
   123 extern DECLSPEC SDL_RWops *SDLCALL SDL_RWFromConstMem(const void *mem,
   124                                                       int size);
   125 
   126 extern DECLSPEC SDL_RWops *SDLCALL SDL_AllocRW(void);
   127 extern DECLSPEC void SDLCALL SDL_FreeRW(SDL_RWops * area);
   128 
   129 #define RW_SEEK_SET	0       /* Seek from the beginning of data */
   130 #define RW_SEEK_CUR	1       /* Seek relative to current read point */
   131 #define RW_SEEK_END	2       /* Seek relative to the end of data */
   132 
   133 /* Macros to easily read and write from an SDL_RWops structure */
   134 #define SDL_RWseek(ctx, offset, whence)	(ctx)->seek(ctx, offset, whence)
   135 #define SDL_RWtell(ctx)			(ctx)->seek(ctx, 0, RW_SEEK_CUR)
   136 #define SDL_RWread(ctx, ptr, size, n)	(ctx)->read(ctx, ptr, size, n)
   137 #define SDL_RWwrite(ctx, ptr, size, n)	(ctx)->write(ctx, ptr, size, n)
   138 #define SDL_RWclose(ctx)		(ctx)->close(ctx)
   139 
   140 
   141 /* Read an item of the specified endianness and return in native format */
   142 extern DECLSPEC Uint16 SDLCALL SDL_ReadLE16(SDL_RWops * src);
   143 extern DECLSPEC Uint16 SDLCALL SDL_ReadBE16(SDL_RWops * src);
   144 extern DECLSPEC Uint32 SDLCALL SDL_ReadLE32(SDL_RWops * src);
   145 extern DECLSPEC Uint32 SDLCALL SDL_ReadBE32(SDL_RWops * src);
   146 extern DECLSPEC Uint64 SDLCALL SDL_ReadLE64(SDL_RWops * src);
   147 extern DECLSPEC Uint64 SDLCALL SDL_ReadBE64(SDL_RWops * src);
   148 
   149 /* Write an item of native format to the specified endianness */
   150 extern DECLSPEC int SDLCALL SDL_WriteLE16(SDL_RWops * dst, Uint16 value);
   151 extern DECLSPEC int SDLCALL SDL_WriteBE16(SDL_RWops * dst, Uint16 value);
   152 extern DECLSPEC int SDLCALL SDL_WriteLE32(SDL_RWops * dst, Uint32 value);
   153 extern DECLSPEC int SDLCALL SDL_WriteBE32(SDL_RWops * dst, Uint32 value);
   154 extern DECLSPEC int SDLCALL SDL_WriteLE64(SDL_RWops * dst, Uint64 value);
   155 extern DECLSPEC int SDLCALL SDL_WriteBE64(SDL_RWops * dst, Uint64 value);
   156 
   157 
   158 /* Ends C function definitions when using C++ */
   159 #ifdef __cplusplus
   160 /* *INDENT-OFF* */
   161 }
   162 /* *INDENT-ON* */
   163 #endif
   164 #include "close_code.h"
   165 
   166 #endif /* _SDL_rwops_h */
   167 
   168 /* vi: set ts=4 sw=4 expandtab: */