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_Array.h>
20 #include <UT/UT_StringHolder.h>
21 #include <UT/UT_StringMap.h>
22 
23 class UT_StringArray;
24 class UT_WorkBuffer;
25 
26 /**
27  * Pattern for matching a list of attributes. A pattern object is constructed
28  * from a valid pattern string, such as: * ^foo.
29  *
30  * A pattern is a whitespace seperated sequence of match conditions. Each
31  * condition can:
32  *
33  * - Optionally begin with a ^
34  * - Be a single * character
35  * - Or, be a valid attribute name with an optional leading *, trailing * or
36  * both a leading a trailing *
37  * - Optionally end with a : followed by a valid type name, for example float,
38  * integer or string.
39  *
40  * The following are valid:
41  *
42  * *
43  * * ^foo:float
44  * * ^*foo ^bar* ^*foobar* ^*:string
45  * *:integer
46  * foo ^bar *:python
47  *
48  * The following are not:
49  * ^
50  * *foo*bar*
51  * **
52  * f*o
53  * ^:integer
54  * ^*:foobar
55  *
56  * Pattern rules are applied left to right when checking if an attribute
57  * name matches.
58  *
59  * If the pattern matcher is constructed with the strict option set to false,
60  * then all characters except for * an ^ are valid for attribute names. This
61  * also disables the type specifier : character, and type name parsing.
62  */
64 {
65 public:
66  /// For compound matching, e.g. *foobar* or bin*:float
68  {
69  /// The pattern is just a *
71 
72  /// The pattern is a * followed by a string name
74 
75  /// The pattern is a string name, followed by a *
77 
78  /// The pattern is a string name with both a prefix and suffix
80 
81  /// The pattern has no *
83  };
84 
85  /// Break down of the components that matched a pattern. For example,
86  /// if the pattern is *foobar* and the input string is thefoobarstring,
87  /// then:
88  /// myPrefix contains "the"
89  /// myMatch contains "foobar"
90  /// mySuffix contains "string"
92  {
96  };
97 
98  /// Map of attribute name to ComponentMatch
100 
101 public:
102 
103  /// Constructs a default-initialized, invalid pattern.
105 
106  /// Parses a pattern into a PDG_AttributePattern instance
108  MatchType default_match_type=eMatchPlain,
109  bool strict=true,
110  const char* delimiters=" \t\n\r");
111 
112  /// Clears the pattern and recompiles it from the specified pattern string
113  bool reset(
114  const UT_StringHolder& pattern,
115  MatchType default_match_type=eMatchPlain,
116  bool strict=true);
117 
118  /// Resets the pattern if doesn't match the specified pattern string and
119  /// configuration arguments.
120  bool tryReset(
121  const UT_StringHolder& pattern,
122  MatchType default_match_type=eMatchPlain,
123  bool strict=true);
124 
125  /// Returns true if the attribute/type is include in the pattern
126  bool contains(const UT_StringHolder& name,
129 
130  /// Returns true if the attribute/type is included in the pattern, and
131  /// writes the match components.
132  bool components(
133  ComponentMatch& components,
134  const UT_StringHolder& name,
137 
138  /// Applies the pattern to the specified list of input names, and returns
139  /// the final list of names that matched the pattern. If keep_plain is
140  /// true, then any non-glob pattern pattern entries are always included
141  /// even if they don't appear in the input names list.
142  void multiMatch(
143  UT_StringArray& match_results,
144  const UT_StringArray& input_names,
145  bool keep_plain) const;
146 
147  /// Applies the pattern to the specified list of input names, and returns
148  /// a map of matched components for each name. If keep_plain is true, then
149  /// any non-glob pattern entries are always included even if they don't
150  /// appear in the input names list.
151  void multiMatch(
152  ComponentMap& match_components,
153  const UT_StringArray& input_names,
154  bool keep_plain) const;
155 
156 private:
157  /// Compound match pattern
158  struct AttributeMatch
159  {
160  /// The string name, possibly empty if match type is All
161  UT_StringHolder myMatchName;
162 
163  /// The attribute type
164  PDG_AttributeType myAttribType;
165 
166  /// The type of match prefix
167  MatchType myMatchType;
168 
169  /// Whether or not this is an exlucsion
170  bool myExclude;
171  };
172 
173 private:
174  bool parse(const UT_StringHolder& pattern);
175  void addPattern(bool exclude,
176  const UT_WorkBuffer& name,
177  MatchType match_type,
178  PDG_AttributeType attrib_type);
179  bool matches(const AttributeMatch& match,
180  const UT_StringHolder& name,
181  PDG_AttributeType type) const;
182  bool matches(ComponentMatch& components,
183  const AttributeMatch& match,
184  const UT_StringHolder& name,
185  PDG_AttributeType type) const;
186 
187 private:
188  /// Extra compound patterns to exclude
189  UT_Array<AttributeMatch> myMatches;
190 
191  /// The default match type
192  MatchType myDefaultMatchType;
193 
194  /// The seperator used when tokenizing the input string
195  const char* myDelimiters;
196 
197  /// Whether or not the pattern has any exclusions
198  bool myHasExclusions;
199 
200  /// Whether or not the pattern is strict
201  bool myIsStrict;
202 };
203 
204 #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
GLint GLint GLsizei GLint GLenum GLenum type
Definition: glcorearb.h:108
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?