• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Common/Wildcard.h
2 
3 #ifndef __COMMON_WILDCARD_H
4 #define __COMMON_WILDCARD_H
5 
6 #include "MyString.h"
7 
8 int CompareFileNames(const wchar_t *s1, const wchar_t *s2) STRING_UNICODE_THROW;
9 #ifndef USE_UNICODE_FSTRING
10   int CompareFileNames(const char *s1, const char *s2);
11 #endif
12 
13 bool IsPath1PrefixedByPath2(const wchar_t *s1, const wchar_t *s2);
14 
15 void SplitPathToParts(const UString &path, UStringVector &pathParts);
16 void SplitPathToParts_2(const UString &path, UString &dirPrefix, UString &name);
17 void SplitPathToParts_Smart(const UString &path, UString &dirPrefix, UString &name); // ignores dir delimiter at the end of (path)
18 
19 UString ExtractDirPrefixFromPath(const UString &path);
20 UString ExtractFileNameFromPath(const UString &path);
21 
22 bool DoesNameContainWildcard(const UString &path);
23 bool DoesWildcardMatchName(const UString &mask, const UString &name);
24 
25 namespace NWildcard {
26 
27 #ifdef _WIN32
28 // returns true, if name is like "a:", "c:", ...
29 bool IsDriveColonName(const wchar_t *s);
30 unsigned GetNumPrefixParts_if_DrivePath(UStringVector &pathParts);
31 #endif
32 
33 struct CItem
34 {
35   UStringVector PathParts;
36   bool Recursive;
37   bool ForFile;
38   bool ForDir;
39   bool WildcardMatching;
40 
41   #ifdef _WIN32
IsDriveItemCItem42   bool IsDriveItem() const
43   {
44     return PathParts.Size() == 1 && !ForFile && ForDir && IsDriveColonName(PathParts[0]);
45   }
46   #endif
47 
48   // CItem(): WildcardMatching(true) {}
49 
50   bool AreAllAllowed() const;
51   bool CheckPath(const UStringVector &pathParts, bool isFile) const;
52 };
53 
54 
55 
56 const Byte kMark_FileOrDir = 0;
57 const Byte kMark_StrictFile = 1;
58 const Byte kMark_StrictFile_IfWildcard = 2;
59 
60 struct CCensorPathProps
61 {
62   bool Recursive;
63   bool WildcardMatching;
64   Byte MarkMode;
65 
CCensorPathPropsCCensorPathProps66   CCensorPathProps():
67       Recursive(false),
68       WildcardMatching(true),
69       MarkMode(kMark_FileOrDir)
70       {}
71 };
72 
73 
74 class CCensorNode  MY_UNCOPYABLE
75 {
76   CCensorNode *Parent;
77 
78   bool CheckPathCurrent(bool include, const UStringVector &pathParts, bool isFile) const;
79   void AddItemSimple(bool include, CItem &item);
80 public:
81   // bool ExcludeDirItems;
82 
CCensorNode()83   CCensorNode():
84       Parent(NULL)
85       // , ExcludeDirItems(false)
86       {};
87 
CCensorNode(const UString & name,CCensorNode * parent)88   CCensorNode(const UString &name, CCensorNode *parent):
89       Parent(parent)
90       // , ExcludeDirItems(false)
91       , Name(name)
92       {}
93 
94   UString Name; // WIN32 doesn't support wildcards in file names
95   CObjectVector<CCensorNode> SubNodes;
96   CObjectVector<CItem> IncludeItems;
97   CObjectVector<CItem> ExcludeItems;
98 
Find_SubNode_Or_Add_New(const UString & name)99   CCensorNode &Find_SubNode_Or_Add_New(const UString &name)
100   {
101     int i = FindSubNode(name);
102     if (i >= 0)
103       return SubNodes[(unsigned)i];
104     // return SubNodes.Add(CCensorNode(name, this));
105     CCensorNode &node = SubNodes.AddNew();
106     node.Parent = this;
107     node.Name = name;
108     return node;
109   }
110 
111   bool AreAllAllowed() const;
112 
113   int FindSubNode(const UString &path) const;
114 
115   void AddItem(bool include, CItem &item, int ignoreWildcardIndex = -1);
116   // void AddItem(bool include, const UString &path, const CCensorPathProps &props);
Add_Wildcard()117   void Add_Wildcard()
118   {
119     CItem item;
120     item.PathParts.Add(L"*");
121     item.Recursive = false;
122     item.ForFile = true;
123     item.ForDir = true;
124     item.WildcardMatching = true;
125     AddItem(
126         true // include
127         , item);
128   }
129 
130   // NeedCheckSubDirs() returns true, if there are IncludeItems rules that affect items in subdirs
131   bool NeedCheckSubDirs() const;
132   bool AreThereIncludeItems() const;
133 
134   /*
135   CheckPathVect() doesn't check path in Parent CCensorNode
136   so use CheckPathVect() for root CCensorNode
137   OUT:
138     returns (true) && (include = false) - file in exlude list
139     returns (true) && (include = true)  - file in include list and is not in exlude list
140     returns (false)  - file is not in (include/exlude) list
141   */
142   bool CheckPathVect(const UStringVector &pathParts, bool isFile, bool &include) const;
143 
144   // bool CheckPath2(bool isAltStream, const UString &path, bool isFile, bool &include) const;
145   // bool CheckPath(bool isAltStream, const UString &path, bool isFile) const;
146 
147   // CheckPathToRoot_Change() changes pathParts !!!
148   bool CheckPathToRoot_Change(bool include, UStringVector &pathParts, bool isFile) const;
149   bool CheckPathToRoot(bool include, const UStringVector &pathParts, bool isFile) const;
150 
151   // bool CheckPathToRoot(const UString &path, bool isFile, bool include) const;
152   void ExtendExclude(const CCensorNode &fromNodes);
153 };
154 
155 
156 struct CPair  MY_UNCOPYABLE
157 {
158   UString Prefix;
159   CCensorNode Head;
160 
161   // CPair(const UString &prefix): Prefix(prefix) { };
162 };
163 
164 
165 enum ECensorPathMode
166 {
167   k_RelatPath,  // absolute prefix as Prefix, remain path in Tree
168   k_FullPath,   // drive prefix as Prefix, remain path in Tree
169   k_AbsPath     // full path in Tree
170 };
171 
172 
173 struct CCensorPath
174 {
175   UString Path;
176   bool Include;
177   CCensorPathProps Props;
178 
CCensorPathCCensorPath179   CCensorPath():
180       Include(true)
181       {}
182 };
183 
184 
185 class CCensor  MY_UNCOPYABLE
186 {
187   int FindPairForPrefix(const UString &prefix) const;
188 public:
189   CObjectVector<CPair> Pairs;
190 
191   bool ExcludeDirItems;
192   bool ExcludeFileItems;
193 
CCensor()194   CCensor():
195       ExcludeDirItems(false),
196       ExcludeFileItems(false)
197       {}
198 
199   CObjectVector<NWildcard::CCensorPath> CensorPaths;
200 
AllAreRelative()201   bool AllAreRelative() const
202     { return (Pairs.Size() == 1 && Pairs.Front().Prefix.IsEmpty()); }
203 
204   void AddItem(ECensorPathMode pathMode, bool include, const UString &path, const CCensorPathProps &props);
205   // bool CheckPath(bool isAltStream, const UString &path, bool isFile) const;
206   void ExtendExclude();
207 
208   void AddPathsToCensor(NWildcard::ECensorPathMode censorPathMode);
209   void AddPreItem(bool include, const UString &path, const CCensorPathProps &props);
210 
AddPreItem_NoWildcard(const UString & path)211   void AddPreItem_NoWildcard(const UString &path)
212   {
213     CCensorPathProps props;
214     props.WildcardMatching = false;
215     AddPreItem(
216         true,  // include
217         path, props);
218   }
AddPreItem_Wildcard()219   void AddPreItem_Wildcard()
220   {
221     CCensorPathProps props;
222     // props.WildcardMatching = true;
223     AddPreItem(
224         true,  // include
225         UString("*"), props);
226   }
227 };
228 
229 }
230 
231 #endif
232