Home | History | Annotate | Download | only in Windows
      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 {
     15   inline bool IsReadOnly(DWORD attrib) { return (attrib & FILE_ATTRIBUTE_READONLY) != 0; }
     16   inline bool IsHidden(DWORD attrib) { return (attrib & FILE_ATTRIBUTE_HIDDEN) != 0; }
     17   inline bool IsSystem(DWORD attrib) { return (attrib & FILE_ATTRIBUTE_SYSTEM) != 0; }
     18   inline bool IsDir(DWORD attrib) { return (attrib & FILE_ATTRIBUTE_DIRECTORY) != 0; }
     19   inline bool IsArchived(DWORD attrib) { return (attrib & FILE_ATTRIBUTE_ARCHIVE) != 0; }
     20   inline bool IsCompressed(DWORD attrib) { return (attrib & FILE_ATTRIBUTE_COMPRESSED) != 0; }
     21   inline bool IsEncrypted(DWORD attrib) { return (attrib & FILE_ATTRIBUTE_ENCRYPTED) != 0; }
     22 }
     23 
     24 class CFileInfoBase
     25 {
     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 
     44   CFileInfoBase() { ClearBase(); }
     45   void ClearBase() throw();
     46 
     47   void SetAsDir() { Attrib = FILE_ATTRIBUTE_DIRECTORY; }
     48 
     49   bool IsArchived() const { return MatchesMask(FILE_ATTRIBUTE_ARCHIVE); }
     50   bool IsCompressed() const { return MatchesMask(FILE_ATTRIBUTE_COMPRESSED); }
     51   bool IsDir() const { return MatchesMask(FILE_ATTRIBUTE_DIRECTORY); }
     52   bool IsEncrypted() const { return MatchesMask(FILE_ATTRIBUTE_ENCRYPTED); }
     53   bool IsHidden() const { return MatchesMask(FILE_ATTRIBUTE_HIDDEN); }
     54   bool IsNormal() const { return MatchesMask(FILE_ATTRIBUTE_NORMAL); }
     55   bool IsOffline() const { return MatchesMask(FILE_ATTRIBUTE_OFFLINE); }
     56   bool IsReadOnly() const { return MatchesMask(FILE_ATTRIBUTE_READONLY); }
     57   bool HasReparsePoint() const { return MatchesMask(FILE_ATTRIBUTE_REPARSE_POINT); }
     58   bool IsSparse() const { return MatchesMask(FILE_ATTRIBUTE_SPARSE_FILE); }
     59   bool IsSystem() const { return MatchesMask(FILE_ATTRIBUTE_SYSTEM); }
     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:
     79   bool IsHandleAllocated() const { return _handle != INVALID_HANDLE_VALUE; }
     80   CFindFileBase(): _handle(INVALID_HANDLE_VALUE) {}
     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:
    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   CEnumerator(const FString &wildcard): _wildcard(wildcard) {}
    138   bool Next(CFileInfo &fileInfo);
    139   bool Next(CFileInfo &fileInfo, bool &found);
    140 };
    141 
    142 class CFindChangeNotification
    143 {
    144   HANDLE _handle;
    145 public:
    146   operator HANDLE () { return _handle; }
    147   bool IsHandleAllocated() const { return _handle != INVALID_HANDLE_VALUE && _handle != 0; }
    148   CFindChangeNotification(): _handle(INVALID_HANDLE_VALUE) {}
    149   ~CFindChangeNotification() { Close(); }
    150   bool Close() throw();
    151   HANDLE FindFirst(CFSTR pathName, bool watchSubtree, DWORD notifyFilter);
    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