FileFind.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. // Windows/FileFind.h
  2. #ifndef __WINDOWS_FILEFIND_H
  3. #define __WINDOWS_FILEFIND_H
  4. #include "../Common/MyString.h"
  5. #include "../Common/Types.h"
  6. #include "FileName.h"
  7. #include "Defs.h"
  8. namespace NWindows {
  9. namespace NFile {
  10. namespace NFind {
  11. namespace NAttributes
  12. {
  13. inline bool IsReadOnly(DWORD attributes) { return (attributes & FILE_ATTRIBUTE_READONLY) != 0; }
  14. inline bool IsHidden(DWORD attributes) { return (attributes & FILE_ATTRIBUTE_HIDDEN) != 0; }
  15. inline bool IsSystem(DWORD attributes) { return (attributes & FILE_ATTRIBUTE_SYSTEM) != 0; }
  16. inline bool IsDirectory(DWORD attributes) { return (attributes & FILE_ATTRIBUTE_DIRECTORY) != 0; }
  17. inline bool IsArchived(DWORD attributes) { return (attributes & FILE_ATTRIBUTE_ARCHIVE) != 0; }
  18. inline bool IsCompressed(DWORD attributes) { return (attributes & FILE_ATTRIBUTE_COMPRESSED) != 0; }
  19. inline bool IsEncrypted(DWORD attributes) { return (attributes & FILE_ATTRIBUTE_ENCRYPTED) != 0; }
  20. }
  21. class CFileInfoBase
  22. {
  23. bool MatchesMask(UINT32 mask) const { return ((Attributes & mask) != 0); }
  24. public:
  25. DWORD Attributes;
  26. FILETIME CreationTime;
  27. FILETIME LastAccessTime;
  28. FILETIME LastWriteTime;
  29. UInt64 Size;
  30. #ifndef _WIN32_WCE
  31. UINT32 ReparseTag;
  32. #else
  33. DWORD ObjectID;
  34. #endif
  35. bool IsArchived() const { return MatchesMask(FILE_ATTRIBUTE_ARCHIVE); }
  36. bool IsCompressed() const { return MatchesMask(FILE_ATTRIBUTE_COMPRESSED); }
  37. bool IsDirectory() const { return MatchesMask(FILE_ATTRIBUTE_DIRECTORY); }
  38. bool IsEncrypted() const { return MatchesMask(FILE_ATTRIBUTE_ENCRYPTED); }
  39. bool IsHidden() const { return MatchesMask(FILE_ATTRIBUTE_HIDDEN); }
  40. bool IsNormal() const { return MatchesMask(FILE_ATTRIBUTE_NORMAL); }
  41. bool IsOffline() const { return MatchesMask(FILE_ATTRIBUTE_OFFLINE); }
  42. bool IsReadOnly() const { return MatchesMask(FILE_ATTRIBUTE_READONLY); }
  43. bool HasReparsePoint() const { return MatchesMask(FILE_ATTRIBUTE_REPARSE_POINT); }
  44. bool IsSparse() const { return MatchesMask(FILE_ATTRIBUTE_SPARSE_FILE); }
  45. bool IsSystem() const { return MatchesMask(FILE_ATTRIBUTE_SYSTEM); }
  46. bool IsTemporary() const { return MatchesMask(FILE_ATTRIBUTE_TEMPORARY); }
  47. };
  48. class CFileInfo: public CFileInfoBase
  49. {
  50. public:
  51. CSysString Name;
  52. bool IsDots() const;
  53. };
  54. #ifdef _UNICODE
  55. typedef CFileInfo CFileInfoW;
  56. #else
  57. class CFileInfoW: public CFileInfoBase
  58. {
  59. public:
  60. UString Name;
  61. bool IsDots() const;
  62. };
  63. #endif
  64. class CFindFile
  65. {
  66. friend class CEnumerator;
  67. HANDLE _handle;
  68. public:
  69. bool IsHandleAllocated() const { return _handle != INVALID_HANDLE_VALUE; }
  70. CFindFile(): _handle(INVALID_HANDLE_VALUE) {}
  71. ~CFindFile() { Close(); }
  72. bool FindFirst(LPCTSTR wildcard, CFileInfo &fileInfo);
  73. bool FindNext(CFileInfo &fileInfo);
  74. #ifndef _UNICODE
  75. bool FindFirst(LPCWSTR wildcard, CFileInfoW &fileInfo);
  76. bool FindNext(CFileInfoW &fileInfo);
  77. #endif
  78. bool Close();
  79. };
  80. bool FindFile(LPCTSTR wildcard, CFileInfo &fileInfo);
  81. bool DoesFileExist(LPCTSTR name);
  82. #ifndef _UNICODE
  83. bool FindFile(LPCWSTR wildcard, CFileInfoW &fileInfo);
  84. bool DoesFileExist(LPCWSTR name);
  85. #endif
  86. class CEnumerator
  87. {
  88. CFindFile _findFile;
  89. CSysString _wildcard;
  90. bool NextAny(CFileInfo &fileInfo);
  91. public:
  92. CEnumerator(): _wildcard(NName::kAnyStringWildcard) {}
  93. CEnumerator(const CSysString &wildcard): _wildcard(wildcard) {}
  94. bool Next(CFileInfo &fileInfo);
  95. bool Next(CFileInfo &fileInfo, bool &found);
  96. };
  97. #ifdef _UNICODE
  98. typedef CEnumerator CEnumeratorW;
  99. #else
  100. class CEnumeratorW
  101. {
  102. CFindFile _findFile;
  103. UString _wildcard;
  104. bool NextAny(CFileInfoW &fileInfo);
  105. public:
  106. CEnumeratorW(): _wildcard(NName::kAnyStringWildcard) {}
  107. CEnumeratorW(const UString &wildcard): _wildcard(wildcard) {}
  108. bool Next(CFileInfoW &fileInfo);
  109. bool Next(CFileInfoW &fileInfo, bool &found);
  110. };
  111. #endif
  112. class CFindChangeNotification
  113. {
  114. HANDLE _handle;
  115. public:
  116. operator HANDLE () { return _handle; }
  117. bool IsHandleAllocated() const { return _handle != INVALID_HANDLE_VALUE && _handle != 0; }
  118. CFindChangeNotification(): _handle(INVALID_HANDLE_VALUE) {}
  119. ~CFindChangeNotification() { Close(); }
  120. bool Close();
  121. HANDLE FindFirst(LPCTSTR pathName, bool watchSubtree, DWORD notifyFilter);
  122. #ifndef _UNICODE
  123. HANDLE FindFirst(LPCWSTR pathName, bool watchSubtree, DWORD notifyFilter);
  124. #endif
  125. bool FindNext() { return BOOLToBool(::FindNextChangeNotification(_handle)); }
  126. };
  127. #ifndef _WIN32_WCE
  128. bool MyGetLogicalDriveStrings(CSysStringVector &driveStrings);
  129. #ifndef _UNICODE
  130. bool MyGetLogicalDriveStrings(UStringVector &driveStrings);
  131. #endif
  132. #endif
  133. }}}
  134. #endif