From ae9cfee6605db373e9aa8b7c054403d83a612595 Mon Sep 17 00:00:00 2001 From: FreeArtMan Date: Wed, 24 Dec 2014 15:43:34 +0900 Subject: File usage lib version up --- file_use.c | 51 ++++++++++++++++++++++++++++++++++++++++++++++----- file_use.h | 1 + 2 files changed, 47 insertions(+), 5 deletions(-) diff --git a/file_use.c b/file_use.c index cd816bc..3aae696 100644 --- a/file_use.c +++ b/file_use.c @@ -1,6 +1,7 @@ #include "file_use.h" +//--------------------------------------------------------------------- void f_file_null( f_file *f_f ) { if ( f_f ) @@ -12,6 +13,8 @@ void f_file_null( f_file *f_f ) } } + +//--------------------------------------------------------------------- int f_file_seek( f_file *f_f, long offset, int seek ) { int ret=-1; @@ -47,10 +50,11 @@ int f_file_seek( f_file *f_f, long offset, int seek ) return ret; } - +//--------------------------------------------------------------------- size_t f_file_read( f_file *f_f, size_t size, void *ptr ) { size_t ret=-1; + PRINT("\n"); if ( f_f ) { @@ -86,6 +90,38 @@ size_t f_file_read( f_file *f_f, size_t size, void *ptr ) } +//--------------------------------------------------------------------- +//not effective algo ofcourse, but who cares while no one cares +//read buffer and then find in buffer newline, calc nl position and +//do the job +size_t f_file_readl( f_file *f_f, size_t size, void *ptr ) +{ + size_t ret=-1; + + PRINT("\n"); + if ( f_f ) + { + int c=0; + size_t counter=0; + //fseek( f_f->fid, 0, SEEK_SET); + c = getc( f_f->fid ); + while ( (c != '\n') && ( c != EOF )) + { + if ( counter < size ) + { + ((char *)(ptr))[counter] = (char)c; + counter++; + } + + c = getc( f_f->fid ); + } + if ( counter > 0 ) ret = counter; + } + + return ret; +} + +//--------------------------------------------------------------------- int f_file_size( f_file *f_f ) { int ret=-1; @@ -110,9 +146,10 @@ int f_file_size( f_file *f_f ) //f_file_read_bl(); +//--------------------------------------------------------------------- size_t f_file_write( f_file *f_f, size_t size, void *ptr ) { - PRINT("%s\n"); + PRINT("\n"); if ( f_f ) { if ((f_f->flags == F_FILE_WRITE) || @@ -128,15 +165,17 @@ size_t f_file_write( f_file *f_f, size_t size, void *ptr ) //f_file_write_bl(); +//--------------------------------------------------------------------- f_file* f_file_open( const char *fname, int flags ) { f_file *ret = NULL; - const char *f_flags_write="r"; - const char *f_flags_read="w"; - const char *f_flags_rw="r+"; + char *f_flags_write="r"; + char *f_flags_read="w"; + char *f_flags_rw="r+"; char *f_flags_tmp=NULL; + PRINT("\n"); if ( fname != NULL ) { ret = malloc( sizeof( f_file ) ); @@ -175,6 +214,7 @@ f_file* f_file_open( const char *fname, int flags ) goto exit_close_f; } ret->size = f_file_size( ret ); + fseek( ret->fid, 0, SEEK_SET ); } return ret; @@ -185,6 +225,7 @@ exit_close_f: } +//--------------------------------------------------------------------- int f_file_close( f_file *f_f ) { PRINT("\n"); diff --git a/file_use.h b/file_use.h index fe7cc08..6aa11da 100644 --- a/file_use.h +++ b/file_use.h @@ -28,6 +28,7 @@ typedef struct f_file void f_file_null( f_file* ); int f_file_seek( f_file*, long, int ); size_t f_file_read( f_file*, size_t, void* ); +size_t f_file_readl( f_file*, size_t, void* ); //read until readline, return how much readed //f_file_read_bl(); int f_file_size( f_file* ); size_t f_file_write( f_file*, size_t, void* ); -- cgit v1.2.3