1 // Copyright (C) 2021 Harry Godden (hgn)
3 // Low level disk reading
4 //=======================================================================================================================
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
);
12 i64
fs_file_size( FILE *fileptr
);
17 // Find file path extension, returns NULL if no ext (0x00)
18 char *csr_findext( char *path
, char const delim
);
20 // gets rid of extension on string only left with (folder)+filename
21 void csr_stripext( char *path
);
23 int csr_path_is_abs( char const *path
);
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
);
30 //=======================================================================================================================
34 i64
fs_file_size( FILE *fileptr
)
36 fseek( fileptr
, 0, SEEK_END
);
37 i64 fsize
= ftell( fileptr
);
38 fseek( fileptr
, 0, SEEK_SET
);
43 void *fs_disk_open_read( const char *path
, int const reserve_end
, i64
*size
)
45 FILE *f
= fopen( path
, "rb" );
48 i64 fsize
= fs_file_size( f
);
49 void *buf
= csr_malloc( fsize
+ reserve_end
);
53 // Invalid / corrupt read
54 if( fread( buf
, 1, fsize
, f
) != fsize
)
72 char *fs_disk_load_text( const char *path
, i64
*size
)
77 if( (buf
= fs_disk_open_read( path
, 1, &fsize
)) )
88 void *csr_asset_read_s( const char *path
, i64
*size
)
90 return fs_disk_open_read( path
, 0, size
);
93 void *csr_asset_read( const char *path
)
96 return fs_disk_open_read( path
, 0, &size
);
99 char *csr_textasset_read_s( const char *path
, i64
*size
)
101 return fs_disk_load_text( path
, size
);
104 char *csr_textasset_read( const char *name
)
107 return fs_disk_load_text( name
, &size
);
110 char *csr_findext( char *path
, char const delim
)
130 void csr_stripext( char *path
)
135 if( !(start
= csr_findext( path
, '/' )) )
140 if( (point
= csr_findext( start
, '.' )) )
149 void csr_path_winunix( char *path
)
163 if( *(idx
-1) == '/' && *idx
== '/') idx
++;
174 int csr_path_is_abs( char const *path
)
177 if( strlen( path
) < 2 ) return 0;
178 return path
[1] == ':';
180 if( strlen( path
) < 1 ) return 0;
181 return path
[0] == '/';