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