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

@ -113,32 +113,38 @@ inline const char* path_remove_directory( const char* path ){
return "";
}
inline bool path_separator( const char c ){
return c == '/' || c == '\\';
}
/// \brief Returns a pointer to the first character of the filename component of \p path.
/// O(n)
inline const char* path_get_filename_start( const char* path ){
{
const char* last_forward_slash = strrchr( path, '/' );
if ( last_forward_slash != 0 ) {
return last_forward_slash + 1;
}
}
const char *src = path + string_length( path );
// not strictly necessary,since paths should not contain '\'
{
const char* last_backward_slash = strrchr( path, '\\' );
if ( last_backward_slash != 0 ) {
return last_backward_slash + 1;
}
while ( src != path && !path_separator( src[-1] ) ){
--src;
}
return src;
}
return path;
inline 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.
/// O(n)
inline const char* path_get_filename_base_end( const char* path ){
const char* last_period = strrchr( path_get_filename_start( path ), '.' );
return ( last_period != 0 ) ? last_period : path + string_length( path );
const char *end = path + string_length( path );
const char *src = end;
while ( src != path && !path_separator( *--src ) ){
if( *src == '.' )
return src;
}
return end;
}
inline char* path_get_filename_base_end( char* path ){
return const_cast<char*>( path_get_filename_base_end( const_cast<const char*>( path ) ) );
}
/// \brief Returns the length of the filename component (not including extension) of \p path.
@ -157,14 +163,20 @@ inline const char* path_make_relative( const char* path, const char* base ){
return path;
}
/// \brief Returns a pointer to the first character of the file extension of \p path, or "" if not found.
/// O(n)
/// \brief Returns a pointer to the first character of the file extension of \p path, or to terminating null character if not found.
inline const char* path_get_extension( const char* path ){
const char* last_period = strrchr( path_get_filename_start( path ), '.' );
if ( last_period != 0 ) {
return ++last_period;
const char *end = path + string_length( path );
const char *src = end;
while ( src != path && !path_separator( *--src ) ){
if( *src == '.' )
return src + 1;
}
return "";
return end;
}
inline char* path_get_extension( char* path ){
return const_cast<char*>( path_get_extension( const_cast<const char*>( path ) ) );
}
/// \brief Returns true if \p extension is of the same type as \p other.

View File

@ -1384,13 +1384,7 @@ void BackgroundImage::set( const VIEWTYPE viewtype ){
free_tex();
const char *filename = background_image_dialog();
if( filename ){
StringOutputStream filename_noext( 1024 );
const char* ext = path_get_extension( filename );
if( string_empty( ext ) )
filename_noext << filename;
else
filename_noext << StringRange( filename, ext - 1 );
const auto filename_noext = StringOutputStream( 256 )( StringRange( filename, path_get_filename_base_end( filename ) ) );
Image *image = QERApp_LoadImage( 0, filename_noext.c_str() );
if ( !image ) {
globalErrorStream() << "Could not load texture " << filename_noext.c_str() << "\n";

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 );