Skip to content

Commit

Permalink
Prefer the raw Win32 API over stdio for file RWops
Browse files Browse the repository at this point in the history
  • Loading branch information
slouken committed Feb 27, 2006
1 parent d1e0c81 commit 5b9b100
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 92 deletions.
16 changes: 7 additions & 9 deletions include/SDL_rwops.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,20 +62,18 @@ typedef struct SDL_RWops {

Uint32 type;
union {

#ifdef __WIN32__
struct {
int autoclose;
int append;
void* h;
} win32io;
#endif
#ifdef HAVE_STDIO_H
struct {
int autoclose;
FILE *fp;
} stdio;
#else
#ifdef __WIN32__
struct {
void* h;
int autoclose;
int append;
} win32io;
#endif
#endif
struct {
Uint8 *base;
Expand Down
146 changes: 63 additions & 83 deletions src/file/SDL_rwops.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,53 +29,10 @@
#include "SDL_rwops.h"


#ifdef HAVE_STDIO_H

/* Functions to read/write stdio file pointers */

static int stdio_seek(SDL_RWops *context, int offset, int whence)
{
if ( fseek(context->hidden.stdio.fp, offset, whence) == 0 ) {
return(ftell(context->hidden.stdio.fp));
} else {
SDL_Error(SDL_EFSEEK);
return(-1);
}
}
static int stdio_read(SDL_RWops *context, void *ptr, int size, int maxnum)
{
size_t nread;

nread = fread(ptr, size, maxnum, context->hidden.stdio.fp);
if ( nread == 0 && ferror(context->hidden.stdio.fp) ) {
SDL_Error(SDL_EFREAD);
}
return(nread);
}
static int stdio_write(SDL_RWops *context, const void *ptr, int size, int num)
{
size_t nwrote;
#ifdef __WIN32__

nwrote = fwrite(ptr, size, num, context->hidden.stdio.fp);
if ( nwrote == 0 && ferror(context->hidden.stdio.fp) ) {
SDL_Error(SDL_EFWRITE);
}
return(nwrote);
}
static int stdio_close(SDL_RWops *context)
{
if ( context ) {
if ( context->hidden.stdio.autoclose ) {
/* WARNING: Check the return value here! */
fclose(context->hidden.stdio.fp);
}
SDL_FreeRW(context);
}
return(0);
}
#else /* HAVE_STDIO_H */
/* Functions to read/write Win32 API file pointers */

#ifdef __WIN32__
#define WINDOWS_LEAN_AND_MEAN
#include <windows.h>

Expand Down Expand Up @@ -126,7 +83,6 @@ static int win32_file_open(SDL_RWops *context, const char *filename, const char

return 0; /* ok */
}

static int win32_file_seek(SDL_RWops *context, int offset, int whence) {
DWORD win32whence;
int file_pos;
Expand Down Expand Up @@ -156,7 +112,6 @@ static int win32_file_seek(SDL_RWops *context, int offset, int whence) {
SDL_Error(SDL_EFSEEK);
return -1; /* error */
}

static int win32_file_read(SDL_RWops *context, void *ptr, int size, int maxnum) {

int total_bytes;
Expand All @@ -174,7 +129,6 @@ static int win32_file_read(SDL_RWops *context, void *ptr, int size, int maxnum)
nread = byte_read/size;
return nread;
}

static int win32_file_write(SDL_RWops *context, const void *ptr, int size, int num) {

int total_bytes;
Expand All @@ -201,7 +155,6 @@ static int win32_file_write(SDL_RWops *context, const void *ptr, int size, int n
nwritten = byte_written/size;
return nwritten;
}

static int win32_file_close(SDL_RWops *context) {

if ( context ) {
Expand All @@ -213,10 +166,52 @@ static int win32_file_close(SDL_RWops *context) {
}
return(0);
}
#endif /* __WIN32__ */

#ifdef HAVE_STDIO_H

/* Functions to read/write stdio file pointers */

#endif /* __WIN32__ */
static int stdio_seek(SDL_RWops *context, int offset, int whence)
{
if ( fseek(context->hidden.stdio.fp, offset, whence) == 0 ) {
return(ftell(context->hidden.stdio.fp));
} else {
SDL_Error(SDL_EFSEEK);
return(-1);
}
}
static int stdio_read(SDL_RWops *context, void *ptr, int size, int maxnum)
{
size_t nread;

nread = fread(ptr, size, maxnum, context->hidden.stdio.fp);
if ( nread == 0 && ferror(context->hidden.stdio.fp) ) {
SDL_Error(SDL_EFREAD);
}
return(nread);
}
static int stdio_write(SDL_RWops *context, const void *ptr, int size, int num)
{
size_t nwrote;

nwrote = fwrite(ptr, size, num, context->hidden.stdio.fp);
if ( nwrote == 0 && ferror(context->hidden.stdio.fp) ) {
SDL_Error(SDL_EFWRITE);
}
return(nwrote);
}
static int stdio_close(SDL_RWops *context)
{
if ( context ) {
if ( context->hidden.stdio.autoclose ) {
/* WARNING: Check the return value here! */
fclose(context->hidden.stdio.fp);
}
SDL_FreeRW(context);
}
return(0);
}
#endif /* !HAVE_STDIO_H */

/* Functions to read/write memory pointers */
Expand Down Expand Up @@ -290,13 +285,8 @@ static int mem_close(SDL_RWops *context)
return(0);
}


/* Functions to create SDL_RWops structures from various data sources */
#ifdef __WIN32__
/* Aggh. You can't (apparently) open a file in an application and
read from it in a DLL.
*/
static int in_sdl = 0;
#endif

#ifdef __MACOS__
/*
Expand Down Expand Up @@ -345,7 +335,20 @@ static char *unix_to_mac(const char *file)
SDL_RWops *SDL_RWFromFile(const char *file, const char *mode)
{
SDL_RWops *rwops = NULL;
#ifdef HAVE_STDIO_H

#ifdef __WIN32__
rwops = SDL_AllocRW();
rwops->hidden.win32io.h = INVALID_HANDLE_VALUE;
if (win32_file_open(rwops,file,mode)) {
SDL_FreeRW(rwops);
return NULL;
}
rwops->seek = win32_file_seek;
rwops->read = win32_file_read;
rwops->write = win32_file_write;
rwops->close = win32_file_close;

#elif HAVE_STDIO_H
FILE *fp;

#ifdef __MACOS__
Expand All @@ -360,28 +363,12 @@ SDL_RWops *SDL_RWFromFile(const char *file, const char *mode)
if ( fp == NULL ) {
SDL_SetError("Couldn't open %s", file);
} else {
#ifdef __WIN32__
in_sdl = 1;
rwops = SDL_RWFromFP(fp, 1);
in_sdl = 0;
#else
rwops = SDL_RWFromFP(fp, 1);
#endif
}
#else /* HAVE_STDIO_H */
#ifdef __WIN32__
rwops = SDL_AllocRW();
rwops->hidden.win32io.h = INVALID_HANDLE_VALUE;
if (win32_file_open(rwops,file,mode)) {
SDL_FreeRW(rwops);
return NULL;
}
rwops->seek = win32_file_seek;
rwops->read = win32_file_read;
rwops->write = win32_file_write;
rwops->close = win32_file_close;
#endif /* __WIN32__ */
#else
SDL_SetError("SDL not compiled with stdio support");
#endif /* !HAVE_STDIO_H */

return(rwops);
}

Expand All @@ -390,13 +377,6 @@ SDL_RWops *SDL_RWFromFP(FILE *fp, int autoclose)
{
SDL_RWops *rwops = NULL;

#ifdef __WIN32__
if ( ! in_sdl ) {
/* It's when SDL and the app are compiled with different C runtimes */
SDL_SetError("You can't pass a FILE pointer to a DLL (?)");
/*return(NULL);*/
}
#endif
rwops = SDL_AllocRW();
if ( rwops != NULL ) {
rwops->seek = stdio_seek;
Expand Down

0 comments on commit 5b9b100

Please sign in to comment.