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  {
57  public:
58  Token(const UT_StringHolder &str,
59  bool do_path_matching,
60  bool has_wildcards)
61  : myString(str),
62  myDoPathMatching(do_path_matching),
63  myHasWildcards(has_wildcards),
64  myIsSpecialToken(false)
65  { }
66 
72  };
73 
74  // Default constructor, only to be used by createEmptyClone, which is used
75  // by create* functions to build a new patterns from pieces of the current
76  // pattern.
77  UT_PathPattern(bool case_sensitive,
78  bool assume_wildcards);
79 
80  UT_PathPattern *createPruningPattern(int tokenidx);
81  UT_PathPattern *createPrecedingGroupPattern(int tokenidx);
83  {
84  return new UT_PathPattern(myCaseSensitive,
85  myAssumeWildcardsAroundPlainTokens);
86  }
87  virtual bool matchSpecialToken(
88  const UT_StringRef &path,
89  const Token &token,
90  bool *excludes_branch) const;
91  void testForExplicitList();
92  void patternInterrupted();
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  {
101  public:
102  enum Operator
103  {
104  GROUP,
105  NOOP,
106  VALUES,
107  ADD,
108  SUBTRACT,
109  PRUNE,
110  INTERSECT
111  };
112 
113  PatternOp(const UT_PathPattern &owner)
114  : myOwner(owner),
115  myOp(GROUP),
116  myParent(nullptr)
117  { }
118  PatternOp(PatternOp &parent, Operator op)
119  : myOwner(parent.myOwner),
120  myOp(op),
121  myParent(&parent)
122  { }
123 
124  PatternOp *push(Operator op);
125  PatternOp *insertGroup();
126  bool containsOnlyAddOperations() const;
127 
128  PatternOp *leftOp() { return myLeftOp.get(); }
129  const PatternOp *leftOp() const { return myLeftOp.get(); }
130  PatternOp *rightOp() { return myRightOp.get(); }
131  const PatternOp *rightOp() const { return myRightOp.get(); }
132 
133  const UT_PathPattern &myOwner;
134  Operator myOp;
135  PatternOp *myParent;
136  UT_UniquePtr<PatternOp> myLeftOp;
137  UT_UniquePtr<PatternOp> myRightOp;
138  UT_IntArray myValues;
139  };
140 
141  class PatternOpStep
142  {
143  public:
144  PatternOpStep(PatternOp *op = nullptr)
145  : myPatternOp(op), myFollowLeftChild(false) { }
146 
147  PatternOp *myPatternOp;
148  bool myFollowLeftChild;
149  };
150 
151  typedef UT_Array<PatternOpStep> PatternOpPath;
152 
153  void init(const UT_StringArray &pattern_tokens);
154  bool matches(const UT_StringRef &path,
155  const PatternOp *op,
156  bool *excludes_branch) const;
157  void printPattern(const PatternOp *patternop,
158  int indent);
159  static bool findToken(int tokenidx,
160  PatternOpPath &oppath);
161  static UT_UniquePtr<PatternOp> duplicatePatternOp(
162  const UT_PathPattern &pattern,
163  const PatternOp *patternop,
164  PatternOp *parent);
165  static bool patternOpPathContains(
166  const PatternOpPath &oppath,
167  const PatternOp *patternop);
168  static bool patternOpPathIntersects(
169  const PatternOpPath &oppath,
170  const PatternOp *patternop);
171  static void makePruningPatternOp(
172  PatternOp *patternop,
173  const PatternOpPath &oppath);
174  static void makePrecedingGroupPatternOp(
175  PatternOp *patternop,
176  const PatternOpPath &oppath,
177  bool &found_token);
178  static bool parsePattern(const char *&pattern,
179  UT_StringArray &tokens,
180  PatternOp *groupop,
181  UT_String &error);
182 
183  UT_UniquePtr<PatternOp> myPatternOp;
184  UT_StringHolder myPatternError;
185  bool myIsExplicitList;
186  // These values can't change after the constructor, because the
187  // constructor can cause evaluation of the pattern or parts of it.
188  const bool myCaseSensitive;
189  const bool myAssumeWildcardsAroundPlainTokens;
190 };
191 
192 #endif
193 
*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
< returns > If no error
Definition: snippets.dox:2
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)
GLushort pattern
Definition: glad.h:2583
virtual ~UT_SpecialTokenData()
UT_StringHolder myString
virtual UT_PathPattern * createEmptyClone() const
UT_SpecialTokenDataPtr mySpecialTokenDataPtr