EMILE/libblock/block_read.c

48 lines
938 B
C
Raw Normal View History

2005-11-23 22:39:03 +00:00
/*
*
* (c) 2005 Laurent Vivier <Laurent@lvivier.info>
2005-11-23 22:39:03 +00:00
*
*/
#include <stdio.h>
2005-11-23 22:39:03 +00:00
#include <string.h>
#include "libblock.h"
size_t block_read(block_FILE *file, void *ptr, size_t size)
{
int read = 0;
int ret;
int blocksize = file->device->get_blocksize(&file->device);
while (size != 0)
{
int part;
int block_nb = (file->offset + file->base) / file->buffer_size;
int block_offset = (file->offset + file->base) % file->buffer_size;
if (block_nb != file->current)
{
ret = file->device->read_sector(file->device->data,
2005-11-23 22:39:03 +00:00
(block_nb * file->buffer_size) / blocksize,
file->buffer,
file->buffer_size);
if (ret == -1)
return read;
file->current = block_nb;
}
part = file->buffer_size - block_offset;
if (part > size)
part = size;
memcpy(ptr, file->buffer + block_offset, part);
size -= part;
ptr = (char*)ptr + part;
file->offset += part;
read += part;
}
return read;
}