#include "core.h"
int fd_open( const char *filename, int flags )
{
int ret;
ret = open( filename, flags );
return ret;
}
int fd_close( int fd )
{
int ret;
ret = close( fd );
return ret;
}
int fd_seek( int fd, off_t offset, int whence )
{
int ret = 0;
off_t off_new=0;
off_new = lseek( fd, offset, whence );
if ( errno != 0)
{
printf("!Cannot seek %s\n", strerror(errno));
errno = 0; //why i need to reset it?
return -1;
}
ret = off_new;
return ret;
}
off_t fd_pos( int fd )
{
off_t cur = 0;
cur = lseek( fd, 0, SEEK_CUR );
return cur;
}
off_t fd_set_pos( int fd, off_t offset )
{
off_t ret;
ret = lseek( fd, offset, SEEK_SET );
return ret;
}
off_t fd_size( int fd )
{
off_t ret;
off_t old;
old = lseek( fd, 0, FD_SEEK_CUR );
ret = lseek( fd, 0, FD_SEEK_END );
lseek( fd, old, FD_SEEK_SET );
return ret;
}
ssize_t fd_read( int fd, void *buf, size_t count )
{
ssize_t ret;
ret = read( fd, buf, count );
return ret;
}
ssize_t fd_write( int fd, void *buf, size_t count )
{
ssize_t ret;
ret = write( fd, buf, count );
if ( errno != 0)
{
printf("Cannot write %s\n", strerror(errno));
errno = 0;
}
return ret;
}
file_t *file_init()
{
file_t *ret = NULL;
ret = (file_t *)malloc( sizeof(file_t) );
memset( ret, 0, sizeof(file_t) );
ret->flags = FD_RO; //di we really need that?
return ret;
}
int file_open_fn( file_t *ft, const char *filename, int flags )
{
int ret = 0;
int fd;
//check if file is allready initialized or opened
if (ft->fd > 0)
{
printf("Trying to open file but file descriptor allready set\n");
return -1;
}
if (ft->filename != NULL)
{
printf("Trying open file, bus structure allready have filename\n");
return -1;
}
fd = fd_open( filename, flags );
if ( fd < 1 )
{
printf("Couldnt open %s\n", filename);
return -1;
}
ft->fd = fd;
ft->flags = flags;
uint8_t fn_sz = strlen( filename );
char *fn = (char *)malloc( fn_sz );
memcpy( fn, filename, fn_sz );
ft->filename = fn;
/* set current file cursor postion and file size */
ft->size = fd_size( fd ); /* if it RO mode - could be important */
ft->position = fd_pos( fd ); /* file cursor position could be bigger
then file size*/
ft->blk_size = DEFAULT_BLK_SIZE;
return ret;
}
int file_seek( file_t *ft, off_t offset )
{
int ret = 0;
off_t new_off=0;
ret = fd_seek( ft->fd, offset, FD_SEEK_CUR );
if (ret >= 0)
{
new_off = fd_pos( ft->fd );
if (new_off > ft->size)
{
fd_seek( ft->fd, ft->size, FD_SEEK_SET );
new_off = fd_pos( ft->fd );
}
ft->position = new_off;
ft->offset = offset;
if ( offset != new_off )
{
printf("Offset set to %zd requested %zd\n", new_off, offset);
}
} else
{
printf("Cannot set offset\n");
ret = -1;
}
return ret;
}
int file_seekp( file_t *ft, off_t offset )
{
int ret = 0;
off_t new_off = 0;
ret = fd_seek( ft->fd, offset, FD_SEEK_SET );
ft->offset = fd_seek( ft->fd, 0, FD_SEEK_CUR ); //cul be errors?
return ret;
}
int file_pos( file_t *ft )
{
int ret = 0;
long pos = fd_pos( ft->fd );
if (pos < 0)
{
printf("Cannot get file cursor position\n");
ret = -1;
}
ft->position = pos;
ret = pos;
return ret;
}
int file_size( file_t *ft )
{
int ret = 0;
off_t size;
size = fd_size( ft->fd );
if (size < 0)
{
printf("Cannot get file size\n");
return -1;
}
ft->size = size;
ret = size;
return ret;
}
int file_resize( file_t *ft, size_t size )
{
int ret = 0;
off_t cur_pos;
off_t cur_size;
off_t resize;
uint8_t *buf;
cur_pos = fd_pos( ft->fd );
cur_size = fd_size( ft->fd );
if (cur_size < 0)
{
return -1;
}
buf = (uint8_t *)malloc( size );
memset( buf, 0, size );
fd_seek( ft->fd, cur_size, FD_SEEK_SET );
resize = fd_write( ft->fd,