FileFind.h revision baa3858d3f5d128a5c8466b700098109edcad5f2
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
11namespace NWindows {
12namespace NFile {
13namespace NFind {
14
15namespace NAttributes
16{
17  inline bool IsReadOnly(DWORD attrib) { return (attrib & FILE_ATTRIBUTE_READONLY) != 0; }
18  inline bool IsHidden(DWORD attrib) { return (attrib & FILE_ATTRIBUTE_HIDDEN) != 0; }
19  inline bool IsSystem(DWORD attrib) { return (attrib & FILE_ATTRIBUTE_SYSTEM) != 0; }
20  inline bool IsDir(DWORD attrib) { return (attrib & FILE_ATTRIBUTE_DIRECTORY) != 0; }
21  inline bool IsArchived(DWORD attrib) { return (attrib & FILE_ATTRIBUTE_ARCHIVE) != 0; }
22  inline bool IsCompressed(DWORD attrib) { return (attrib & FILE_ATTRIBUTE_COMPRESSED) != 0; }
23  inline bool IsEncrypted(DWORD attrib) { return (attrib & FILE_ATTRIBUTE_ENCRYPTED) != 0; }
24}
25
26class CFileInfoBase
27{
28  bool MatchesMask(UINT32 mask) const { return ((Attrib & mask) != 0); }
29protected:
30  void Clear();
31public:
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
47  bool IsArchived() const { return MatchesMask(FILE_ATTRIBUTE_ARCHIVE); }
48  bool IsCompressed() const { return MatchesMask(FILE_ATTRIBUTE_COMPRESSED); }
49  bool IsDir() const { return MatchesMask(FILE_ATTRIBUTE_DIRECTORY); }
50  bool IsEncrypted() const { return MatchesMask(FILE_ATTRIBUTE_ENCRYPTED); }
51  bool IsHidden() const { return MatchesMask(FILE_ATTRIBUTE_HIDDEN); }
52  bool IsNormal() const { return MatchesMask(FILE_ATTRIBUTE_NORMAL); }
53  bool IsOffline() const { return MatchesMask(FILE_ATTRIBUTE_OFFLINE); }
54  bool IsReadOnly() const { return MatchesMask(FILE_ATTRIBUTE_READONLY); }
55  bool HasReparsePoint() const { return MatchesMask(FILE_ATTRIBUTE_REPARSE_POINT); }
56  bool IsSparse() const { return MatchesMask(FILE_ATTRIBUTE_SPARSE_FILE); }
57  bool IsSystem() const { return MatchesMask(FILE_ATTRIBUTE_SYSTEM); }
58  bool IsTemporary() const { return MatchesMask(FILE_ATTRIBUTE_TEMPORARY); }
59};
60
61struct CFileInfo: public CFileInfoBase
62{
63  CSysString Name;
64
65  bool IsDots() const;
66  bool Find(LPCTSTR wildcard);
67};
68
69#ifdef _UNICODE
70typedef CFileInfo CFileInfoW;
71#else
72struct CFileInfoW: public CFileInfoBase
73{
74  UString Name;
75
76  bool IsDots() const;
77  bool Find(LPCWSTR wildcard);
78};
79#endif
80
81class CFindFile
82{
83  friend class CEnumerator;
84  HANDLE _handle;
85public:
86  bool IsHandleAllocated() const { return _handle != INVALID_HANDLE_VALUE; }
87  CFindFile(): _handle(INVALID_HANDLE_VALUE) {}
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
98bool DoesFileExist(LPCTSTR name);
99bool DoesDirExist(LPCTSTR name);
100bool DoesFileOrDirExist(LPCTSTR name);
101#ifndef _UNICODE
102bool DoesFileExist(LPCWSTR name);
103bool DoesDirExist(LPCWSTR name);
104bool DoesFileOrDirExist(LPCWSTR name);
105#endif
106
107class CEnumerator
108{
109  CFindFile _findFile;
110  CSysString _wildcard;
111  bool NextAny(CFileInfo &fileInfo);
112public:
113  CEnumerator(): _wildcard(NName::kAnyStringWildcard) {}
114  CEnumerator(const CSysString &wildcard): _wildcard(wildcard) {}
115  bool Next(CFileInfo &fileInfo);
116  bool Next(CFileInfo &fileInfo, bool &found);
117};
118
119#ifdef _UNICODE
120typedef CEnumerator CEnumeratorW;
121#else
122class CEnumeratorW
123{
124  CFindFile _findFile;
125  UString _wildcard;
126  bool NextAny(CFileInfoW &fileInfo);
127public:
128  CEnumeratorW(): _wildcard(NName::kAnyStringWildcard) {}
129  CEnumeratorW(const UString &wildcard): _wildcard(wildcard) {}
130  bool Next(CFileInfoW &fileInfo);
131  bool Next(CFileInfoW &fileInfo, bool &found);
132};
133#endif
134
135class CFindChangeNotification
136{
137  HANDLE _handle;
138public:
139  operator HANDLE () { return _handle; }
140  bool IsHandleAllocated() const { return _handle != INVALID_HANDLE_VALUE && _handle != 0; }
141  CFindChangeNotification(): _handle(INVALID_HANDLE_VALUE) {}
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
148  bool FindNext() { return BOOLToBool(::FindNextChangeNotification(_handle)); }
149};
150
151#ifndef UNDER_CE
152bool MyGetLogicalDriveStrings(CSysStringVector &driveStrings);
153#ifndef _UNICODE
154bool MyGetLogicalDriveStrings(UStringVector &driveStrings);
155#endif
156#endif
157
158}}}
159
160#endif
161
162