1238104Sdes/*
2238104Sdes * File:	ExcludesListMatcher.cpp
3285206Sdes *
4238104Sdes * Copyright (c) Freescale Semiconductor, Inc. All rights reserved.
5238104Sdes * See included license file for license details.
6238104Sdes */
7238104Sdes
8285206Sdes#include "ExcludesListMatcher.h"
9238104Sdes
10238104Sdesusing namespace elftosb;
11238104Sdes
12238104SdesExcludesListMatcher::ExcludesListMatcher()
13238104Sdes:	GlobMatcher("")
14238104Sdes{
15238104Sdes}
16238104Sdes
17238104SdesExcludesListMatcher::~ExcludesListMatcher()
18238104Sdes{
19238104Sdes}
20238104Sdes
21238104Sdes//! \param isInclude True if this pattern is an include, false if it is an exclude.
22238104Sdes//! \param pattern String containing the glob pattern.
23238104Sdesvoid ExcludesListMatcher::addPattern(bool isInclude, const std::string & pattern)
24238104Sdes{
25238104Sdes	glob_list_item_t item;
26238104Sdes	item.m_isInclude = isInclude;
27238104Sdes	item.m_glob = pattern;
28238104Sdes
29238104Sdes	// add to end of list
30238104Sdes	m_patterns.push_back(item);
31238104Sdes}
32238104Sdes
33238104Sdes//! If there are no entries in the match list, the match fails.
34238104Sdes//!
35238104Sdes//! \param testValue The string to match against the pattern list.
36238104Sdes//! \retval true The \a testValue argument matches.
37238104Sdes//! \retval false No match was made against the argument.
38238104Sdesbool ExcludesListMatcher::match(const std::string & testValue)
39238104Sdes{
40238104Sdes	if (!m_patterns.size())
41238104Sdes	{
42238104Sdes		return false;
43238104Sdes	}
44238104Sdes
45238104Sdes	// Iterate over the match list. Includes act as an OR operator, while
46238104Sdes	// excludes act as an AND operator.
47238104Sdes	bool didMatch = false;
48238104Sdes	bool isFirstItem = true;
49238104Sdes	glob_list_t::iterator it = m_patterns.begin();
50238104Sdes	for (; it != m_patterns.end(); ++it)
51238104Sdes	{
52238104Sdes		glob_list_item_t & item = *it;
53238104Sdes
54238104Sdes		// if this pattern is an include and it doesn't match, or
55238104Sdes		// if this pattern is an exclude and it does match, then the match fails
56238104Sdes		bool didItemMatch = globMatch(testValue.c_str(), item.m_glob.c_str());
57238104Sdes
58238104Sdes		if (item.m_isInclude)
59238104Sdes		{
60238104Sdes			// Include
61238104Sdes			if (isFirstItem)
62238104Sdes			{
63238104Sdes				didMatch = didItemMatch;
64238104Sdes			}
65238104Sdes			else
66238104Sdes			{
67238104Sdes				didMatch = didMatch || didItemMatch;
68238104Sdes			}
69238104Sdes		}
70238104Sdes		else
71238104Sdes		{
72238104Sdes			// Exclude
73238104Sdes			if (isFirstItem)
74238104Sdes			{
75238104Sdes				didMatch = !didItemMatch;
76238104Sdes			}
77238104Sdes			else
78238104Sdes			{
79238104Sdes				didMatch = didMatch && !didItemMatch;
80238104Sdes			}
81238104Sdes		}
82238104Sdes
83238104Sdes		isFirstItem = false;
84238104Sdes	}
85238104Sdes
86238104Sdes	return didMatch;
87238104Sdes}
88238104Sdes
89238104Sdes