// MatchWild.cpp				6/01/96 JJS (joe@strout.net)#include <string.h>#include "MatchWild.h"const int MAXPIECES = 32;			// maximum non-wild parts we can handleconst int MAXWILDS = MAXPIECES+2;	// maximum wildcardsstatic char *wilds[MAXPIECES];		// parts of given string which fit in wildcardsstatic int numWilds=0;				// how many wild parts we haveint IsWild( const char *pPat ){	// return whether pPat contains any wildcards	const char *p=pPat;	for (; *p && *p != '*'; p++) {}	return (*p != '\0');}int CountNonwild( const char *pPtr ){	// return how many chars at the beginning of pPtr are not wildcards	const char *p=pPtr;	for (; *p && *p != '*'; p++) {}	return p-pPtr;}int FindSubstr( const char *pStr, const char *pSubstr ){	// return the offset of the first occurance of pSubstr in pStr, or -1 if not found	if (!*pSubstr) return 0;	const char *p = pStr;	int len = strlen(pSubstr);	const char *maxp = pStr + strlen(pStr) - len;	for (; p <= maxp; p++) {		if (!strncmp( p, pSubstr, len ) ) return p-pStr;	// found it!	}	return -1;}void ClearWilds(){	for (int i=0; i<numWilds; i++)		if (wilds[i]) delete wilds[i];	numWilds = 0;}void AddWild( const char *pStart, const int pNumChars ){	if (numWilds==MAXWILDS || !pNumChars) return;	wilds[numWilds] = new char[pNumChars+1];	strncpy( wilds[numWilds], pStart, pNumChars );	wilds[numWilds][pNumChars] = '\0';	numWilds++;}int MatchWild( const char *pStr, const char *pPat ){	// first, break the pattern string (pPat) into non-wildcard pieces	char *pieces[MAXPIECES];		// array of tame pieces	int pieceLen[MAXPIECES];		// the length of each pieces	int patLen = strlen(pPat);		// total length of the pattern string	int numPieces = 0;				// number of pieces found	ClearWilds();					// clear out wildcard match holders			const char *p = pPat;			// temporary pointer into pPat	while (p < pPat+patLen && numPieces < MAXPIECES) {			pieceLen[numPieces] = CountNonwild(p);	// get length of nonwild piece		if (pieceLen[numPieces]) {				// if we have such a pieces...			pieces[numPieces] = new char[pieceLen[numPieces]+1];	// make room			strncpy( pieces[numPieces], p, pieceLen[numPieces] );	// to copy it			pieces[numPieces][pieceLen[numPieces]] = '\0';			// end in null			p += pieceLen[numPieces] + 1;		// advance the pointer			numPieces++;						// increase piece count		}		else p++;		// if piece size was zero, just advance pointer 1 char	}	if (numPieces == MAXPIECES) return 0;	// whoops -- exceeded capacity!	// now find the first occurrance in pStr of each piece	const char *s = pStr;			// temporary pointer into pStr	int offset,i;					// offset of each matching substring	for (i=0; i<numPieces; i++) {	// for each piece...		offset = FindSubstr( s, pieces[i] );	// look for a match in s		if (offset < 0) return 0;				// piece not found -- no match!		if (!i && pPat[0] != '*' && offset > 0) {			// unaccounted-for chars at beginning of string -- no match			return 0;		}		// copy the stuff that matched the wildcard...		AddWild( s, offset );		s += offset + pieceLen[i];	// advance pointer to look for next piece	}		// check end of string	if (pPat[patLen-1] == '*') {		AddWild( s, pStr+strlen(pStr)-s );	// copy one last wild	}	else {		// if no wildcard at end of string, then we'd better have munched it all...		if (s < pStr + strlen(pStr)) return 0;		// nope, unaccounted-for chars	}		return 1;}void ReplaceWild( char *pDest, const int pDestSize, const char *pPat ){	// crawl through pStr, replacing wildcards with stored data from previous MatchWild	int wildnum = 0;						// which wildcard we're on	char *d=pDest;							// pointer into the destination string	const char *s = pPat;					// pointer into the pattern string	const char *w;							// pointer into the current wildcard	const char *dEnd = d+pDestSize-1;		// end of the destination buffer		while (*s && d < dEnd) {		// are we looking at a wildcard?		if (*s == '*' && wildnum < numWilds) {			// yep... copy a wild item (if we have one)			for (w = wilds[wildnum]; *w && d < dEnd; w++,d++)				*d = *w;			wildnum++;			s++;		}		// if not a wildcard, just copy from the original string		else *d++ = *s++;	}	*d = '\0';}int MatchReplaceWild( char *pDest, const int pDestSize, 					  const char *pStr, const char *pPat1, const char *pPat2 ){	if (!MatchWild( pStr, pPat1 )) return 0;		// no match	ReplaceWild( pDest, pDestSize, pPat2 );	return 1;}