1/*
2 * File:	ExcludesListMatcher.cpp
3 *
4 * Copyright (c) Freescale Semiconductor, Inc. All rights reserved.
5 * See included license file for license details.
6 */
7
8#include "ExcludesListMatcher.h"
9
10using namespace elftosb;
11
12ExcludesListMatcher::ExcludesListMatcher()
13:	GlobMatcher("")
14{
15}
16
17ExcludesListMatcher::~ExcludesListMatcher()
18{
19}
20
21//! \param isInclude True if this pattern is an include, false if it is an exclude.
22//! \param pattern String containing the glob pattern.
23void ExcludesListMatcher::addPattern(bool isInclude, const std::string & pattern)
24{
25	glob_list_item_t item;
26	item.m_isInclude = isInclude;
27	item.m_glob = pattern;
28
29	// add to end of list
30	m_patterns.push_back(item);
31}
32
33//! If there are no entries in the match list, the match fails.
34//!
35//! \param testValue The string to match against the pattern list.
36//! \retval true The \a testValue argument matches.
37//! \retval false No match was made against the argument.
38bool ExcludesListMatcher::match(const std::string & testValue)
39{
40	if (!m_patterns.size())
41	{
42		return false;
43	}
44
45	// Iterate over the match list. Includes act as an OR operator, while
46	// excludes act as an AND operator.
47	bool didMatch = false;
48	bool isFirstItem = true;
49	glob_list_t::iterator it = m_patterns.begin();
50	for (; it != m_patterns.end(); ++it)
51	{
52		glob_list_item_t & item = *it;
53
54		// if this pattern is an include and it doesn't match, or
55		// if this pattern is an exclude and it does match, then the match fails
56		bool didItemMatch = globMatch(testValue.c_str(), item.m_glob.c_str());
57
58		if (item.m_isInclude)
59		{
60			// Include
61			if (isFirstItem)
62			{
63				didMatch = didItemMatch;
64			}
65			else
66			{
67				didMatch = didMatch || didItemMatch;
68			}
69		}
70		else
71		{
72			// Exclude
73			if (isFirstItem)
74			{
75				didMatch = !didItemMatch;
76			}
77			else
78			{
79				didMatch = didMatch && !didItemMatch;
80			}
81		}
82
83		isFirstItem = false;
84	}
85
86	return didMatch;
87}
88
89