avr-fw-modules/core/src/i2cee_define_file.c

80 lines
1.7 KiB
C
Executable File

#include <sys/i2ceeprom.h>
#include <sys/fastfile.h>
#include <sys/errno.h>
struct i2cee_fastfile {
fastfile_t fastfile;
int32_t offset,
size,
position;
};
int i2cee_ff_seek (fastfile_t *ff,int position){
struct i2cee_fastfile *f = (struct i2cee_fastfile*)ff;
if ((position >= 0) && (position < f->size)){
f->position = position;
return ESUCCESS;
};
return -ESHORT;
};
int i2cee_ff_tell (fastfile_t *ff){
struct i2cee_fastfile *f = (struct i2cee_fastfile*)ff;
return f->position;
};
int i2cee_ff_read (fastfile_t *ff,void *buffer,int len){
struct i2cee_fastfile *f = (struct i2cee_fastfile*)ff;
if (!buffer){
return -ENULLPTR;
};
if (f->position + len > f->size){
len = f->size - f->position;
};
len = i2cee_load( f->offset + f->position, buffer, len );
assert(len);
f->position += len;
return len;
};
int i2cee_ff_write (fastfile_t *ff,void *buffer,int len){
struct i2cee_fastfile *f = (struct i2cee_fastfile*)ff;
if (!buffer){
return -ENULLPTR;
};
if (f->position + len > f->size){
len = f->size - f->position;
};
len = i2cee_save( f->offset + f->position, buffer, len );
assert(len);
f->position += len;
return len;
};
int i2cee_ff_close (fastfile_t *ff){
return ESUCCESS;
};
fastfile_t* i2cee_define_file(int32_t offset,int32_t size){
struct i2cee_fastfile *f = (struct i2cee_fastfile*)malloc( sizeof( struct i2cee_fastfile) );
if (!f){
return NULL;
};
memset(f, 0x00, sizeof( *f ) );
f->fastfile.ops.seek = i2cee_ff_seek;
f->fastfile.ops.tell = i2cee_ff_tell;
f->fastfile.ops.read = i2cee_ff_read;
f->fastfile.ops.write = i2cee_ff_write;
f->fastfile.ops.close = i2cee_ff_close;
return & (f->fastfile);
};