Commit 3a608a44 authored by Ryan C. Gordon's avatar Ryan C. Gordon

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.

--HG--
extra : convert_revision : svn%3Ac70aab31-4412-0410-b14c-859654838e24/trunk%401079
parent a3c1fe41
...@@ -110,15 +110,23 @@ static int mem_seek(SDL_RWops *context, int offset, int whence) ...@@ -110,15 +110,23 @@ static int mem_seek(SDL_RWops *context, int offset, int whence)
} }
static int mem_read(SDL_RWops *context, void *ptr, int size, int maxnum) static int mem_read(SDL_RWops *context, void *ptr, int size, int maxnum)
{ {
int num; int total_bytes;
int mem_available;
num = maxnum; total_bytes = (maxnum * size);
if ( (context->hidden.mem.here + (num*size)) > context->hidden.mem.stop ) { if ( (maxnum <= 0) || (size <= 0) || ((total_bytes / maxnum) != size) ) {
num = (context->hidden.mem.stop-context->hidden.mem.here)/size; return 0;
} }
memcpy(ptr, context->hidden.mem.here, num*size);
context->hidden.mem.here += num*size; mem_available = (context->hidden.mem.stop - context->hidden.mem.here);
return(num); if (total_bytes > mem_available) {
total_bytes = mem_available;
}
memcpy(ptr, context->hidden.mem.here, total_bytes);
context->hidden.mem.here += total_bytes;
return (total_bytes / size);
} }
static int mem_write(SDL_RWops *context, const void *ptr, int size, int num) static int mem_write(SDL_RWops *context, const void *ptr, int size, int num)
{ {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment