Added utility function to strip out a base file name from a full path.

This commit is contained in:
Matthew Budd 2020-08-24 19:52:28 -04:00
parent e8f1ffdf8d
commit 08feb4710b
3 changed files with 26 additions and 23 deletions

View File

@ -743,25 +743,10 @@ void GuiCheatsDialog_t::saveCheatFile(void)
if ( GameInfo ) if ( GameInfo )
{ {
char *_filename; getFileBaseName( GameInfo->filename, dir );
if ((_filename = strrchr(GameInfo->filename, '\\')) || (_filename = strrchr(GameInfo->filename, '/')))
{ strcat( dir, ".cht");
strcpy( dir, _filename + 1);
}
else
{
strcpy( dir, GameInfo->filename);
}
_filename = strrchr( dir, '.');
if (_filename)
{
strcpy(_filename, ".cht");
}
else
{
strcat( dir, ".cht");
}
dialog.selectFile( dir ); dialog.selectFile( dir );
} }

View File

@ -45,25 +45,41 @@ const char *getRomFile( void )
return NULL; return NULL;
} }
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
// TODO Finish writing this func // Return file base name stripping out preceding path and trailing suffix.
int getFileBaseName( const char *filepath, char *base ) int getFileBaseName( const char *filepath, char *base )
{ {
int i,j=0, start=0; int i=0,j=0,end=0;
if ( filepath == NULL ) if ( filepath == NULL )
{ {
base[0] = 0;
return 0; return 0;
} }
i=0; j=0; i=0; j=0;
while ( filepath[i] != 0 ) while ( filepath[i] != 0 )
{ {
if ( filepath[i] == '/' ) if ( (filepath[i] == '/') || (filepath[i] == '\\') )
{ {
j = i; j = i;
} }
i++; i++;
} }
start = j; i = j;
return 0; j=0;
while ( filepath[i] != 0 )
{
base[j] = filepath[i]; i++; j++;
}
base[j] = 0; end=j;
while ( j > 1 )
{
j--;
if ( base[j] == '.' )
{
end=j; base[j] = 0; break;
}
}
return end;
} }
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------

View File

@ -3,3 +3,5 @@
int getDirFromFile( const char *path, char *dir ); int getDirFromFile( const char *path, char *dir );
const char *getRomFile( void ); const char *getRomFile( void );
int getFileBaseName( const char *filepath, char *base );