findMatch.cc 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2013 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 "platform/platform.h"
  23. #include "string/findMatch.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( const 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. expression = new char[dStrlen(_expression) + 1];
  65. dStrcpy(expression, _expression);
  66. dStrupr(expression);
  67. }
  68. bool FindMatch::findMatch( const char *str, bool caseSensitive )
  69. {
  70. if ( isFull() )
  71. return false;
  72. char nstr[512];
  73. dStrcpy( nstr,str );
  74. dStrupr(nstr);
  75. if ( isMatch( expression, nstr, caseSensitive ) )
  76. {
  77. matchList.push_back( (char*)str );
  78. return true;
  79. }
  80. return false;
  81. }
  82. bool FindMatch::isMatch( const char *exp, const char *str, bool caseSensitive )
  83. {
  84. const char *e=exp;
  85. const char *s=str;
  86. bool match=true;
  87. while ( match && *e && *s )
  88. {
  89. switch( *e )
  90. {
  91. case '*':
  92. e++;
  93. match = false;
  94. while( ((s=dStrchr(s,*e)) !=NULL) && !match )
  95. {
  96. match = isMatch( e, s, caseSensitive );
  97. s++;
  98. }
  99. return( match );
  100. case '?':
  101. e++;
  102. s++;
  103. break;
  104. default:
  105. if (caseSensitive) match = ( *e++ == *s++ );
  106. else match = ( dToupper(*e++) == dToupper(*s++) );
  107. break;
  108. }
  109. }
  110. if (*e != *s) // both exp and str should be at '\0' if match was successfull
  111. match = false;
  112. return ( match );
  113. }
  114. bool FindMatch::isMatchMultipleExprs( const char *exps, const char *str, bool caseSensitive )
  115. {
  116. char *tok = 0;
  117. int len = dStrlen(exps);
  118. char *e = new char[len+1];
  119. dStrcpy(e,exps);
  120. // [tom, 12/18/2006] This no longer supports space seperated expressions as
  121. // they don't work when the paths have spaces in.
  122. // search for each expression. return true soon as we see one.
  123. for( tok = dStrtok(e,"\t"); tok != NULL; tok = dStrtok(NULL,"\t"))
  124. {
  125. if( isMatch( tok, str, caseSensitive) )
  126. {
  127. delete []e;
  128. return true;
  129. }
  130. }
  131. delete []e;
  132. return false;
  133. }