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