include/SDL_rwops.h
author Sam Lantinga <slouken@libsdl.org>
Fri, 08 Apr 2011 13:03:26 -0700
changeset 5535 96594ac5fd1a
parent 5262 b530ef003506
child 5582 1281a3f1f0a6
permissions -rw-r--r--
SDL 1.3 is now under the zlib license.
slouken@0
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@5535
     3
  Copyright (C) 1997-2011 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
slouken@3407
    43
/**
slouken@3407
    44
 * This is the read/write operation structure -- very basic.
slouken@3407
    45
 */
slouken@1895
    46
typedef struct SDL_RWops
slouken@1895
    47
{
slouken@3407
    48
    /**
slouken@3407
    49
     *  Seek to \c offset relative to \c whence, one of stdio's whence values:
slouken@3407
    50
     *  RW_SEEK_SET, RW_SEEK_CUR, RW_SEEK_END
slouken@3407
    51
     *  
slouken@4971
    52
     *  \return the final offset in the data stream.
slouken@1895
    53
     */
slouken@2160
    54
    long (SDLCALL * seek) (struct SDL_RWops * context, long offset,
slouken@2160
    55
                           int whence);
slouken@0
    56
slouken@3407
    57
    /**
icculus@3614
    58
     *  Read up to \c maxnum objects each of size \c size from the data
slouken@4971
    59
     *  stream to the area pointed at by \c ptr.
slouken@3407
    60
     *  
slouken@3407
    61
     *  \return the number of objects read, or 0 at error or end of file.
slouken@1895
    62
     */
slouken@3407
    63
    size_t(SDLCALL * read) (struct SDL_RWops * context, void *ptr,
slouken@3407
    64
                            size_t size, size_t maxnum);
slouken@0
    65
slouken@3407
    66
    /**
slouken@4864
    67
     *  Write exactly \c num objects each of size \c size from the area
slouken@4971
    68
     *  pointed at by \c ptr to data stream.
slouken@3407
    69
     *  
slouken@3407
    70
     *  \return the number of objects written, or 0 at error or end of file.
slouken@1895
    71
     */
slouken@3407
    72
    size_t(SDLCALL * write) (struct SDL_RWops * context, const void *ptr,
slouken@3407
    73
                             size_t size, size_t num);
slouken@0
    74
slouken@3407
    75
    /**
slouken@3407
    76
     *  Close and free an allocated SDL_RWops structure.
slouken@3407
    77
     *  
slouken@3407
    78
     *  \return 0 if successful or -1 on write error when flushing data.
slouken@2160
    79
     */
slouken@1895
    80
    int (SDLCALL * close) (struct SDL_RWops * context);
slouken@0
    81
slouken@1895
    82
    Uint32 type;
slouken@1895
    83
    union
slouken@1895
    84
    {
slouken@5086
    85
#ifdef __WIN32__
slouken@1895
    86
        struct
slouken@1895
    87
        {
slouken@2160
    88
            SDL_bool append;
slouken@1895
    89
            void *h;
slouken@2159
    90
            struct
slouken@2159
    91
            {
slouken@2159
    92
                void *data;
slouken@3253
    93
                size_t size;
slouken@3253
    94
                size_t left;
slouken@2159
    95
            } buffer;
slouken@5062
    96
        } windowsio;
slouken@1447
    97
#endif
slouken@1895
    98
#ifdef HAVE_STDIO_H
slouken@1895
    99
        struct
slouken@1895
   100
        {
slouken@2160
   101
            SDL_bool autoclose;
slouken@1895
   102
            FILE *fp;
slouken@1895
   103
        } stdio;
slouken@1330
   104
#endif
slouken@1895
   105
        struct
slouken@1895
   106
        {
slouken@1895
   107
            Uint8 *base;
slouken@1895
   108
            Uint8 *here;
slouken@1895
   109
            Uint8 *stop;
slouken@1895
   110
        } mem;
slouken@1895
   111
        struct
slouken@1895
   112
        {
slouken@1895
   113
            void *data1;
slouken@1895
   114
        } unknown;
slouken@1895
   115
    } hidden;
slouken@0
   116
slouken@0
   117
} SDL_RWops;
slouken@0
   118
slouken@0
   119
slouken@3407
   120
/**
slouken@3407
   121
 *  \name RWFrom functions
slouken@3407
   122
 *  
slouken@4971
   123
 *  Functions to create SDL_RWops structures from various data streams.
slouken@3407
   124
 */
slouken@3407
   125
/*@{*/
slouken@0
   126
slouken@1895
   127
extern DECLSPEC SDL_RWops *SDLCALL SDL_RWFromFile(const char *file,
slouken@1895
   128
                                                  const char *mode);
slouken@0
   129
slouken@1330
   130
#ifdef HAVE_STDIO_H
slouken@2161
   131
extern DECLSPEC SDL_RWops *SDLCALL SDL_RWFromFP(FILE * fp,
slouken@2161
   132
                                                SDL_bool autoclose);
slouken@3564
   133
#else
slouken@3564
   134
extern DECLSPEC SDL_RWops *SDLCALL SDL_RWFromFP(void * fp,
slouken@3564
   135
                                                SDL_bool autoclose);
slouken@1330
   136
#endif
slouken@0
   137
slouken@1895
   138
extern DECLSPEC SDL_RWops *SDLCALL SDL_RWFromMem(void *mem, int size);
slouken@1895
   139
extern DECLSPEC SDL_RWops *SDLCALL SDL_RWFromConstMem(const void *mem,
slouken@1895
   140
                                                      int size);
slouken@0
   141
slouken@3407
   142
/*@}*//*RWFrom functions*/
slouken@3407
   143
slouken@3407
   144
slouken@1895
   145
extern DECLSPEC SDL_RWops *SDLCALL SDL_AllocRW(void);
slouken@1895
   146
extern DECLSPEC void SDLCALL SDL_FreeRW(SDL_RWops * area);
slouken@0
   147
slouken@3407
   148
#define RW_SEEK_SET	0       /**< Seek from the beginning of data */
slouken@3407
   149
#define RW_SEEK_CUR	1       /**< Seek relative to current read point */
slouken@3407
   150
#define RW_SEEK_END	2       /**< Seek relative to the end of data */
slouken@1330
   151
slouken@3407
   152
/**
slouken@3407
   153
 *  \name Read/write macros
slouken@3407
   154
 *  
slouken@3407
   155
 *  Macros to easily read and write from an SDL_RWops structure.
slouken@3407
   156
 */
slouken@3407
   157
/*@{*/
slouken@0
   158
#define SDL_RWseek(ctx, offset, whence)	(ctx)->seek(ctx, offset, whence)
slouken@1330
   159
#define SDL_RWtell(ctx)			(ctx)->seek(ctx, 0, RW_SEEK_CUR)
slouken@0
   160
#define SDL_RWread(ctx, ptr, size, n)	(ctx)->read(ctx, ptr, size, n)
slouken@0
   161
#define SDL_RWwrite(ctx, ptr, size, n)	(ctx)->write(ctx, ptr, size, n)
slouken@0
   162
#define SDL_RWclose(ctx)		(ctx)->close(ctx)
slouken@3407
   163
/*@}*//*Read/write macros*/
slouken@0
   164
slouken@0
   165
slouken@3407
   166
/** 
slouken@3407
   167
 *  \name Read endian functions
slouken@3407
   168
 *  
slouken@3407
   169
 *  Read an item of the specified endianness and return in native format.
slouken@3407
   170
 */
slouken@3407
   171
/*@{*/
slouken@1895
   172
extern DECLSPEC Uint16 SDLCALL SDL_ReadLE16(SDL_RWops * src);
slouken@1895
   173
extern DECLSPEC Uint16 SDLCALL SDL_ReadBE16(SDL_RWops * src);
slouken@1895
   174
extern DECLSPEC Uint32 SDLCALL SDL_ReadLE32(SDL_RWops * src);
slouken@1895
   175
extern DECLSPEC Uint32 SDLCALL SDL_ReadBE32(SDL_RWops * src);
slouken@1895
   176
extern DECLSPEC Uint64 SDLCALL SDL_ReadLE64(SDL_RWops * src);
slouken@1895
   177
extern DECLSPEC Uint64 SDLCALL SDL_ReadBE64(SDL_RWops * src);
slouken@3407
   178
/*@}*//*Read endian functions*/
slouken@1354
   179
slouken@3407
   180
/** 
slouken@3407
   181
 *  \name Write endian functions
slouken@3407
   182
 *  
slouken@3407
   183
 *  Write an item of native format to the specified endianness.
slouken@3407
   184
 */
slouken@3407
   185
/*@{*/
slouken@3253
   186
extern DECLSPEC size_t SDLCALL SDL_WriteLE16(SDL_RWops * dst, Uint16 value);
slouken@3253
   187
extern DECLSPEC size_t SDLCALL SDL_WriteBE16(SDL_RWops * dst, Uint16 value);
slouken@3253
   188
extern DECLSPEC size_t SDLCALL SDL_WriteLE32(SDL_RWops * dst, Uint32 value);
slouken@3253
   189
extern DECLSPEC size_t SDLCALL SDL_WriteBE32(SDL_RWops * dst, Uint32 value);
slouken@3253
   190
extern DECLSPEC size_t SDLCALL SDL_WriteLE64(SDL_RWops * dst, Uint64 value);
slouken@3253
   191
extern DECLSPEC size_t SDLCALL SDL_WriteBE64(SDL_RWops * dst, Uint64 value);
slouken@3407
   192
/*@}*//*Write endian functions*/
slouken@1354
   193
slouken@1354
   194
slouken@0
   195
/* Ends C function definitions when using C++ */
slouken@0
   196
#ifdef __cplusplus
slouken@1895
   197
/* *INDENT-OFF* */
slouken@0
   198
}
slouken@1895
   199
/* *INDENT-ON* */
slouken@0
   200
#endif
slouken@0
   201
#include "close_code.h"
slouken@0
   202
slouken@1402
   203
#endif /* _SDL_rwops_h */
slouken@1895
   204
slouken@1895
   205
/* vi: set ts=4 sw=4 expandtab: */