| 1 | // Mask.C -*- C++ -*- |
| 2 | // Copyright (c) 1997, 1998 Etienne BERNARD |
| 3 | |
| 4 | // This program is free software; you can redistribute it and/or modify |
| 5 | // it under the terms of the GNU General Public License as published by |
| 6 | // the Free Software Foundation; either version 2 of the License, or |
| 7 | // any later version. |
| 8 | |
| 9 | // This program is distributed in the hope that it will be useful, |
| 10 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | // GNU General Public License for more details. |
| 13 | |
| 14 | // You should have received a copy of the GNU General Public License |
| 15 | // along with this program; if not, write to the Free Software |
| 16 | // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 17 | |
| 18 | #include <string.h> |
| 19 | |
| 20 | #include "Mask.H" |
| 21 | |
| 22 | Mask::Mask(String s) |
| 23 | : mask(s) |
| 24 | { } |
| 25 | |
| 26 | String |
| 27 | Mask::getMask() |
| 28 | { |
| 29 | return mask; |
| 30 | } |
| 31 | |
| 32 | bool |
| 33 | Mask::matches(String s) |
| 34 | { |
| 35 | return match((const char *)mask, (const char *)s); |
| 36 | } |
| 37 | |
| 38 | bool |
| 39 | Mask::matches(Mask & m) |
| 40 | { |
| 41 | return match((const char *)mask, (const char *)m.mask); |
| 42 | } |
| 43 | |
| 44 | bool |
| 45 | Mask::match(const char * m, const char * n) |
| 46 | { |
| 47 | if (!*m) { |
| 48 | if (!*n) |
| 49 | return true; |
| 50 | else |
| 51 | return false; |
| 52 | } else |
| 53 | if (!*n) |
| 54 | return false; |
| 55 | |
| 56 | if (m[0] == '*') { |
| 57 | while (*m && (m[0] == '*' || m[0] == '?')) |
| 58 | m++; |
| 59 | |
| 60 | if (*m == '\0') |
| 61 | return true; |
| 62 | |
| 63 | const char *c = n; |
| 64 | while ((c = strchr(c, *m)) != 0) { |
| 65 | if (match(m, c)) |
| 66 | return 1; |
| 67 | c++; |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | if ((std::tolower (m[0]) == std::tolower (n[0])) || |
| 72 | (m[0] == '?') || |
| 73 | ((m[0] == '\\') && ((m[1] == '?' && n[0] == '?') || |
| 74 | (m[1] == '*' && n[0] == '*')))) |
| 75 | return match(++m, ++n); |
| 76 | |
| 77 | return false; |
| 78 | } |