1// Common/Wildcard.h
2
3#ifndef __COMMON_WILDCARD_H
4#define __COMMON_WILDCARD_H
5
6#include "MyString.h"
7
8int 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
13bool IsPath1PrefixedByPath2(const wchar_t *s1, const wchar_t *s2);
14
15void SplitPathToParts(const UString &path, UStringVector &pathParts);
16void SplitPathToParts_2(const UString &path, UString &dirPrefix, UString &name);
17void SplitPathToParts_Smart(const UString &path, UString &dirPrefix, UString &name); // ignores dir delimiter at the end of (path)
18
19UString ExtractDirPrefixFromPath(const UString &path);
20UString ExtractFileNameFromPath(const UString &path);
21
22bool DoesNameContainWildcard(const UString &path);
23bool DoesWildcardMatchName(const UString &mask, const UString &name);
24
25namespace NWildcard {
26
27#ifdef _WIN32
28// returns true, if name is like "a:", "c:", ...
29bool IsDriveColonName(const wchar_t *s);
30unsigned GetNumPrefixParts_if_DrivePath(UStringVector &pathParts);
31#endif
32
33struct CItem
34{
35  UStringVector PathParts;
36  bool Recursive;
37  bool ForFile;
38  bool ForDir;
39  bool WildcardMatching;
40
41  #ifdef _WIN32
42  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
54class CCensorNode
55{
56  CCensorNode *Parent;
57
58  bool CheckPathCurrent(bool include, const UStringVector &pathParts, bool isFile) const;
59  void AddItemSimple(bool include, CItem &item);
60public:
61  bool CheckPathVect(const UStringVector &pathParts, bool isFile, bool &include) const;
62
63  CCensorNode(): Parent(0) { };
64  CCensorNode(const UString &name, CCensorNode *parent): Name(name), Parent(parent) { };
65
66  UString Name; // WIN32 doesn't support wildcards in file names
67  CObjectVector<CCensorNode> SubNodes;
68  CObjectVector<CItem> IncludeItems;
69  CObjectVector<CItem> ExcludeItems;
70
71  bool AreAllAllowed() const;
72
73  int FindSubNode(const UString &path) const;
74
75  void AddItem(bool include, CItem &item, int ignoreWildcardIndex = -1);
76  void AddItem(bool include, const UString &path, bool recursive, bool forFile, bool forDir, bool wildcardMatching);
77  void AddItem2(bool include, const UString &path, bool recursive, bool wildcardMatching);
78
79  bool NeedCheckSubDirs() const;
80  bool AreThereIncludeItems() const;
81
82  // bool CheckPath2(bool isAltStream, const UString &path, bool isFile, bool &include) const;
83  // bool CheckPath(bool isAltStream, const UString &path, bool isFile) const;
84
85  bool CheckPathToRoot(bool include, UStringVector &pathParts, bool isFile) const;
86  // bool CheckPathToRoot(const UString &path, bool isFile, bool include) const;
87  void ExtendExclude(const CCensorNode &fromNodes);
88};
89
90struct CPair
91{
92  UString Prefix;
93  CCensorNode Head;
94
95  CPair(const UString &prefix): Prefix(prefix) { };
96};
97
98enum ECensorPathMode
99{
100  k_RelatPath,  // absolute prefix as Prefix, remain path in Tree
101  k_FullPath,   // drive prefix as Prefix, remain path in Tree
102  k_AbsPath     // full path in Tree
103};
104
105struct CCensorPath
106{
107  UString Path;
108  bool Include;
109  bool Recursive;
110  bool WildcardMatching;
111
112  CCensorPath():
113    Include(true),
114    Recursive(false),
115    WildcardMatching(true)
116    {}
117};
118
119class CCensor
120{
121  int FindPrefix(const UString &prefix) const;
122public:
123  CObjectVector<CPair> Pairs;
124
125  CObjectVector<NWildcard::CCensorPath> CensorPaths;
126
127  bool AllAreRelative() const
128    { return (Pairs.Size() == 1 && Pairs.Front().Prefix.IsEmpty()); }
129
130  void AddItem(ECensorPathMode pathMode, bool include, const UString &path, bool recursive, bool wildcardMatching);
131  // bool CheckPath(bool isAltStream, const UString &path, bool isFile) const;
132  void ExtendExclude();
133
134  void AddPathsToCensor(NWildcard::ECensorPathMode censorPathMode);
135  void AddPreItem(bool include, const UString &path, bool recursive, bool wildcardMatching);
136  void AddPreItem(const UString &path)
137  {
138    AddPreItem(true, path, false, false);
139  }
140  void AddPreItem_Wildcard()
141  {
142    AddPreItem(true, L"*", false, true);
143  }
144};
145
146
147}
148
149#endif
150