I have defined such structure:
typedef struct _Lib3dsBuffer {
long offset; // current position
long size; // buffer size
const char *buffer; // buffer
} Lib3dsBuffer;
//Seek function:
static long
bufferio_seek_func(void *self, long offset, Lib3dsIoSeek origin) {
Lib3dsBuffer *f = (Lib3dsBuffer *)self;
switch (origin)
{
case LIB3DS_SEEK_SET:
f->offset = offset; // no need of this I think
break;
case LIB3DS_SEEK_CUR:
f->offset += offset;
break;
case LIB3DS_SEEK_END:
f->offset = (f->size + offset);
break;
default:
ASSERT(0);
return(0);
}
return 1;
}
// Tell function:
static long
bufferio_tell_func(void *self) {
Lib3dsBuffer *f = (Lib3dsBuffer*)self;
return f->offset;
}
// Read function
static int
bufferio_read_func(void *self, Lib3dsByte *buffer, int size) {
Lib3dsBuffer *f = (Lib3dsBuffer *)self;
memcpy(buffer, f->buffer + f->offset, size);
f->offset += size;
return size;
}
// error handling (don't know what should be here....)
static Lib3dsBool
bufferio_error_func(void *self) {
// no errors handled
return 0;
}
// Main function to read data
Lib3dsFile*
lib3ds_buffer_load(const char *buffer)
{
Lib3dsBuffer *f;
Lib3dsFile *file;
Lib3dsIo *io;
f = (Lib3dsBuffer*)calloc(sizeof(Lib3dsBuffer),1); // not sure about
calloc usage
f->buffer = buffer;
if (!f)
{
return(0);
}
file = lib3ds_file_new();
if (!file)
{
free(f);
return(0);
}
io = lib3ds_io_new(
f,
bufferio_error_func,
bufferio_seek_func,
bufferio_tell_func,
bufferio_read_func,
NULL // no write function
);
if (!io)
{
lib3ds_file_free(file);
free(f);
return(0);
}
if (!lib3ds_file_read(file, io)) {
free(file);
free(f);
return(0);
}
lib3ds_io_free(io);
free(f);
return(file);
}
To do tests on implemented functions I have changed player.c
FILE *pFile;
....
pFile = fopen(filepath, "rb");
fseek(pFile, 0, SEEK_END);
nFileSize = ftell(pFile);
fseek(pFile, 0, SEEK_SET);
buffer = malloc(nFileSize)
fread(buffer, 1, nFileSize, pFile);
fclose(pFile);
// file=lib3ds_file_load(filepath);
file=lib3ds_buffer_load(buffer);
Tomas
P.S. Thanks to the author of lib3ds for the very good design
-------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc. Do you grep through log files
for problems? Stop! Download the new AJAX search engine that makes
searching your log files as easy as surfing the web. DOWNLOAD SPLUNK!
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=103432&bid=230486&dat=121642
_______________________________________________
lib3ds-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/lib3ds-devel