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