src/file/SDL_rwops.c
author Sam Lantinga <slouken@libsdl.org>
Sun, 17 Nov 2002 18:59:10 +0000
changeset 543 522e5202014d
parent 297 f6ffac90895c
child 764 974c0fb74bf8
permissions -rw-r--r--
*** empty log message ***
slouken@0
     1
/*
slouken@0
     2
    SDL - Simple DirectMedia Layer
slouken@297
     3
    Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002  Sam Lantinga
slouken@0
     4
slouken@0
     5
    This library is free software; you can redistribute it and/or
slouken@0
     6
    modify it under the terms of the GNU Library General Public
slouken@0
     7
    License as published by the Free Software Foundation; either
slouken@0
     8
    version 2 of the License, or (at your option) any later version.
slouken@0
     9
slouken@0
    10
    This library is distributed in the hope that it will be useful,
slouken@0
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
slouken@0
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
slouken@0
    13
    Library General Public License for more details.
slouken@0
    14
slouken@0
    15
    You should have received a copy of the GNU Library General Public
slouken@0
    16
    License along with this library; if not, write to the Free
slouken@0
    17
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
slouken@0
    18
slouken@0
    19
    Sam Lantinga
slouken@252
    20
    slouken@libsdl.org
slouken@0
    21
*/
slouken@0
    22
slouken@0
    23
#ifdef SAVE_RCSID
slouken@0
    24
static char rcsid =
slouken@0
    25
 "@(#) $Id$";
slouken@0
    26
#endif
slouken@0
    27
slouken@0
    28
/* This file provides a general interface for SDL to read and write
slouken@0
    29
   data sources.  It can easily be extended to files, memory, etc.
slouken@0
    30
*/
slouken@0
    31
slouken@0
    32
#include <stdlib.h>
slouken@36
    33
#include <stdio.h>
slouken@0
    34
#include <string.h>
slouken@0
    35
slouken@0
    36
#include "SDL_error.h"
slouken@0
    37
#include "SDL_rwops.h"
slouken@0
    38
slouken@0
    39
/* Functions to read/write stdio file pointers */
slouken@0
    40
slouken@0
    41
static int stdio_seek(SDL_RWops *context, int offset, int whence)
slouken@0
    42
{
slouken@0
    43
	if ( fseek(context->hidden.stdio.fp, offset, whence) == 0 ) {
slouken@0
    44
		return(ftell(context->hidden.stdio.fp));
slouken@0
    45
	} else {
slouken@0
    46
		SDL_Error(SDL_EFSEEK);
slouken@0
    47
		return(-1);
slouken@0
    48
	}
slouken@0
    49
}
slouken@0
    50
static int stdio_read(SDL_RWops *context, void *ptr, int size, int maxnum)
slouken@0
    51
{
slouken@0
    52
	size_t nread;
slouken@0
    53
slouken@0
    54
	nread = fread(ptr, size, maxnum, context->hidden.stdio.fp); 
slouken@0
    55
	if ( nread == 0 && ferror(context->hidden.stdio.fp) ) {
slouken@0
    56
		SDL_Error(SDL_EFREAD);
slouken@0
    57
	}
slouken@0
    58
	return(nread);
slouken@0
    59
}
slouken@0
    60
static int stdio_write(SDL_RWops *context, const void *ptr, int size, int num)
slouken@0
    61
{
slouken@0
    62
	size_t nwrote;
slouken@0
    63
slouken@0
    64
	nwrote = fwrite(ptr, size, num, context->hidden.stdio.fp);
slouken@0
    65
	if ( nwrote == 0 && ferror(context->hidden.stdio.fp) ) {
slouken@0
    66
		SDL_Error(SDL_EFWRITE);
slouken@0
    67
	}
slouken@0
    68
	return(nwrote);
slouken@0
    69
}
slouken@0
    70
static int stdio_close(SDL_RWops *context)
slouken@0
    71
{
slouken@0
    72
	if ( context ) {
slouken@0
    73
		if ( context->hidden.stdio.autoclose ) {
slouken@0
    74
			/* WARNING:  Check the return value here! */
slouken@0
    75
			fclose(context->hidden.stdio.fp);
slouken@0
    76
		}
slouken@0
    77
		free(context);
slouken@0
    78
	}
slouken@0
    79
	return(0);
slouken@0
    80
}
slouken@0
    81
slouken@0
    82
/* Functions to read/write memory pointers */
slouken@0
    83
slouken@0
    84
static int mem_seek(SDL_RWops *context, int offset, int whence)
slouken@0
    85
{
slouken@0
    86
	Uint8 *newpos;
slouken@0
    87
slouken@0
    88
	switch (whence) {
slouken@0
    89
		case SEEK_SET:
slouken@0
    90
			newpos = context->hidden.mem.base+offset;
slouken@0
    91
			break;
slouken@0
    92
		case SEEK_CUR:
slouken@0
    93
			newpos = context->hidden.mem.here+offset;
slouken@0
    94
			break;
slouken@0
    95
		case SEEK_END:
slouken@0
    96
			newpos = context->hidden.mem.stop+offset;
slouken@0
    97
			break;
slouken@0
    98
		default:
slouken@0
    99
			SDL_SetError("Unknown value for 'whence'");
slouken@0
   100
			return(-1);
slouken@0
   101
	}
slouken@0
   102
	if ( newpos < context->hidden.mem.base ) {
slouken@0
   103
		newpos = context->hidden.mem.base;
slouken@0
   104
	}
slouken@0
   105
	if ( newpos > context->hidden.mem.stop ) {
slouken@0
   106
		newpos = context->hidden.mem.stop;
slouken@0
   107
	}
slouken@0
   108
	context->hidden.mem.here = newpos;
slouken@0
   109
	return(context->hidden.mem.here-context->hidden.mem.base);
slouken@0
   110
}
slouken@0
   111
static int mem_read(SDL_RWops *context, void *ptr, int size, int maxnum)
slouken@0
   112
{
slouken@0
   113
	int num;
slouken@0
   114
slouken@0
   115
	num = maxnum;
slouken@0
   116
	if ( (context->hidden.mem.here + (num*size)) > context->hidden.mem.stop ) {
slouken@0
   117
		num = (context->hidden.mem.stop-context->hidden.mem.here)/size;
slouken@0
   118
	}
slouken@0
   119
	memcpy(ptr, context->hidden.mem.here, num*size);
slouken@0
   120
	context->hidden.mem.here += num*size;
slouken@0
   121
	return(num);
slouken@0
   122
}
slouken@0
   123
static int mem_write(SDL_RWops *context, const void *ptr, int size, int num)
slouken@0
   124
{
slouken@0
   125
	if ( (context->hidden.mem.here + (num*size)) > context->hidden.mem.stop ) {
slouken@0
   126
		num = (context->hidden.mem.stop-context->hidden.mem.here)/size;
slouken@0
   127
	}
slouken@0
   128
	memcpy(context->hidden.mem.here, ptr, num*size);
slouken@0
   129
	context->hidden.mem.here += num*size;
slouken@0
   130
	return(num);
slouken@0
   131
}
slouken@0
   132
static int mem_close(SDL_RWops *context)
slouken@0
   133
{
slouken@0
   134
	if ( context ) {
slouken@0
   135
		free(context);
slouken@0
   136
	}
slouken@0
   137
	return(0);
slouken@0
   138
}
slouken@0
   139
slouken@0
   140
/* Functions to create SDL_RWops structures from various data sources */
slouken@0
   141
#ifdef WIN32
slouken@0
   142
/* Aggh.  You can't (apparently) open a file in an application and
slouken@0
   143
   read from it in a DLL.
slouken@0
   144
*/
slouken@0
   145
static int in_sdl = 0;
slouken@0
   146
#endif
slouken@0
   147
slouken@0
   148
#ifdef macintosh
slouken@0
   149
/*
slouken@0
   150
 * translate unix-style slash-separated filename to mac-style colon-separated
slouken@0
   151
 * name; return malloced string
slouken@0
   152
 */
slouken@0
   153
static char *unix_to_mac(const char *file)
slouken@0
   154
{
slouken@0
   155
	int flen = strlen(file);
slouken@0
   156
	char *path = malloc(flen + 2);
slouken@0
   157
	const char *src = file;
slouken@0
   158
	char *dst = path;
slouken@0
   159
	if(*src == '/') {
slouken@0
   160
		/* really depends on filesystem layout, hope for the best */
slouken@0
   161
		src++;
slouken@0
   162
	} else {
slouken@0
   163
		/* Check if this is a MacOS path to begin with */
slouken@0
   164
		if(*src != ':')
slouken@0
   165
			*dst++ = ':';   /* relative paths begin with ':' */
slouken@0
   166
	}
slouken@0
   167
	while(src < file + flen) {
slouken@0
   168
		const char *end = strchr(src, '/');
slouken@0
   169
		int len;
slouken@0
   170
		if(!end)
slouken@0
   171
			end = file + flen; /* last component */
slouken@0
   172
		len = end - src;
slouken@0
   173
		if(len == 0 || (len == 1 && src[0] == '.')) {
slouken@0
   174
			/* remove repeated slashes and . */
slouken@0
   175
		} else {
slouken@0
   176
			if(len == 2 && src[0] == '.' && src[1] == '.') {
slouken@0
   177
				/* replace .. with the empty string */
slouken@0
   178
			} else {
slouken@0
   179
				memcpy(dst, src, len);
slouken@0
   180
				dst += len;
slouken@0
   181
			}
slouken@0
   182
			if(end < file + flen)
slouken@0
   183
				*dst++ = ':';
slouken@0
   184
		}
slouken@0
   185
		src = end + 1;
slouken@0
   186
	}
slouken@0
   187
	*dst++ = '\0';
slouken@0
   188
	return path;
slouken@0
   189
}
slouken@0
   190
#endif /* macintosh */
slouken@0
   191
slouken@0
   192
SDL_RWops *SDL_RWFromFile(const char *file, const char *mode)
slouken@0
   193
{
slouken@0
   194
	FILE *fp;
slouken@0
   195
	SDL_RWops *rwops;
slouken@0
   196
slouken@0
   197
	rwops = NULL;
slouken@0
   198
slouken@0
   199
#ifdef macintosh
slouken@0
   200
	{
slouken@0
   201
		char *mpath = unix_to_mac(file);
slouken@0
   202
		fp = fopen(mpath, mode);
slouken@0
   203
		free(mpath);
slouken@0
   204
	}
slouken@0
   205
#else
slouken@0
   206
	fp = fopen(file, mode);
slouken@0
   207
#endif
slouken@0
   208
	if ( fp == NULL ) {
slouken@0
   209
		SDL_SetError("Couldn't open %s", file);
slouken@0
   210
	} else {
slouken@0
   211
#ifdef WIN32
slouken@0
   212
		in_sdl = 1;
slouken@0
   213
		rwops = SDL_RWFromFP(fp, 1);
slouken@0
   214
		in_sdl = 0;
slouken@0
   215
#else
slouken@0
   216
		rwops = SDL_RWFromFP(fp, 1);
slouken@0
   217
#endif
slouken@0
   218
	}
slouken@0
   219
	return(rwops);
slouken@0
   220
}
slouken@0
   221
slouken@0
   222
SDL_RWops *SDL_RWFromFP(FILE *fp, int autoclose)
slouken@0
   223
{
slouken@0
   224
	SDL_RWops *rwops;
slouken@0
   225
slouken@0
   226
#ifdef WIN32
slouken@0
   227
	if ( ! in_sdl ) {
slouken@543
   228
		SDL_SetError("You can't pass a FILE pointer to a DLL (?)");
slouken@0
   229
		/*return(NULL);*/
slouken@0
   230
	}
slouken@0
   231
#endif
slouken@0
   232
	rwops = SDL_AllocRW();
slouken@0
   233
	if ( rwops != NULL ) {
slouken@0
   234
		rwops->seek = stdio_seek;
slouken@0
   235
		rwops->read = stdio_read;
slouken@0
   236
		rwops->write = stdio_write;
slouken@0
   237
		rwops->close = stdio_close;
slouken@0
   238
		rwops->hidden.stdio.fp = fp;
slouken@0
   239
		rwops->hidden.stdio.autoclose = autoclose;
slouken@0
   240
	}
slouken@0
   241
	return(rwops);
slouken@0
   242
}
slouken@0
   243
slouken@0
   244
SDL_RWops *SDL_RWFromMem(void *mem, int size)
slouken@0
   245
{
slouken@0
   246
	SDL_RWops *rwops;
slouken@0
   247
slouken@0
   248
	rwops = SDL_AllocRW();
slouken@0
   249
	if ( rwops != NULL ) {
slouken@0
   250
		rwops->seek = mem_seek;
slouken@0
   251
		rwops->read = mem_read;
slouken@0
   252
		rwops->write = mem_write;
slouken@0
   253
		rwops->close = mem_close;
slouken@0
   254
		rwops->hidden.mem.base = (Uint8 *)mem;
slouken@0
   255
		rwops->hidden.mem.here = rwops->hidden.mem.base;
slouken@0
   256
		rwops->hidden.mem.stop = rwops->hidden.mem.base+size;
slouken@0
   257
	}
slouken@0
   258
	return(rwops);
slouken@0
   259
}
slouken@0
   260
slouken@0
   261
SDL_RWops *SDL_AllocRW(void)
slouken@0
   262
{
slouken@0
   263
	SDL_RWops *area;
slouken@0
   264
slouken@0
   265
	area = (SDL_RWops *)malloc(sizeof *area);
slouken@0
   266
	if ( area == NULL ) {
slouken@0
   267
		SDL_OutOfMemory();
slouken@0
   268
	}
slouken@0
   269
	return(area);
slouken@0
   270
}
slouken@0
   271
slouken@0
   272
void SDL_FreeRW(SDL_RWops *area)
slouken@0
   273
{
slouken@0
   274
	free(area);
slouken@0
   275
}