pre-move config validation
[fishladder.git] / vg / vg_io.h
1 // Copyright (C) 2021 Harry Godden (hgn) - All Rights Reserved
2
3 // Coloured logging
4 // ===========================================================================================================
5
6 #define KNRM "\x1B[0m"
7 #define KRED "\x1B[31m"
8 #define KGRN "\x1B[32m"
9 #define KYEL "\x1B[33m"
10 #define KBLU "\x1B[34m"
11 #define KMAG "\x1B[35m"
12 #define KCYN "\x1B[36m"
13 #define KWHT "\x1B[37m"
14
15 #define VG_LOG_WRITE( FILE, PREFIX ) \
16 fprintf( FILE, PREFIX ); \
17 va_list args; \
18 va_start( args, fmt ); \
19 vfprintf( FILE, fmt, args ); \
20 va_end( args ); \
21 fprintf( FILE, KNRM );
22
23 void vg_success( const char *fmt, ... ) { VG_LOG_WRITE( stdout, (KGRN "success" KWHT "| " KGRN) ); }
24 void vg_info( const char *fmt, ... ) { VG_LOG_WRITE( stdout, (KNRM " info" KWHT "| " KNRM) ); }
25 void vg_warn( const char *fmt, ... ) { VG_LOG_WRITE( stdout, (KYEL " warn" KWHT "| " KYEL) ); }
26 void vg_error( const char *fmt, ... ) { VG_LOG_WRITE( stderr, (KRED " error" KWHT "| " KRED) ); }
27
28 // FILE IO
29 // ===========================================================================================================
30
31 i64 vg_file_size( FILE *fileptr )
32 {
33 fseek( fileptr, 0, SEEK_END );
34 i64 fsize = ftell( fileptr );
35 fseek( fileptr, 0, SEEK_SET );
36
37 return fsize;
38 }
39
40 void *vg_disk_open_read( const char *path, int const reserve_end, i64 *size )
41 {
42 FILE *f = fopen( path, "rb" );
43 if( f )
44 {
45 i64 fsize = vg_file_size( f );
46 void *buf = malloc( fsize + reserve_end );
47
48 if( buf )
49 {
50 // Invalid / corrupt read
51 if( fread( buf, 1, fsize, f ) != fsize )
52 {
53 free( buf );
54 buf = NULL;
55 }
56 }
57
58 *size = fsize;
59
60 fclose( f );
61 return buf;
62 }
63 else
64 {
65 return NULL;
66 }
67 }
68
69 char *vg_disk_load_text( const char *path, i64 *size )
70 {
71 char *buf;
72 i64 fsize;
73
74 if( (buf = vg_disk_open_read( path, 1, &fsize )) )
75 {
76 buf[ fsize ] = 0x00;
77 *size = fsize +1;
78
79 return buf;
80 }
81
82 return NULL;
83 }
84
85 void *vg_asset_read_s( const char *path, i64 *size )
86 {
87 return vg_disk_open_read( path, 0, size );
88 }
89
90 void *vg_asset_read( const char *path )
91 {
92 i64 size;
93 return vg_disk_open_read( path, 0, &size );
94 }
95
96 char *vg_textasset_read_s( const char *path, i64 *size )
97 {
98 return vg_disk_load_text( path, size );
99 }
100
101 char *vg_textasset_read( const char *name )
102 {
103 i64 size;
104 return vg_disk_load_text( name, &size );
105 }