76bed7e228fd138f0cc5652fad8ff3f4a30042c9
[csRadar.git] / csrIO.h
1 // Copyright (C) 2021 Harry Godden (hgn)
2
3 // Low level disk reading
4 //=======================================================================================================================
5
6 // Read binary, or text assets. _s variants also give size in bytes
7 void *csr_asset_read_s( const char *path, i64 *size );
8 void *csr_asset_read( const char *path );
9 char *csr_textasset_read_s( const char *path, i64 *size );
10 char *csr_textasset_read( const char *name );
11
12 i64 fs_file_size( FILE *fileptr );
13
14 // Path handling
15 // -------------
16
17 // Find file path extension, returns NULL if no ext (0x00)
18 char *csr_findext( char *path, char const delim );
19
20 // gets rid of extension on string only left with (folder)+filename
21 void csr_stripext( char *path );
22
23 int csr_path_is_abs( char const *path );
24
25 // Convert windows paths to unix.. sortof ( \something\\blahblah .. ) -> /something/blahblah/
26 // Does not handle drive letters, idea is to increase windows compatibility will the other functions above
27 void csr_path_winunix( char *path );
28
29 // Implementation
30 //=======================================================================================================================
31
32 #ifdef CSR_EXECUTABLE
33
34 i64 fs_file_size( FILE *fileptr )
35 {
36 fseek( fileptr, 0, SEEK_END );
37 i64 fsize = ftell( fileptr );
38 fseek( fileptr, 0, SEEK_SET );
39
40 return fsize;
41 }
42
43 void *fs_disk_open_read( const char *path, int const reserve_end, i64 *size )
44 {
45 FILE *f = fopen( path, "rb" );
46 if( f )
47 {
48 i64 fsize = fs_file_size( f );
49 void *buf = csr_malloc( fsize + reserve_end );
50
51 if( buf )
52 {
53 // Invalid / corrupt read
54 if( fread( buf, 1, fsize, f ) != fsize )
55 {
56 free( buf );
57 buf = NULL;
58 }
59 }
60
61 *size = fsize;
62
63 fclose( f );
64 return buf;
65 }
66 else
67 {
68 return NULL;
69 }
70 }
71
72 char *fs_disk_load_text( const char *path, i64 *size )
73 {
74 char *buf;
75 i64 fsize;
76
77 if( (buf = fs_disk_open_read( path, 1, &fsize )) )
78 {
79 buf[ fsize ] = 0x00;
80 *size = fsize +1;
81
82 return buf;
83 }
84
85 return NULL;
86 }
87
88 void *csr_asset_read_s( const char *path, i64 *size )
89 {
90 return fs_disk_open_read( path, 0, size );
91 }
92
93 void *csr_asset_read( const char *path )
94 {
95 i64 size;
96 return fs_disk_open_read( path, 0, &size );
97 }
98
99 char *csr_textasset_read_s( const char *path, i64 *size )
100 {
101 return fs_disk_load_text( path, size );
102 }
103
104 char *csr_textasset_read( const char *name )
105 {
106 i64 size;
107 return fs_disk_load_text( name, &size );
108 }
109
110 char *csr_findext( char *path, char const delim )
111 {
112 char *c, *ptr;
113
114 c = path;
115 ptr = NULL;
116
117 while( *c )
118 {
119 if( *c == delim )
120 {
121 ptr = c + 1;
122 }
123
124 c ++;
125 }
126
127 return ptr;
128 }
129
130 void csr_stripext( char *path )
131 {
132 char *point, *start;
133
134 // Skip folders
135 if( !(start = csr_findext( path, '/' )) )
136 {
137 start = path;
138 }
139
140 if( (point = csr_findext( start, '.' )) )
141 {
142 if( point > path )
143 {
144 *(point-1) = 0x00;
145 }
146 }
147 }
148
149 void csr_path_winunix( char *path )
150 {
151 char *idx, *wr;
152 wr = idx = path;
153
154 while( *idx )
155 {
156 if( *idx == '\\' )
157 {
158 *idx = '/';
159 }
160
161 if( idx > path )
162 {
163 if( *(idx -1) == '/' && *idx == '/') idx ++;
164 }
165
166 *( wr ++ ) = *idx;
167
168 idx ++;
169 }
170
171 *wr = 0x00;
172 }
173
174 int csr_path_is_abs( char const *path )
175 {
176 #ifdef _WIN32
177 if( strlen( path ) < 2 ) return 0;
178 return path[1] == ':';
179 #else
180 if( strlen( path ) < 1 ) return 0;
181 return path[0] == '/';
182 #endif
183 }
184
185 #endif