X-Git-Url: https://harrygodden.com/git/?p=csRadar.git;a=blobdiff_plain;f=csrIO.h;h=437a462fc5d580b463e03f194e0514dba8882786;hp=898b41a3d11977ea2e8b0062b8137b8edc06724b;hb=HEAD;hpb=a97099abba0a239e20929f04ece9d6839c96ac14 diff --git a/csrIO.h b/csrIO.h index 898b41a..437a462 100644 --- a/csrIO.h +++ b/csrIO.h @@ -1,6 +1,45 @@ +// Copyright (C) 2021 Harry Godden (hgn) + // Low level disk reading //======================================================================================================================= +// Read binary, or text assets. _s variants also give size in bytes +void *csr_asset_read_s( const char *path, i64 *size ); +void *csr_asset_read( const char *path ); +char *csr_textasset_read_s( const char *path, i64 *size ); +char *csr_textasset_read( const char *name ); + +i64 fs_file_size( FILE *fileptr ); + +// Path handling +// ------------- + +// Find file path extension, returns NULL if no ext (0x00) example: /test/file.jpg returns pointer to: jpg +char *csr_findext( char *path, char const delim ); +char *csr_findsep( char *path ); // Same as above but operates on \ and / + +// gets rid of extension on string only left with (folder)+filename +void csr_stripext( char *path ); + +int csr_path_is_abs( char const *path ); + +// Remove one level (nop if can't) eg: /home/harry/test.file -> /home/harry/ +void csr_downlvl( char *path ); + +// Get only the file name example: /test/file.jpg returns file.jpg +char *csr_filename( char *path ); + +// Implementation +//======================================================================================================================= + +#ifdef _WIN32 + #define CSR_FOLDER_CHAR '\\' +#else + #define CSR_FOLDER_CHAR '/' +#endif + +#ifdef CSR_EXECUTABLE + i64 fs_file_size( FILE *fileptr ) { fseek( fileptr, 0, SEEK_END ); @@ -77,7 +116,6 @@ char *csr_textasset_read( const char *name ) return fs_disk_load_text( name, &size ); } -// Find file path extension, returns NULL if no ext (0x00) char *csr_findext( char *path, char const delim ) { char *c, *ptr; @@ -98,13 +136,33 @@ char *csr_findext( char *path, char const delim ) return ptr; } -// gets rid of extension on string only left with folder/filename +// Find 'seperator'.. because folders can be / or \ on windows.. +char *csr_findsep( char *path ) +{ + char *c, *ptr; + + c = path; + ptr = NULL; + + while( *c ) + { + if( *c == '/' || *c == '\\' ) + { + ptr = c + 1; + } + + c ++; + } + + return ptr; +} + void csr_stripext( char *path ) { char *point, *start; // Skip folders - if( !(start = csr_findext( path, '/' )) ) + if( !(start = csr_findsep( path )) ) { start = path; } @@ -118,30 +176,31 @@ void csr_stripext( char *path ) } } -// Convert windows paths to unix-ish ( \something\\blahblah .. ) -> /something/blahblah/ -void csr_path_winunix( char *path ) +void csr_downlvl( char *path ) { - char *idx, *wr; - wr = idx = path; + char *start_name, *c; + + c = path; + while( *c ) + c ++; + int len = c - path; - while( *idx ) - { - if( *idx == '\\' ) - { - *idx = '/'; - } - - if( idx > path ) - { - if( *(idx -1) == '/' && *idx == '/') idx ++; - } - - *( wr ++ ) = *idx; - - idx ++; - } + if( len ) + path[ len -1 ] = 0x00; - *wr = 0x00; + if( (start_name = csr_findsep( path ) )) + *start_name = 0x00; + else + path[0] = 0x00; +} + +char *csr_filename( char *path ) +{ + char *base_name; + if( (base_name = csr_findsep( path ) )) + return base_name; + + return path; } int csr_path_is_abs( char const *path ) @@ -155,8 +214,4 @@ int csr_path_is_abs( char const *path ) #endif } -#ifdef _WIN32 - #define GetWorkingDir _getcwd -#else - #define GetWorkingDir getcwd #endif