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  void reset(
112  const UT_StringHolder& pattern,
113  MatchType default_match_type=eMatchPlain,
114  bool strict=true);
115 
116  /// Returns true if the attribute/type is include in the pattern
117  bool contains(const UT_StringHolder& name,
120 
121  /// Returns true if the attribute/type is included in the pattern, and
122  /// writes the match components.
123  bool components(
124  ComponentMatch& components,
125  const UT_StringHolder& name,
128 
129  /// Applies the pattern to the specified list of input names, and returns
130  /// the final list of names that matched the pattern. If keep_plain is
131  /// true, then any non-glob pattern pattern entries are always included
132  /// even if they don't appear in the input names list.
133  void multiMatch(
134  UT_StringArray& match_results,
135  const UT_StringArray& input_names,
136  bool keep_plain) const;
137 
138  /// Applies the pattern to the specified list of input names, and returns
139  /// a map of matched components for each name. If keep_plain is true, then
140  /// any non-glob pattern entries are always included even if they don't
141  /// appear in the input names list.
142  void multiMatch(
143  ComponentMap& match_components,
144  const UT_StringArray& input_names,
145  bool keep_plain) const;
146 
147 private:
148  /// Compound match pattern
149  struct AttributeMatch
150  {
151  /// The string name, possibly empty if match type is All
152  UT_StringHolder myMatchName;
153 
154  /// The attribute type
155  PDG_AttributeType myAttribType;
156 
157  /// The type of match prefix
158  MatchType myMatchType;
159 
160  /// Whether or not this is an exlucsion
161  bool myExclude;
162  };
163 
164 private:
165  bool parse(const UT_StringHolder& pattern,
166  MatchType default_match_type,
167  bool strict);
168  void addPattern(bool exclude,
169  const UT_WorkBuffer& name,
170  MatchType match_type,
171  PDG_AttributeType attrib_type);
172  bool matches(const AttributeMatch& match,
173  const UT_StringHolder& name,
174  PDG_AttributeType type) const;
175  bool matches(ComponentMatch& components,
176  const AttributeMatch& match,
177  const UT_StringHolder& name,
178  PDG_AttributeType type) const;
179 
180 private:
181  /// Extra compound patterns to exclude
182  UT_Array<AttributeMatch> myMatches;
183 
184  /// Whether or not the pattern has any exclusions
185  bool myHasExclusions;
186 };
187 
188 #endif
The pattern is a string name with both a prefix and suffix.
The pattern is a * followed by a string name.
#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
Undefined or uninitialized attribute type.
GLuint const GLchar * name
Definition: glcorearb.h:786
GLushort pattern
Definition: glad.h:2583
MatchType
For compound matching, e.g. foobar or bin*:float.
PDG_AttributeType
Enumeration of possible attribute types.
bool OIIO_UTIL_API contains(string_view a, string_view b)
Does 'a' contain the string 'b' within it?
type
Definition: core.h:1059