Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

156 lines
2.8 KiB

  1. /*++
  2. Copyright (c) 1997 Microsoft Corporation
  3. Module Name:
  4. utils.c
  5. Abstract:
  6. Implements various utilities.
  7. Author:
  8. Calin Negreanu (calinn) 20-Jan-1999
  9. Revision History:
  10. <alias> <date> <comments>
  11. --*/
  12. #include "utils.h"
  13. #define CHARTYPE WORD
  14. #define PCHARTYPE PWORD
  15. CHARTYPE
  16. pGetNextChar (
  17. PCTSTR Source
  18. )
  19. {
  20. CHARTYPE result;
  21. #ifdef UNICODE
  22. result = Source [0];
  23. #else
  24. if (IsDBCSLeadByte (Source [0])) {
  25. result = *((PCHARTYPE)Source);
  26. } else {
  27. result = Source [0];
  28. }
  29. #endif
  30. return result;
  31. }
  32. CHARTYPE
  33. pCharLower (
  34. CHARTYPE ch
  35. )
  36. {
  37. return ((CHARTYPE) (CharLower ((PTSTR) ch)));
  38. }
  39. BOOL
  40. ShIsPatternMatch (
  41. IN PCTSTR strPat,
  42. IN PCTSTR strStr
  43. )
  44. {
  45. CHARTYPE chSrc, chPat;
  46. while (*strStr) {
  47. chSrc = pCharLower (pGetNextChar (strStr));
  48. chPat = pCharLower (pGetNextChar (strPat));
  49. if (chPat == TEXT('*')) {
  50. // Check if asterisk is at the end. If so, we have a match already.
  51. chPat = pCharLower (pGetNextChar (CharNext (strPat)));
  52. if (!chPat)
  53. return TRUE;
  54. // Otherwise check if next pattern char matches current char
  55. if (chPat == chSrc || chPat == TEXT('?')) {
  56. // do recursive check for rest of pattern
  57. if (ShIsPatternMatch (CharNext (strPat), strStr))
  58. return TRUE;
  59. }
  60. //
  61. // Allow any character and continue
  62. //
  63. strStr = CharNext (strStr);
  64. continue;
  65. }
  66. if (chPat != TEXT('?')) {
  67. //
  68. // if next pattern character is not a question mark, src and pat
  69. // must be identical.
  70. //
  71. if (chSrc != chPat)
  72. return FALSE;
  73. }
  74. //
  75. // Advance when pattern character matches string character
  76. //
  77. strPat = CharNext (strPat);
  78. strStr = CharNext (strStr);
  79. }
  80. //
  81. // Fail when there is more pattern and pattern does not end in an asterisk
  82. //
  83. chPat = pCharLower (pGetNextChar (strPat));
  84. while (chPat == TEXT('*')) {
  85. strPat = CharNext (strPat);
  86. chPat = pCharLower (pGetNextChar (strPat));
  87. }
  88. if (chPat) {
  89. return FALSE;
  90. }
  91. return TRUE;
  92. }
  93. PCTSTR
  94. ShGetLastChar (
  95. IN PCTSTR String,
  96. IN CHARTYPE Char
  97. )
  98. {
  99. PCTSTR lastCharPtr = NULL;
  100. while (*String) {
  101. if (pGetNextChar (String) == Char) {
  102. lastCharPtr = String;
  103. }
  104. String = CharNext (String);
  105. }
  106. return lastCharPtr;
  107. }
  108. PCTSTR
  109. ShGetFileNameFromPath (
  110. IN PCTSTR PathSpec
  111. )
  112. {
  113. PCTSTR p;
  114. p = ShGetLastChar (PathSpec, TEXT('\\'));
  115. if (p) {
  116. p = CharNext (p);
  117. } else {
  118. p = PathSpec;
  119. }
  120. return p;
  121. }