Debian Codegraph @

misc/psglob.cpp

raw symbols SCIP
1 /*
2 * Descent 3
3 * Copyright (C) 2024 Parallax Software
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17
18 --- HISTORICAL COMMENTS FOLLOW ---
19
20 * $Logfile: /DescentIII/Main/misc/psglob.cpp $
21 * $Revision: 3 $
22 * $Date: 4/15/99 11:10p $
23 * $Author: Jeff $
24 *
25 * string globbing functions
26 *
27 * $Log: /DescentIII/Main/misc/psglob.cpp $
28 *
29 * 3 4/15/99 11:10p Jeff
30 * fixed gcc warning
31 *
32 * 2 1/07/99 10:51p Jeff
33 * added psglob and support to do find in files for hog files
34 *
35 * $NoKeywords: $
36 */
37
38 #include "psglob.h"
39 #include <stdlib.h>
40 #include <ctype.h>
41
42 #if defined(POSIX)
43 #include <ctype.h>
44 #endif
45
46 // Returns 1 if string contains globbing characters in it
47 int PSGlobHasPattern(char *string) {
48 char *p = string;
49 char c;
50 int open = 0;
51
52 while ((c = *p++) != '\0') {
53 switch (c) {
54 case '?':
55 case '*':
56 return 1;
57 case '[': // Open brackets must have a matching close bracket in order to be a glob
58 open++;
59 continue;
60 case ']':
61 if (open)
62 return 1;
63 continue;
64 case '\\':
65 if (*p++ == '\0')
66 return 0;
67 }
68 }
69
70 return 0;
71 }
72
73 // PSGlobMatch
74 // Matches the pattern passed in to the string in text. If the pattern matches
75 // it returns 1, if not, it returns 0. In order to have a match, the following
76 // conditions must be met:
77 // 1) The entire string (text) is used for matching.
78 // 2) '*' matches any sequence of characters.
79 // 3) '?' matches any character
80 // 4) [SET] matches any character in the specified set.
81 // 5) [!SET] matches any character _not_ in the specified set.
82 // A set is composed of characters or ranges. A range looks like
83 // character hyphen character (as in 0-9 or A-Z). [0-9a-zA-Z_] is the set
84 // of all characters allowed in C identifiers.
85 // 6) Any other character in the pattern must be matched exactly.(see 9)
86 // 7) Because of the syntactic significance of []*?!- and \ to match
87 // these characters exactly, preced it with a '\'.
88 // 8) If dot_special is not zero, '*' and '?' do not match '.' at the beginning of text
89 // 9) If case_sensitive is 0, than case does not matter for the non-pattern characters
90 int PSGlobMatch(char *pattern, char *text, int case_sensitive, int dot_special) {
91 char *p = pattern, *t = text;
92 char c;
93
94 while ((c = *p++) != '\0') {
95 switch (c) {
96 case '?':
97 if (*t == '\0' || (dot_special && t == text && *t == '.'))
98 return 0;
99 else
100 ++t;
101 break;
102 case '\\':
103 if (*p++ != *t++)
104 return 0;
105 break;
106 case '*':
107 if (dot_special && t == text && *t == '.')
108 return 0;
109 return PSGlobMatchAfterStar(p, case_sensitive, t);
110 case '[': {
111 char c1 = *t++;
112 int invert;
113
114 if (!c1)
115 return (0);
116
117 invert = ((*p == '!') || (*p == '^'));
118 if (invert)
119 p++;
120 c = *p++;
121
122 while (1) {
123 char cstart = c, cend = c;
124 if (c == '\\') {
125 cstart = *p++;
126 cend = cstart;
127 }
128
129 if (c == '\0')
130 return 0;
131 c = *p++;
132
133 if (c == '-' && *p != ']') {
134 cend = *p++;
135 if (cend == '\\')
136 cend = *p++;
137 if (cend == '\0')
138 return 0;
139 c = *p++;
140 }
141
142 if (c1 >= cstart && c1 <= cend)
143 goto match;
144 if (c == ']')
145 break;
146 }
147
148 if (!invert)
149 return 0;
150 break;
151 match:
152 /* Skip the rest of the [...] construct that already matched. */
153 while (c != ']') {
154 if (c == '\0')
155 return 0;
156 c = *p++;
157 if (c == '\0')
158 return 0;
159 else if (c == '\\')
160 ++p;
161 }
162
163 if (invert)
164 return 0;
165 break;
166 }
167 default:
168 if (case_sensitive) {
169 if (c != *t++)
170 return 0;
171 } else {
172 if (tolower(c) != tolower(*t++))
173 return 0;
174 }
175 }
176 }
177
178 return *t == '\0';
179 }
180
181 // Like PSGlobMatch, but match pattern against any final segment of text
182 int PSGlobMatchAfterStar(char *pattern, int case_sensitive, char *text) {
183 char *p = pattern, *t = text;
184 char c, c1;
185
186 while ((c = *p++) == '?' || c == '*') {
187 if (c == '?' && *t++ == '\0')
188 return 0;
189 }
190
191 if (c == '\0')
192 return 1;
193 if (c == '\\')
194 c1 = *p;
195 else
196 c1 = c;
197
198 while (1) {
199 if (case_sensitive) {
200 if ((c == '[' || *t == c1) && PSGlobMatch(p - 1, t, case_sensitive, 0))
201 return 1;
202 } else {
203 if ((c == '[' || tolower(*t) == tolower(c1)) && PSGlobMatch(p - 1, t, case_sensitive, 0))
204 return 1;
205 }
206
207 if (*t++ == '\0')
208 return 0;
209 }
210 }