HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
UT_PathPattern.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  */
7 
8 #ifndef __UT_PathPattern_h__
9 #define __UT_PathPattern_h__
10 
11 #include "UT_API.h"
12 #include "UT_Array.h"
13 #include "UT_IntArray.h"
14 #include "UT_IntrusivePtr.h"
15 #include "UT_StringHolder.h"
16 #include "UT_StringArray.h"
17 #include "UT_UniquePtr.h"
18 
19 class UT_SpecialTokenData : public UT_IntrusiveRefCounter<UT_SpecialTokenData>
20 {
21 public:
23  { }
25  { }
26 };
28 
29 // Matches a pattern against a USD path. The pattern uses roughly rsync style
30 // matching, where "*" only matches within a path component, and "**" matches
31 // across path components. Also supports standard Houdini "^" operator to
32 // exclude paths. In addition, "-" will also exclude paths, "&" will perform
33 // intersections between two sets of paths, and "~" can be used to quickly
34 // prune branches during a full traversal of the path hierarchy.
36 {
37 public:
39  bool case_sensitive = true,
40  bool assume_wildcards = false);
41  virtual ~UT_PathPattern();
42 
43  bool matches(const UT_StringRef &path,
44  bool *excludes_branch = nullptr) const;
45  bool getExplicitList(UT_StringArray &tokens) const;
46 
47  bool getCaseSensitive() const
48  { return myCaseSensitive; }
50  { return myAssumeWildcardsAroundPlainTokens; }
52  { return myPatternError; }
53 
54 protected:
55  class Token {
56  public:
57  Token(const UT_StringHolder &str,
58  bool do_path_matching,
59  bool has_wildcards)
60  : myString(str),
61  myDoPathMatching(do_path_matching),
62  myHasWildcards(has_wildcards),
63  myIsSpecialToken(false)
64  { }
66  { }
67 
73  };
74 
75  // Default constructor, only to be used by createEmptyClone, which is used
76  // by create* functions to build a new patterns from pieces of the current
77  // pattern.
78  UT_PathPattern(bool case_sensitive,
79  bool assume_wildcards);
80 
81  UT_PathPattern *createPruningPattern(int tokenidx);
82  UT_PathPattern *createPrecedingGroupPattern(int tokenidx);
84  {
85  return new UT_PathPattern(myCaseSensitive,
86  myAssumeWildcardsAroundPlainTokens);
87  }
88  virtual bool matchSpecialToken(
89  const UT_StringRef &path,
90  const Token &token,
91  bool *excludes_branch) const;
92  void testForExplicitList();
93 
95 
96 private:
97  // This class is used to represent the parsed expression tree generated
98  // from a string search pattern.
99  class PatternOp {
100  public:
101  enum Operator {
102  GROUP,
103  NOOP,
104  VALUES,
105  ADD,
106  SUBTRACT,
107  PRUNE,
108  INTERSECT
109  };
110 
111  PatternOp(const UT_PathPattern &owner)
112  : myOwner(owner),
113  myParent(nullptr),
114  myOp(GROUP),
115  myLeftOp(nullptr),
116  myRightOp(nullptr)
117  { }
118  PatternOp(PatternOp &parent, Operator op)
119  : myOwner(parent.myOwner),
120  myParent(&parent),
121  myOp(op),
122  myLeftOp(nullptr),
123  myRightOp(nullptr)
124  { }
125  ~PatternOp()
126  {
127  delete myLeftOp;
128  delete myRightOp;
129  }
130 
131  PatternOp *push(Operator op);
132  PatternOp *insertGroup();
133  bool containsOnlyAddOperations() const;
134 
135  const UT_PathPattern &myOwner;
136  Operator myOp;
137  PatternOp *myParent;
138  PatternOp *myLeftOp;
139  PatternOp *myRightOp;
140  UT_IntArray myValues;
141  };
142 
143  class PatternOpStep
144  {
145  public:
146  PatternOpStep(PatternOp *op = nullptr)
147  : myPatternOp(op), myFollowLeftChild(false) { }
148 
149  PatternOp *myPatternOp;
150  bool myFollowLeftChild;
151  };
152 
153  typedef UT_Array<PatternOpStep> PatternOpPath;
154 
155  void init(const UT_StringArray &pattern_tokens);
156  bool matches(const UT_StringRef &path,
157  PatternOp *op,
158  bool *excludes_branch) const;
159  void printPattern(const PatternOp *patternop,
160  int indent);
161  static bool findToken(int tokenidx,
162  PatternOpPath &oppath);
163  static PatternOp *duplicatePatternOp(
164  const UT_PathPattern &pattern,
165  const PatternOp *patternop,
166  PatternOp *parent);
167  static bool patternOpPathContains(
168  const PatternOpPath &oppath,
169  const PatternOp *patternop);
170  static bool patternOpPathIntersects(
171  const PatternOpPath &oppath,
172  const PatternOp *patternop);
173  static void makePruningPatternOp(
174  PatternOp *patternop,
175  const PatternOpPath &oppath);
176  static void makePrecedingGroupPatternOp(
177  PatternOp *patternop,
178  const PatternOpPath &oppath,
179  bool &found_token);
180  static bool parsePattern(const char *&pattern,
181  UT_StringArray &tokens,
182  PatternOp *groupop,
183  UT_String &error);
184 
185  UT_UniquePtr<PatternOp> myPatternOp;
186  UT_StringHolder myPatternError;
187  bool myIsExplicitList;
188  // These values can't change after the constructor, because the
189  // constructor can cause evaluation of the pattern or parts of it.
190  const bool myCaseSensitive;
191  const bool myAssumeWildcardsAroundPlainTokens;
192 };
193 
194 #endif
195 
*pool push(my_func, arg1,...)
GLsizei const GLchar *const * path
Definition: glcorearb.h:3341
#define UT_API
Definition: UT_API.h:14
const UT_StringHolder & getPatternError() const
A reference counter base class for use with UT_IntrusivePtr.
bool getCaseSensitive() const
std::unique_ptr< T, Deleter > UT_UniquePtr
A smart pointer for unique ownership of dynamically allocated objects.
Definition: UT_UniquePtr.h:39
UT_Array< Token > myTokens
bool getAssumeWildcardsAroundPlainTokens() const
UT_IntrusivePtr< UT_SpecialTokenData > UT_SpecialTokenDataPtr
Token(const UT_StringHolder &str, bool do_path_matching, bool has_wildcards)
GLubyte * pattern
Definition: glew.h:5741
virtual ~UT_SpecialTokenData()
UT_StringHolder myString
virtual UT_PathPattern * createEmptyClone() const
UT_SpecialTokenDataPtr mySpecialTokenDataPtr