1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401
|
////////////////////////////////////////////////////////////////////////////////
// This source file is part of the ZipArchive library source distribution and
// is Copyrighted 2000 - 2007 by Artpol Software - Tadeusz Dracz
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// For the licensing details refer to the License.txt file.
//
// Web Site: http://www.artpol-software.com
////////////////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "Wildcard.h"
namespace ZipArchiveLib
{
bool CWildcard::IsPatternValid(LPCTSTR lpszPattern, int* iErrorType)
{
try
{
/* loop through pattern to EOS */
while (*lpszPattern)
{
/* determine pattern type */
switch (*lpszPattern)
{
/* check literal escape, it cannot be at end of pattern */
case _T('\\'):
if (!*++lpszPattern)
throw patternEsc;
lpszPattern++;
break;
/* the [..] construct must be well formed */
case _T('['):
lpszPattern++;
/* if the next character is ']' then bad pattern */
if (*lpszPattern == _T(']'))
throw patternEmpty;
/* if end of pattern here then bad pattern */
if (!*lpszPattern)
throw patternClose;
/* loop to end of [..] construct */
while (*lpszPattern != _T(']'))
{
/* check for literal escape */
if (*lpszPattern == _T('\\'))
{
lpszPattern++;
/* if end of pattern here then bad pattern */
if (!*lpszPattern++)
throw patternEsc;
}
else lpszPattern++;
/* if end of pattern here then bad pattern */
if (!*lpszPattern)
throw patternClose;
/* if this a range */
if (*lpszPattern == _T('-'))
{
/* we must have an end of range */
if (!*++lpszPattern || *lpszPattern == ']')
throw patternRange;
else
{
/* check for literal escape */
if (*lpszPattern == _T('\\'))
lpszPattern++;
/* if end of pattern here
then bad pattern */
if (!*lpszPattern++)
throw patternEsc;
}
}
}
break;
/* all other characters are valid pattern elements */
case '*':
case '?':
default:
lpszPattern++; /* "normal" character */
break;
}
}
throw patternValid;
}
catch (int i)
{
if (iErrorType)
*iErrorType = i;
return i == patternValid;
}
}
bool CWildcard::IsPattern(LPCTSTR lpszPattern)
{
while (*lpszPattern)
{
switch (*lpszPattern++)
{
case _T('?'):
case _T('*'):
case _T('['):
case _T('\\'):
return true;
}
}
return false;
}
bool CWildcard::IsMatch(LPCTSTR lpszText, int *iRetCode)
{
CZipString sz;
if (!m_bCaseSensitive)
{
sz = lpszText;
sz.MakeLower();
lpszText = (LPCTSTR)sz;
}
int i = Match((LPCTSTR)m_szPattern, lpszText);
if (iRetCode)
*iRetCode = i;
return i == matchValid;
}
int CWildcard::MatchAfterStar(LPCTSTR p, LPCTSTR t)
{
int iMatch = matchNone;
TCHAR nextp;
/* pass over existing ? and * in pattern */
while ( *p == _T('?') || *p == _T('*') )
{
/* take one char for each ? and + */
if (*p == _T('?'))
{
/* if end of text then no match */
if (!*t++)
return matchAbort;
}
/* move to next char in pattern */
p++;
}
/* if end of pattern we have matched regardless of text left */
if (!*p)
return matchValid;
/* get the next character to match which must be a literal or '[' */
nextp = *p;
if (nextp == _T('\\'))
{
nextp = p[1];
/* if end of text then we have a bad pattern */
if (!nextp)
return matchPattern;
}
/* Continue until we run out of text or definite result seen */
do
{
/* a precondition for matching is that the next character
in the pattern match the next character in the text or that
the next pattern char is the beginning of a range. Increment
text pointer as we go here */
if (nextp == *t || nextp == _T('['))
iMatch = Match(p, t);
/* try finding another precondition */
if (iMatch == matchPattern)
iMatch = matchNone;
/* if the end of text is reached then no iMatch */
if (!*t++)
iMatch = matchAbort;
} while ( iMatch != matchValid &&
iMatch != matchAbort);
/* return result */
return iMatch;
}
int CWildcard::Match(LPCTSTR lpszPattern, LPCTSTR lpszText)
{
TCHAR range_start, range_end; /* start and end in range */
bool bInvert; /* is this [..] or [!..] */
bool bMemberMatch; /* have I matched the [..] construct? */
bool bLoop; /* should I terminate? */
for ( ; *lpszPattern; lpszPattern++, lpszText++)
{
/* if this is the end of the text
then this is the end of the match */
if (!*lpszText)
{
if ( *lpszPattern == _T('*') && *++lpszPattern == _T('\0') )
return matchValid;
else
return matchAbort;
}
/* determine and react to pattern type */
switch (*lpszPattern)
{
case _T('?'): /* single any character match */
break;
case _T('*'): /* multiple any character match */
return MatchAfterStar (lpszPattern, lpszText);
/* [..] construct, single member/exclusion character match */
case _T('['):
{
/* move to beginning of range */
lpszPattern++;
/* check if this is a member match or exclusion match */
bInvert = false;
if (*lpszPattern == _T('!') || *lpszPattern == _T('^'))
{
bInvert = true;
lpszPattern++;
}
/* if closing bracket here or at range start then we have a
malformed pattern */
if (*lpszPattern == _T(']'))
return matchPattern;
bMemberMatch = false;
bLoop = true;
while (bLoop)
{
/* if end of construct then bLoop is done */
if (*lpszPattern == _T(']'))
{
bLoop = false;
continue;
}
/* matching a '!', '^', '-', '\' or a ']' */
if (*lpszPattern == _T('\\'))
range_start = range_end = *++lpszPattern;
else
range_start = range_end = *lpszPattern;
/* if end of pattern then bad pattern (Missing ']') */
if (!*lpszPattern)
return matchPattern;
/* check for range bar */
if (*++lpszPattern == _T('-'))
{
/* get the range end */
range_end = *++lpszPattern;
/* if end of pattern or construct
then bad pattern */
if (range_end == _T('\0') || range_end == _T(']'))
return matchPattern;
/* special character range end */
if (range_end == _T('\\'))
{
range_end = *++lpszPattern;
/* if end of text then
we have a bad pattern */
if (!range_end)
return matchPattern;
}
/* move just beyond this range */
lpszPattern++;
}
/* if the text character is in range then match found.
make sure the range letters have the proper
relationship to one another before comparison */
if (range_start < range_end)
{
if (*lpszText >= range_start && *lpszText <= range_end)
{
bMemberMatch = true;
bLoop = false;
}
}
else
{
if (*lpszText >= range_end && *lpszText <= range_start)
{
bMemberMatch = true;
bLoop = false;
}
}
}
/* if there was a match in an exclusion set then no match */
/* if there was no match in a member set then no match */
if ((bInvert && bMemberMatch) || !(bInvert || bMemberMatch))
return matchRange;
/* if this is not an exclusion then skip the rest of
the [...] construct that already matched. */
if (bMemberMatch)
{
while (*lpszPattern != _T(']'))
{
/* bad pattern (Missing ']') */
if (!*lpszPattern)
return matchPattern;
/* skip exact match */
if (*lpszPattern == _T('\\'))
{
lpszPattern++;
/* if end of text then
we have a bad pattern */
if (!*lpszPattern)
return matchPattern;
}
/* move to next pattern char */
lpszPattern++;
}
}
break;
}
case _T('\\'): /* next character is quoted and must match exactly */
/* move pattern pointer to quoted char and fall through */
lpszPattern++;
/* if end of text then we have a bad pattern */
if (!*lpszPattern)
return matchPattern;
/* must match this character exactly */
default:
if (*lpszPattern != *lpszText)
return matchPattern;
}
}
/* if end of text not reached then the pattern fails */
if (*lpszText)
return matchEnd;
else
return matchValid;
}
} // namespace
|