Browse Source

public: better fix for ExtractFilePath

pull/2/head
Alibek Omarov 1 year ago
parent
commit
6c40104c66
  1. 13
      public/crtlib.c
  2. 40
      public/tests/test_efp.c
  3. 1
      public/wscript

13
public/crtlib.c

@ -642,20 +642,13 @@ COM_ExtractFilePath
*/ */
void COM_ExtractFilePath( const char *path, char *dest ) void COM_ExtractFilePath( const char *path, char *dest )
{ {
size_t len = Q_strlen( path ); const char *src = path + Q_strlen( path ) - 1;
const char *src = path + len - 1;
if( len == 0 )
{
dest[0] = 0;
return;
}
// back up until a \ or the start // back up until a \ or the start
while( src != path && !(*(src - 1) == '\\' || *(src - 1) == '/' )) while( src > path && !(*(src - 1) == '\\' || *(src - 1) == '/' ))
src--; src--;
if( src != path ) if( src > path )
{ {
memcpy( dest, path, src - path ); memcpy( dest, path, src - path );
dest[src - path - 1] = 0; // cutoff backslash dest[src - path - 1] = 0; // cutoff backslash

40
public/tests/test_efp.c

@ -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
public/wscript

@ -28,6 +28,7 @@ def build(bld):
'strings': 'tests/test_strings.c', 'strings': 'tests/test_strings.c',
'build': 'tests/test_build.c', 'build': 'tests/test_build.c',
'filebase': 'tests/test_filebase.c', 'filebase': 'tests/test_filebase.c',
'efp': 'tests/test_efp.c',
} }
for i in tests: for i in tests:

Loading…
Cancel
Save