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

Go to the source code of this file.

Functions

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. More...
 
bool match (const std::string &name, 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. More...
 
bool match (const std::string &attribute, 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. More...
 
bool match (const FieldRes *f, const std::string &patterns, const MatchFlags flags)
 
FIELD3D_NAMESPACE_OPEN 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 pattern matching implementations

Definition in file PatternMatch.cpp.

Function Documentation

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 at line 59 of file PatternMatch.cpp.

Referenced by match().

60 {
61  return split(s, " ");
62 }
FIELD3D_NAMESPACE_OPEN std::vector< std::string > split(const std::string &s)
Splits a string into a vector of strings, using &#39;,&#39; as the separator.
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 67 of file PatternMatch.cpp.

68 {
69  typedef boost::char_separator<char> CharSeparator;
70  typedef boost::tokenizer<CharSeparator> Tokenizer;
71 
72  std::vector<std::string> result;
73  CharSeparator separators(separatorChars.c_str());
74  Tokenizer tokenizer(s, separators);
75 
76  BOOST_FOREACH (const std::string &i, tokenizer) {
77  result.push_back(i);
78  }
79 
80  return result;
81 }
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 86 of file PatternMatch.cpp.

References MatchEmptyPattern.

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

89 {
90  bool foundMatch = false;
91  bool foundExclusion = false;
92 
93  if (patterns.size() == 0) {
94  return flags && MatchEmptyPattern;
95  }
96 
97  BOOST_FOREACH (const std::string &i, patterns) {
98 
99  if (i.size() == 0) {
100  continue;
101  }
102 
103  // Check exclusion string
104  bool isExclusion = i[0] == '-' || i[0] == '^';
105  // Update string
106  const std::string pattern = isExclusion ? i.substr(1) : i;
107 
108  // String to match
109  std::string s;
110 
111  // Determine type of matching
112  if (pattern.find(":") != std::string::npos) {
113  // Pattern includes separator. Match against name:attribute
114  s = name + ":" + attribute;
115  } else {
116  // No separator. Just match against attribute
117  s = attribute;
118  }
119 
120  // Match with wildcards
121  if (fnmatch(pattern.c_str(), s.c_str(), FNM_NOESCAPE) == 0) {
122  if (isExclusion) {
123  foundExclusion = true;
124  } else {
125  foundMatch = true;
126  }
127  }
128 
129  }
130 
131  return foundMatch && !foundExclusion;
132 }
bool match ( const std::string &  name,
const std::string &  attribute,
const std::string &  patterns,
const MatchFlags  flags 
)

Definition at line 137 of file PatternMatch.cpp.

References match(), and split().

140 {
141  return match(name, attribute, split(patterns), flags);
142 }
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::string > split(const std::string &s)
Splits a string into a vector of strings, using &#39;,&#39; as the separator.
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 147 of file PatternMatch.cpp.

References MatchEmptyPattern.

149 {
150  bool foundMatch = false;
151  bool foundExclusion = false;
152 
153  if (patterns.size() == 0) {
154  return flags && MatchEmptyPattern;
155  }
156 
157  BOOST_FOREACH (const std::string &i, patterns) {
158 
159  if (i.size() == 0) {
160  continue;
161  }
162 
163  // Check exclusion string
164  bool isExclusion = i[0] == '-' || i[0] == '^';
165  // Update string
166  std::string pattern = isExclusion ? i.substr(1) : i;
167 
168  // Determine type of matching
169  size_t pos = pattern.find(":");
170  if (pos != std::string::npos) {
171  // Pattern includes separator. Just use second half
172  pattern = pattern.substr(pos + 1);
173  }
174 
175  // Match with wildcards
176  if (fnmatch(pattern.c_str(), attribute.c_str(), FNM_NOESCAPE) == 0) {
177  if (isExclusion) {
178  foundExclusion = true;
179  } else {
180  foundMatch = true;
181  }
182  }
183 
184  }
185 
186  return foundMatch && !foundExclusion;
187 }
bool match ( const std::string &  attribute,
const std::string &  patterns,
const MatchFlags  flags 
)

Definition at line 192 of file PatternMatch.cpp.

References match(), and split().

194 {
195  return match(attribute, split(patterns), flags);
196 }
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::string > split(const std::string &s)
Splits a string into a vector of strings, using &#39;,&#39; as the separator.
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 201 of file PatternMatch.cpp.

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

203 {
204  return match(f->name, f->attribute, patterns, flags);
205 }
std::string attribute
Optional name of the attribute the field represents.
Definition: Field.h:178
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.
std::string name
Optional name of the field.
Definition: Field.h:176
bool match ( const FieldRes f,
const std::string &  patterns,
const MatchFlags  flags 
)

Definition at line 210 of file PatternMatch.cpp.

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

212 {
213  return match(f->name, f->attribute, split(patterns), flags);
214 }
std::string attribute
Optional name of the attribute the field represents.
Definition: Field.h:178
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::string > split(const std::string &s)
Splits a string into a vector of strings, using &#39;,&#39; as the separator.
std::string name
Optional name of the field.
Definition: Field.h:176