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