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.

129 lines
3.7KB

  1. //
  2. // "$Id: filename_match.cxx 7903 2010-11-28 21:06:39Z matt $"
  3. //
  4. // Pattern matching routines for the Fast Light Tool Kit (FLTK).
  5. //
  6. // Copyright 1998-2010 by Bill Spitzak and others.
  7. //
  8. // This library is free software; you can redistribute it and/or
  9. // modify it under the terms of the GNU Library General Public
  10. // License as published by the Free Software Foundation; either
  11. // version 2 of the License, or (at your option) any later version.
  12. //
  13. // This library is distributed in the hope that it will be useful,
  14. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. // Library General Public License for more details.
  17. //
  18. // You should have received a copy of the GNU Library General Public
  19. // License along with this library; if not, write to the Free Software
  20. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  21. // USA.
  22. //
  23. // Please report all bugs and problems on the following page:
  24. //
  25. // http://www.fltk.org/str.php
  26. //
  27. /* Adapted from Rich Salz. */
  28. #include <FL/filename.H>
  29. #include <ctype.h>
  30. /**
  31. Checks if a string \p s matches a pattern \p p.
  32. The following syntax is used for the pattern:
  33. - * matches any sequence of 0 or more characters.
  34. - ? matches any single character.
  35. - [set] matches any character in the set. Set can contain any single characters, or a-z to represent a range.
  36. To match ] or - they must be the first characters. To match ^ or ! they must not be the first characters.
  37. - [^set] or [!set] matches any character not in the set.
  38. - {X|Y|Z} or {X,Y,Z} matches any one of the subexpressions literally.
  39. - \\x quotes the character x so it has no special meaning.
  40. - x all other characters must be matched exactly.
  41. \b Include:
  42. \code
  43. #include <FL/filename.H>
  44. \endcode
  45. \param[in] s the string to check for a match
  46. \param[in] p the string pattern
  47. \return non zero if the string matches the pattern
  48. */
  49. int fl_filename_match(const char *s, const char *p) {
  50. int matched;
  51. for (;;) {
  52. switch(*p++) {
  53. case '?' : // match any single character
  54. if (!*s++) return 0;
  55. break;
  56. case '*' : // match 0-n of any characters
  57. if (!*p) return 1; // do trailing * quickly
  58. while (!fl_filename_match(s, p)) if (!*s++) return 0;
  59. return 1;
  60. case '[': { // match one character in set of form [abc-d] or [^a-b]
  61. if (!*s) return 0;
  62. int reverse = (*p=='^' || *p=='!'); if (reverse) p++;
  63. matched = 0;
  64. char last = 0;
  65. while (*p) {
  66. if (*p=='-' && last) {
  67. if (*s <= *++p && *s >= last ) matched = 1;
  68. last = 0;
  69. } else {
  70. if (*s == *p) matched = 1;
  71. }
  72. last = *p++;
  73. if (*p==']') break;
  74. }
  75. if (matched == reverse) return 0;
  76. s++; p++;}
  77. break;
  78. case '{' : // {pattern1|pattern2|pattern3}
  79. NEXTCASE:
  80. if (fl_filename_match(s,p)) return 1;
  81. for (matched = 0;;) {
  82. switch (*p++) {
  83. case '\\': if (*p) p++; break;
  84. case '{': matched++; break;
  85. case '}': if (!matched--) return 0; break;
  86. case '|': case ',': if (matched==0) goto NEXTCASE;
  87. case 0: return 0;
  88. }
  89. }
  90. case '|': // skip rest of |pattern|pattern} when called recursively
  91. case ',':
  92. for (matched = 0; *p && matched >= 0;) {
  93. switch (*p++) {
  94. case '\\': if (*p) p++; break;
  95. case '{': matched++; break;
  96. case '}': matched--; break;
  97. }
  98. }
  99. break;
  100. case '}':
  101. break;
  102. case 0: // end of pattern
  103. return !*s;
  104. case '\\': // quote next character
  105. if (*p) p++;
  106. /* FALLTHROUGH */
  107. default:
  108. if (tolower(*s) != tolower(*(p-1))) return 0;
  109. s++;
  110. break;
  111. }
  112. }
  113. }
  114. //
  115. // End of "$Id: filename_match.cxx 7903 2010-11-28 21:06:39Z matt $".
  116. //