HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
PDG_AttributePattern.h
Go to the documentation of this file.
1 /*
2  * PROPRIETARY INFORMATION. This software is proprietary to
3  * Side Effects Software Inc., and is not to be reproduced,
4  * transmitted, or disclosed in any way without written permission.
5  *
6  * COMMENTS:
7  */
8 
9 #ifndef __PDG_ATTRIBUTE_PATTERN_H__
10 #define __PDG_ATTRIBUTE_PATTERN_H__
11 
12 #include "PDG_API.h"
13 
14 #include "PDG_AttributeTypes.h"
15 #include "PDG_BasePattern.h"
16 
17 #include <SYS/SYS_Types.h>
18 
19 #include <UT/UT_StringHolder.h>
20 #include <UT/UT_StringMap.h>
21 
22 class UT_StringArray;
23 class UT_WorkBuffer;
24 
25 /**
26  * Pattern for matching a list of attributes. A pattern object is constructed
27  * from a valid pattern string, such as: * ^foo.
28  *
29  * A pattern is a whitespace seperated sequence of match conditions. Each
30  * condition can:
31  *
32  * - Optionally begin with a ^
33  * - Be a single * character
34  * - Or, be a valid attribute name with an optional leading *, trailing * or
35  * both a leading a trailing *
36  * - Optionally end with a : followed by a valid type name, for example float,
37  * integer or string.
38  *
39  * The following are valid:
40  *
41  * *
42  * * ^foo:float
43  * * ^*foo ^bar* ^*foobar* ^*:string
44  * *:integer
45  * foo ^bar *:python
46  *
47  * The following are not:
48  * ^
49  * *foo*bar*
50  * **
51  * f*o
52  * ^:integer
53  * ^*:foobar
54  *
55  * Pattern rules are applied left to right when checking if an attribute
56  * name matches.
57  *
58  * If the pattern matcher is constructed with the strict option set to false,
59  * then all characters except for * an ^ are valid for attribute names. This
60  * also disables the type specifier : character, and type name parsing.
61  */
63 {
64 public:
65  /// For compound matching, e.g. *foobar* or bin*:float
67  {
68  /// The pattern is just a *
70 
71  /// The pattern is a * followed by a string name
73 
74  /// The pattern is a string name, followed by a *
76 
77  /// The pattern is a string name with both a prefix and suffix
79 
80  /// The pattern has no *
82  };
83 
84  /// Break down of the components that matched a pattern. For example,
85  /// if the pattern is *foobar* and the input string is thefoobarstring,
86  /// then:
87  /// myPrefix contains "the"
88  /// myMatch contains "foobar"
89  /// mySuffix contains "string"
91  {
95  };
96 
97  /// Map of attribute name to ComponentMatch
99 
100 public:
101 
102  /// Constructs a default-initialized, invalid pattern.
104 
105  /// Parses a pattern into a PDG_AttributePattern instance
107  MatchType default_match_type=eMatchPlain,
108  bool strict=true);
109 
110  /// Clears the pattern and recompiles it from the specified pattern string
111  bool reset(
112  const UT_StringHolder& pattern,
113  MatchType default_match_type=eMatchPlain,
114  bool strict=true);
115 
116  /// Resets the pattern if doesn't match the specified pattern string and
117  /// configuration arguments.
118  bool tryReset(
119  const UT_StringHolder& pattern,
120  MatchType default_match_type=eMatchPlain,
121  bool strict=true);
122 
123  /// Returns true if the attribute/type is include in the pattern
124  bool contains(const UT_StringHolder& name,
127 
128  /// Returns true if the attribute/type is included in the pattern, and
129  /// writes the match components.
130  bool components(
131  ComponentMatch& components,
132  const UT_StringHolder& name,
135 
136  /// Applies the pattern to the specified list of input names, and returns
137  /// the final list of names that matched the pattern. If keep_plain is
138  /// true, then any non-glob pattern pattern entries are always included
139  /// even if they don't appear in the input names list.
140  void multiMatch(
141  UT_StringArray& match_results,
142  const UT_StringArray& input_names,
143  bool keep_plain) const;
144 
145  /// Applies the pattern to the specified list of input names, and returns
146  /// a map of matched components for each name. If keep_plain is true, then
147  /// any non-glob pattern entries are always included even if they don't
148  /// appear in the input names list.
149  void multiMatch(
150  ComponentMap& match_components,
151  const UT_StringArray& input_names,
152  bool keep_plain) const;
153 
154 private:
155  /// Compound match pattern
156  struct AttributeMatch
157  {
158  /// The string name, possibly empty if match type is All
159  UT_StringHolder myMatchName;
160 
161  /// The attribute type
162  PDG_AttributeType myAttribType;
163 
164  /// The type of match prefix
165  MatchType myMatchType;
166 
167  /// Whether or not this is an exlucsion
168  bool myExclude;
169  };
170 
171 private:
172  bool parse(const UT_StringHolder& pattern);
173  void addPattern(bool exclude,
174  const UT_WorkBuffer& name,
175  MatchType match_type,
176  PDG_AttributeType attrib_type);
177  bool matches(const AttributeMatch& match,
178  const UT_StringHolder& name,
179  PDG_AttributeType type) const;
180  bool matches(ComponentMatch& components,
181  const AttributeMatch& match,
182  const UT_StringHolder& name,
183  PDG_AttributeType type) const;
184 
185 private:
186  /// Extra compound patterns to exclude
187  UT_Array<AttributeMatch> myMatches;
188 
189  /// The default match type
190  MatchType myDefaultMatchType;
191 
192  /// Whether or not the pattern has any exclusions
193  bool myHasExclusions;
194 
195  /// Whether or not the pattern is strict
196  bool myIsStrict;
197 };
198 
199 #endif
The pattern is a string name with both a prefix and suffix.
The pattern is a * followed by a string name.
Undefined or uninitialized attribute type.
#define PDG_API
Definition: PDG_API.h:23
The pattern is a string name, followed by a *.
void reset(const UT_StringHolder &pattern)
Resets the pattern.
unsigned char uint8
Definition: SYS_Types.h:36
PDG_AttributeType
Enumeration of possible attribute types.
GLuint const GLchar * name
Definition: glcorearb.h:786
GLushort pattern
Definition: glad.h:2583
MatchType
For compound matching, e.g. foobar or bin*:float.
bool OIIO_UTIL_API contains(string_view a, string_view b)
Does 'a' contain the string 'b' within it?
type
Definition: core.h:1059