Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

213 lines
6.4 KiB

  1. /*******************************************************************************
  2. *
  3. * (C) COPYRIGHT MICROSOFT CORPORATION, 1998, 1999, 2000
  4. *
  5. * TITLE: FINDTHRD.H
  6. *
  7. * VERSION: 1.0
  8. *
  9. * AUTHOR: ShaunIv
  10. *
  11. * DATE: 12/4/1999
  12. *
  13. * DESCRIPTION:
  14. *
  15. *******************************************************************************/
  16. #ifndef __FINDTHRD_H_INCLUDED
  17. #define __FINDTHRD_H_INCLUDED
  18. #include <gphelper.h>
  19. class CFoundFileMessageData
  20. {
  21. private:
  22. CSimpleString m_strFilename;
  23. private:
  24. CFoundFileMessageData(void);
  25. CFoundFileMessageData( const CFoundFileMessageData & );
  26. CFoundFileMessageData &operator=( const CFoundFileMessageData & );
  27. public:
  28. CFoundFileMessageData( const CSimpleString &strFilename )
  29. : m_strFilename(strFilename)
  30. {
  31. }
  32. ~CFoundFileMessageData(void)
  33. {
  34. }
  35. CSimpleString Name(void) const
  36. {
  37. return m_strFilename;
  38. }
  39. };
  40. class CFindFilesThread
  41. {
  42. private:
  43. CSimpleString m_strDirectory;
  44. CSimpleString m_strMask;
  45. HWND m_hwndNotify;
  46. UINT m_nNotifyMessage;
  47. HANDLE m_hEventCancel;
  48. int m_nDirectoryCount;
  49. int m_nFailedFileCount;
  50. int m_nSuccessfulFileCount;
  51. int m_nMaxFailedFiles;
  52. int m_nMaxSuccessfulFiles;
  53. int m_nMaxDirectories;
  54. CImageFileFormatVerifier m_ImageFileFormatVerifier;
  55. private:
  56. CFindFilesThread(
  57. const CSimpleString &strDirectory,
  58. const CSimpleString &strMask,
  59. HWND hwndNotify,
  60. UINT nNotifyMessage,
  61. HANDLE hEventCancel,
  62. int nMaxFailedFiles,
  63. int nMaxSuccessfulFiles,
  64. int nMaxDirectories
  65. )
  66. : m_strDirectory(strDirectory),
  67. m_strMask(strMask),
  68. m_hwndNotify(hwndNotify),
  69. m_nNotifyMessage(nNotifyMessage),
  70. m_hEventCancel(NULL),
  71. m_nDirectoryCount(0),
  72. m_nFailedFileCount(0),
  73. m_nSuccessfulFileCount(0),
  74. m_nMaxFailedFiles(nMaxFailedFiles),
  75. m_nMaxSuccessfulFiles(nMaxSuccessfulFiles),
  76. m_nMaxDirectories(nMaxDirectories)
  77. {
  78. if (!DuplicateHandle( GetCurrentProcess(), hEventCancel, GetCurrentProcess(), &m_hEventCancel, 0, FALSE, DUPLICATE_SAME_ACCESS ))
  79. m_hEventCancel = NULL;
  80. }
  81. ~CFindFilesThread(void)
  82. {
  83. if (m_hEventCancel)
  84. {
  85. CloseHandle(m_hEventCancel);
  86. m_hEventCancel = NULL;
  87. }
  88. }
  89. private:
  90. static bool FoundFile( bool bIsFile, LPCTSTR pszFilename, const WIN32_FIND_DATA *, PVOID pvParam )
  91. {
  92. CFindFilesThread *pThis = reinterpret_cast<CFindFilesThread*>(pvParam);
  93. if (pThis)
  94. return pThis->FoundFile( bIsFile, pszFilename );
  95. return false;
  96. }
  97. bool FoundFile( bool bIsFile, LPCTSTR pszFilename )
  98. {
  99. WIA_PUSH_FUNCTION((TEXT("CFindFilesThread::FoundFile( %d, %s )"), bIsFile, pszFilename ));
  100. // Check to see if we've been cancelled
  101. if (m_hEventCancel)
  102. {
  103. DWORD dwRes = WaitForSingleObject(m_hEventCancel,0);
  104. if (WAIT_OBJECT_0 == dwRes)
  105. return false;
  106. }
  107. // If this is a file, and it is an image file that we can decode, package up a message and send it off
  108. if (bIsFile)
  109. {
  110. if (m_nNotifyMessage && m_hwndNotify && IsWindow(m_hwndNotify))
  111. {
  112. if (m_ImageFileFormatVerifier.IsImageFile(pszFilename))
  113. {
  114. m_nSuccessfulFileCount++;
  115. CFoundFileMessageData *pFoundFileMessageData = new CFoundFileMessageData( pszFilename );
  116. if (pFoundFileMessageData)
  117. {
  118. PostMessage( m_hwndNotify, m_nNotifyMessage, true, reinterpret_cast<LPARAM>(pFoundFileMessageData) );
  119. }
  120. }
  121. else m_nFailedFileCount++;
  122. }
  123. }
  124. else m_nDirectoryCount++;
  125. // If we've exceeded the number of failures we're allowed, stop searching
  126. if (m_nMaxFailedFiles && m_nFailedFileCount >= m_nMaxFailedFiles)
  127. {
  128. WIA_TRACE((TEXT("FailedFileCount exceeded MaxFailedFiles, bailing out")));
  129. return false;
  130. }
  131. // If we've exceeded the number of files we want to handle, stop searching
  132. if (m_nMaxSuccessfulFiles && m_nSuccessfulFileCount >= m_nMaxSuccessfulFiles)
  133. {
  134. WIA_TRACE((TEXT("m_nSuccessfulFileCount exceeded MaxSuccessfulFiles, bailing out")));
  135. return false;
  136. }
  137. // If we've exceeded the number of directories we're allowed, stop searching
  138. if (m_nMaxDirectories && m_nDirectoryCount >= m_nMaxDirectories)
  139. {
  140. WIA_TRACE((TEXT("DirectoryCount exceeded MaxDirectories, bailing out")));
  141. return false;
  142. }
  143. return true;
  144. }
  145. bool Find(void)
  146. {
  147. bool bResult = RecursiveFindFiles( m_strDirectory, m_strMask, FoundFile, this );
  148. // Tell the window we're done
  149. if (m_nNotifyMessage && m_hwndNotify && IsWindow(m_hwndNotify))
  150. {
  151. PostMessage( m_hwndNotify, m_nNotifyMessage, FALSE, FALSE );
  152. }
  153. return bResult;
  154. }
  155. static DWORD __stdcall ThreadProc( PVOID pVoid )
  156. {
  157. CFindFilesThread *pFindFilesThread = reinterpret_cast<CFindFilesThread*>(pVoid);
  158. if (pFindFilesThread)
  159. {
  160. pFindFilesThread->Find();
  161. delete pFindFilesThread;
  162. }
  163. return 0;
  164. }
  165. public:
  166. static HANDLE Find(
  167. const CSimpleString &strDirectory,
  168. const CSimpleString &strMask,
  169. HWND hwndNotify,
  170. UINT nNotifyMessage,
  171. HANDLE hEventCancel,
  172. int nMaxFailedFiles,
  173. int nMaxSuccessfulFiles,
  174. int nMaxDirectories
  175. )
  176. {
  177. HANDLE hThread = NULL;
  178. CFindFilesThread *pFindFilesThread = new CFindFilesThread( strDirectory, strMask, hwndNotify, nNotifyMessage, hEventCancel, nMaxFailedFiles, nMaxSuccessfulFiles, nMaxDirectories );
  179. if (pFindFilesThread)
  180. {
  181. DWORD dwThreadId;
  182. hThread = CreateThread( NULL, 0, ThreadProc, pFindFilesThread, 0, &dwThreadId );
  183. if (!hThread)
  184. {
  185. delete pFindFilesThread;
  186. }
  187. else
  188. {
  189. SetThreadPriority( hThread, THREAD_PRIORITY_LOWEST );
  190. }
  191. }
  192. return hThread;
  193. }
  194. };
  195. #endif //__FINDTHRD_H_INCLUDED