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

#include "ExcludesListMatcher.h"

using namespace elftosb;

ExcludesListMatcher::ExcludesListMatcher()
:	GlobMatcher("")
{
}

ExcludesListMatcher::~ExcludesListMatcher()
{
}

//! \param isInclude True if this pattern is an include, false if it is an exclude.
//! \param pattern String containing the glob pattern.
void ExcludesListMatcher::addPattern(bool isInclude, const std::string & pattern)
{
	glob_list_item_t item;
	item.m_isInclude = isInclude;
	item.m_glob = pattern;
	
	// add to end of list
	m_patterns.push_back(item);
}

//! If there are no entries in the match list, the match fails.
//!
//! \param testValue The string to match against the pattern list.
//! \retval true The \a testValue argument matches.
//! \retval false No match was made against the argument.
bool ExcludesListMatcher::match(const std::string & testValue)
{
	if (!m_patterns.size())
	{
		return false;
	}
	
	// Iterate over the match list. Includes act as an OR operator, while
	// excludes act as an AND operator.
	bool didMatch = false;
	bool isFirstItem = true;
	glob_list_t::iterator it = m_patterns.begin();
	for (; it != m_patterns.end(); ++it)
	{
		glob_list_item_t & item = *it;
		
		// if this pattern is an include and it doesn't match, or
		// if this pattern is an exclude and it does match, then the match fails
		bool didItemMatch = globMatch(testValue.c_str(), item.m_glob.c_str());
		
		if (item.m_isInclude)
		{
			// Include
			if (isFirstItem)
			{
				didMatch = didItemMatch;
			}
			else
			{
				didMatch = didMatch || didItemMatch;
			}
		}
		else
		{
			// Exclude
			if (isFirstItem)
			{
				didMatch = !didItemMatch;
			}
			else
			{
				didMatch = didMatch && !didItemMatch;
			}
		}
		
		isFirstItem = false;
	}
	
	return didMatch;
}