WildCardMatch.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. #include "stdafx.h"
  2. #include "WildCardMatch.h"
  3. CWildCardMatch::CWildCardMatch(void)
  4. {
  5. }
  6. CWildCardMatch::~CWildCardMatch(void)
  7. {
  8. }
  9. BOOL CWildCardMatch::WildMatch(CString sWild, CString sString, CString sLimitChar)
  10. {
  11. BOOL bAny = FALSE;
  12. BOOL bNextIsOptional = FALSE;
  13. BOOL bAutorizedChar = TRUE;
  14. int i=0;
  15. int j=0;
  16. // Check all the string char by char
  17. while (i < sString.GetLength())
  18. {
  19. // Check index for array overflow
  20. if (j < sWild.GetLength())
  21. {
  22. // Manage '*' in the wildcard
  23. if (sWild[j]=='*')
  24. {
  25. // Go to next character in the wildcard
  26. j++;
  27. // Enf of the string and wildcard end
  28. // with *, only test string validity
  29. if (j >= sWild.GetLength())
  30. {
  31. // Check end of the string
  32. while (!sLimitChar.IsEmpty() && i < sString.GetLength())
  33. {
  34. // If this char is not ok, return false
  35. if (sLimitChar.Find(sString[i]) < 0)
  36. return FALSE;
  37. i++;
  38. }
  39. return TRUE;
  40. }
  41. bAny = TRUE;
  42. bNextIsOptional = FALSE;
  43. }
  44. else
  45. {
  46. // Optional char in the wildcard
  47. if (sWild[j] == '^')
  48. {
  49. // Go to next char in the wildcard and indicate
  50. // that the next is optional
  51. j++;
  52. bNextIsOptional = TRUE;
  53. }
  54. else
  55. {
  56. bAutorizedChar = ((sLimitChar.IsEmpty()) || (sLimitChar.Find(sString[i])>=0));
  57. // IF :
  58. // Current char match the wildcard
  59. // '?' is used and current char is in authorized char list
  60. // Char is optional and it's not in the string
  61. // and it's necessary to test if '*' make any
  62. // char browsing
  63. if (sWild[j] == sString[i] ||
  64. (sWild[j] == '?' && bAutorizedChar) ||
  65. (bNextIsOptional && !(bAny && bAutorizedChar)))
  66. {
  67. // If current char match wildcard,
  68. // we stop for any char browsing
  69. if (sWild[j] == sString[i])
  70. bAny = FALSE;
  71. // If it's not an optional char who is not present,
  72. // go to next
  73. if (sWild[j] == sString[i] || sWild[j] == '?')
  74. i++;
  75. j++;
  76. bNextIsOptional = FALSE;
  77. }
  78. else
  79. {
  80. // If we are in any char browsing ('*')
  81. // and current char is authorized
  82. if (bAny && bAutorizedChar)
  83. {
  84. // Go to next
  85. i++;
  86. }
  87. else
  88. {
  89. return FALSE;
  90. }
  91. }
  92. }
  93. }
  94. }
  95. else
  96. {
  97. // End of the wildcard but not the
  98. // end of the string =>
  99. // not matching
  100. return FALSE;
  101. }
  102. }
  103. if (j < sWild.GetLength() && sWild[j] == '^')
  104. {
  105. bNextIsOptional = TRUE;
  106. j++;
  107. }
  108. // If the string is shorter than wildcard
  109. // we test end of the
  110. // wildcard to check matching
  111. while ((j < sWild.GetLength() && sWild[j] == '*') || bNextIsOptional)
  112. {
  113. j++;
  114. bNextIsOptional = FALSE;
  115. if (j < sWild.GetLength() && sWild[j] == '^')
  116. {
  117. bNextIsOptional = TRUE;
  118. j++;
  119. }
  120. }
  121. return j >= sWild.GetLength();
  122. }