findMatch.cpp 4.3 KB

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