1baa3858d3f5d128a5c8466b700098109edcad5f2repo sync// Common/Wildcard.h
2baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
3baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#ifndef __COMMON_WILDCARD_H
4baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#define __COMMON_WILDCARD_H
5baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
6baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#include "MyString.h"
7baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
8cd66d540cead3f8200b0c73bad9c276d67896c3dDavid Srbeckyint CompareFileNames(const wchar_t *s1, const wchar_t *s2) STRING_UNICODE_THROW;
9cd66d540cead3f8200b0c73bad9c276d67896c3dDavid Srbecky#ifndef USE_UNICODE_FSTRING
10cd66d540cead3f8200b0c73bad9c276d67896c3dDavid Srbecky  int CompareFileNames(const char *s1, const char *s2);
11cd66d540cead3f8200b0c73bad9c276d67896c3dDavid Srbecky#endif
12cd66d540cead3f8200b0c73bad9c276d67896c3dDavid Srbecky
13cd66d540cead3f8200b0c73bad9c276d67896c3dDavid Srbeckybool IsPath1PrefixedByPath2(const wchar_t *s1, const wchar_t *s2);
14cd66d540cead3f8200b0c73bad9c276d67896c3dDavid Srbecky
15cd66d540cead3f8200b0c73bad9c276d67896c3dDavid Srbeckyinline bool IsCharDirLimiter(wchar_t c)
16cd66d540cead3f8200b0c73bad9c276d67896c3dDavid Srbecky{
17cd66d540cead3f8200b0c73bad9c276d67896c3dDavid Srbecky  return c == WCHAR_PATH_SEPARATOR
18cd66d540cead3f8200b0c73bad9c276d67896c3dDavid Srbecky    #ifdef _WIN32
19cd66d540cead3f8200b0c73bad9c276d67896c3dDavid Srbecky      || c == L'/'
20cd66d540cead3f8200b0c73bad9c276d67896c3dDavid Srbecky    #endif
21cd66d540cead3f8200b0c73bad9c276d67896c3dDavid Srbecky    ;
22cd66d540cead3f8200b0c73bad9c276d67896c3dDavid Srbecky}
23baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
24baa3858d3f5d128a5c8466b700098109edcad5f2repo syncvoid SplitPathToParts(const UString &path, UStringVector &pathParts);
25cd66d540cead3f8200b0c73bad9c276d67896c3dDavid Srbeckyvoid SplitPathToParts_2(const UString &path, UString &dirPrefix, UString &name);
26cd66d540cead3f8200b0c73bad9c276d67896c3dDavid Srbeckyvoid SplitPathToParts_Smart(const UString &path, UString &dirPrefix, UString &name); // ignores dir delimiter at the end of (path)
27cd66d540cead3f8200b0c73bad9c276d67896c3dDavid Srbecky
28baa3858d3f5d128a5c8466b700098109edcad5f2repo syncUString ExtractDirPrefixFromPath(const UString &path);
29baa3858d3f5d128a5c8466b700098109edcad5f2repo syncUString ExtractFileNameFromPath(const UString &path);
30cd66d540cead3f8200b0c73bad9c276d67896c3dDavid Srbecky
31cd66d540cead3f8200b0c73bad9c276d67896c3dDavid Srbeckybool DoesNameContainWildcard(const UString &path);
32cd66d540cead3f8200b0c73bad9c276d67896c3dDavid Srbeckybool DoesWildcardMatchName(const UString &mask, const UString &name);
33baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
34baa3858d3f5d128a5c8466b700098109edcad5f2repo syncnamespace NWildcard {
35baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
36cd66d540cead3f8200b0c73bad9c276d67896c3dDavid Srbecky#ifdef _WIN32
37cd66d540cead3f8200b0c73bad9c276d67896c3dDavid Srbecky// returns true, if name is like "a:", "c:", ...
38cd66d540cead3f8200b0c73bad9c276d67896c3dDavid Srbeckybool IsDriveColonName(const wchar_t *s);
39cd66d540cead3f8200b0c73bad9c276d67896c3dDavid Srbecky#endif
40cd66d540cead3f8200b0c73bad9c276d67896c3dDavid Srbecky
41cd66d540cead3f8200b0c73bad9c276d67896c3dDavid Srbecky
42baa3858d3f5d128a5c8466b700098109edcad5f2repo syncstruct CItem
43baa3858d3f5d128a5c8466b700098109edcad5f2repo sync{
44baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  UStringVector PathParts;
45baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  bool Recursive;
46baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  bool ForFile;
47baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  bool ForDir;
48cd66d540cead3f8200b0c73bad9c276d67896c3dDavid Srbecky  bool WildcardMatching;
49cd66d540cead3f8200b0c73bad9c276d67896c3dDavid Srbecky
50cd66d540cead3f8200b0c73bad9c276d67896c3dDavid Srbecky  #ifdef _WIN32
51cd66d540cead3f8200b0c73bad9c276d67896c3dDavid Srbecky  bool IsDriveItem() const
52cd66d540cead3f8200b0c73bad9c276d67896c3dDavid Srbecky  {
53cd66d540cead3f8200b0c73bad9c276d67896c3dDavid Srbecky    return PathParts.Size() == 1 && !ForFile && ForDir && IsDriveColonName(PathParts[0]);
54cd66d540cead3f8200b0c73bad9c276d67896c3dDavid Srbecky  }
55cd66d540cead3f8200b0c73bad9c276d67896c3dDavid Srbecky  #endif
56cd66d540cead3f8200b0c73bad9c276d67896c3dDavid Srbecky
57cd66d540cead3f8200b0c73bad9c276d67896c3dDavid Srbecky  // CItem(): WildcardMatching(true) {}
58cd66d540cead3f8200b0c73bad9c276d67896c3dDavid Srbecky
59cd66d540cead3f8200b0c73bad9c276d67896c3dDavid Srbecky  bool AreAllAllowed() const;
60baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  bool CheckPath(const UStringVector &pathParts, bool isFile) const;
61baa3858d3f5d128a5c8466b700098109edcad5f2repo sync};
62baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
63baa3858d3f5d128a5c8466b700098109edcad5f2repo syncclass CCensorNode
64baa3858d3f5d128a5c8466b700098109edcad5f2repo sync{
65baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  CCensorNode *Parent;
66cd66d540cead3f8200b0c73bad9c276d67896c3dDavid Srbecky
67baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  bool CheckPathCurrent(bool include, const UStringVector &pathParts, bool isFile) const;
68baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  void AddItemSimple(bool include, CItem &item);
69cd66d540cead3f8200b0c73bad9c276d67896c3dDavid Srbecky  bool CheckPathVect(const UStringVector &pathParts, bool isFile, bool &include) const;
70baa3858d3f5d128a5c8466b700098109edcad5f2repo syncpublic:
71baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  CCensorNode(): Parent(0) { };
72baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  CCensorNode(const UString &name, CCensorNode *parent): Name(name), Parent(parent) { };
73cd66d540cead3f8200b0c73bad9c276d67896c3dDavid Srbecky
74cd66d540cead3f8200b0c73bad9c276d67896c3dDavid Srbecky  UString Name; // wildcard is not allowed here
75baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  CObjectVector<CCensorNode> SubNodes;
76baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  CObjectVector<CItem> IncludeItems;
77baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  CObjectVector<CItem> ExcludeItems;
78baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
79cd66d540cead3f8200b0c73bad9c276d67896c3dDavid Srbecky  bool AreAllAllowed() const;
80cd66d540cead3f8200b0c73bad9c276d67896c3dDavid Srbecky
81baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  int FindSubNode(const UString &path) const;
82baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
83baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  void AddItem(bool include, CItem &item);
84cd66d540cead3f8200b0c73bad9c276d67896c3dDavid Srbecky  void AddItem(bool include, const UString &path, bool recursive, bool forFile, bool forDir, bool wildcardMatching);
85cd66d540cead3f8200b0c73bad9c276d67896c3dDavid Srbecky  void AddItem2(bool include, const UString &path, bool recursive, bool wildcardMatching);
86baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
87baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  bool NeedCheckSubDirs() const;
88baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  bool AreThereIncludeItems() const;
89baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
90cd66d540cead3f8200b0c73bad9c276d67896c3dDavid Srbecky  bool CheckPath2(bool isAltStream, const UString &path, bool isFile, bool &include) const;
91cd66d540cead3f8200b0c73bad9c276d67896c3dDavid Srbecky  bool CheckPath(bool isAltStream, const UString &path, bool isFile) const;
92baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
93baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  bool CheckPathToRoot(bool include, UStringVector &pathParts, bool isFile) const;
94baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  // bool CheckPathToRoot(const UString &path, bool isFile, bool include) const;
95baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  void ExtendExclude(const CCensorNode &fromNodes);
96baa3858d3f5d128a5c8466b700098109edcad5f2repo sync};
97baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
98baa3858d3f5d128a5c8466b700098109edcad5f2repo syncstruct CPair
99baa3858d3f5d128a5c8466b700098109edcad5f2repo sync{
100baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  UString Prefix;
101baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  CCensorNode Head;
102cd66d540cead3f8200b0c73bad9c276d67896c3dDavid Srbecky
103baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  CPair(const UString &prefix): Prefix(prefix) { };
104baa3858d3f5d128a5c8466b700098109edcad5f2repo sync};
105baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
106cd66d540cead3f8200b0c73bad9c276d67896c3dDavid Srbeckyenum ECensorPathMode
107cd66d540cead3f8200b0c73bad9c276d67896c3dDavid Srbecky{
108cd66d540cead3f8200b0c73bad9c276d67896c3dDavid Srbecky  k_RelatPath,  // absolute prefix as Prefix, remain path in Tree
109cd66d540cead3f8200b0c73bad9c276d67896c3dDavid Srbecky  k_FullPath,   // drive prefix as Prefix, remain path in Tree
110cd66d540cead3f8200b0c73bad9c276d67896c3dDavid Srbecky  k_AbsPath     // full path in Tree
111cd66d540cead3f8200b0c73bad9c276d67896c3dDavid Srbecky};
112cd66d540cead3f8200b0c73bad9c276d67896c3dDavid Srbecky
113cd66d540cead3f8200b0c73bad9c276d67896c3dDavid Srbeckystruct CCensorPath
114cd66d540cead3f8200b0c73bad9c276d67896c3dDavid Srbecky{
115cd66d540cead3f8200b0c73bad9c276d67896c3dDavid Srbecky  UString Path;
116cd66d540cead3f8200b0c73bad9c276d67896c3dDavid Srbecky  bool Include;
117cd66d540cead3f8200b0c73bad9c276d67896c3dDavid Srbecky  bool Recursive;
118cd66d540cead3f8200b0c73bad9c276d67896c3dDavid Srbecky  bool WildcardMatching;
119cd66d540cead3f8200b0c73bad9c276d67896c3dDavid Srbecky
120cd66d540cead3f8200b0c73bad9c276d67896c3dDavid Srbecky  CCensorPath():
121cd66d540cead3f8200b0c73bad9c276d67896c3dDavid Srbecky    Include(true),
122cd66d540cead3f8200b0c73bad9c276d67896c3dDavid Srbecky    Recursive(false),
123cd66d540cead3f8200b0c73bad9c276d67896c3dDavid Srbecky    WildcardMatching(true)
124cd66d540cead3f8200b0c73bad9c276d67896c3dDavid Srbecky    {}
125cd66d540cead3f8200b0c73bad9c276d67896c3dDavid Srbecky};
126cd66d540cead3f8200b0c73bad9c276d67896c3dDavid Srbecky
127baa3858d3f5d128a5c8466b700098109edcad5f2repo syncclass CCensor
128baa3858d3f5d128a5c8466b700098109edcad5f2repo sync{
129baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  int FindPrefix(const UString &prefix) const;
130baa3858d3f5d128a5c8466b700098109edcad5f2repo syncpublic:
131baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  CObjectVector<CPair> Pairs;
132cd66d540cead3f8200b0c73bad9c276d67896c3dDavid Srbecky
133cd66d540cead3f8200b0c73bad9c276d67896c3dDavid Srbecky  CObjectVector<NWildcard::CCensorPath> CensorPaths;
134cd66d540cead3f8200b0c73bad9c276d67896c3dDavid Srbecky
135baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  bool AllAreRelative() const
136baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    { return (Pairs.Size() == 1 && Pairs.Front().Prefix.IsEmpty()); }
137cd66d540cead3f8200b0c73bad9c276d67896c3dDavid Srbecky
138cd66d540cead3f8200b0c73bad9c276d67896c3dDavid Srbecky  void AddItem(ECensorPathMode pathMode, bool include, const UString &path, bool recursive, bool wildcardMatching);
139cd66d540cead3f8200b0c73bad9c276d67896c3dDavid Srbecky  bool CheckPath(bool isAltStream, const UString &path, bool isFile) const;
140baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  void ExtendExclude();
141cd66d540cead3f8200b0c73bad9c276d67896c3dDavid Srbecky
142cd66d540cead3f8200b0c73bad9c276d67896c3dDavid Srbecky  void AddPathsToCensor(NWildcard::ECensorPathMode censorPathMode);
143cd66d540cead3f8200b0c73bad9c276d67896c3dDavid Srbecky  void AddPreItem(bool include, const UString &path, bool recursive, bool wildcardMatching);
144cd66d540cead3f8200b0c73bad9c276d67896c3dDavid Srbecky  void AddPreItem(const UString &path)
145cd66d540cead3f8200b0c73bad9c276d67896c3dDavid Srbecky  {
146cd66d540cead3f8200b0c73bad9c276d67896c3dDavid Srbecky    AddPreItem(true, path, false, false);
147cd66d540cead3f8200b0c73bad9c276d67896c3dDavid Srbecky  }
148cd66d540cead3f8200b0c73bad9c276d67896c3dDavid Srbecky  void AddPreItem_Wildcard()
149cd66d540cead3f8200b0c73bad9c276d67896c3dDavid Srbecky  {
150cd66d540cead3f8200b0c73bad9c276d67896c3dDavid Srbecky    AddPreItem(true, L"*", false, true);
151cd66d540cead3f8200b0c73bad9c276d67896c3dDavid Srbecky  }
152baa3858d3f5d128a5c8466b700098109edcad5f2repo sync};
153baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
154cd66d540cead3f8200b0c73bad9c276d67896c3dDavid Srbecky
155baa3858d3f5d128a5c8466b700098109edcad5f2repo sync}
156baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
157baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#endif
158