Field3D
PatternMatch.h File Reference

Contains functions for pattern matching field name/attributes. More...

#include <vector>
#include <boost/foreach.hpp>
#include <boost/tokenizer.hpp>
#include <boost/thread/mutex.hpp>
#include "Field.h"
#include "ns.h"

Go to the source code of this file.

Enumerations

enum  MatchFlags { MatchNoFlags = 0, MatchEmptyPattern = 1 << 0 }
 

Functions

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

Detailed Description

Contains functions for pattern matching field name/attributes.

Definition in file PatternMatch.h.

Enumeration Type Documentation

◆ MatchFlags

enum MatchFlags
Enumerator
MatchNoFlags 
MatchEmptyPattern 

Definition at line 64 of file PatternMatch.h.

64  {
65  MatchNoFlags = 0,
66  MatchEmptyPattern = 1 << 0
67 };

Function Documentation

◆ split() [1/2]

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

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 }

Referenced by match().

◆ 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;
89  CharSeparator separators(separatorChars.c_str());
90  Tokenizer tokenizer(s, separators);
91 
92  BOOST_FOREACH (const std::string &i, tokenizer) {
93  result.push_back(i);
94  }
95 
96  return result;
97 }

◆ match() [1/6]

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

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 }

References MatchEmptyPattern.

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

◆ match() [2/6]

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

Definition at line 153 of file PatternMatch.cpp.

156 {
157  return match(name, attribute, split(patterns), flags);
158 }

References match(), and split().

◆ match() [3/6]

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

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 MatchEmptyPattern.

◆ match() [4/6]

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

Definition at line 208 of file PatternMatch.cpp.

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

References match(), and split().

◆ match() [5/6]

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

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 FieldBase::attribute, match(), and FieldBase::name.

◆ match() [6/6]

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

Definition at line 226 of file PatternMatch.cpp.

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

References FieldBase::attribute, match(), FieldBase::name, and split().

FieldBase::name
std::string name
Optional name of the field.
Definition: Field.h:171
split
FIELD3D_NAMESPACE_OPEN std::vector< std::string > split(const std::string &s)
Splits a string into a vector of strings, using ',' as the separator.
Definition: PatternMatch.cpp:75
MatchNoFlags
Definition: PatternMatch.h:65
FieldBase::attribute
std::string attribute
Optional name of the attribute the field represents.
Definition: Field.h:173
MatchEmptyPattern
Definition: PatternMatch.h:66
match
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: PatternMatch.cpp:102