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) example: /test/file.jpg returns pointer to: jpg
18 char *csr_findext( char *path
, char const delim
);
19 char *csr_findsep( char *path
); // Same as above but operates on \ and /
21 // gets rid of extension on string only left with (folder)+filename
22 void csr_stripext( char *path
);
24 int csr_path_is_abs( char const *path
);
26 // Remove one level (nop if can't) eg: /home/harry/test.file -> /home/harry/
27 void csr_downlvl( char *path
);
29 // Get only the file name example: /test/file.jpg returns file.jpg
30 char *csr_filename( char *path
);
33 //=======================================================================================================================
36 #define CSR_FOLDER_CHAR '\\'
38 #define CSR_FOLDER_CHAR '/'
43 i64
fs_file_size( FILE *fileptr
)
45 fseek( fileptr
, 0, SEEK_END
);
46 i64 fsize
= ftell( fileptr
);
47 fseek( fileptr
, 0, SEEK_SET
);
52 void *fs_disk_open_read( const char *path
, int const reserve_end
, i64
*size
)
54 FILE *f
= fopen( path
, "rb" );
57 i64 fsize
= fs_file_size( f
);
58 void *buf
= csr_malloc( fsize
+ reserve_end
);
62 // Invalid / corrupt read
63 if( fread( buf
, 1, fsize
, f
) != fsize
)
81 char *fs_disk_load_text( const char *path
, i64
*size
)
86 if( (buf
= fs_disk_open_read( path
, 1, &fsize
)) )
97 void *csr_asset_read_s( const char *path
, i64
*size
)
99 return fs_disk_open_read( path
, 0, size
);
102 void *csr_asset_read( const char *path
)
105 return fs_disk_open_read( path
, 0, &size
);
108 char *csr_textasset_read_s( const char *path
, i64
*size
)
110 return fs_disk_load_text( path
, size
);
113 char *csr_textasset_read( const char *name
)
116 return fs_disk_load_text( name
, &size
);
119 char *csr_findext( char *path
, char const delim
)
139 // Find 'seperator'.. because folders can be / or \ on windows..
140 char *csr_findsep( char *path
)
149 if( *c
== '/' || *c
== '\\' )
160 void csr_stripext( char *path
)
165 if( !(start
= csr_findsep( path
)) )
170 if( (point
= csr_findext( start
, '.' )) )
179 void csr_downlvl( char *path
)
181 char *start_name
, *c
;
189 path
[ len
-1 ] = 0x00;
191 if( (start_name
= csr_findsep( path
) ))
197 char *csr_filename( char *path
)
200 if( (base_name
= csr_findsep( path
) ))
206 int csr_path_is_abs( char const *path
)
209 if( strlen( path
) < 2 ) return 0;
210 return path
[1] == ':';
212 if( strlen( path
) < 1 ) return 0;
213 return path
[0] == '/';