X-Git-Url: https://harrygodden.com/git/?a=blobdiff_plain;f=src%2Fvg%2Fvg_io.h;h=b5440ec201be29fd95a234054efd1e594819e16b;hb=9917df2d289c6c35d6bf54319aed3aed421b53f1;hp=afd134f45a05a76d72d0a69ee7a5076a2eeecd93;hpb=6cfa3e0895e42f702276e97c85ad371f3512c67d;p=vg.git diff --git a/src/vg/vg_io.h b/src/vg/vg_io.h index afd134f..b5440ec 100644 --- a/src/vg/vg_io.h +++ b/src/vg/vg_io.h @@ -1,130 +1,118 @@ -// Copyright (C) 2021 Harry Godden (hgn) - All Rights Reserved +/* Copyright (C) 2021-2022 Harry Godden (hgn) - All Rights Reserved */ -// Coloured logging -// =========================================================================================================== +#ifndef VG_IO_H +#define VG_IO_H -#define KNRM "\x1B[0m" -#define KRED "\x1B[31m" -#define KGRN "\x1B[32m" -#define KYEL "\x1B[33m" -#define KBLU "\x1B[34m" -#define KMAG "\x1B[35m" -#define KCYN "\x1B[36m" -#define KWHT "\x1B[37m" +#include "vg_stdint.h" +#include "vg_platform.h" +#include "vg_log.h" +#include "vg_mem.h" +#include +#include -void (*vg_log_callback)( const char *str ) = NULL; +/* + * File I/O + */ -static void vg_log_write( FILE *file, const char *prefix, const char *fmt, va_list args ) -{ - char buffer[512]; - int i, j; - - for( i = 0; i < vg_list_size( buffer ); i ++ ) - { - if( prefix[i] ) - buffer[i] = prefix[i]; - else - break; - } - - j = i + vsnprintf( buffer + i, vg_list_size( buffer ) - i -2, fmt, args ); - strcpy( buffer + j, KNRM ); - - fputs( buffer, file ); - - if( vg_log_callback ) - vg_log_callback( buffer ); -} - -static void vg_success( const char *fmt, ... ) - { va_list args; va_start( args, fmt ); vg_log_write( stdout, (KGRN "success" KWHT "| " KGRN), fmt, args ); va_end( args ); } -static void vg_info( const char *fmt, ... ) - { va_list args; va_start( args, fmt ); vg_log_write( stdout, (KNRM " info" KWHT "| " KNRM), fmt, args ); va_end( args ); } -static void vg_warn( const char *fmt, ... ) - { va_list args; va_start( args, fmt ); vg_log_write( stdout, (KYEL " warn" KWHT "| " KYEL), fmt, args ); va_end( args ); } -static void vg_error( const char *fmt, ... ) - { va_list args; va_start( args, fmt ); vg_log_write( stderr, (KRED " error" KWHT "| " KRED), fmt, args ); va_end( args ); } - -// FILE IO -// =========================================================================================================== +#define VG_FILE_IO_CHUNK_SIZE 1024*256 -static i64 vg_file_size( FILE *fileptr ) +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 const 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 = 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 ) @@ -138,3 +126,5 @@ static int vg_asset_write( const char *path, void *data, i64 size ) return 0; } } + +#endif /* VG_IO_H */