Simplified Win32 file opening code SDL-1.2
authorSam Lantinga <slouken@libsdl.org>
Tue, 10 Jul 2007 16:01:46 +0000
branchSDL-1.2
changeset 4038eeb0862b9697
parent 4037 48465c97d4bc
child 4039 7fd9a811efc7
Simplified Win32 file opening code
src/file/SDL_rwops.c
     1.1 --- a/src/file/SDL_rwops.c	Tue Jul 10 15:39:14 2007 +0000
     1.2 +++ b/src/file/SDL_rwops.c	Tue Jul 10 16:01:46 2007 +0000
     1.3 @@ -56,12 +56,7 @@
     1.4  	int		a_mode;
     1.5  
     1.6  	if (!context)
     1.7 -		return -1;
     1.8 -		
     1.9 -	context->hidden.win32io.h = INVALID_HANDLE_VALUE; /* mark this as unusable */
    1.10 -	context->hidden.win32io.buffer.data = NULL;
    1.11 -	context->hidden.win32io.buffer.size = 0;
    1.12 -	context->hidden.win32io.buffer.left = 0;
    1.13 +		return -1; /* failed (invalid call) */
    1.14  
    1.15  	/* "r" = reading, file must exist */
    1.16  	/* "w" = writing, truncate existing, file may not exist */
    1.17 @@ -77,13 +72,7 @@
    1.18  	w_right    = ( a_mode || SDL_strchr(mode,'+') || truncate ) ? GENERIC_WRITE : 0;
    1.19  
    1.20  	if (!r_right && !w_right) /* inconsistent mode */
    1.21 -		return -1; /* failed (invalid call)*/
    1.22 -
    1.23 -	context->hidden.win32io.buffer.data = (char *)SDL_malloc(READAHEAD_BUFFER_SIZE);
    1.24 -	if (!context->hidden.win32io.buffer.data) {
    1.25 -		SDL_OutOfMemory();
    1.26 -		return -1;
    1.27 -	}
    1.28 +		return -1; /* failed (invalid call) */
    1.29  
    1.30  #ifdef _WIN32_WCE
    1.31  	{
    1.32 @@ -93,8 +82,6 @@
    1.33  		if ( MultiByteToWideChar(CP_UTF8, 0, filename, -1, filenameW, size) == 0 ) {
    1.34  			SDL_SetError("Unable to convert filename to Unicode");
    1.35  			SDL_stack_free(filenameW);
    1.36 -			SDL_free(context->hidden.win32io.buffer.data);
    1.37 -			context->hidden.win32io.buffer.data = NULL;
    1.38  			return -1;
    1.39  		}
    1.40  		h = CreateFile(filenameW, (w_right|r_right), (w_right)? 0 : FILE_SHARE_READ, 
    1.41 @@ -114,13 +101,20 @@
    1.42  
    1.43  	if (h==INVALID_HANDLE_VALUE) {
    1.44  		SDL_SetError("Couldn't open %s",filename);
    1.45 -		SDL_free(context->hidden.win32io.buffer.data);
    1.46 -		context->hidden.win32io.buffer.data = NULL;
    1.47  		return -2; /* failed (CreateFile) */
    1.48  	}
    1.49  	context->hidden.win32io.h = h;
    1.50  	context->hidden.win32io.append = a_mode;
    1.51  
    1.52 +	context->hidden.win32io.buffer.data = (char *)SDL_malloc(READAHEAD_BUFFER_SIZE);
    1.53 +	if (!context->hidden.win32io.buffer.data) {
    1.54 +		SDL_OutOfMemory();
    1.55 +		CloseHandle(context->hidden.win32io.h);
    1.56 +		return -1;
    1.57 +	}
    1.58 +	context->hidden.win32io.buffer.size = 0;
    1.59 +	context->hidden.win32io.buffer.left = 0;
    1.60 +
    1.61  	return 0; /* ok */
    1.62  }
    1.63  static int SDLCALL win32_file_seek(SDL_RWops *context, int offset, int whence)
    1.64 @@ -435,8 +429,7 @@
    1.65  	rwops = SDL_AllocRW();
    1.66  	if (!rwops)
    1.67  		return NULL; /* SDL_SetError already setup by SDL_AllocRW() */
    1.68 -	rwops->hidden.win32io.h = INVALID_HANDLE_VALUE;
    1.69 -	if (win32_file_open(rwops,file,mode)) {
    1.70 +	if (win32_file_open(rwops,file,mode) < 0) {
    1.71  		SDL_FreeRW(rwops);
    1.72  		return NULL;
    1.73  	}