1 /* Copyright (C) 2021-2022 Harry Godden (hgn) - All Rights Reserved */
7 #include "vg_platform.h"
14 #define _TINYDIR_MALLOC(_size) vg_linear_alloc( vg_mem.scratch, _size )
15 #define _TINYDIR_FREE(_size)
17 #include "submodules/tinydir/tinydir.h"
21 * Create directory and silently ignore errors for already exists
23 VG_STATIC
int vg_mkdir( const char *path
)
25 if( mkdir( path
, S_IRWXU
|S_IRWXG
|S_IWOTH
|S_IXOTH
) ){
29 vg_error( "Failed to create directory: %s\n", path
);
41 #define VG_FILE_IO_CHUNK_SIZE 1024*256
43 VG_STATIC
void vg_file_print_invalid( FILE *fp
)
46 vg_error( "mdl_open: header too short\n" );
50 vg_error( "mdl_open: %s\n", strerror(errno
) );
52 vg_error( "mdl_open: unkown failure\n" );
57 /* read entire binary file */
58 VG_STATIC
void *vg_file_read( void *lin_alloc
, const char *path
, u32
*size
)
60 FILE *f
= fopen( path
, "rb" );
62 void *buffer
= vg_linear_alloc( lin_alloc
, 0 );
66 for( u32 i
=0; 1; i
++ ){
67 buffer
= vg_linear_extend( lin_alloc
, buffer
, VG_FILE_IO_CHUNK_SIZE
);
69 u64 l
= fread( buffer
+ current
, 1, VG_FILE_IO_CHUNK_SIZE
, f
);
72 if( l
!= VG_FILE_IO_CHUNK_SIZE
){
79 vg_fatal_error( "read error" );
83 vg_fatal_error( "unknown error codition" );
89 buffer
= vg_linear_resize( lin_alloc
, buffer
, vg_align8(current
) );
96 vg_error( "vg_disk_open_read: %s\n", strerror(errno
) );
101 /* read entire file and append a null on the end */
102 VG_STATIC
char *vg_file_read_text( void *lin_alloc
, const char *path
, u32
*sz
)
105 char *str
= vg_file_read( lin_alloc
, path
, &size
);
110 /* include null terminator */
111 str
= vg_linear_extend( lin_alloc
, str
, 1 );
119 VG_STATIC
int vg_asset_write( const char *path
, void *data
, i64 size
){
120 FILE *f
= fopen( path
, "wb" );
122 fwrite( data
, size
, 1, f
);
131 /* TODO: error handling if read fails */
132 VG_STATIC
int vg_file_copy( const char *src
, const char *dst
, void *lin_alloc
)
134 vg_info( "vg_file_copy( %s -> %s )\n", src
, dst
);
136 void *data
= vg_file_read( lin_alloc
, src
, &size
);
137 return vg_asset_write( dst
, data
, size
);
140 VG_STATIC
const char *vg_path_filename( const char *path
)
142 const char *base
= path
;
144 for( int i
=0; i
<1024; i
++ ){
145 if( path
[i
] == '\0' ) break;
146 if( path
[i
] == '/' ){