From 6c40104c667b6097dc6ae9189add51814e21ff82 Mon Sep 17 00:00:00 2001 From: Alibek Omarov Date: Sat, 28 Oct 2023 19:31:17 +0300 Subject: [PATCH] public: better fix for ExtractFilePath --- public/crtlib.c | 13 +++---------- public/tests/test_efp.c | 40 ++++++++++++++++++++++++++++++++++++++++ public/wscript | 1 + 3 files changed, 44 insertions(+), 10 deletions(-) create mode 100644 public/tests/test_efp.c diff --git a/public/crtlib.c b/public/crtlib.c index 6004b59b..bf6ab578 100644 --- a/public/crtlib.c +++ b/public/crtlib.c @@ -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 diff --git a/public/tests/test_efp.c b/public/tests/test_efp.c new file mode 100644 index 00000000..c7aafddf --- /dev/null +++ b/public/tests/test_efp.c @@ -0,0 +1,40 @@ +#include +#include "crtlib.h" +#include + +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; +} diff --git a/public/wscript b/public/wscript index c1002e78..400a19d0 100644 --- a/public/wscript +++ b/public/wscript @@ -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: