/*
    SDL - Simple DirectMedia Layer
    Copyright (C) 1997-2006 Sam Lantinga

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 2.1 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public
    License along with this library; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

    Sam Lantinga
    slouken@libsdl.org
*/
module sdl.c.sdl_rwops;

public
{
	import sdl.c.sdl_stdinc;
	import sdl.c.sdl_error;
}


extern (C):

typedef int function(SDL_RWops* context, int offset, int whence) _seek_func_t;
typedef int function(SDL_RWops *context, void *ptr, int size, int maxnum) _read_func_t;
typedef int function(SDL_RWops *context, void *ptr, int size, int num) _write_func_t;
typedef int function(SDL_RWops *context) _close_func_t;

struct SDL_RWops
{
	/* Seek to 'offset' relative to whence, one of stdio's whence values:
		SEEK_SET, SEEK_CUR, SEEK_END
	   Returns the final offset in the data source.
	 */
	_seek_func_t seek;

	/* Read up to 'num' objects each of size 'objsize' from the data
	   source to the area pointed at by 'ptr'.
	   Returns the number of objects read, or -1 if the read failed.
	 */
	_read_func_t read;

	/* Write exactly 'num' objects each of size 'objsize' from the area
	   pointed at by 'ptr' to data source.
	   Returns 'num', or -1 if the write failed.
	 */
	_write_func_t write;

	/* Close and free an allocated SDL_FSops structure */
	_close_func_t close;

	Uint32 type;
	union {
	    struct {
			int autoclose;
		 	void *fp;
	    } // stdio;
	    struct {
			Uint8 *base;
		 	Uint8 *here;
			Uint8 *stop;
	    } // mem;
	    struct {
			void *data1;
	    } // unknown;
	} // hidden;}
}


SDL_RWops * SDL_RWFromFile(char* file, char* mode);
SDL_RWops * SDL_RWFromFP(void* fp, int autoclose);
SDL_RWops * SDL_RWFromMem(void* mem, int size);
SDL_RWops * SDL_RWFromConstMem(void* mem, int size);
SDL_RWops * SDL_AllocRW();
void SDL_FreeRW(SDL_RWops *area);

enum {
	RW_SEEK_SET = 0,
	RW_SEEK_CUR = 1,
	RW_SEEK_END = 2
}

int SDL_RWseek(SDL_RWops* ctx, int offset, int whence)
{
	_seek_func_t seek;
	seek = ctx.seek;
	return (*seek)(ctx, offset, whence);
}


int SDL_RWtell(SDL_RWops* ctx)
{
	_seek_func_t seek;
	seek = ctx.seek;
	return (*seek)(ctx, 0, RW_SEEK_CUR);
}

int SDL_RWread(SDL_RWops* ctx, void* ptr, int size, int n)
{
	_read_func_t read;
	read = ctx.read;
	return (*read)(ctx, ptr, size, n);
}

int SDL_RWwrite(SDL_RWops* ctx, void* ptr, int size, int n)
{
	_write_func_t write;
	write = ctx.write;
	return (*write)(ctx, ptr, size, n);
}

int SDL_RWclose(SDL_RWops* ctx)
{
	_close_func_t close;
	return (*close)(ctx);
}

Uint16 SDL_ReadLE16(SDL_RWops* src);
Uint16 SDL_ReadBE16(SDL_RWops *src);
Uint32 SDL_ReadLE32(SDL_RWops *src);
Uint32 SDL_ReadBE32(SDL_RWops *src);
Uint64 SDL_ReadLE64(SDL_RWops *src);
Uint64 SDL_ReadBE64(SDL_RWops *src);

int SDL_WriteLE16(SDL_RWops *dst, Uint16 value);
int SDL_WriteBE16(SDL_RWops *dst, Uint16 value);
int SDL_WriteLE32(SDL_RWops *dst, Uint32 value);
int SDL_WriteBE32(SDL_RWops *dst, Uint32 value);
int SDL_WriteLE64(SDL_RWops *dst, Uint64 value);
int SDL_WriteBE64(SDL_RWops *dst, Uint64 value);

