audio rework pt 1
[vg.git] / src / vg / vg_io.h
1 /* Copyright (C) 2021-2022 Harry Godden (hgn) - All Rights Reserved */
2
3 #ifndef VG_IO_H
4 #define VG_IO_H
5
6 #include "vg_stdint.h"
7 #include "vg_platform.h"
8 #include <stdio.h>
9 #include <stdarg.h>
10 #include <string.h>
11 #include <malloc.h>
12
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"
21
22 void (*vg_log_callback)( const char *str ) = NULL;
23
24 static void vg_log_write( FILE *file, const char *prefix,
25 const char *fmt, va_list args )
26 {
27 char buffer[512];
28 int i, j;
29
30 for( i = 0; i < vg_list_size( buffer ); i ++ )
31 {
32 if( prefix[i] )
33 buffer[i] = prefix[i];
34 else
35 break;
36 }
37
38 j = i + vsnprintf( buffer + i, vg_list_size( buffer ) - i -12, fmt, args );
39 strcpy( buffer + j, KNRM );
40
41 fputs( buffer, file );
42
43 if( vg_log_callback )
44 vg_log_callback( buffer );
45 }
46
47 #define VG_LOGX( NAME, PIPE, PFX ) \
48 static void NAME(const char *fmt, ...) \
49 { \
50 va_list args; \
51 va_start( args, fmt ); \
52 vg_log_write( PIPE, (PFX), fmt, args ); \
53 va_end( args ); \
54 }
55
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) )
61
62 /*
63 * FIle I/O
64 */
65 static i64 vg_file_size( FILE *fileptr )
66 {
67 fseek( fileptr, 0, SEEK_END );
68 i64 fsize = ftell( fileptr );
69 fseek( fileptr, 0, SEEK_SET );
70
71 return fsize;
72 }
73
74 static void *vg_disk_open_read( const char *path, int reserve_end, i64 *size )
75 {
76 FILE *f = fopen( path, "rb" );
77 if( f )
78 {
79 i64 fsize = vg_file_size( f );
80 void *buf = malloc( fsize + reserve_end );
81
82 if( buf )
83 {
84 /* Invalid / corrupt read */
85 if( fread( buf, 1, fsize, f ) != fsize )
86 {
87 free( buf );
88 buf = NULL;
89 }
90 }
91
92 *size = fsize;
93
94 fclose( f );
95 return buf;
96 }
97 else
98 {
99 return NULL;
100 }
101 }
102
103 static char *vg_disk_load_text( const char *path, i64 *size )
104 {
105 char *buf;
106 i64 fsize;
107
108 if( (buf = (char *)vg_disk_open_read( path, 1, &fsize )) )
109 {
110 buf[ fsize ] = 0x00;
111 *size = fsize +1;
112
113 return buf;
114 }
115
116 return NULL;
117 }
118
119 static void *vg_asset_read_s( const char *path, i64 *size )
120 {
121 return vg_disk_open_read( path, 0, size );
122 }
123
124 static void *vg_asset_read( const char *path )
125 {
126 i64 size;
127 return vg_disk_open_read( path, 0, &size );
128 }
129
130 static char *vg_textasset_read_s( const char *path, i64 *size )
131 {
132 return vg_disk_load_text( path, size );
133 }
134
135 static char *vg_textasset_read( const char *name )
136 {
137 i64 size;
138 return vg_disk_load_text( name, &size );
139 }
140
141 static int vg_asset_write( const char *path, void *data, i64 size )
142 {
143 FILE *f = fopen( path, "wb" );
144 if( f )
145 {
146 fwrite( data, size, 1, f );
147 fclose( f );
148 return 1;
149 }
150 else
151 {
152 return 0;
153 }
154 }
155
156 #endif /* VG_IO_H */