findMatch.cpp
Engine/source/core/strings/findMatch.cpp
Public Functions
bool
IsCharMatch(char e, char s, bool caseSensitive)
Detailed Description
Public Functions
IsCharMatch(char e, char s, bool caseSensitive)
1 2//----------------------------------------------------------------------------- 3// Copyright (c) 2012 GarageGames, LLC 4// 5// Permission is hereby granted, free of charge, to any person obtaining a copy 6// of this software and associated documentation files (the "Software"), to 7// deal in the Software without restriction, including without limitation the 8// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 9// sell copies of the Software, and to permit persons to whom the Software is 10// furnished to do so, subject to the following conditions: 11// 12// The above copyright notice and this permission notice shall be included in 13// all copies or substantial portions of the Software. 14// 15// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 20// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 21// IN THE SOFTWARE. 22//----------------------------------------------------------------------------- 23 24#include "core/strings/findMatch.h" 25#include "core/strings/stringFunctions.h" 26 27 28//-------------------------------------------------------------------------------- 29// NAME 30// FindMatch::FindMatch( const char *_expression, S32 maxNumMatches ) 31// 32// DESCRIPTION 33// Class to match regular expressions (file names) 34// only works with '*','?', and 'chars' 35// 36// ARGUMENTS 37// _expression - The regular expression you intend to match (*.??abc.bmp) 38// _maxMatches - The maximum number of strings you wish to match. 39// 40// RETURNS 41// 42// NOTES 43// 44//-------------------------------------------------------------------------------- 45 46FindMatch::FindMatch( U32 _maxMatches ) 47{ 48 VECTOR_SET_ASSOCIATION(matchList); 49 50 expression = NULL; 51 maxMatches = _maxMatches; 52 matchList.reserve( maxMatches ); 53} 54 55FindMatch::FindMatch( char *_expression, U32 _maxMatches ) 56{ 57 VECTOR_SET_ASSOCIATION(matchList); 58 59 expression = NULL; 60 setExpression( _expression ); 61 maxMatches = _maxMatches; 62 matchList.reserve( maxMatches ); 63} 64 65FindMatch::~FindMatch() 66{ 67 delete [] expression; 68 matchList.clear(); 69} 70 71void FindMatch::setExpression( const char *_expression ) 72{ 73 delete [] expression; 74 75 dsize_t expressionLen = dStrlen(_expression) + 1; 76 expression = new char[expressionLen]; 77 dStrcpy(expression, _expression, expressionLen); 78 dStrupr(expression); 79} 80 81bool FindMatch::findMatch( const char *str, bool caseSensitive ) 82{ 83 if ( isFull() ) 84 return false; 85 86 char nstr[512]; 87 dStrcpy( nstr,str,512 ); 88 dStrupr(nstr); 89 if ( isMatch( expression, nstr, caseSensitive ) ) 90 { 91 matchList.push_back( (char*)str ); 92 return true; 93 } 94 return false; 95} 96 97inline bool IsCharMatch( char e, char s, bool caseSensitive ) 98{ 99 return ( ( e == '?' ) || ( caseSensitive && e == s ) || ( dToupper(e) == dToupper(s) ) ); 100} 101 102bool FindMatch::isMatch( const char *exp, const char *str, bool caseSensitive ) 103{ 104 if (str == NULL || exp == NULL) 105 return false; 106 107 while ( *str && ( *exp != '*' ) ) 108 { 109 if ( !IsCharMatch( *exp++, *str++, caseSensitive ) ) 110 return false; 111 } 112 113 const char* cp = NULL; 114 const char* mp = NULL; 115 116 while ( *str ) 117 { 118 if ( *exp == '*' ) 119 { 120 if ( !*++exp ) 121 return true; 122 123 mp = exp; 124 cp = str+1; 125 } 126 else if ( IsCharMatch( *exp, *str, caseSensitive ) ) 127 { 128 exp++; 129 str++; 130 } 131 else 132 { 133 exp = mp; 134 str = cp++; 135 } 136 } 137 138 while ( *exp == '*' ) 139 exp++; 140 141 return !*exp; 142} 143 144 145bool FindMatch::isMatchMultipleExprs( const char *exps, const char *str, bool caseSensitive ) 146{ 147 char *tok = 0; 148 S32 len = dStrlen(exps); 149 150 char *e = new char[len+1]; 151 dStrcpy(e,exps,len+1); 152 153 // [tom, 12/18/2006] This no longer supports space separated expressions as 154 // they don't work when the paths have spaces in. 155 156 // search for each expression. return true soon as we see one. 157 for( tok = dStrtok(e,"\t"); tok != NULL; tok = dStrtok(NULL,"\t")) 158 { 159 if( isMatch( tok, str, caseSensitive) ) 160 { 161 delete []e; 162 return true; 163 } 164 } 165 166 delete []e; 167 return false; 168} 169