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:
parent
15e4b8e850
commit
7fc079c658
|
|
@ -113,32 +113,38 @@ inline const char* path_remove_directory( const char* path ){
|
||||||
return "";
|
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.
|
/// \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 ){
|
inline const char* path_get_filename_start( const char* path ){
|
||||||
{
|
const char *src = path + string_length( path );
|
||||||
const char* last_forward_slash = strrchr( path, '/' );
|
|
||||||
if ( last_forward_slash != 0 ) {
|
|
||||||
return last_forward_slash + 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// not strictly necessary,since paths should not contain '\'
|
while ( src != path && !path_separator( src[-1] ) ){
|
||||||
{
|
--src;
|
||||||
const char* last_backward_slash = strrchr( path, '\\' );
|
|
||||||
if ( last_backward_slash != 0 ) {
|
|
||||||
return last_backward_slash + 1;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
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.
|
/// \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 ){
|
inline const char* path_get_filename_base_end( const char* path ){
|
||||||
const char* last_period = strrchr( path_get_filename_start( path ), '.' );
|
const char *end = path + string_length( path );
|
||||||
return ( last_period != 0 ) ? last_period : 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.
|
/// \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;
|
return path;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// \brief Returns a pointer to the first character of the file extension of \p path, or "" if not found.
|
/// \brief Returns a pointer to the first character of the file extension of \p path, or to terminating null character if not found.
|
||||||
/// O(n)
|
|
||||||
inline const char* path_get_extension( const char* path ){
|
inline const char* path_get_extension( const char* path ){
|
||||||
const char* last_period = strrchr( path_get_filename_start( path ), '.' );
|
const char *end = path + string_length( path );
|
||||||
if ( last_period != 0 ) {
|
const char *src = end;
|
||||||
return ++last_period;
|
|
||||||
|
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.
|
/// \brief Returns true if \p extension is of the same type as \p other.
|
||||||
|
|
|
||||||
|
|
@ -1384,13 +1384,7 @@ void BackgroundImage::set( const VIEWTYPE viewtype ){
|
||||||
free_tex();
|
free_tex();
|
||||||
const char *filename = background_image_dialog();
|
const char *filename = background_image_dialog();
|
||||||
if( filename ){
|
if( filename ){
|
||||||
StringOutputStream filename_noext( 1024 );
|
const auto filename_noext = StringOutputStream( 256 )( StringRange( filename, path_get_filename_base_end( filename ) ) );
|
||||||
const char* ext = path_get_extension( filename );
|
|
||||||
if( string_empty( ext ) )
|
|
||||||
filename_noext << filename;
|
|
||||||
else
|
|
||||||
filename_noext << StringRange( filename, ext - 1 );
|
|
||||||
|
|
||||||
Image *image = QERApp_LoadImage( 0, filename_noext.c_str() );
|
Image *image = QERApp_LoadImage( 0, filename_noext.c_str() );
|
||||||
if ( !image ) {
|
if ( !image ) {
|
||||||
globalErrorStream() << "Could not load texture " << filename_noext.c_str() << "\n";
|
globalErrorStream() << "Could not load texture " << filename_noext.c_str() << "\n";
|
||||||
|
|
|
||||||
|
|
@ -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.
|
/// \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* path_get_last_separator( const char* path ){
|
||||||
const char *end = path + strlen( 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 ) ) );
|
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.
|
/// \brief Appends trailing slash, unless \p path is empty or already has slash.
|
||||||
void path_add_slash( char *path ){
|
void path_add_slash( char *path ){
|
||||||
char* end = path + strlen( path );
|
char* end = path + strlen( path );
|
||||||
|
|
|
||||||
|
|
@ -46,6 +46,8 @@
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
#include <stdarg.h>
|
#include <stdarg.h>
|
||||||
|
|
||||||
|
#include "os/path.h"
|
||||||
|
|
||||||
#ifdef _MSC_VER
|
#ifdef _MSC_VER
|
||||||
|
|
||||||
#pragma intrinsic( memset, memcpy )
|
#pragma intrinsic( memset, memcpy )
|
||||||
|
|
@ -185,18 +187,8 @@ void SaveFile( const char *filename, const void *buffer, int count );
|
||||||
bool FileExists( const char *filename );
|
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 );
|
const char* path_get_last_separator( const char* path );
|
||||||
char* path_get_last_separator( 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_add_slash( char *path );
|
||||||
void path_set_extension( char *path, const char *extension );
|
void path_set_extension( char *path, const char *extension );
|
||||||
void DefaultExtension( char *path, const char *extension );
|
void DefaultExtension( char *path, const char *extension );
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user