• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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() { ClearBase(); }
45   void ClearBase() 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 path);
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; // returns ":Name"
100   // UString GetReducedName2() const; // returns "Name"
101   bool IsMainStream() const throw();
102 };
103 
104 class CFindStream: public CFindFileBase
105 {
106 public:
107   bool FindFirst(CFSTR filePath, CStreamInfo &streamInfo);
108   bool FindNext(CStreamInfo &streamInfo);
109 };
110 
111 class CStreamEnumerator
112 {
113   CFindStream _find;
114   FString _filePath;
115 
116   bool NextAny(CFileInfo &fileInfo);
117 public:
CStreamEnumerator(const FString & filePath)118   CStreamEnumerator(const FString &filePath): _filePath(filePath) {}
119   bool Next(CStreamInfo &streamInfo, bool &found);
120 };
121 
122 #endif
123 
124 bool DoesFileExist(CFSTR name);
125 bool DoesDirExist(CFSTR name);
126 bool DoesFileOrDirExist(CFSTR name);
127 
128 DWORD GetFileAttrib(CFSTR path);
129 
130 class CEnumerator
131 {
132   CFindFile _findFile;
133   FString _wildcard;
134 
135   bool NextAny(CFileInfo &fileInfo);
136 public:
137   void SetDirPrefix(const FString &dirPrefix);
138   bool Next(CFileInfo &fileInfo);
139   bool Next(CFileInfo &fileInfo, bool &found);
140 };
141 
142 class CFindChangeNotification
143 {
144   HANDLE _handle;
145 public:
HANDLE()146   operator HANDLE () { return _handle; }
IsHandleAllocated()147   bool IsHandleAllocated() const { return _handle != INVALID_HANDLE_VALUE && _handle != 0; }
CFindChangeNotification()148   CFindChangeNotification(): _handle(INVALID_HANDLE_VALUE) {}
~CFindChangeNotification()149   ~CFindChangeNotification() { Close(); }
150   bool Close() throw();
151   HANDLE FindFirst(CFSTR pathName, bool watchSubtree, DWORD notifyFilter);
FindNext()152   bool FindNext() { return BOOLToBool(::FindNextChangeNotification(_handle)); }
153 };
154 
155 #ifndef UNDER_CE
156 bool MyGetLogicalDriveStrings(CObjectVector<FString> &driveStrings);
157 #endif
158 
159 }}}
160 
161 #endif
162