src/file/SDL_rwops.c
changeset 1895 c121d94672cb
parent 1769 290b5baf2fca
child 1978 542c78b6fb12
     1.1 --- a/src/file/SDL_rwops.c	Thu Jul 06 18:01:37 2006 +0000
     1.2 +++ b/src/file/SDL_rwops.c	Mon Jul 10 21:04:37 2006 +0000
     1.3 @@ -43,157 +43,179 @@
     1.4  #define INVALID_SET_FILE_POINTER 0xFFFFFFFF
     1.5  #endif
     1.6  
     1.7 -static int SDLCALL win32_file_open(SDL_RWops *context, const char *filename, const char *mode)
     1.8 +static int SDLCALL
     1.9 +win32_file_open(SDL_RWops * context, const char *filename, const char *mode)
    1.10  {
    1.11  #ifndef _WIN32_WCE
    1.12 -	UINT	old_error_mode;
    1.13 +    UINT old_error_mode;
    1.14  #endif
    1.15 -	HANDLE	h;
    1.16 -	DWORD	r_right, w_right;
    1.17 -	DWORD	must_exist, truncate;
    1.18 -	int		a_mode;
    1.19 +    HANDLE h;
    1.20 +    DWORD r_right, w_right;
    1.21 +    DWORD must_exist, truncate;
    1.22 +    int a_mode;
    1.23  
    1.24 -	if (!context)
    1.25 -		return -1;
    1.26 -		
    1.27 -	context->hidden.win32io.h = INVALID_HANDLE_VALUE; /* mark this as unusable */
    1.28 +    if (!context)
    1.29 +        return -1;
    1.30 +
    1.31 +    context->hidden.win32io.h = INVALID_HANDLE_VALUE;   /* mark this as unusable */
    1.32  
    1.33 -	/* "r" = reading, file must exist */
    1.34 -	/* "w" = writing, truncate existing, file may not exist */
    1.35 -	/* "r+"= reading or writing, file must exist            */
    1.36 -	/* "a" = writing, append file may not exist             */
    1.37 -	/* "a+"= append + read, file may not exist              */
    1.38 -	/* "w+" = read, write, truncate. file may not exist    */
    1.39 -	
    1.40 -	must_exist = ( SDL_strchr(mode,'r') != NULL ) ? OPEN_EXISTING : 0;
    1.41 -	truncate   = ( SDL_strchr(mode,'w') != NULL ) ? CREATE_ALWAYS : 0;
    1.42 -	r_right    = ( SDL_strchr(mode,'+') != NULL || must_exist ) ? GENERIC_READ : 0;
    1.43 -	a_mode     = ( SDL_strchr(mode,'a') != NULL ) ? OPEN_ALWAYS : 0;
    1.44 -	w_right    = ( a_mode || SDL_strchr(mode,'+') || truncate ) ? GENERIC_WRITE : 0;
    1.45 +    /* "r" = reading, file must exist */
    1.46 +    /* "w" = writing, truncate existing, file may not exist */
    1.47 +    /* "r+"= reading or writing, file must exist            */
    1.48 +    /* "a" = writing, append file may not exist             */
    1.49 +    /* "a+"= append + read, file may not exist              */
    1.50 +    /* "w+" = read, write, truncate. file may not exist    */
    1.51  
    1.52 -	if (!r_right && !w_right) /* inconsistent mode */
    1.53 -		return -1; /* failed (invalid call)*/
    1.54 +    must_exist = (SDL_strchr(mode, 'r') != NULL) ? OPEN_EXISTING : 0;
    1.55 +    truncate = (SDL_strchr(mode, 'w') != NULL) ? CREATE_ALWAYS : 0;
    1.56 +    r_right = (SDL_strchr(mode, '+') != NULL
    1.57 +               || must_exist) ? GENERIC_READ : 0;
    1.58 +    a_mode = (SDL_strchr(mode, 'a') != NULL) ? OPEN_ALWAYS : 0;
    1.59 +    w_right = (a_mode || SDL_strchr(mode, '+')
    1.60 +               || truncate) ? GENERIC_WRITE : 0;
    1.61 +
    1.62 +    if (!r_right && !w_right)   /* inconsistent mode */
    1.63 +        return -1;              /* failed (invalid call) */
    1.64  
    1.65  #ifdef _WIN32_WCE
    1.66 -	{
    1.67 -		size_t size = SDL_strlen(filename)+1;
    1.68 -		wchar_t *filenameW = SDL_stack_alloc(wchar_t, size);
    1.69 +    {
    1.70 +        size_t size = SDL_strlen(filename) + 1;
    1.71 +        wchar_t *filenameW = SDL_stack_alloc(wchar_t, size);
    1.72  
    1.73 -		if ( MultiByteToWideChar(CP_UTF8, 0, filename, -1, filenameW, size) == 0 ) {
    1.74 -			SDL_SetError("Unable to convert filename to Unicode");
    1.75 -			SDL_stack_free(filenameW);
    1.76 -			return -1;
    1.77 -		}
    1.78 -		h = CreateFile(filenameW, (w_right|r_right), (w_right)? 0 : FILE_SHARE_READ, 
    1.79 -					   NULL, (must_exist|truncate|a_mode), FILE_ATTRIBUTE_NORMAL,NULL);
    1.80 -		SDL_stack_free(filenameW);
    1.81 -	}
    1.82 +        if (MultiByteToWideChar(CP_UTF8, 0, filename, -1, filenameW, size) ==
    1.83 +            0) {
    1.84 +            SDL_SetError("Unable to convert filename to Unicode");
    1.85 +            SDL_stack_free(filenameW);
    1.86 +            return -1;
    1.87 +        }
    1.88 +        h = CreateFile(filenameW, (w_right | r_right),
    1.89 +                       (w_right) ? 0 : FILE_SHARE_READ, NULL,
    1.90 +                       (must_exist | truncate | a_mode),
    1.91 +                       FILE_ATTRIBUTE_NORMAL, NULL);
    1.92 +        SDL_stack_free(filenameW);
    1.93 +    }
    1.94  #else
    1.95 -	/* Do not open a dialog box if failure */
    1.96 -	old_error_mode = SetErrorMode(SEM_NOOPENFILEERRORBOX|SEM_FAILCRITICALERRORS);
    1.97 +    /* Do not open a dialog box if failure */
    1.98 +    old_error_mode =
    1.99 +        SetErrorMode(SEM_NOOPENFILEERRORBOX | SEM_FAILCRITICALERRORS);
   1.100  
   1.101 -	h = CreateFile(filename, (w_right|r_right), (w_right)? 0 : FILE_SHARE_READ, 
   1.102 -		           NULL, (must_exist|truncate|a_mode), FILE_ATTRIBUTE_NORMAL,NULL);
   1.103 +    h = CreateFile(filename, (w_right | r_right),
   1.104 +                   (w_right) ? 0 : FILE_SHARE_READ, NULL,
   1.105 +                   (must_exist | truncate | a_mode), FILE_ATTRIBUTE_NORMAL,
   1.106 +                   NULL);
   1.107  
   1.108 -	/* restore old behaviour */
   1.109 -	SetErrorMode(old_error_mode);
   1.110 +    /* restore old behaviour */
   1.111 +    SetErrorMode(old_error_mode);
   1.112  #endif /* _WIN32_WCE */
   1.113  
   1.114 -	if (h==INVALID_HANDLE_VALUE) {
   1.115 -		SDL_SetError("Couldn't open %s",filename);
   1.116 -		return -2; /* failed (CreateFile) */
   1.117 -	}
   1.118 -	context->hidden.win32io.h = h;
   1.119 -	context->hidden.win32io.append = a_mode;
   1.120 -	
   1.121 -	return 0; /* ok */
   1.122 +    if (h == INVALID_HANDLE_VALUE) {
   1.123 +        SDL_SetError("Couldn't open %s", filename);
   1.124 +        return -2;              /* failed (CreateFile) */
   1.125 +    }
   1.126 +    context->hidden.win32io.h = h;
   1.127 +    context->hidden.win32io.append = a_mode;
   1.128 +
   1.129 +    return 0;                   /* ok */
   1.130  }
   1.131 -static int SDLCALL win32_file_seek(SDL_RWops *context, int offset, int whence)
   1.132 +static int SDLCALL
   1.133 +win32_file_seek(SDL_RWops * context, int offset, int whence)
   1.134  {
   1.135 -	DWORD win32whence;
   1.136 -	int   file_pos;
   1.137 -	
   1.138 -	if (!context || context->hidden.win32io.h == INVALID_HANDLE_VALUE) {
   1.139 -		SDL_SetError("win32_file_seek: invalid context/file not opened");
   1.140 -		return -1;
   1.141 -	}
   1.142 -	
   1.143 -	switch (whence) {
   1.144 -		case RW_SEEK_SET:		
   1.145 -			win32whence = FILE_BEGIN; break;
   1.146 -		case RW_SEEK_CUR:
   1.147 -			win32whence = FILE_CURRENT; break;
   1.148 -		case RW_SEEK_END:
   1.149 -			win32whence = FILE_END; break;
   1.150 -		default:
   1.151 -			SDL_SetError("win32_file_seek: Unknown value for 'whence'");			
   1.152 -			return -1;
   1.153 -	}
   1.154 -	
   1.155 -	file_pos = SetFilePointer(context->hidden.win32io.h,offset,NULL,win32whence);
   1.156 +    DWORD win32whence;
   1.157 +    int file_pos;
   1.158 +
   1.159 +    if (!context || context->hidden.win32io.h == INVALID_HANDLE_VALUE) {
   1.160 +        SDL_SetError("win32_file_seek: invalid context/file not opened");
   1.161 +        return -1;
   1.162 +    }
   1.163  
   1.164 -	if ( file_pos != INVALID_SET_FILE_POINTER )
   1.165 -		return file_pos; /* success */
   1.166 -	
   1.167 -	SDL_Error(SDL_EFSEEK);
   1.168 -	return -1; /* error */
   1.169 +    switch (whence) {
   1.170 +    case RW_SEEK_SET:
   1.171 +        win32whence = FILE_BEGIN;
   1.172 +        break;
   1.173 +    case RW_SEEK_CUR:
   1.174 +        win32whence = FILE_CURRENT;
   1.175 +        break;
   1.176 +    case RW_SEEK_END:
   1.177 +        win32whence = FILE_END;
   1.178 +        break;
   1.179 +    default:
   1.180 +        SDL_SetError("win32_file_seek: Unknown value for 'whence'");
   1.181 +        return -1;
   1.182 +    }
   1.183 +
   1.184 +    file_pos =
   1.185 +        SetFilePointer(context->hidden.win32io.h, offset, NULL, win32whence);
   1.186 +
   1.187 +    if (file_pos != INVALID_SET_FILE_POINTER)
   1.188 +        return file_pos;        /* success */
   1.189 +
   1.190 +    SDL_Error(SDL_EFSEEK);
   1.191 +    return -1;                  /* error */
   1.192  }
   1.193 -static int SDLCALL win32_file_read(SDL_RWops *context, void *ptr, int size, int maxnum)
   1.194 +static int SDLCALL
   1.195 +win32_file_read(SDL_RWops * context, void *ptr, int size, int maxnum)
   1.196  {
   1.197 -	
   1.198 -	int		total_bytes; 
   1.199 -	DWORD	byte_read,nread;
   1.200 -	
   1.201 -	total_bytes = size*maxnum;
   1.202 -	
   1.203 -	if (!context || context->hidden.win32io.h == INVALID_HANDLE_VALUE || total_bytes<=0 || !size) 	
   1.204 -		return 0;
   1.205 -	
   1.206 -	if (!ReadFile(context->hidden.win32io.h,ptr,total_bytes,&byte_read,NULL)) {
   1.207 -		SDL_Error(SDL_EFREAD);
   1.208 -		return 0;
   1.209 -	}
   1.210 -	nread = byte_read/size;
   1.211 -	return nread;
   1.212 +
   1.213 +    int total_bytes;
   1.214 +    DWORD byte_read, nread;
   1.215 +
   1.216 +    total_bytes = size * maxnum;
   1.217 +
   1.218 +    if (!context || context->hidden.win32io.h == INVALID_HANDLE_VALUE
   1.219 +        || total_bytes <= 0 || !size)
   1.220 +        return 0;
   1.221 +
   1.222 +    if (!ReadFile
   1.223 +        (context->hidden.win32io.h, ptr, total_bytes, &byte_read, NULL)) {
   1.224 +        SDL_Error(SDL_EFREAD);
   1.225 +        return 0;
   1.226 +    }
   1.227 +    nread = byte_read / size;
   1.228 +    return nread;
   1.229  }
   1.230 -static int SDLCALL win32_file_write(SDL_RWops *context, const void *ptr, int size, int num)
   1.231 +static int SDLCALL
   1.232 +win32_file_write(SDL_RWops * context, const void *ptr, int size, int num)
   1.233  {
   1.234 -	
   1.235 -	int		total_bytes; 
   1.236 -	DWORD	byte_written,nwritten;
   1.237 -	
   1.238 -	total_bytes = size*num;
   1.239 +
   1.240 +    int total_bytes;
   1.241 +    DWORD byte_written, nwritten;
   1.242  
   1.243 -	if (!context || context->hidden.win32io.h==INVALID_HANDLE_VALUE || total_bytes<=0 || !size) 	
   1.244 -		return 0;
   1.245 +    total_bytes = size * num;
   1.246 +
   1.247 +    if (!context || context->hidden.win32io.h == INVALID_HANDLE_VALUE
   1.248 +        || total_bytes <= 0 || !size)
   1.249 +        return 0;
   1.250  
   1.251 -	/* if in append mode, we must go to the EOF before write */
   1.252 -	if (context->hidden.win32io.append) {
   1.253 -		if ( SetFilePointer(context->hidden.win32io.h,0L,NULL,FILE_END) == INVALID_SET_FILE_POINTER ) {
   1.254 -			SDL_Error(SDL_EFWRITE);
   1.255 -			return 0;
   1.256 -		}
   1.257 -	}
   1.258 -	
   1.259 -	if (!WriteFile(context->hidden.win32io.h,ptr,total_bytes,&byte_written,NULL)) {
   1.260 -		SDL_Error(SDL_EFWRITE);
   1.261 -		return 0;
   1.262 -	}
   1.263 -	
   1.264 -	nwritten = byte_written/size;
   1.265 -	return nwritten;
   1.266 +    /* if in append mode, we must go to the EOF before write */
   1.267 +    if (context->hidden.win32io.append) {
   1.268 +        if (SetFilePointer(context->hidden.win32io.h, 0L, NULL, FILE_END)
   1.269 +            == INVALID_SET_FILE_POINTER) {
   1.270 +            SDL_Error(SDL_EFWRITE);
   1.271 +            return 0;
   1.272 +        }
   1.273 +    }
   1.274 +
   1.275 +    if (!WriteFile
   1.276 +        (context->hidden.win32io.h, ptr, total_bytes, &byte_written, NULL)) {
   1.277 +        SDL_Error(SDL_EFWRITE);
   1.278 +        return 0;
   1.279 +    }
   1.280 +
   1.281 +    nwritten = byte_written / size;
   1.282 +    return nwritten;
   1.283  }
   1.284 -static int SDLCALL win32_file_close(SDL_RWops *context)
   1.285 +static int SDLCALL
   1.286 +win32_file_close(SDL_RWops * context)
   1.287  {
   1.288 -	
   1.289 -	if ( context ) {								
   1.290 -		if (context->hidden.win32io.h != INVALID_HANDLE_VALUE) {
   1.291 -			CloseHandle(context->hidden.win32io.h);
   1.292 -			context->hidden.win32io.h = INVALID_HANDLE_VALUE; /* to be sure */
   1.293 -		}
   1.294 -		SDL_FreeRW(context);
   1.295 -	}
   1.296 -	return(0);
   1.297 +
   1.298 +    if (context) {
   1.299 +        if (context->hidden.win32io.h != INVALID_HANDLE_VALUE) {
   1.300 +            CloseHandle(context->hidden.win32io.h);
   1.301 +            context->hidden.win32io.h = INVALID_HANDLE_VALUE;   /* to be sure */
   1.302 +        }
   1.303 +        SDL_FreeRW(context);
   1.304 +    }
   1.305 +    return (0);
   1.306  }
   1.307  #endif /* __WIN32__ */
   1.308  
   1.309 @@ -201,117 +223,127 @@
   1.310  
   1.311  /* Functions to read/write stdio file pointers */
   1.312  
   1.313 -static int SDLCALL stdio_seek(SDL_RWops *context, int offset, int whence)
   1.314 +static int SDLCALL
   1.315 +stdio_seek(SDL_RWops * context, int offset, int whence)
   1.316  {
   1.317 -	if ( fseek(context->hidden.stdio.fp, offset, whence) == 0 ) {
   1.318 -		return(ftell(context->hidden.stdio.fp));
   1.319 -	} else {
   1.320 -		SDL_Error(SDL_EFSEEK);
   1.321 -		return(-1);
   1.322 -	}
   1.323 +    if (fseek(context->hidden.stdio.fp, offset, whence) == 0) {
   1.324 +        return (ftell(context->hidden.stdio.fp));
   1.325 +    } else {
   1.326 +        SDL_Error(SDL_EFSEEK);
   1.327 +        return (-1);
   1.328 +    }
   1.329  }
   1.330 -static int SDLCALL stdio_read(SDL_RWops *context, void *ptr, int size, int maxnum)
   1.331 +static int SDLCALL
   1.332 +stdio_read(SDL_RWops * context, void *ptr, int size, int maxnum)
   1.333  {
   1.334 -	size_t nread;
   1.335 +    size_t nread;
   1.336  
   1.337 -	nread = fread(ptr, size, maxnum, context->hidden.stdio.fp); 
   1.338 -	if ( nread == 0 && ferror(context->hidden.stdio.fp) ) {
   1.339 -		SDL_Error(SDL_EFREAD);
   1.340 -	}
   1.341 -	return(nread);
   1.342 +    nread = fread(ptr, size, maxnum, context->hidden.stdio.fp);
   1.343 +    if (nread == 0 && ferror(context->hidden.stdio.fp)) {
   1.344 +        SDL_Error(SDL_EFREAD);
   1.345 +    }
   1.346 +    return (nread);
   1.347  }
   1.348 -static int SDLCALL stdio_write(SDL_RWops *context, const void *ptr, int size, int num)
   1.349 +static int SDLCALL
   1.350 +stdio_write(SDL_RWops * context, const void *ptr, int size, int num)
   1.351  {
   1.352 -	size_t nwrote;
   1.353 +    size_t nwrote;
   1.354  
   1.355 -	nwrote = fwrite(ptr, size, num, context->hidden.stdio.fp);
   1.356 -	if ( nwrote == 0 && ferror(context->hidden.stdio.fp) ) {
   1.357 -		SDL_Error(SDL_EFWRITE);
   1.358 -	}
   1.359 -	return(nwrote);
   1.360 +    nwrote = fwrite(ptr, size, num, context->hidden.stdio.fp);
   1.361 +    if (nwrote == 0 && ferror(context->hidden.stdio.fp)) {
   1.362 +        SDL_Error(SDL_EFWRITE);
   1.363 +    }
   1.364 +    return (nwrote);
   1.365  }
   1.366 -static int SDLCALL stdio_close(SDL_RWops *context)
   1.367 +static int SDLCALL
   1.368 +stdio_close(SDL_RWops * context)
   1.369  {
   1.370 -	if ( context ) {
   1.371 -		if ( context->hidden.stdio.autoclose ) {
   1.372 -			/* WARNING:  Check the return value here! */
   1.373 -			fclose(context->hidden.stdio.fp);
   1.374 -		}
   1.375 -		SDL_FreeRW(context);
   1.376 -	}
   1.377 -	return(0);
   1.378 +    if (context) {
   1.379 +        if (context->hidden.stdio.autoclose) {
   1.380 +            /* WARNING:  Check the return value here! */
   1.381 +            fclose(context->hidden.stdio.fp);
   1.382 +        }
   1.383 +        SDL_FreeRW(context);
   1.384 +    }
   1.385 +    return (0);
   1.386  }
   1.387  #endif /* !HAVE_STDIO_H */
   1.388  
   1.389  /* Functions to read/write memory pointers */
   1.390  
   1.391 -static int SDLCALL mem_seek(SDL_RWops *context, int offset, int whence)
   1.392 +static int SDLCALL
   1.393 +mem_seek(SDL_RWops * context, int offset, int whence)
   1.394  {
   1.395 -	Uint8 *newpos;
   1.396 +    Uint8 *newpos;
   1.397  
   1.398 -	switch (whence) {
   1.399 -		case RW_SEEK_SET:
   1.400 -			newpos = context->hidden.mem.base+offset;
   1.401 -			break;
   1.402 -		case RW_SEEK_CUR:
   1.403 -			newpos = context->hidden.mem.here+offset;
   1.404 -			break;
   1.405 -		case RW_SEEK_END:
   1.406 -			newpos = context->hidden.mem.stop+offset;
   1.407 -			break;
   1.408 -		default:
   1.409 -			SDL_SetError("Unknown value for 'whence'");
   1.410 -			return(-1);
   1.411 -	}
   1.412 -	if ( newpos < context->hidden.mem.base ) {
   1.413 -		newpos = context->hidden.mem.base;
   1.414 -	}
   1.415 -	if ( newpos > context->hidden.mem.stop ) {
   1.416 -		newpos = context->hidden.mem.stop;
   1.417 -	}
   1.418 -	context->hidden.mem.here = newpos;
   1.419 -	return(context->hidden.mem.here-context->hidden.mem.base);
   1.420 +    switch (whence) {
   1.421 +    case RW_SEEK_SET:
   1.422 +        newpos = context->hidden.mem.base + offset;
   1.423 +        break;
   1.424 +    case RW_SEEK_CUR:
   1.425 +        newpos = context->hidden.mem.here + offset;
   1.426 +        break;
   1.427 +    case RW_SEEK_END:
   1.428 +        newpos = context->hidden.mem.stop + offset;
   1.429 +        break;
   1.430 +    default:
   1.431 +        SDL_SetError("Unknown value for 'whence'");
   1.432 +        return (-1);
   1.433 +    }
   1.434 +    if (newpos < context->hidden.mem.base) {
   1.435 +        newpos = context->hidden.mem.base;
   1.436 +    }
   1.437 +    if (newpos > context->hidden.mem.stop) {
   1.438 +        newpos = context->hidden.mem.stop;
   1.439 +    }
   1.440 +    context->hidden.mem.here = newpos;
   1.441 +    return (context->hidden.mem.here - context->hidden.mem.base);
   1.442  }
   1.443 -static int SDLCALL mem_read(SDL_RWops *context, void *ptr, int size, int maxnum)
   1.444 +static int SDLCALL
   1.445 +mem_read(SDL_RWops * context, void *ptr, int size, int maxnum)
   1.446  {
   1.447 -	size_t total_bytes;
   1.448 -	size_t mem_available;
   1.449 +    size_t total_bytes;
   1.450 +    size_t mem_available;
   1.451  
   1.452 -	total_bytes = (maxnum * size);
   1.453 -	if ( (maxnum <= 0) || (size <= 0) || ((total_bytes / maxnum) != (size_t) size) ) {
   1.454 -		return 0;
   1.455 -	}
   1.456 +    total_bytes = (maxnum * size);
   1.457 +    if ((maxnum <= 0) || (size <= 0)
   1.458 +        || ((total_bytes / maxnum) != (size_t) size)) {
   1.459 +        return 0;
   1.460 +    }
   1.461  
   1.462 -	mem_available = (context->hidden.mem.stop - context->hidden.mem.here);
   1.463 -	if (total_bytes > mem_available) {
   1.464 -		total_bytes = mem_available;
   1.465 -	}
   1.466 +    mem_available = (context->hidden.mem.stop - context->hidden.mem.here);
   1.467 +    if (total_bytes > mem_available) {
   1.468 +        total_bytes = mem_available;
   1.469 +    }
   1.470  
   1.471 -	SDL_memcpy(ptr, context->hidden.mem.here, total_bytes);
   1.472 -	context->hidden.mem.here += total_bytes;
   1.473 +    SDL_memcpy(ptr, context->hidden.mem.here, total_bytes);
   1.474 +    context->hidden.mem.here += total_bytes;
   1.475  
   1.476 -	return (total_bytes / size);
   1.477 +    return (total_bytes / size);
   1.478  }
   1.479 -static int SDLCALL mem_write(SDL_RWops *context, const void *ptr, int size, int num)
   1.480 +static int SDLCALL
   1.481 +mem_write(SDL_RWops * context, const void *ptr, int size, int num)
   1.482  {
   1.483 -	if ( (context->hidden.mem.here + (num*size)) > context->hidden.mem.stop ) {
   1.484 -		num = (context->hidden.mem.stop-context->hidden.mem.here)/size;
   1.485 -	}
   1.486 -	SDL_memcpy(context->hidden.mem.here, ptr, num*size);
   1.487 -	context->hidden.mem.here += num*size;
   1.488 -	return(num);
   1.489 +    if ((context->hidden.mem.here + (num * size)) > context->hidden.mem.stop) {
   1.490 +        num = (context->hidden.mem.stop - context->hidden.mem.here) / size;
   1.491 +    }
   1.492 +    SDL_memcpy(context->hidden.mem.here, ptr, num * size);
   1.493 +    context->hidden.mem.here += num * size;
   1.494 +    return (num);
   1.495  }
   1.496 -static int SDLCALL mem_writeconst(SDL_RWops *context, const void *ptr, int size, int num)
   1.497 +static int SDLCALL
   1.498 +mem_writeconst(SDL_RWops * context, const void *ptr, int size, int num)
   1.499  {
   1.500 -	SDL_SetError("Can't write to read-only memory");
   1.501 -	return(-1);
   1.502 +    SDL_SetError("Can't write to read-only memory");
   1.503 +    return (-1);
   1.504  }
   1.505 -static int SDLCALL mem_close(SDL_RWops *context)
   1.506 +static int SDLCALL
   1.507 +mem_close(SDL_RWops * context)
   1.508  {
   1.509 -	if ( context ) {
   1.510 -		SDL_FreeRW(context);
   1.511 -	}
   1.512 -	return(0);
   1.513 +    if (context) {
   1.514 +        SDL_FreeRW(context);
   1.515 +    }
   1.516 +    return (0);
   1.517  }
   1.518  
   1.519  
   1.520 @@ -322,233 +354,263 @@
   1.521   * translate unix-style slash-separated filename to mac-style colon-separated
   1.522   * name; return malloced string
   1.523   */
   1.524 -static char *unix_to_mac(const char *file)
   1.525 +static char *
   1.526 +unix_to_mac(const char *file)
   1.527  {
   1.528 -	int flen = SDL_strlen(file);
   1.529 -	char *path = SDL_malloc(flen + 2);
   1.530 -	const char *src = file;
   1.531 -	char *dst = path;
   1.532 -	if(*src == '/') {
   1.533 -		/* really depends on filesystem layout, hope for the best */
   1.534 -		src++;
   1.535 -	} else {
   1.536 -		/* Check if this is a MacOS path to begin with */
   1.537 -		if(*src != ':')
   1.538 -			*dst++ = ':';   /* relative paths begin with ':' */
   1.539 -	}
   1.540 -	while(src < file + flen) {
   1.541 -		const char *end = SDL_strchr(src, '/');
   1.542 -		int len;
   1.543 -		if(!end)
   1.544 -			end = file + flen; /* last component */
   1.545 -		len = end - src;
   1.546 -		if(len == 0 || (len == 1 && src[0] == '.')) {
   1.547 -			/* remove repeated slashes and . */
   1.548 -		} else {
   1.549 -			if(len == 2 && src[0] == '.' && src[1] == '.') {
   1.550 -				/* replace .. with the empty string */
   1.551 -			} else {
   1.552 -				SDL_memcpy(dst, src, len);
   1.553 -				dst += len;
   1.554 -			}
   1.555 -			if(end < file + flen)
   1.556 -				*dst++ = ':';
   1.557 -		}
   1.558 -		src = end + 1;
   1.559 -	}
   1.560 -	*dst++ = '\0';
   1.561 -	return path;
   1.562 +    int flen = SDL_strlen(file);
   1.563 +    char *path = SDL_malloc(flen + 2);
   1.564 +    const char *src = file;
   1.565 +    char *dst = path;
   1.566 +    if (*src == '/') {
   1.567 +        /* really depends on filesystem layout, hope for the best */
   1.568 +        src++;
   1.569 +    } else {
   1.570 +        /* Check if this is a MacOS path to begin with */
   1.571 +        if (*src != ':')
   1.572 +            *dst++ = ':';       /* relative paths begin with ':' */
   1.573 +    }
   1.574 +    while (src < file + flen) {
   1.575 +        const char *end = SDL_strchr(src, '/');
   1.576 +        int len;
   1.577 +        if (!end)
   1.578 +            end = file + flen;  /* last component */
   1.579 +        len = end - src;
   1.580 +        if (len == 0 || (len == 1 && src[0] == '.')) {
   1.581 +            /* remove repeated slashes and . */
   1.582 +        } else {
   1.583 +            if (len == 2 && src[0] == '.' && src[1] == '.') {
   1.584 +                /* replace .. with the empty string */
   1.585 +            } else {
   1.586 +                SDL_memcpy(dst, src, len);
   1.587 +                dst += len;
   1.588 +            }
   1.589 +            if (end < file + flen)
   1.590 +                *dst++ = ':';
   1.591 +        }
   1.592 +        src = end + 1;
   1.593 +    }
   1.594 +    *dst++ = '\0';
   1.595 +    return path;
   1.596  }
   1.597  #endif /* __MACOS__ */
   1.598  
   1.599 -SDL_RWops *SDL_RWFromFile(const char *file, const char *mode)
   1.600 +SDL_RWops *
   1.601 +SDL_RWFromFile(const char *file, const char *mode)
   1.602  {
   1.603 -	SDL_RWops *rwops = NULL;
   1.604 +    SDL_RWops *rwops = NULL;
   1.605  #ifdef HAVE_STDIO_H
   1.606 -	FILE *fp = NULL;
   1.607 +    FILE *fp = NULL;
   1.608  #endif
   1.609 -	if ( !file || !*file || !mode || !*mode ) {
   1.610 -		SDL_SetError("SDL_RWFromFile(): No file or no mode specified");
   1.611 -		return NULL;
   1.612 -	}
   1.613 -
   1.614 +    if (!file || !*file || !mode || !*mode) {
   1.615 +        SDL_SetError("SDL_RWFromFile(): No file or no mode specified");
   1.616 +        return NULL;
   1.617 +    }
   1.618  #if defined(__WIN32__)
   1.619 -	rwops = SDL_AllocRW();
   1.620 -	if (!rwops)
   1.621 -		return NULL; /* SDL_SetError already setup by SDL_AllocRW() */
   1.622 -	rwops->hidden.win32io.h = INVALID_HANDLE_VALUE;
   1.623 -	if (win32_file_open(rwops,file,mode)) {
   1.624 -		SDL_FreeRW(rwops);
   1.625 -		return NULL;
   1.626 -	}	
   1.627 -	rwops->seek  = win32_file_seek;
   1.628 -	rwops->read  = win32_file_read;
   1.629 -	rwops->write = win32_file_write;
   1.630 -	rwops->close = win32_file_close;
   1.631 +    rwops = SDL_AllocRW();
   1.632 +    if (!rwops)
   1.633 +        return NULL;            /* SDL_SetError already setup by SDL_AllocRW() */
   1.634 +    rwops->hidden.win32io.h = INVALID_HANDLE_VALUE;
   1.635 +    if (win32_file_open(rwops, file, mode)) {
   1.636 +        SDL_FreeRW(rwops);
   1.637 +        return NULL;
   1.638 +    }
   1.639 +    rwops->seek = win32_file_seek;
   1.640 +    rwops->read = win32_file_read;
   1.641 +    rwops->write = win32_file_write;
   1.642 +    rwops->close = win32_file_close;
   1.643  
   1.644  #elif HAVE_STDIO_H
   1.645  
   1.646  #ifdef __MACOS__
   1.647 -	{
   1.648 -		char *mpath = unix_to_mac(file);
   1.649 -		fp = fopen(mpath, mode);
   1.650 -		SDL_free(mpath);
   1.651 -	}
   1.652 +    {
   1.653 +        char *mpath = unix_to_mac(file);
   1.654 +        fp = fopen(mpath, mode);
   1.655 +        SDL_free(mpath);
   1.656 +    }
   1.657  #else
   1.658 -	fp = fopen(file, mode);
   1.659 +    fp = fopen(file, mode);
   1.660  #endif
   1.661 -	if ( fp == NULL ) {
   1.662 -		SDL_SetError("Couldn't open %s", file);
   1.663 -	} else {
   1.664 -		rwops = SDL_RWFromFP(fp, 1);
   1.665 -	}
   1.666 +    if (fp == NULL) {
   1.667 +        SDL_SetError("Couldn't open %s", file);
   1.668 +    } else {
   1.669 +        rwops = SDL_RWFromFP(fp, 1);
   1.670 +    }
   1.671  #else
   1.672 -	SDL_SetError("SDL not compiled with stdio support");
   1.673 +    SDL_SetError("SDL not compiled with stdio support");
   1.674  #endif /* !HAVE_STDIO_H */
   1.675  
   1.676 -	return(rwops);
   1.677 +    return (rwops);
   1.678  }
   1.679  
   1.680  #ifdef HAVE_STDIO_H
   1.681 -SDL_RWops *SDL_RWFromFP(FILE *fp, int autoclose)
   1.682 +SDL_RWops *
   1.683 +SDL_RWFromFP(FILE * fp, int autoclose)
   1.684  {
   1.685 -	SDL_RWops *rwops = NULL;
   1.686 +    SDL_RWops *rwops = NULL;
   1.687  
   1.688 -	rwops = SDL_AllocRW();
   1.689 -	if ( rwops != NULL ) {
   1.690 -		rwops->seek = stdio_seek;
   1.691 -		rwops->read = stdio_read;
   1.692 -		rwops->write = stdio_write;
   1.693 -		rwops->close = stdio_close;
   1.694 -		rwops->hidden.stdio.fp = fp;
   1.695 -		rwops->hidden.stdio.autoclose = autoclose;
   1.696 -	}
   1.697 -	return(rwops);
   1.698 +    rwops = SDL_AllocRW();
   1.699 +    if (rwops != NULL) {
   1.700 +        rwops->seek = stdio_seek;
   1.701 +        rwops->read = stdio_read;
   1.702 +        rwops->write = stdio_write;
   1.703 +        rwops->close = stdio_close;
   1.704 +        rwops->hidden.stdio.fp = fp;
   1.705 +        rwops->hidden.stdio.autoclose = autoclose;
   1.706 +    }
   1.707 +    return (rwops);
   1.708  }
   1.709  #endif /* HAVE_STDIO_H */
   1.710  
   1.711 -SDL_RWops *SDL_RWFromMem(void *mem, int size)
   1.712 +SDL_RWops *
   1.713 +SDL_RWFromMem(void *mem, int size)
   1.714  {
   1.715 -	SDL_RWops *rwops;
   1.716 +    SDL_RWops *rwops;
   1.717  
   1.718 -	rwops = SDL_AllocRW();
   1.719 -	if ( rwops != NULL ) {
   1.720 -		rwops->seek = mem_seek;
   1.721 -		rwops->read = mem_read;
   1.722 -		rwops->write = mem_write;
   1.723 -		rwops->close = mem_close;
   1.724 -		rwops->hidden.mem.base = (Uint8 *)mem;
   1.725 -		rwops->hidden.mem.here = rwops->hidden.mem.base;
   1.726 -		rwops->hidden.mem.stop = rwops->hidden.mem.base+size;
   1.727 -	}
   1.728 -	return(rwops);
   1.729 +    rwops = SDL_AllocRW();
   1.730 +    if (rwops != NULL) {
   1.731 +        rwops->seek = mem_seek;
   1.732 +        rwops->read = mem_read;
   1.733 +        rwops->write = mem_write;
   1.734 +        rwops->close = mem_close;
   1.735 +        rwops->hidden.mem.base = (Uint8 *) mem;
   1.736 +        rwops->hidden.mem.here = rwops->hidden.mem.base;
   1.737 +        rwops->hidden.mem.stop = rwops->hidden.mem.base + size;
   1.738 +    }
   1.739 +    return (rwops);
   1.740  }
   1.741  
   1.742 -SDL_RWops *SDL_RWFromConstMem(const void *mem, int size)
   1.743 +SDL_RWops *
   1.744 +SDL_RWFromConstMem(const void *mem, int size)
   1.745  {
   1.746 -	SDL_RWops *rwops;
   1.747 +    SDL_RWops *rwops;
   1.748  
   1.749 -	rwops = SDL_AllocRW();
   1.750 -	if ( rwops != NULL ) {
   1.751 -		rwops->seek = mem_seek;
   1.752 -		rwops->read = mem_read;
   1.753 -		rwops->write = mem_writeconst;
   1.754 -		rwops->close = mem_close;
   1.755 -		rwops->hidden.mem.base = (Uint8 *)mem;
   1.756 -		rwops->hidden.mem.here = rwops->hidden.mem.base;
   1.757 -		rwops->hidden.mem.stop = rwops->hidden.mem.base+size;
   1.758 -	}
   1.759 -	return(rwops);
   1.760 +    rwops = SDL_AllocRW();
   1.761 +    if (rwops != NULL) {
   1.762 +        rwops->seek = mem_seek;
   1.763 +        rwops->read = mem_read;
   1.764 +        rwops->write = mem_writeconst;
   1.765 +        rwops->close = mem_close;
   1.766 +        rwops->hidden.mem.base = (Uint8 *) mem;
   1.767 +        rwops->hidden.mem.here = rwops->hidden.mem.base;
   1.768 +        rwops->hidden.mem.stop = rwops->hidden.mem.base + size;
   1.769 +    }
   1.770 +    return (rwops);
   1.771  }
   1.772  
   1.773 -SDL_RWops *SDL_AllocRW(void)
   1.774 +SDL_RWops *
   1.775 +SDL_AllocRW(void)
   1.776  {
   1.777 -	SDL_RWops *area;
   1.778 +    SDL_RWops *area;
   1.779  
   1.780 -	area = (SDL_RWops *)SDL_malloc(sizeof *area);
   1.781 -	if ( area == NULL ) {
   1.782 -		SDL_OutOfMemory();
   1.783 -	}
   1.784 -	return(area);
   1.785 +    area = (SDL_RWops *) SDL_malloc(sizeof *area);
   1.786 +    if (area == NULL) {
   1.787 +        SDL_OutOfMemory();
   1.788 +    }
   1.789 +    return (area);
   1.790  }
   1.791  
   1.792 -void SDL_FreeRW(SDL_RWops *area)
   1.793 +void
   1.794 +SDL_FreeRW(SDL_RWops * area)
   1.795  {
   1.796 -	SDL_free(area);
   1.797 +    SDL_free(area);
   1.798  }
   1.799  
   1.800  /* Functions for dynamically reading and writing endian-specific values */
   1.801  
   1.802 -Uint16 SDL_ReadLE16 (SDL_RWops *src)
   1.803 -{
   1.804 -	Uint16 value;
   1.805 -
   1.806 -	SDL_RWread(src, &value, (sizeof value), 1);
   1.807 -	return(SDL_SwapLE16(value));
   1.808 -}
   1.809 -Uint16 SDL_ReadBE16 (SDL_RWops *src)
   1.810 +Uint16
   1.811 +SDL_ReadLE16(SDL_RWops * src)
   1.812  {
   1.813 -	Uint16 value;
   1.814 +    Uint16 value;
   1.815  
   1.816 -	SDL_RWread(src, &value, (sizeof value), 1);
   1.817 -	return(SDL_SwapBE16(value));
   1.818 +    SDL_RWread(src, &value, (sizeof value), 1);
   1.819 +    return (SDL_SwapLE16(value));
   1.820  }
   1.821 -Uint32 SDL_ReadLE32 (SDL_RWops *src)
   1.822 +
   1.823 +Uint16
   1.824 +SDL_ReadBE16(SDL_RWops * src)
   1.825  {
   1.826 -	Uint32 value;
   1.827 +    Uint16 value;
   1.828  
   1.829 -	SDL_RWread(src, &value, (sizeof value), 1);
   1.830 -	return(SDL_SwapLE32(value));
   1.831 +    SDL_RWread(src, &value, (sizeof value), 1);
   1.832 +    return (SDL_SwapBE16(value));
   1.833  }
   1.834 -Uint32 SDL_ReadBE32 (SDL_RWops *src)
   1.835 +
   1.836 +Uint32
   1.837 +SDL_ReadLE32(SDL_RWops * src)
   1.838  {
   1.839 -	Uint32 value;
   1.840 +    Uint32 value;
   1.841  
   1.842 -	SDL_RWread(src, &value, (sizeof value), 1);
   1.843 -	return(SDL_SwapBE32(value));
   1.844 +    SDL_RWread(src, &value, (sizeof value), 1);
   1.845 +    return (SDL_SwapLE32(value));
   1.846  }
   1.847 -Uint64 SDL_ReadLE64 (SDL_RWops *src)
   1.848 +
   1.849 +Uint32
   1.850 +SDL_ReadBE32(SDL_RWops * src)
   1.851  {
   1.852 -	Uint64 value;
   1.853 +    Uint32 value;
   1.854  
   1.855 -	SDL_RWread(src, &value, (sizeof value), 1);
   1.856 -	return(SDL_SwapLE64(value));
   1.857 +    SDL_RWread(src, &value, (sizeof value), 1);
   1.858 +    return (SDL_SwapBE32(value));
   1.859  }
   1.860 -Uint64 SDL_ReadBE64 (SDL_RWops *src)
   1.861 +
   1.862 +Uint64
   1.863 +SDL_ReadLE64(SDL_RWops * src)
   1.864  {
   1.865 -	Uint64 value;
   1.866 +    Uint64 value;
   1.867  
   1.868 -	SDL_RWread(src, &value, (sizeof value), 1);
   1.869 -	return(SDL_SwapBE64(value));
   1.870 +    SDL_RWread(src, &value, (sizeof value), 1);
   1.871 +    return (SDL_SwapLE64(value));
   1.872  }
   1.873  
   1.874 -int SDL_WriteLE16 (SDL_RWops *dst, Uint16 value)
   1.875 -{
   1.876 -	value = SDL_SwapLE16(value);
   1.877 -	return(SDL_RWwrite(dst, &value, (sizeof value), 1));
   1.878 -}
   1.879 -int SDL_WriteBE16 (SDL_RWops *dst, Uint16 value)
   1.880 +Uint64
   1.881 +SDL_ReadBE64(SDL_RWops * src)
   1.882  {
   1.883 -	value = SDL_SwapBE16(value);
   1.884 -	return(SDL_RWwrite(dst, &value, (sizeof value), 1));
   1.885 +    Uint64 value;
   1.886 +
   1.887 +    SDL_RWread(src, &value, (sizeof value), 1);
   1.888 +    return (SDL_SwapBE64(value));
   1.889  }
   1.890 -int SDL_WriteLE32 (SDL_RWops *dst, Uint32 value)
   1.891 +
   1.892 +int
   1.893 +SDL_WriteLE16(SDL_RWops * dst, Uint16 value)
   1.894  {
   1.895 -	value = SDL_SwapLE32(value);
   1.896 -	return(SDL_RWwrite(dst, &value, (sizeof value), 1));
   1.897 +    value = SDL_SwapLE16(value);
   1.898 +    return (SDL_RWwrite(dst, &value, (sizeof value), 1));
   1.899 +}
   1.900 +
   1.901 +int
   1.902 +SDL_WriteBE16(SDL_RWops * dst, Uint16 value)
   1.903 +{
   1.904 +    value = SDL_SwapBE16(value);
   1.905 +    return (SDL_RWwrite(dst, &value, (sizeof value), 1));
   1.906  }
   1.907 -int SDL_WriteBE32 (SDL_RWops *dst, Uint32 value)
   1.908 +
   1.909 +int
   1.910 +SDL_WriteLE32(SDL_RWops * dst, Uint32 value)
   1.911  {
   1.912 -	value = SDL_SwapBE32(value);
   1.913 -	return(SDL_RWwrite(dst, &value, (sizeof value), 1));
   1.914 +    value = SDL_SwapLE32(value);
   1.915 +    return (SDL_RWwrite(dst, &value, (sizeof value), 1));
   1.916  }
   1.917 -int SDL_WriteLE64 (SDL_RWops *dst, Uint64 value)
   1.918 +
   1.919 +int
   1.920 +SDL_WriteBE32(SDL_RWops * dst, Uint32 value)
   1.921  {
   1.922 -	value = SDL_SwapLE64(value);
   1.923 -	return(SDL_RWwrite(dst, &value, (sizeof value), 1));
   1.924 +    value = SDL_SwapBE32(value);
   1.925 +    return (SDL_RWwrite(dst, &value, (sizeof value), 1));
   1.926 +}
   1.927 +
   1.928 +int
   1.929 +SDL_WriteLE64(SDL_RWops * dst, Uint64 value)
   1.930 +{
   1.931 +    value = SDL_SwapLE64(value);
   1.932 +    return (SDL_RWwrite(dst, &value, (sizeof value), 1));
   1.933  }
   1.934 -int SDL_WriteBE64 (SDL_RWops *dst, Uint64 value)
   1.935 +
   1.936 +int
   1.937 +SDL_WriteBE64(SDL_RWops * dst, Uint64 value)
   1.938  {
   1.939 -	value = SDL_SwapBE64(value);
   1.940 -	return(SDL_RWwrite(dst, &value, (sizeof value), 1));
   1.941 +    value = SDL_SwapBE64(value);
   1.942 +    return (SDL_RWwrite(dst, &value, (sizeof value), 1));
   1.943  }
   1.944 +
   1.945 +/* vi: set ts=4 sw=4 expandtab: */