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