Skip to content

Commit

Permalink
public: better fix for ExtractFilePath
Browse files Browse the repository at this point in the history
  • Loading branch information
a1batross committed Oct 28, 2023
1 parent 4d7d592 commit 6c40104
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 10 deletions.
13 changes: 3 additions & 10 deletions public/crtlib.c
Original file line number Diff line number Diff line change
Expand Up @@ -642,20 +642,13 @@ COM_ExtractFilePath
*/
void COM_ExtractFilePath( const char *path, char *dest )
{
size_t len = Q_strlen( path );
const char *src = path + len - 1;

if( len == 0 )
{
dest[0] = 0;
return;
}
const char *src = path + Q_strlen( path ) - 1;

// back up until a \ or the start
while( src != path && !(*(src - 1) == '\\' || *(src - 1) == '/' ))
while( src > path && !(*(src - 1) == '\\' || *(src - 1) == '/' ))
src--;

if( src != path )
if( src > path )
{
memcpy( dest, path, src - path );
dest[src - path - 1] = 0; // cutoff backslash
Expand Down
40 changes: 40 additions & 0 deletions public/tests/test_efp.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#include <stdlib.h>
#include "crtlib.h"
#include <stdio.h>

int Test_ExtractFilePath( void )
{
char dst[64];
const char *strings[] =
{
"dir/file", "dir",
"bark\\meow", "bark",
"nopath", "",
"knee/deep/in/paths", "knee/deep/in",
// yes, it removes the behavior/ even if it might be technically a directory
"keep/the/original/func/behavior/", "keep/the/original/func",
"backslashes\\are\\annoying\\af", "backslashes\\are\\annoying",
"", ""
};
size_t i;

for( i = 0; i < sizeof( strings ) / sizeof( strings[0] ); i += 2 )
{
COM_ExtractFilePath( strings[i], dst );
if( Q_strcmp( dst, strings[i+1] ))
{
printf( "%s %s %s\n", strings[i], strings[i+1], dst );
return (i >> 1) + 1;
}
}

return 0;
}

int main( void )
{
if( Test_ExtractFilePath( ))
return EXIT_FAILURE;

return EXIT_SUCCESS;
}
1 change: 1 addition & 0 deletions public/wscript
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ def build(bld):
'strings': 'tests/test_strings.c',
'build': 'tests/test_build.c',
'filebase': 'tests/test_filebase.c',
'efp': 'tests/test_efp.c',
}

for i in tests:
Expand Down

0 comments on commit 6c40104

Please sign in to comment.