summaryrefslogtreecommitdiffstats
path: root/common/GlobMatcher.cpp
blob: 24e7b910aeec777e58b1a0a48b6064091c3ccf17 (plain)
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
/*
 * File:	GlobMatcher.cpp
 *
 * Copyright (c) Freescale Semiconductor, Inc. All rights reserved.
 * See included license file for license details.
 */

#include "GlobMatcher.h"

#ifndef NEGATE
#define NEGATE	'^'			// std cset negation char
#endif

using namespace elftosb;

//! The glob pattern must match the \e entire test value argument in order
//! for the match to be considered successful. Thus, even if, for example,
//! the pattern matches all but the last character the result will be false.
//!
//! \retval true The test value does match the glob pattern.
//! \retval false The test value does not match the glob pattern.
bool GlobMatcher::match(const std::string & testValue)
{
	return globMatch(testValue.c_str(), m_pattern.c_str());
}

//! \note This glob implementation was originally written by ozan s. yigit in
//!		December 1994. This is public domain source code.
bool GlobMatcher::globMatch(const char *str, const char *p)
{
	int negate;
	int match;
	int c;

	while (*p) {
		if (!*str && *p != '*')
			return false;

		switch (c = *p++) {

		case '*':
			while (*p == '*')
				p++;

			if (!*p)
				return true;

			if (*p != '?' && *p != '[' && *p != '\\')
				while (*str && *p != *str)
					str++;

			while (*str) {
				if (globMatch(str, p))
					return true;
				str++;
			}
			return false;

		case '?':
			if (*str)
				break;
			return false;
		
		// set specification is inclusive, that is [a-z] is a, z and
		// everything in between. this means [z-a] may be interpreted
		// as a set that contains z, a and nothing in between.
		case '[':
			if (*p != NEGATE)
				negate = false;
			else {
				negate = true;
				p++;
			}

			match = false;

			while (!match && (c = *p++)) {
				if (!*p)
					return false;
				if (*p == '-') {	// c-c
					if (!*++p)
						return false;
					if (*p != ']') {
						if (*str == c || *str == *p ||
						    (*str > c && *str < *p))
							match = true;
					}
					else {		// c-]
						if (*str >= c)
							match = true;
						break;
					}
				}
				else {			// cc or c]
					if (c == *str)
						match = true;
					if (*p != ']') {
						if (*p == *str)
							match = true;
					}
					else
						break;
				}
			}

			if (negate == match)
				return false;
			// if there is a match, skip past the cset and continue on
			while (*p && *p != ']')
				p++;
			if (!*p++)	// oops!
				return false;
			break;

		case '\\':
			if (*p)
				c = *p++;
		default:
			if (c != *str)
				return false;
			break;

		}
		str++;
	}

	return !*str;
}