Field3D
PatternMatch.cpp File Reference
#include "PatternMatch.h"
#include <fnmatch.h>

Go to the source code of this file.

Functions

bool match (const FieldRes *f, const std::string &patterns, const MatchFlags flags)
 
bool match (const FieldRes *f, const std::vector< std::string > &patterns, const MatchFlags flags)
 Matches a field's name and attribute against a set of patterns.
 
bool match (const std::string &attribute, const std::string &patterns, const MatchFlags flags)
 
bool match (const std::string &attribute, const std::vector< std::string > &patterns, const MatchFlags flags)
 Matches an <attribute> string against a set of patterns.
 
bool match (const std::string &name, const std::string &attribute, const std::string &patterns, const MatchFlags flags)
 
bool match (const std::string &name, const std::string &attribute, const std::vector< std::string > &patterns, const MatchFlags flags)
 Matches a <name>:<attribute> string against a set of patterns.
 
FIELD3D_NAMESPACE_OPEN std::vector< std::stringsplit (const std::string &s)
 Splits a string into a vector of strings, using ',' as the separator.
 
std::vector< std::stringsplit (const std::string &s, const std::string &separatorChars)
 Splits a string into a vector of strings, given separator characters.
 

Detailed Description

Contains pattern matching implementations

Definition in file PatternMatch.cpp.

Function Documentation

◆ split() [1/2]

Splits a string into a vector of strings, using ',' as the separator.

Definition at line 75 of file PatternMatch.cpp.

76{
77 return split(s, " ");
78}
FIELD3D_NAMESPACE_OPEN std::vector< std::string > split(const std::string &s)
Splits a string into a vector of strings, using ',' as the separator.
#define FIELD3D_MTX_T
Definition StdMathLib.h:99

References FIELD3D_MTX_T, and split().

Referenced by match(), match(), match(), and split().

◆ split() [2/2]

std::vector< std::string > split ( const std::string & s,
const std::string & separatorChars )

Splits a string into a vector of strings, given separator characters.

Definition at line 83 of file PatternMatch.cpp.

84{
85 typedef boost::char_separator<char> CharSeparator;
86 typedef boost::tokenizer<CharSeparator> Tokenizer;
87
88 std::vector<std::string> result;
91
92 BOOST_FOREACH (const std::string &i, tokenizer) {
93 result.push_back(i);
94 }
95
96 return result;
97}

References FIELD3D_MTX_T.

◆ match() [1/6]

bool match ( const std::string & name,
const std::string & attribute,
const std::vector< std::string > & patterns,
const MatchFlags flags )

Matches a <name>:<attribute> string against a set of patterns.

Definition at line 102 of file PatternMatch.cpp.

105{
106 bool foundMatch = false;
107 bool foundExclusion = false;
108
109 if (patterns.size() == 0) {
110 return flags && MatchEmptyPattern;
111 }
112
113 BOOST_FOREACH (const std::string &i, patterns) {
114
115 if (i.size() == 0) {
116 continue;
117 }
118
119 // Check exclusion string
120 bool isExclusion = i[0] == '-' || i[0] == '^';
121 // Update string
122 const std::string pattern = isExclusion ? i.substr(1) : i;
123
124 // String to match
125 std::string s;
126
127 // Determine type of matching
128 if (pattern.find(":") != std::string::npos) {
129 // Pattern includes separator. Match against name:attribute
130 s = name + ":" + attribute;
131 } else {
132 // No separator. Just match against attribute
133 s = attribute;
134 }
135
136 // Match with wildcards
137 if (fnmatch(pattern.c_str(), s.c_str(), FNM_NOESCAPE) == 0) {
138 if (isExclusion) {
139 foundExclusion = true;
140 } else {
141 foundMatch = true;
142 }
143 }
144
145 }
146
147 return foundMatch && !foundExclusion;
148}
@ MatchEmptyPattern

References FIELD3D_MTX_T, and MatchEmptyPattern.

Referenced by Sparse::CheckAllEqual< Data_T >::check(), match(), match(), match(), and match().

◆ match() [2/6]

bool match ( const std::string & name,
const std::string & attribute,
const std::string & patterns,
const MatchFlags flags )

Definition at line 153 of file PatternMatch.cpp.

156{
157 return match(name, attribute, split(patterns), flags);
158}
bool match(const std::string &name, const std::string &attribute, const std::vector< std::string > &patterns, const MatchFlags flags)
Matches a <name>:<attribute> string against a set of patterns.

References FIELD3D_MTX_T, match(), and split().

◆ match() [3/6]

bool match ( const std::string & attribute,
const std::vector< std::string > & patterns,
const MatchFlags flags )

Matches an <attribute> string against a set of patterns.

Definition at line 163 of file PatternMatch.cpp.

165{
166 bool foundMatch = false;
167 bool foundExclusion = false;
168
169 if (patterns.size() == 0) {
170 return flags && MatchEmptyPattern;
171 }
172
173 BOOST_FOREACH (const std::string &i, patterns) {
174
175 if (i.size() == 0) {
176 continue;
177 }
178
179 // Check exclusion string
180 bool isExclusion = i[0] == '-' || i[0] == '^';
181 // Update string
182 std::string pattern = isExclusion ? i.substr(1) : i;
183
184 // Determine type of matching
185 size_t pos = pattern.find(":");
186 if (pos != std::string::npos) {
187 // Pattern includes separator. Just use second half
188 pattern = pattern.substr(pos + 1);
189 }
190
191 // Match with wildcards
192 if (fnmatch(pattern.c_str(), attribute.c_str(), FNM_NOESCAPE) == 0) {
193 if (isExclusion) {
194 foundExclusion = true;
195 } else {
196 foundMatch = true;
197 }
198 }
199
200 }
201
202 return foundMatch && !foundExclusion;
203}

References FIELD3D_MTX_T, and MatchEmptyPattern.

◆ match() [4/6]

bool match ( const std::string & attribute,
const std::string & patterns,
const MatchFlags flags )

Definition at line 208 of file PatternMatch.cpp.

210{
211 return match(attribute, split(patterns), flags);
212}

References FIELD3D_MTX_T, match(), and split().

◆ match() [5/6]

bool match ( const FieldRes * f,
const std::vector< std::string > & patterns,
const MatchFlags flags )

Matches a field's name and attribute against a set of patterns.

Definition at line 217 of file PatternMatch.cpp.

219{
220 return match(f->name, f->attribute, patterns, flags);
221}

References FIELD3D_MTX_T, and match().

◆ match() [6/6]

bool match ( const FieldRes * f,
const std::string & patterns,
const MatchFlags flags )

Definition at line 226 of file PatternMatch.cpp.

228{
229 return match(f->name, f->attribute, split(patterns), flags);
230}

References FIELD3D_MTX_T, match(), and split().