src/file/SDL_rwops.c
changeset 1330 450721ad5436
parent 1312 c9b51268668f
child 1336 3692456e7b0f
equal deleted inserted replaced
1329:bc67bbf87818 1330:450721ad5436
    22 
    22 
    23 /* This file provides a general interface for SDL to read and write
    23 /* This file provides a general interface for SDL to read and write
    24    data sources.  It can easily be extended to files, memory, etc.
    24    data sources.  It can easily be extended to files, memory, etc.
    25 */
    25 */
    26 
    26 
    27 #include <stdlib.h>
       
    28 #include <stdio.h>
       
    29 #include <string.h>
       
    30 
       
    31 #include "SDL_error.h"
    27 #include "SDL_error.h"
    32 #include "SDL_rwops.h"
    28 #include "SDL_rwops.h"
       
    29 #include "SDL_stdlib.h"
       
    30 #include "SDL_string.h"
       
    31 
       
    32 #ifdef HAVE_STDIO_H
    33 
    33 
    34 /* Functions to read/write stdio file pointers */
    34 /* Functions to read/write stdio file pointers */
    35 
    35 
    36 static int stdio_seek(SDL_RWops *context, int offset, int whence)
    36 static int stdio_seek(SDL_RWops *context, int offset, int whence)
    37 {
    37 {
    72 		SDL_FreeRW(context);
    72 		SDL_FreeRW(context);
    73 	}
    73 	}
    74 	return(0);
    74 	return(0);
    75 }
    75 }
    76 
    76 
       
    77 #endif /* HAVE_STDIO_H */
       
    78 
    77 /* Functions to read/write memory pointers */
    79 /* Functions to read/write memory pointers */
    78 
    80 
    79 static int mem_seek(SDL_RWops *context, int offset, int whence)
    81 static int mem_seek(SDL_RWops *context, int offset, int whence)
    80 {
    82 {
    81 	Uint8 *newpos;
    83 	Uint8 *newpos;
    82 
    84 
    83 	switch (whence) {
    85 	switch (whence) {
    84 		case SEEK_SET:
    86 		case RW_SEEK_SET:
    85 			newpos = context->hidden.mem.base+offset;
    87 			newpos = context->hidden.mem.base+offset;
    86 			break;
    88 			break;
    87 		case SEEK_CUR:
    89 		case RW_SEEK_CUR:
    88 			newpos = context->hidden.mem.here+offset;
    90 			newpos = context->hidden.mem.here+offset;
    89 			break;
    91 			break;
    90 		case SEEK_END:
    92 		case RW_SEEK_END:
    91 			newpos = context->hidden.mem.stop+offset;
    93 			newpos = context->hidden.mem.stop+offset;
    92 			break;
    94 			break;
    93 		default:
    95 		default:
    94 			SDL_SetError("Unknown value for 'whence'");
    96 			SDL_SetError("Unknown value for 'whence'");
    95 			return(-1);
    97 			return(-1);
   197 }
   199 }
   198 #endif /* macintosh */
   200 #endif /* macintosh */
   199 
   201 
   200 SDL_RWops *SDL_RWFromFile(const char *file, const char *mode)
   202 SDL_RWops *SDL_RWFromFile(const char *file, const char *mode)
   201 {
   203 {
       
   204 	SDL_RWops *rwops = NULL;
       
   205 #ifdef HAVE_STDIO_H
   202 	FILE *fp;
   206 	FILE *fp;
   203 	SDL_RWops *rwops;
       
   204 
       
   205 	rwops = NULL;
       
   206 
   207 
   207 #ifdef macintosh
   208 #ifdef macintosh
   208 	{
   209 	{
   209 		char *mpath = unix_to_mac(file);
   210 		char *mpath = unix_to_mac(file);
   210 		fp = fopen(mpath, mode);
   211 		fp = fopen(mpath, mode);
   222 		in_sdl = 0;
   223 		in_sdl = 0;
   223 #else
   224 #else
   224 		rwops = SDL_RWFromFP(fp, 1);
   225 		rwops = SDL_RWFromFP(fp, 1);
   225 #endif
   226 #endif
   226 	}
   227 	}
       
   228 #endif /* HAVE_STDIO_H */
   227 	return(rwops);
   229 	return(rwops);
   228 }
   230 }
   229 
   231 
       
   232 #ifdef HAVE_STDIO_H
   230 SDL_RWops *SDL_RWFromFP(FILE *fp, int autoclose)
   233 SDL_RWops *SDL_RWFromFP(FILE *fp, int autoclose)
   231 {
   234 {
   232 	SDL_RWops *rwops;
   235 	SDL_RWops *rwops = NULL;
   233 
   236 
   234 #ifdef WIN32
   237 #ifdef WIN32
   235 	if ( ! in_sdl ) {
   238 	if ( ! in_sdl ) {
   236 		/* It's when SDL and the app are compiled with different C runtimes */
   239 		/* It's when SDL and the app are compiled with different C runtimes */
   237 		SDL_SetError("You can't pass a FILE pointer to a DLL (?)");
   240 		SDL_SetError("You can't pass a FILE pointer to a DLL (?)");
   247 		rwops->hidden.stdio.fp = fp;
   250 		rwops->hidden.stdio.fp = fp;
   248 		rwops->hidden.stdio.autoclose = autoclose;
   251 		rwops->hidden.stdio.autoclose = autoclose;
   249 	}
   252 	}
   250 	return(rwops);
   253 	return(rwops);
   251 }
   254 }
       
   255 #endif /* HAVE_STDIO_H */
   252 
   256 
   253 SDL_RWops *SDL_RWFromMem(void *mem, int size)
   257 SDL_RWops *SDL_RWFromMem(void *mem, int size)
   254 {
   258 {
   255 	SDL_RWops *rwops;
   259 	SDL_RWops *rwops;
   256 
   260