From e700a7498ed1b61dff19a558ad53ba4f2439c175 Mon Sep 17 00:00:00 2001 From: Alibek Omarov Date: Tue, 9 Jul 2019 16:36:15 +0300 Subject: [PATCH] engine: move matchpattern from old engine --- engine/common/filesystem.c | 49 ---------------------------- public/crtlib.c | 66 ++++++++++++++++++++++++++++++++++++++ public/crtlib.h | 2 ++ 3 files changed, 68 insertions(+), 49 deletions(-) diff --git a/engine/common/filesystem.c b/engine/common/filesystem.c index 1a19d3e5..a5d1dbd2 100644 --- a/engine/common/filesystem.c +++ b/engine/common/filesystem.c @@ -168,55 +168,6 @@ FILEMATCH COMMON SYSTEM ============================================================================= */ -static int matchpattern( const char *str, const char *cmp, qboolean caseinsensitive ) -{ - int c1, c2; - - while( *cmp ) - { - switch( *cmp ) - { - case 0: return 1; // end of pattern - case '?': // match any single character - if( *str == 0 || *str == '/' || *str == '\\' || *str == ':' ) - return 0; // no match - str++; - cmp++; - break; - case '*': // match anything until following string - if( !*str ) return 1; // match - cmp++; - while( *str ) - { - if( *str == '/' || *str == '\\' || *str == ':' ) - break; - // see if pattern matches at this offset - if( matchpattern( str, cmp, caseinsensitive )) - return 1; - // nope, advance to next offset - str++; - } - break; - default: - if( *str != *cmp ) - { - if( !caseinsensitive ) - return 0; // no match - c1 = Q_tolower( *str ); - c2 = Q_tolower( *cmp ); - if( c1 != c2 ) return 0; // no match - } - - str++; - cmp++; - break; - } - } - - // reached end of pattern but not end of input? - return (*str) ? 0 : 1; -} - static void stringlistinit( stringlist_t *list ) { memset( list, 0, sizeof( *list )); diff --git a/public/crtlib.c b/public/crtlib.c index 2895ae20..bda7e504 100644 --- a/public/crtlib.c +++ b/public/crtlib.c @@ -885,3 +885,69 @@ void COM_ReplaceExtension( char *path, const char *extension ) COM_StripExtension( path ); COM_DefaultExtension( path, extension ); } + +int matchpattern( const char *in, const char *pattern, qboolean caseinsensitive ) +{ + return matchpattern_with_separator( in, pattern, caseinsensitive, "/\\:", false ); +} + +// wildcard_least_one: if true * matches 1 or more characters +// if false * matches 0 or more characters +int matchpattern_with_separator( const char *in, const char *pattern, qboolean caseinsensitive, const char *separators, qboolean wildcard_least_one ) +{ + int c1, c2; + + while( *pattern ) + { + switch( *pattern ) + { + case 0: + return 1; // end of pattern + case '?': // match any single character + if( *in == 0 || Q_strchr( separators, *in )) + return 0; // no match + in++; + pattern++; + break; + case '*': // match anything until following string + if( wildcard_least_one ) + { + if( *in == 0 || Q_strchr( separators, *in )) + return 0; // no match + in++; + } + pattern++; + while( *in ) + { + if( Q_strchr(separators, *in )) + break; + // see if pattern matches at this offset + if( matchpattern_with_separator(in, pattern, caseinsensitive, separators, wildcard_least_one )) + return 1; + // nope, advance to next offset + in++; + } + break; + default: + if( *in != *pattern ) + { + if( !caseinsensitive ) + return 0; // no match + c1 = *in; + if( c1 >= 'A' && c1 <= 'Z' ) + c1 += 'a' - 'A'; + c2 = *pattern; + if( c2 >= 'A' && c2 <= 'Z' ) + c2 += 'a' - 'A'; + if( c1 != c2 ) + return 0; // no match + } + in++; + pattern++; + break; + } + } + if( *in ) + return 0; // reached end of pattern but not end of input + return 1; // success +} diff --git a/public/crtlib.h b/public/crtlib.h index 468d436a..5fd83504 100644 --- a/public/crtlib.h +++ b/public/crtlib.h @@ -81,5 +81,7 @@ void COM_ExtractFilePath( const char *path, char *dest ); const char *COM_FileWithoutPath( const char *in ); void COM_StripExtension( char *path ); #define COM_CheckString( string ) ( ( !string || !*string ) ? 0 : 1 ) +int matchpattern( const char *in, const char *pattern, qboolean caseinsensitive ); +int matchpattern_with_separator( const char *in, const char *pattern, qboolean caseinsensitive, const char *separators, qboolean wildcard_least_one ); #endif//STDLIB_H