Patch from Antonio SJ Musumeci:
authorRyan C. Gordon <icculus@icculus.org>
Fri, 24 Jun 2005 12:48:38 +0000
changeset 1078e2ef6b7001fd
parent 1077 f122afdfa025
child 1079 39b5606fa543
Patch from Antonio SJ Musumeci:

"
This code with SDL-1.2.8 and CVS:

#include <stdio.h>
#include <stdlib.h>
#include <SDL.h>

int
main(int argc, char** argv)
{
char alphabet[26] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
char buffer_file[27] = {0};
char buffer_mem[27] = {0};
int rv_file;
int rv_mem;
FILE* file_ptr;
SDL_RWops* rwop_file;
SDL_RWops* rwop_mem;

file_ptr = fopen("./blah", "w");
fwrite(alphabet, 1, sizeof(alphabet), file_ptr);
fclose(file_ptr);

rwop_mem = SDL_RWFromMem(alphabet, sizeof(alphabet));
rwop_file = SDL_RWFromFile("./blah", "r");
rv_mem = SDL_RWread(rwop_mem , buffer_mem, 5, 6);
rv_file = SDL_RWread(rwop_file, buffer_file, 5, 6);
printf("From File: %d %s\n"
"From Mem: %d %s\n",
rv_file,
buffer_file,
rv_mem,
buffer_mem);
printf("Seek end of File: %d\n"
"Seek end of Mem: %d\n",
SDL_RWseek(rwop_file, 0, SEEK_END),
SDL_RWseek(rwop_mem , 0, SEEK_END));
SDL_RWclose(rwop_file);
SDL_RWclose(rwop_mem);

return 0;
}


Produces this output:

From File: 5 ABCDEFGHIJKLMNOPQRSTUVWXYZ
From Mem: 5 ABCDEFGHIJKLMNOPQRSTUVWXY
Seek end of File: 26
Seek end of Mem: 26
"

--ryan.
src/file/SDL_rwops.c
     1.1 --- a/src/file/SDL_rwops.c	Wed Jun 15 23:41:57 2005 +0000
     1.2 +++ b/src/file/SDL_rwops.c	Fri Jun 24 12:48:38 2005 +0000
     1.3 @@ -110,15 +110,23 @@
     1.4  }
     1.5  static int mem_read(SDL_RWops *context, void *ptr, int size, int maxnum)
     1.6  {
     1.7 -	int num;
     1.8 +	int total_bytes;
     1.9 +	int mem_available;
    1.10 +
    1.11 +	total_bytes = (maxnum * size);
    1.12 +	if ( (maxnum <= 0) || (size <= 0) || ((total_bytes / maxnum) != size) ) {
    1.13 +		return 0;
    1.14 +	}
    1.15  
    1.16 -	num = maxnum;
    1.17 -	if ( (context->hidden.mem.here + (num*size)) > context->hidden.mem.stop ) {
    1.18 -		num = (context->hidden.mem.stop-context->hidden.mem.here)/size;
    1.19 +	mem_available = (context->hidden.mem.stop - context->hidden.mem.here);
    1.20 +	if (total_bytes > mem_available) {
    1.21 +		total_bytes = mem_available;
    1.22  	}
    1.23 -	memcpy(ptr, context->hidden.mem.here, num*size);
    1.24 -	context->hidden.mem.here += num*size;
    1.25 -	return(num);
    1.26 +
    1.27 +	memcpy(ptr, context->hidden.mem.here, total_bytes);
    1.28 +	context->hidden.mem.here += total_bytes;
    1.29 +
    1.30 +	return (total_bytes / size);
    1.31  }
    1.32  static int mem_write(SDL_RWops *context, const void *ptr, int size, int num)
    1.33  {