Quake-III-Arena

Quake III Arena GPL Source Release
Log | Files | Refs

FNMATCH.CPP (2039B)


      1 /*
      2 ===========================================================================
      3 Copyright (C) 1999-2005 Id Software, Inc.
      4 
      5 This file is part of Quake III Arena source code.
      6 
      7 Quake III Arena source code is free software; you can redistribute it
      8 and/or modify it under the terms of the GNU General Public License as
      9 published by the Free Software Foundation; either version 2 of the License,
     10 or (at your option) any later version.
     11 
     12 Quake III Arena source code is distributed in the hope that it will be
     13 useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
     14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     15 GNU General Public License for more details.
     16 
     17 You should have received a copy of the GNU General Public License
     18 along with Foobar; if not, write to the Free Software
     19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
     20 ===========================================================================
     21 */
     22 #include "stdafx.h"
     23 #include <stdlib.h>
     24 
     25 #define Reg1 register
     26 int match(char *mask, char *name)
     27 {
     28 	Reg1	unsigned char	*m = (unsigned char *)mask, *n = (unsigned char *)name;
     29 	char	*ma = mask, *na = name;
     30 	int	wild = 0, q = 0;
     31 
     32 	while (1)
     33 	    {
     34 		if (*m == '*')
     35 		   {
     36 			while (*m == '*')
     37 				m++;
     38 			wild = 1;
     39 			ma = (char *)m;
     40 			na = (char *)n;
     41 		    }
     42 
     43 		if (!*m)
     44 		    {
     45 	  		if (!*n)
     46 				return 0;
     47 	  		for (m--; (m > (unsigned char *)mask) && (*m == '?'); m--)
     48 				;
     49 			if ((*m == '*') && (m > (unsigned char *)mask) &&
     50 			    (m[-1] != '\\'))
     51 				return 0;
     52 			if (!wild) 
     53 				return 1;
     54 			m = (unsigned char *)ma;
     55 			n = (unsigned char *)++na;
     56 		    }
     57 		else if (!*n)
     58 		    {
     59 			while(*m == '*')
     60 				m++;
     61 			return (*m != 0);
     62 		    }
     63 		if ((*m == '\\') && ((m[1] == '*') || (m[1] == '?')))
     64 		    {
     65 			m++;
     66 			q = 1;
     67 		    }
     68 		else
     69 			q = 0;
     70 
     71 		if ((tolower(*m) != tolower(*n)) && ((*m != '?') || q))
     72 		    {
     73 			if (!wild)
     74 				return 1;
     75 			m = (unsigned char *)ma;
     76 			n = (unsigned char *)++na;
     77 		    }
     78 		else
     79 		    {
     80 			if (*m)
     81 				m++;
     82 			if (*n)
     83 				n++;
     84 		    }
     85 	    }
     86 }
     87