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