cb21075d |
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 |
39b022cb |
16 | // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
cb21075d |
17 | |
18 | #include <string.h> |
19 | |
20 | #include "Mask.H" |
21 | |
22 | Mask::Mask(String s) |
23 | : mask(s) |
24 | { } |
25 | |
26 | String |
e99c573b |
27 | Mask::getMask() const |
cb21075d |
28 | { |
29 | return mask; |
30 | } |
31 | |
32 | bool |
e99c573b |
33 | Mask::matches(String s) const |
cb21075d |
34 | { |
815a1816 |
35 | return match(mask.c_str (), s.c_str ()); |
cb21075d |
36 | } |
37 | |
38 | bool |
e99c573b |
39 | Mask::matches(const Mask & m) const |
cb21075d |
40 | { |
815a1816 |
41 | return match(mask.c_str (), m.mask.c_str ()); |
cb21075d |
42 | } |
43 | |
44 | bool |
e99c573b |
45 | Mask::match(const char * m, const char * n) const |
cb21075d |
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 | |
cb278e5a |
71 | if ((std::tolower (m[0]) == std::tolower (n[0])) || |
cb21075d |
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 | } |