rvwildcard.c

来自「h.248协议源码」· C语言 代码 · 共 96 行

C
96
字号
/******************************************************************************
Filename   : rvWildcard.c
Description: Matched a string to a wildcard pattern
******************************************************************************
                Copyright (c) 2001 RADVision Inc.
************************************************************************
NOTICE:
This document contains information that is proprietary to RADVision LTD.
No part of this publication may be reproduced in any form whatsoever 
without written prior approval by RADVision LTD..

RADVision LTD. reserves the right to revise this publication and make 
changes without obligation to notify any person of such revisions or 
changes.
******************************************************************************
$Revision:$
$Date:$
$Author: S. Cipolli$
******************************************************************************/
#include <ctype.h>
#include <string.h>
#include "rvtypes.h"
#include "rvwildcard.h"

#define ANY_CHARACTER	'?'
#define ALL_CHARACTERS	'*'
#define NO_MATCH	((size_t)(~(0U)))

static RvBool Equal(char a, char b, RvBool caseSensitive) {
	return a == b || ((!caseSensitive) && (tolower(a) == tolower(b)));
}

static size_t Find(char c, const char* s, size_t i, RvBool caseSensitive) {
	for (; i < strlen(s); ++i)
		if (c == ANY_CHARACTER || Equal(s[i], c, caseSensitive))
			return i;
		
	return NO_MATCH;
}

static size_t LookAhead(const char* pattern, size_t i,char allCharacters) {
	while (i < strlen(pattern) && pattern[i] == allCharacters)
		++i;
	return i;
}

/* This constructor allows to set the character used to match ALL CHARACTERS */
RvWildcard* rvWildcardConstructEx(RvWildcard* w, const char* pattern, RvBool caseSensitive,char allCharacters) { 
	w->pattern = pattern;
	w->caseSensitive = caseSensitive;
	w->allCharacters = allCharacters;
	return w;
}


RvWildcard* rvWildcardConstruct(RvWildcard* w, const char* pattern, RvBool caseSensitive) { 
	return rvWildcardConstructEx(w,pattern,caseSensitive,ALL_CHARACTERS);
}

static RvBool Match(const char* pattern, const char* name, RvBool caseSensitive,char allCharacters) {
	size_t j = 0, i;

	for (i = 0; i < strlen(pattern); ++i) {

		if (pattern[i] == ANY_CHARACTER) {
			++j;
		} else if (pattern[i] == allCharacters) {
			i = LookAhead(pattern, i + 1,allCharacters);

			/* If the wildcard is at the end of the pattern */
			if (i == strlen(pattern))
				return rvTrue;

			/* For all matches to the lookahead char, match substrings */
			while ((j = Find(pattern[i], name, j, caseSensitive)) != NO_MATCH) {
				if (Match(&(pattern[i + 1]), &(name[j + 1]), caseSensitive,allCharacters))
					return rvTrue;
				++j;
			}

			return rvFalse;
		} else {
			if (!Equal(pattern[i], name[j], caseSensitive))
				return rvFalse;
			++j;
		}
	}

	return j == strlen(name);
}


RvBool rvWildcardMatch(RvWildcard* w, const char* name) {
	return Match(w->pattern, name, w->caseSensitive,w->allCharacters);
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?