Rewrite COM_FileExtention()

COM_FileExtension() was parsing strings from beginning to end, bailing
out as soon as '.' was found and treating everything thereafter as the
file extension. That behavior caused problem with relatives pathes like
models/monsters/tank/../ctank/skin.pcx. The new implementation uses
strrchr() to determine the last '.'.

This fixes issue #48. The bug was introduced in e07294b which replaced
hand rolled code with COM_FileExtention().
This commit is contained in:
Yamagi Burmeister 2014-07-24 20:58:08 +02:00
parent 87ba687f14
commit 5a7950683d
3 changed files with 7 additions and 21 deletions

View file

@ -202,7 +202,7 @@ void RotatePointAroundVector(vec3_t dst,
char *COM_SkipPath(char *pathname); char *COM_SkipPath(char *pathname);
void COM_StripExtension(char *in, char *out); void COM_StripExtension(char *in, char *out);
char *COM_FileExtension(char *in); const char *COM_FileExtension(const char *in);
void COM_FileBase(char *in, char *out); void COM_FileBase(char *in, char *out);
void COM_FilePath(const char *in, char *out); void COM_FilePath(const char *in, char *out);
void COM_DefaultExtension(char *path, const char *extension); void COM_DefaultExtension(char *path, const char *extension);

View file

@ -668,31 +668,17 @@ COM_StripExtension(char *in, char *out)
*out = 0; *out = 0;
} }
char * const char *
COM_FileExtension(char *in) COM_FileExtension(const char *in)
{ {
static char exten[8]; const char *ext = strrchr(in, '.');
int i;
while (*in && *in != '.') if (!ext || ext == in)
{
in++;
}
if (!*in)
{ {
return ""; return "";
} }
in++; return ext + 1;
for (i = 0; i < 7 && *in; i++, in++)
{
exten[i] = *in;
}
exten[i] = 0;
return exten;
} }
void void

View file

@ -73,7 +73,7 @@ extern short BigShort ( short l ) ;
extern void COM_DefaultExtension ( char * path , const char * extension ) ; extern void COM_DefaultExtension ( char * path , const char * extension ) ;
extern void COM_FilePath ( const char * in , char * out ) ; extern void COM_FilePath ( const char * in , char * out ) ;
extern void COM_FileBase ( char * in , char * out ) ; extern void COM_FileBase ( char * in , char * out ) ;
extern char * COM_FileExtension ( char * in ) ; extern const char * COM_FileExtension ( const char * in ) ;
extern void COM_StripExtension ( char * in , char * out ) ; extern void COM_StripExtension ( char * in , char * out ) ;
extern char * COM_SkipPath ( char * pathname ) ; extern char * COM_SkipPath ( char * pathname ) ;
extern int Q_log2 ( int val ) ; extern int Q_log2 ( int val ) ;