1 // Windows/FileFind.h
2 
3 #ifndef __WINDOWS_FILE_FIND_H
4 #define __WINDOWS_FILE_FIND_H
5 
6 #include "../Common/MyString.h"
7 #include "Defs.h"
8 
9 namespace NWindows {
10 namespace NFile {
11 namespace NFind {
12 
13 namespace NAttributes
14 {
IsReadOnly(DWORD attrib)15   inline bool IsReadOnly(DWORD attrib) { return (attrib & FILE_ATTRIBUTE_READONLY) != 0; }
IsHidden(DWORD attrib)16   inline bool IsHidden(DWORD attrib) { return (attrib & FILE_ATTRIBUTE_HIDDEN) != 0; }
IsSystem(DWORD attrib)17   inline bool IsSystem(DWORD attrib) { return (attrib & FILE_ATTRIBUTE_SYSTEM) != 0; }
IsDir(DWORD attrib)18   inline bool IsDir(DWORD attrib) { return (attrib & FILE_ATTRIBUTE_DIRECTORY) != 0; }
IsArchived(DWORD attrib)19   inline bool IsArchived(DWORD attrib) { return (attrib & FILE_ATTRIBUTE_ARCHIVE) != 0; }
IsCompressed(DWORD attrib)20   inline bool IsCompressed(DWORD attrib) { return (attrib & FILE_ATTRIBUTE_COMPRESSED) != 0; }
IsEncrypted(DWORD attrib)21   inline bool IsEncrypted(DWORD attrib) { return (attrib & FILE_ATTRIBUTE_ENCRYPTED) != 0; }
22 }
23 
24 class CFileInfoBase
25 {
MatchesMask(UINT32 mask)26   bool MatchesMask(UINT32 mask) const { return ((Attrib & mask) != 0); }
27 public:
28   UInt64 Size;
29   FILETIME CTime;
30   FILETIME ATime;
31   FILETIME MTime;
32   DWORD Attrib;
33   bool IsAltStream;
34   bool IsDevice;
35 
36   /*
37   #ifdef UNDER_CE
38   DWORD ObjectID;
39   #else
40   UINT32 ReparseTag;
41   #endif
42   */
43 
CFileInfoBase()44   CFileInfoBase() { Clear(); }
45   void Clear() throw();
46 
SetAsDir()47   void SetAsDir() { Attrib = FILE_ATTRIBUTE_DIRECTORY; }
48 
IsArchived()49   bool IsArchived() const { return MatchesMask(FILE_ATTRIBUTE_ARCHIVE); }
IsCompressed()50   bool IsCompressed() const { return MatchesMask(FILE_ATTRIBUTE_COMPRESSED); }
IsDir()51   bool IsDir() const { return MatchesMask(FILE_ATTRIBUTE_DIRECTORY); }
IsEncrypted()52   bool IsEncrypted() const { return MatchesMask(FILE_ATTRIBUTE_ENCRYPTED); }
IsHidden()53   bool IsHidden() const { return MatchesMask(FILE_ATTRIBUTE_HIDDEN); }
IsNormal()54   bool IsNormal() const { return MatchesMask(FILE_ATTRIBUTE_NORMAL); }
IsOffline()55   bool IsOffline() const { return MatchesMask(FILE_ATTRIBUTE_OFFLINE); }
IsReadOnly()56   bool IsReadOnly() const { return MatchesMask(FILE_ATTRIBUTE_READONLY); }
HasReparsePoint()57   bool HasReparsePoint() const { return MatchesMask(FILE_ATTRIBUTE_REPARSE_POINT); }
IsSparse()58   bool IsSparse() const { return MatchesMask(FILE_ATTRIBUTE_SPARSE_FILE); }
IsSystem()59   bool IsSystem() const { return MatchesMask(FILE_ATTRIBUTE_SYSTEM); }
IsTemporary()60   bool IsTemporary() const { return MatchesMask(FILE_ATTRIBUTE_TEMPORARY); }
61 };
62 
63 struct CFileInfo: public CFileInfoBase
64 {
65   FString Name;
66   #if defined(_WIN32) && !defined(UNDER_CE)
67   // FString ShortName;
68   #endif
69 
70   bool IsDots() const throw();
71   bool Find(CFSTR wildcard);
72 };
73 
74 class CFindFileBase
75 {
76 protected:
77   HANDLE _handle;
78 public:
IsHandleAllocated()79   bool IsHandleAllocated() const { return _handle != INVALID_HANDLE_VALUE; }
CFindFileBase()80   CFindFileBase(): _handle(INVALID_HANDLE_VALUE) {}
~CFindFileBase()81   ~CFindFileBase() { Close(); }
82   bool Close() throw();
83 };
84 
85 class CFindFile: public CFindFileBase
86 {
87 public:
88   bool FindFirst(CFSTR wildcard, CFileInfo &fileInfo);
89   bool FindNext(CFileInfo &fileInfo);
90 };
91 
92 #if defined(_WIN32) && !defined(UNDER_CE)
93 
94 struct CStreamInfo
95 {
96   UString Name;
97   UInt64 Size;
98 
99   UString GetReducedName() const;
100   bool IsMainStream() const throw();
101 };
102 
103 class CFindStream: public CFindFileBase
104 {
105 public:
106   bool FindFirst(CFSTR filePath, CStreamInfo &streamInfo);
107   bool FindNext(CStreamInfo &streamInfo);
108 };
109 
110 class CStreamEnumerator
111 {
112   CFindStream _find;
113   FString _filePath;
114 
115   bool NextAny(CFileInfo &fileInfo);
116 public:
CStreamEnumerator(const FString & filePath)117   CStreamEnumerator(const FString &filePath): _filePath(filePath) {}
118   bool Next(CStreamInfo &streamInfo, bool &found);
119 };
120 
121 #endif
122 
123 bool DoesFileExist(CFSTR name);
124 bool DoesDirExist(CFSTR name);
125 bool DoesFileOrDirExist(CFSTR name);
126 
127 class CEnumerator
128 {
129   CFindFile _findFile;
130   FString _wildcard;
131 
132   bool NextAny(CFileInfo &fileInfo);
133 public:
CEnumerator(const FString & wildcard)134   CEnumerator(const FString &wildcard): _wildcard(wildcard) {}
135   bool Next(CFileInfo &fileInfo);
136   bool Next(CFileInfo &fileInfo, bool &found);
137 };
138 
139 class CFindChangeNotification
140 {
141   HANDLE _handle;
142 public:
HANDLE()143   operator HANDLE () { return _handle; }
IsHandleAllocated()144   bool IsHandleAllocated() const { return _handle != INVALID_HANDLE_VALUE && _handle != 0; }
CFindChangeNotification()145   CFindChangeNotification(): _handle(INVALID_HANDLE_VALUE) {}
~CFindChangeNotification()146   ~CFindChangeNotification() { Close(); }
147   bool Close() throw();
148   HANDLE FindFirst(CFSTR pathName, bool watchSubtree, DWORD notifyFilter);
FindNext()149   bool FindNext() { return BOOLToBool(::FindNextChangeNotification(_handle)); }
150 };
151 
152 #ifndef UNDER_CE
153 bool MyGetLogicalDriveStrings(CObjectVector<FString> &driveStrings);
154 #endif
155 
156 }}}
157 
158 #endif
159