aboutsummaryrefslogtreecommitdiffstats
path: root/core.c
diff options
context:
space:
mode:
authorFreeArtMan <=>2016-02-04 23:02:20 +0000
committerFreeArtMan <=>2016-02-04 23:02:20 +0000
commitebe6923b17de88d37d97846be2567fb75f53292c (patch)
treeca14f19a6634546d93a02775b407e8197c805760 /core.c
downloadihe-ebe6923b17de88d37d97846be2567fb75f53292c.tar.gz
ihe-ebe6923b17de88d37d97846be2567fb75f53292c.zip
Initial commit
Diffstat (limited to 'core.c')
-rw-r--r--core.c326
1 files changed, 326 insertions, 0 deletions
diff --git a/core.c b/core.c
new file mode 100644
index 0000000..2711111
--- /dev/null
+++ b/core.c
@@ -0,0 +1,326 @@
+#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;
+ }
+
+ 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;
+
+ printf("%d %x %d\n", fd, buf, count);
+ 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 = 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;
+
+ fd = fd_open( filename, flags );
+ if ( fd < 1 )
+ {
+ printf("Couldnt open %s\n", filename);
+ return -1;
+ }
+
+ ft->fd = fd;
+ ft->flags = flags;
+ ft->filename = filename;
+
+
+ /* 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_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_open( file_t *ft, const char *filename, int flags, int mode )
+{
+ int ret = 0;
+ printf("Not implemented\n");
+ ret = -1;
+ return ret;
+}
+
+
+int file_read_blk( file_t *ft, uint8_t *buf )
+{
+ int ret = 0;
+
+ if ( ft->fd < 0 )
+ return -1;
+
+ file_pos( ft );
+ ret = fd_read( ft->fd, buf, ft->blk_size );
+ fd_set_pos( ft->fd, ft->position ); //chck err?
+
+ if ( ret < 0)
+ {
+ printf("Error while reading from file\n");
+ }
+
+ return ret;
+}
+
+
+int file_read( file_t *ft, uint8_t *buf, size_t count )
+{
+ int ret = 0;
+
+ ret = fd_read( ft->fd, buf, count );
+ if ( ret < 0 )
+ {
+ printf("Cannot read\n");
+ } else if ( ret < count )
+ {
+ printf("Requestd %d readed %d\n", count, ret);
+ }
+
+ return ret;
+}
+
+
+int file_write_blk( file_t *ft, uint8_t *buf )
+{
+ int ret = 0;
+ unsigned int sz;
+
+ file_pos( ft );
+ if ( ft->position + ft->blk_size <= ft->size )
+ {
+ sz = ft->blk_size;
+ } else
+ {
+ sz = ft->size - ft->position; //when pos 0 ans size 1 then will write 1 byte
+ }
+ printf(" %d %x %u \n", ft->fd, buf, sz);
+ ret = fd_write( ft->fd, buf, sz );
+ if ( ret < 0 )
+ {
+ printf("Error while writing block to file\n");
+ }
+ fd_set_pos( ft->fd, ft->position );
+
+
+
+ return ret;
+}
+
+
+int file_write( file_t *ft, uint8_t *buf, size_t count )
+{
+ int ret = 0;
+
+ ret = fd_write( ft->fd, buf, count );
+ if ( ret < 0 )
+ {
+ printf("Cannot write\n");
+ } else if ( ret < count )
+ {
+ printf("Requested %d written %d\n", count, ret);
+ }
+
+ return ret;
+}
+
+
+int file_s_mode( file_t *ft, int mode )
+{
+ int ret = 0;
+
+ if ( ft == NULL)
+ return -1;
+
+ ft->mode = mode;
+
+ return ret;
+}
+
+
+int file_close( file_t *ft )
+{
+ int ret = 0;
+
+ if ( ft->fd < 1 )
+ {
+ printf("File descriptor <1\n");
+ ret = -1;
+ }
+
+ if ( ft->filename == NULL )
+ {
+ printf("File name is empty\n");
+ ret = -1;
+ }
+
+ fd_close( ft->fd );
+ memset( ft, 0, sizeof(file_t) );
+
+ return ret;
+}