1 /* Copyright (C) 2021-2022 Harry Godden (hgn) - All Rights Reserved */
7 #include "vg_platform.h"
13 #define KNRM "\x1B[0m"
14 #define KRED "\x1B[31m"
15 #define KGRN "\x1B[32m"
16 #define KYEL "\x1B[33m"
17 #define KBLU "\x1B[34m"
18 #define KMAG "\x1B[35m"
19 #define KCYN "\x1B[36m"
20 #define KWHT "\x1B[37m"
22 void (*vg_log_callback
)( const char *str
) = NULL
;
24 static void vg_log_write( FILE *file
, const char *prefix
,
25 const char *fmt
, va_list args
)
30 for( i
= 0; i
< vg_list_size( buffer
); i
++ )
33 buffer
[i
] = prefix
[i
];
38 j
= i
+ vsnprintf( buffer
+ i
, vg_list_size( buffer
) - i
-12, fmt
, args
);
39 strcpy( buffer
+ j
, KNRM
);
41 fputs( buffer
, file
);
44 vg_log_callback( buffer
);
47 #define VG_LOGX( NAME, PIPE, PFX ) \
48 static void NAME(const char *fmt, ...) \
51 va_start( args, fmt ); \
52 vg_log_write( PIPE, (PFX), fmt, args ); \
56 VG_LOGX( vg_success
, stdout
, (KGRN
"success" KWHT
"| " KGRN
) )
57 VG_LOGX( vg_info
, stdout
, (KNRM
" info" KWHT
"| " KNRM
) )
58 VG_LOGX( vg_log
, stdout
, (KWHT
" log" KWHT
"| " KWHT
) )
59 VG_LOGX( vg_warn
, stdout
, (KYEL
" warn" KWHT
"| " KYEL
) )
60 VG_LOGX( vg_error
, stderr
, (KRED
" error" KWHT
"| " KRED
) )
65 static i64
vg_file_size( FILE *fileptr
)
67 fseek( fileptr
, 0, SEEK_END
);
68 i64 fsize
= ftell( fileptr
);
69 fseek( fileptr
, 0, SEEK_SET
);
74 static void *vg_disk_open_read( const char *path
, int reserve_end
, i64
*size
)
76 FILE *f
= fopen( path
, "rb" );
79 i64 fsize
= vg_file_size( f
);
80 void *buf
= malloc( fsize
+ reserve_end
);
84 /* Invalid / corrupt read */
85 if( fread( buf
, 1, fsize
, f
) != fsize
)
103 static char *vg_disk_load_text( const char *path
, i64
*size
)
108 if( (buf
= (char *)vg_disk_open_read( path
, 1, &fsize
)) )
119 static void *vg_asset_read_s( const char *path
, i64
*size
)
121 return vg_disk_open_read( path
, 0, size
);
124 static void *vg_asset_read( const char *path
)
127 return vg_disk_open_read( path
, 0, &size
);
130 static char *vg_textasset_read_s( const char *path
, i64
*size
)
132 return vg_disk_load_text( path
, size
);
135 static char *vg_textasset_read( const char *name
)
138 return vg_disk_load_text( name
, &size
);
141 static int vg_asset_write( const char *path
, void *data
, i64 size
)
143 FILE *f
= fopen( path
, "wb" );
146 fwrite( data
, size
, 1, f
);