}
/* read entire binary file */
-static void *vg_file_read( void *lin_alloc, const char *path, u32 *size )
-{
+static void *vg_file_read( void *lin_alloc, const char *path, u32 *size ){
FILE *f = fopen( path, "rb" );
if( f ){
- void *buffer = vg_linear_alloc( lin_alloc, 0 );
+ void *buffer = lin_alloc? vg_linear_alloc( lin_alloc, 0 ):
+ NULL;
u64 current = 0;
/* read in chunks */
for( u32 i=0; 1; i++ ){
- buffer = vg_linear_extend( lin_alloc, buffer, VG_FILE_IO_CHUNK_SIZE );
+ if( lin_alloc )
+ buffer = vg_linear_extend( lin_alloc,buffer,VG_FILE_IO_CHUNK_SIZE );
+ else
+ buffer = realloc( buffer, current + VG_FILE_IO_CHUNK_SIZE );
u64 l = fread( buffer + current, 1, VG_FILE_IO_CHUNK_SIZE, f );
current += l;
}
}
- buffer = vg_linear_resize( lin_alloc, buffer, vg_align8(current) );
+ if( lin_alloc )
+ buffer = vg_linear_resize( lin_alloc, buffer, vg_align8(current) );
+ else
+ buffer = realloc( buffer, vg_align8(current) );
+
fclose( f );
*size = (u32)current;
return buffer;
}
else{
- vg_error( "vg_disk_open_read: %s\n", strerror(errno) );
+ vg_error( "vg_disk_open_read: %s (file: %s)\n", strerror(errno), path );
return NULL;
}
}
/* read entire file and append a null on the end */
-static char *vg_file_read_text( void *lin_alloc, const char *path, u32 *sz )
-{
+static char *vg_file_read_text( void *lin_alloc, const char *path, u32 *sz ){
u32 size;
char *str = vg_file_read( lin_alloc, path, &size );
return NULL;
/* include null terminator */
- str = vg_linear_extend( lin_alloc, str, 1 );
+ if( lin_alloc )
+ str = vg_linear_extend( lin_alloc, str, 1 );
+ else
+ str = realloc( str, size+1 );
+
str[ size ] = '\0';
*sz = size+1;