Fix handling of paths with dots in dir names, fix #299, #301

idStr::StripFileExtension() (and SetFileExtension() which uses it) and
others didn't work correctly if there was a dot in a directory name,
because they just searched from last to first char for '.', so if the
current filename didn't have an extension to cut off, they'd just cut off
at any other '.' they found.
So D:\dev\doom3.data\base\maps\bla could turn into D:\dev\doom3
(or, for SetFileExtension(), D:\dev\doom3.map)

While at it, I made most of the idStr code that explicitly checked for '\\'
and '/' (and maybe ':' for AROS) use a little "bool isDirSeparator(int c)"
function so we don't have the #ifdefs for different platforms all over
the place.
This commit is contained in:
Daniel Gibson 2020-07-21 05:16:55 +02:00
parent b9e13d2f17
commit aa77e49de7

View file

@ -753,6 +753,26 @@ idStr &idStr::SetFileExtension( const char *extension ) {
return *this; return *this;
} }
// DG: helper-function that returns true if the character c is a directory separator
// on the current platform
static ID_INLINE bool isDirSeparator( int c )
{
if ( c == '/' ) {
return true;
}
#ifdef _WIN32
if ( c == '\\' ) {
return true;
}
#elif defined(__AROS__)
if ( c == ':' ) {
return true;
}
#endif
return false;
}
// DG end
/* /*
============ ============
idStr::StripFileExtension idStr::StripFileExtension
@ -762,6 +782,10 @@ idStr &idStr::StripFileExtension( void ) {
int i; int i;
for ( i = len-1; i >= 0; i-- ) { for ( i = len-1; i >= 0; i-- ) {
// DG: we're at a directory separator, nothing to strip at filename
if ( isDirSeparator( data[i] ) ) {
break;
} // DG end
if ( data[i] == '.' ) { if ( data[i] == '.' ) {
data[i] = '\0'; data[i] = '\0';
len = i; len = i;
@ -778,7 +802,9 @@ idStr::StripAbsoluteFileExtension
*/ */
idStr &idStr::StripAbsoluteFileExtension( void ) { idStr &idStr::StripAbsoluteFileExtension( void ) {
int i; int i;
// FIXME DG: seems like this is unused, but it probably doesn't do what's expected
// (if you wanna strip .tar.gz this will fail with dots in path,
// if you indeed wanna strip the first dot in *path* (even in some directory) this is right)
for ( i = 0; i < len; i++ ) { for ( i = 0; i < len; i++ ) {
if ( data[i] == '.' ) { if ( data[i] == '.' ) {
data[i] = '\0'; data[i] = '\0';
@ -800,6 +826,10 @@ idStr &idStr::DefaultFileExtension( const char *extension ) {
// do nothing if the string already has an extension // do nothing if the string already has an extension
for ( i = len-1; i >= 0; i-- ) { for ( i = len-1; i >= 0; i-- ) {
// DG: we're at a directory separator, there was no file extension
if ( isDirSeparator( data[i] ) ) {
break;
} // DG end
if ( data[i] == '.' ) { if ( data[i] == '.' ) {
return *this; return *this;
} }
@ -817,11 +847,7 @@ idStr::DefaultPath
================== ==================
*/ */
idStr &idStr::DefaultPath( const char *basepath ) { idStr &idStr::DefaultPath( const char *basepath ) {
#if defined(__AROS__) if ( isDirSeparator( ( *this )[ 0 ] ) ) {
if ( ( ( *this )[ 0 ] == '/' ) || ( ( *this )[ 0 ] == '\\' ) || ( ( *this )[ 0 ] == ':' ) ) {
#else
if ( ( ( *this )[ 0 ] == '/' ) || ( ( *this )[ 0 ] == '\\' ) ) {
#endif
// absolute path location // absolute path location
return *this; return *this;
} }
@ -844,19 +870,12 @@ void idStr::AppendPath( const char *text ) {
EnsureAlloced( len + strlen( text ) + 2 ); EnsureAlloced( len + strlen( text ) + 2 );
if ( pos ) { if ( pos ) {
#if defined(__AROS__) if ( !isDirSeparator( data[ pos-1 ] ) ) {
if (( data[ pos-1 ] != '/' ) || ( data[ pos-1 ] != ':' )) {
#else
if ( data[ pos-1 ] != '/' ) {
#endif
data[ pos++ ] = '/'; data[ pos++ ] = '/';
} }
} }
#if defined(__AROS__)
if (( text[i] == '/' ) || ( text[i] == ':' )) { if ( isDirSeparator( text[ i ] ) ) {
#else
if ( text[i] == '/' ) {
#endif
i++; i++;
} }
@ -881,11 +900,7 @@ idStr &idStr::StripFilename( void ) {
int pos; int pos;
pos = Length() - 1; pos = Length() - 1;
#if defined(__AROS__) while( ( pos > 0 ) && !isDirSeparator( ( *this )[ pos ] ) ) {
while( ( pos > 0 ) && ( ( *this )[ pos ] != '/' ) && ( ( *this )[ pos ] != '\\' ) && ( ( *this )[ pos ] != ':' ) ) {
#else
while( ( pos > 0 ) && ( ( *this )[ pos ] != '/' ) && ( ( *this )[ pos ] != '\\' ) ) {
#endif
pos--; pos--;
} }
@ -906,11 +921,7 @@ idStr &idStr::StripPath( void ) {
int pos; int pos;
pos = Length(); pos = Length();
#if defined(__AROS__) while( ( pos > 0 ) && !isDirSeparator( ( *this )[ pos - 1 ] ) ) {
while( ( pos > 0 ) && ( ( *this )[ pos - 1 ] != '/' ) && ( ( *this )[ pos - 1 ] != '\\' ) && ( ( *this )[ pos - 1 ] != ':' ) ) {
#else
while( ( pos > 0 ) && ( ( *this )[ pos - 1 ] != '/' ) && ( ( *this )[ pos - 1 ] != '\\' ) ) {
#endif
pos--; pos--;
} }
@ -930,11 +941,7 @@ void idStr::ExtractFilePath( idStr &dest ) const {
// back up until a \ or the start // back up until a \ or the start
// //
pos = Length(); pos = Length();
#if defined(__AROS__) while( ( pos > 0 ) && !isDirSeparator( ( *this )[ pos - 1 ] ) ) {
while( ( pos > 0 ) && ( ( *this )[ pos - 1 ] != '/' ) && ( ( *this )[ pos - 1 ] != '\\' ) && ( ( *this )[ pos - 1 ] != ':' ) ) {
#else
while( ( pos > 0 ) && ( ( *this )[ pos - 1 ] != '/' ) && ( ( *this )[ pos - 1 ] != '\\' ) ) {
#endif
pos--; pos--;
} }
@ -953,11 +960,7 @@ void idStr::ExtractFileName( idStr &dest ) const {
// back up until a \ or the start // back up until a \ or the start
// //
pos = Length() - 1; pos = Length() - 1;
#if defined(__AROS__) while( ( pos > 0 ) && !isDirSeparator( ( *this )[ pos - 1 ] ) ) {
while( ( pos > 0 ) && ( ( *this )[ pos - 1 ] != '/' ) && ( ( *this )[ pos - 1 ] != '\\' ) && ( ( *this )[ pos - 1 ] != ':' ) ) {
#else
while( ( pos > 0 ) && ( ( *this )[ pos - 1 ] != '/' ) && ( ( *this )[ pos - 1 ] != '\\' ) ) {
#endif
pos--; pos--;
} }
@ -977,11 +980,7 @@ void idStr::ExtractFileBase( idStr &dest ) const {
// back up until a \ or the start // back up until a \ or the start
// //
pos = Length() - 1; pos = Length() - 1;
#if defined(__AROS__) while( ( pos > 0 ) && !isDirSeparator( ( *this )[ pos - 1 ] ) ) {
while( ( pos > 0 ) && ( ( *this )[ pos - 1 ] != '/' ) && ( ( *this )[ pos - 1 ] != '\\' ) && ( ( *this )[ pos - 1 ] != ':' ) ) {
#else
while( ( pos > 0 ) && ( ( *this )[ pos - 1 ] != '/' ) && ( ( *this )[ pos - 1 ] != '\\' ) ) {
#endif
pos--; pos--;
} }
@ -1007,6 +1006,10 @@ void idStr::ExtractFileExtension( idStr &dest ) const {
pos = Length() - 1; pos = Length() - 1;
while( ( pos > 0 ) && ( ( *this )[ pos - 1 ] != '.' ) ) { while( ( pos > 0 ) && ( ( *this )[ pos - 1 ] != '.' ) ) {
pos--; pos--;
if( isDirSeparator( ( *this )[ pos ] ) ) { // DG: check for directory separator
// no extension in the whole filename
dest.Empty();
} // DG end
} }
if ( !pos ) { if ( !pos ) {