c78930075c135fac28ec6565b95e1dcbdeda8b12
1 /* Copyright (C) 2021-2022 Harry Godden (hgn) - All Rights Reserved */
7 #include "vg_platform.h"
17 #define VG_FILE_IO_CHUNK_SIZE 1024*256
19 VG_STATIC
void vg_file_print_invalid( FILE *fp
)
23 vg_error( "mdl_open: header too short\n" );
28 vg_error( "mdl_open: %s\n", strerror(errno
) );
30 vg_error( "mdl_open: unkown failure\n" );
35 /* read entire binary file */
36 VG_STATIC
void *vg_file_read( void *lin_alloc
, const char *path
)
38 FILE *f
= fopen( path
, "rb" );
41 void *buffer
= vg_linear_alloc( lin_alloc
, 0 );
45 for( u32 i
=0; 1; i
++ )
47 buffer
= vg_linear_extend( lin_alloc
, buffer
, VG_FILE_IO_CHUNK_SIZE
);
49 u64 l
= fread( buffer
+ current
, 1, VG_FILE_IO_CHUNK_SIZE
, f
);
52 if( l
!= VG_FILE_IO_CHUNK_SIZE
)
63 vg_fatal_exit_loop( "read error" );
68 vg_fatal_exit_loop( "unknown error codition" );
74 buffer
= vg_linear_resize( lin_alloc
, buffer
, current
);
81 vg_error( "vg_disk_open_read: %s\n", strerror(errno
) );
86 /* get the size of the file just loaded */
87 VG_STATIC u32
vg_linear_last_size( void *allocator
); /* ? */
88 VG_STATIC u32
vg_file_size( void *lin_alloc
)
90 return vg_linear_last_size( lin_alloc
);
93 /* read entire file and append a null on the end */
94 VG_STATIC
char *vg_file_read_text( void *lin_alloc
, const char *path
)
96 char *str
= vg_file_read( lin_alloc
, path
);
101 /* include null terminator */
102 str
= vg_linear_extend( lin_alloc
, str
, 1 );
103 str
[ vg_file_size(lin_alloc
) ] = '\0';
109 VG_STATIC
int vg_asset_write( const char *path
, void *data
, i64 size
)
111 FILE *f
= fopen( path
, "wb" );
114 fwrite( data
, size
, 1, f
);