X-Git-Url: https://harrygodden.com/git/?a=blobdiff_plain;f=src%2Fvg%2Fvg_io.h;h=b5440ec201be29fd95a234054efd1e594819e16b;hb=3dd767bb10e6fee9cbffeb185d1a9685810c17b5;hp=19d468b8e1ce39634cdbe31dcdac7e1447c6a363;hpb=6aeba08ad9ad8bdecafcfd9f946173e99a84fc59;p=vg.git diff --git a/src/vg/vg_io.h b/src/vg/vg_io.h index 19d468b..b5440ec 100644 --- a/src/vg/vg_io.h +++ b/src/vg/vg_io.h @@ -5,88 +5,114 @@ #include "vg_stdint.h" #include "vg_platform.h" +#include "vg_log.h" +#include "vg_mem.h" #include +#include /* - * FIle I/O + * File I/O */ -static i64 vg_file_size( FILE *fileptr ) + +#define VG_FILE_IO_CHUNK_SIZE 1024*256 + +VG_STATIC void vg_file_print_invalid( FILE *fp ) { - fseek( fileptr, 0, SEEK_END ); - i64 fsize = ftell( fileptr ); - fseek( fileptr, 0, SEEK_SET ); - - return fsize; + if( feof( fp )) + { + vg_error( "mdl_open: header too short\n" ); + } + else + { + if( ferror( fp )) + vg_error( "mdl_open: %s\n", strerror(errno) ); + else + vg_error( "mdl_open: unkown failure\n" ); + + } } -static void *vg_disk_open_read( const char *path, int reserve_end, i64 *size ) +/* read entire binary file */ +VG_STATIC void *vg_file_read( void *lin_alloc, const char *path, u32 *size ) { FILE *f = fopen( path, "rb" ); if( f ) { - i64 fsize = vg_file_size( f ); - void *buf = malloc( fsize + reserve_end ); - - if( buf ) - { - /* Invalid / corrupt read */ - if( fread( buf, 1, fsize, f ) != fsize ) - { - free( buf ); - buf = NULL; - } - } - - *size = fsize; - + void *buffer = vg_linear_alloc( lin_alloc, 0 ); + u64 current = 0; + + /* read in chunks */ + for( u32 i=0; 1; i++ ) + { + buffer = vg_linear_extend( lin_alloc, buffer, VG_FILE_IO_CHUNK_SIZE ); + + u64 l = fread( buffer + current, 1, VG_FILE_IO_CHUNK_SIZE, f ); + current += l; + + if( l != VG_FILE_IO_CHUNK_SIZE ) + { + if( feof( f ) ) + { + break; + } + else + { + if( ferror( f ) ) + { + fclose(f); + vg_fatal_exit_loop( "read error" ); + } + else + { + fclose(f); + vg_fatal_exit_loop( "unknown error codition" ); + } + } + } + } + + buffer = vg_linear_resize( lin_alloc, buffer, current ); fclose( f ); - return buf; + + *size = (u32)current; + + return buffer; } else { + vg_error( "vg_disk_open_read: %s\n", strerror(errno) ); return NULL; } } -static char *vg_disk_load_text( const char *path, i64 *size ) +/* get the size of the file just loaded */ +#if 0 +VG_STATIC u32 vg_linear_last_size( void *allocator ); /* ? */ +VG_STATIC u32 vg_file_size( void *lin_alloc ) { - char *buf; - i64 fsize; - - if( (buf = (char *)vg_disk_open_read( path, 1, &fsize )) ) - { - buf[ fsize ] = 0x00; - *size = fsize +1; - - return buf; - } - - return NULL; + return vg_linear_last_size( lin_alloc ); } +#endif -static void *vg_asset_read_s( const char *path, i64 *size ) +/* read entire file and append a null on the end */ +VG_STATIC char *vg_file_read_text( void *lin_alloc, const char *path, u32 *sz ) { - return vg_disk_open_read( path, 0, size ); -} + u32 size; + char *str = vg_file_read( lin_alloc, path, &size ); -static void *vg_asset_read( const char *path ) -{ - i64 size; - return vg_disk_open_read( path, 0, &size ); -} + if( !str ) + return NULL; -static char *vg_textasset_read_s( const char *path, i64 *size ) -{ - return vg_disk_load_text( path, size ); -} + /* include null terminator */ + str = vg_linear_extend( lin_alloc, str, 1 ); + str[ size ] = '\0'; + *sz = size+1; -static char *vg_textasset_read( const char *name ) -{ - i64 size; - return vg_disk_load_text( name, &size ); + return str; } -static int vg_asset_write( const char *path, void *data, i64 size ) + +VG_STATIC int vg_asset_write( const char *path, void *data, i64 size ) { FILE *f = fopen( path, "wb" ); if( f )