Fri, 16 Jan 2015 18:13:44 +0100
Integrate suggestion from review to improve consistency with existing code.
1 // Windows/FileFind.h
3 #ifndef __WINDOWS_FILEFIND_H
4 #define __WINDOWS_FILEFIND_H
6 #include "../Common/String.h"
7 #include "../Common/Types.h"
8 #include "FileName.h"
9 #include "Defs.h"
11 namespace NWindows {
12 namespace NFile {
13 namespace NFind {
15 namespace NAttributes
16 {
17 inline bool IsReadOnly(DWORD attributes) { return (attributes & FILE_ATTRIBUTE_READONLY) != 0; }
18 inline bool IsHidden(DWORD attributes) { return (attributes & FILE_ATTRIBUTE_HIDDEN) != 0; }
19 inline bool IsSystem(DWORD attributes) { return (attributes & FILE_ATTRIBUTE_SYSTEM) != 0; }
20 inline bool IsDirectory(DWORD attributes) { return (attributes & FILE_ATTRIBUTE_DIRECTORY) != 0; }
21 inline bool IsArchived(DWORD attributes) { return (attributes & FILE_ATTRIBUTE_ARCHIVE) != 0; }
22 inline bool IsCompressed(DWORD attributes) { return (attributes & FILE_ATTRIBUTE_COMPRESSED) != 0; }
23 inline bool IsEncrypted(DWORD attributes) { return (attributes & FILE_ATTRIBUTE_ENCRYPTED) != 0; }
24 }
26 class CFileInfoBase
27 {
28 bool MatchesMask(UINT32 mask) const { return ((Attributes & mask) != 0); }
29 public:
30 DWORD Attributes;
31 FILETIME CreationTime;
32 FILETIME LastAccessTime;
33 FILETIME LastWriteTime;
34 UInt64 Size;
36 #ifndef _WIN32_WCE
37 UINT32 ReparseTag;
38 #else
39 DWORD ObjectID;
40 #endif
42 bool IsArchived() const { return MatchesMask(FILE_ATTRIBUTE_ARCHIVE); }
43 bool IsCompressed() const { return MatchesMask(FILE_ATTRIBUTE_COMPRESSED); }
44 bool IsDirectory() const { return MatchesMask(FILE_ATTRIBUTE_DIRECTORY); }
45 bool IsEncrypted() const { return MatchesMask(FILE_ATTRIBUTE_ENCRYPTED); }
46 bool IsHidden() const { return MatchesMask(FILE_ATTRIBUTE_HIDDEN); }
47 bool IsNormal() const { return MatchesMask(FILE_ATTRIBUTE_NORMAL); }
48 bool IsOffline() const { return MatchesMask(FILE_ATTRIBUTE_OFFLINE); }
49 bool IsReadOnly() const { return MatchesMask(FILE_ATTRIBUTE_READONLY); }
50 bool HasReparsePoint() const { return MatchesMask(FILE_ATTRIBUTE_REPARSE_POINT); }
51 bool IsSparse() const { return MatchesMask(FILE_ATTRIBUTE_SPARSE_FILE); }
52 bool IsSystem() const { return MatchesMask(FILE_ATTRIBUTE_SYSTEM); }
53 bool IsTemporary() const { return MatchesMask(FILE_ATTRIBUTE_TEMPORARY); }
54 };
56 class CFileInfo: public CFileInfoBase
57 {
58 public:
59 CSysString Name;
60 bool IsDots() const;
61 };
63 #ifdef _UNICODE
64 typedef CFileInfo CFileInfoW;
65 #else
66 class CFileInfoW: public CFileInfoBase
67 {
68 public:
69 UString Name;
70 bool IsDots() const;
71 };
72 #endif
74 class CFindFile
75 {
76 friend class CEnumerator;
77 HANDLE _handle;
78 bool _handleAllocated;
79 public:
80 bool IsHandleAllocated() const { return _handleAllocated; }
81 CFindFile(): _handleAllocated(false) {}
82 ~CFindFile() { Close(); }
83 bool FindFirst(LPCTSTR wildcard, CFileInfo &fileInfo);
84 bool FindNext(CFileInfo &fileInfo);
85 #ifndef _UNICODE
86 bool FindFirst(LPCWSTR wildcard, CFileInfoW &fileInfo);
87 bool FindNext(CFileInfoW &fileInfo);
88 #endif
89 bool Close();
90 };
92 bool FindFile(LPCTSTR wildcard, CFileInfo &fileInfo);
94 bool DoesFileExist(LPCTSTR name);
95 #ifndef _UNICODE
96 bool FindFile(LPCWSTR wildcard, CFileInfoW &fileInfo);
97 bool DoesFileExist(LPCWSTR name);
98 #endif
100 class CEnumerator
101 {
102 CFindFile _findFile;
103 CSysString _wildcard;
104 bool NextAny(CFileInfo &fileInfo);
105 public:
106 CEnumerator(): _wildcard(NName::kAnyStringWildcard) {}
107 CEnumerator(const CSysString &wildcard): _wildcard(wildcard) {}
108 bool Next(CFileInfo &fileInfo);
109 bool Next(CFileInfo &fileInfo, bool &found);
110 };
112 #ifdef _UNICODE
113 typedef CEnumerator CEnumeratorW;
114 #else
115 class CEnumeratorW
116 {
117 CFindFile _findFile;
118 UString _wildcard;
119 bool NextAny(CFileInfoW &fileInfo);
120 public:
121 CEnumeratorW(): _wildcard(NName::kAnyStringWildcard) {}
122 CEnumeratorW(const UString &wildcard): _wildcard(wildcard) {}
123 bool Next(CFileInfoW &fileInfo);
124 bool Next(CFileInfoW &fileInfo, bool &found);
125 };
126 #endif
128 class CFindChangeNotification
129 {
130 HANDLE _handle;
131 public:
132 operator HANDLE () { return _handle; }
133 CFindChangeNotification(): _handle(INVALID_HANDLE_VALUE) {}
134 ~CFindChangeNotification() { Close(); }
135 bool Close();
136 HANDLE FindFirst(LPCTSTR pathName, bool watchSubtree, DWORD notifyFilter);
137 #ifndef _UNICODE
138 HANDLE FindFirst(LPCWSTR pathName, bool watchSubtree, DWORD notifyFilter);
139 #endif
140 bool FindNext()
141 { return BOOLToBool(::FindNextChangeNotification(_handle)); }
142 };
144 #ifndef _WIN32_WCE
145 bool MyGetLogicalDriveStrings(CSysStringVector &driveStrings);
146 #ifndef _UNICODE
147 bool MyGetLogicalDriveStrings(UStringVector &driveStrings);
148 #endif
149 #endif
151 inline bool MyGetCompressedFileSize(LPCTSTR fileName, UInt64 &size)
152 {
153 DWORD highPart;
154 DWORD lowPart = ::GetCompressedFileSize(fileName, &highPart);
155 if (lowPart == INVALID_FILE_SIZE)
156 if (::GetLastError() != NO_ERROR)
157 return false;
158 size = (UInt64(highPart) << 32) | lowPart;
159 return true;
160 }
162 inline bool MyGetCompressedFileSizeW(LPCWSTR fileName, UInt64 &size)
163 {
164 DWORD highPart;
165 DWORD lowPart = ::GetCompressedFileSizeW(fileName, &highPart);
166 if (lowPart == INVALID_FILE_SIZE)
167 if (::GetLastError() != NO_ERROR)
168 return false;
169 size = (UInt64(highPart) << 32) | lowPart;
170 return true;
171 }
173 }}}
175 #endif