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