merge duplicated path_ functions

handle backslashes, which is out of convention, but safer, as practically paths may contain them in many spots
This commit is contained in:
Garux
2021-01-21 08:53:34 +03:00
parent 15e4b8e850
commit 7fc079c658
4 changed files with 38 additions and 96 deletions

View File

@@ -564,16 +564,6 @@ void SaveFile( const char *filename, const void *buffer, int count ){
====================
*/
/// \brief Returns true if \p path is a fully qualified file-system path.
bool path_is_absolute( const char* path ){
#if defined( WIN32 )
return path[0] == '/'
|| ( path[0] != '\0' && path[1] == ':' ); // local drive
#elif defined( POSIX )
return path[0] == '/';
#endif
}
/// \brief Returns a pointer to the last slash or to terminating null character if not found.
const char* path_get_last_separator( const char* path ){
const char *end = path + strlen( path );
@@ -590,52 +580,6 @@ char* path_get_last_separator( char* path ){
return const_cast<char*>( path_get_last_separator( const_cast<const char*>( path ) ) );
}
/// \brief Returns a pointer to the first character of the filename component of \p path.
const char* path_get_filename_start( const char* path ){
const char *src = path + strlen( path );
while ( src != path && !path_separator( src[-1] ) ){
--src;
}
return src;
}
char* path_get_filename_start( char* path ){
return const_cast<char*>( path_get_filename_start( const_cast<const char*>( path ) ) );
}
/// \brief Returns a pointer to the character after the end of the filename component of \p path - either the extension separator or the terminating null character.
const char* path_get_filename_base_end( const char* path ){
const char *end = path + strlen( path );
const char *src = end;
while ( src != path && !path_separator( *--src ) ){
if( *src == '.' )
return src;
}
return end;
}
char* path_get_filename_base_end( char* path ){
return const_cast<char*>( path_get_filename_base_end( const_cast<const char*>( path ) ) );
}
/// \brief Returns a pointer to the first character of the file extension of \p path, or to terminating null character if not found.
const char* path_get_extension( const char* path ){
const char *end = path + strlen( path );
const char *src = end;
while ( src != path && !path_separator( *--src ) ){
if( *src == '.' )
return src + 1;
}
return end;
}
char* path_get_extension( char* path ){
return const_cast<char*>( path_get_extension( const_cast<const char*>( path ) ) );
}
/// \brief Appends trailing slash, unless \p path is empty or already has slash.
void path_add_slash( char *path ){
char* end = path + strlen( path );

View File

@@ -46,6 +46,8 @@
#include <time.h>
#include <stdarg.h>
#include "os/path.h"
#ifdef _MSC_VER
#pragma intrinsic( memset, memcpy )
@@ -185,18 +187,8 @@ void SaveFile( const char *filename, const void *buffer, int count );
bool FileExists( const char *filename );
static inline bool path_separator( const char c ){
return c == '/' || c == '\\';
}
bool path_is_absolute( const char* path );
const char* path_get_last_separator( const char* path );
char* path_get_last_separator( char* path );
const char* path_get_filename_start( const char* path );
char* path_get_filename_start( char* path );
const char* path_get_filename_base_end( const char* path );
char* path_get_filename_base_end( char* path );
const char* path_get_extension( const char* path );
char* path_get_extension( char* path );
void path_add_slash( char *path );
void path_set_extension( char *path, const char *extension );
void DefaultExtension( char *path, const char *extension );