filefind.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. // This is a part of the Microsoft Foundation Classes C++ library.
  2. // Copyright (C) 1992-1998 Microsoft Corporation
  3. // All rights reserved.
  4. //
  5. // This source code is only intended as a supplement to the
  6. // Microsoft Foundation Classes Reference and related
  7. // electronic documentation provided with the library.
  8. // See these sources for detailed information regarding the
  9. // Microsoft Foundation Classes product.
  10. #include "stdafx.h"
  11. ////////////////////////////////////////////////////////////////////////////
  12. // CFileFind implementation
  13. CFileFind::CFileFind()
  14. {
  15. m_pFoundInfo = NULL;
  16. m_pNextInfo = NULL;
  17. m_hContext = NULL;
  18. m_chDirSeparator = '\\';
  19. }
  20. CFileFind::~CFileFind()
  21. {
  22. Close();
  23. }
  24. void CFileFind::Close()
  25. {
  26. if (m_pFoundInfo != NULL)
  27. {
  28. delete m_pFoundInfo;
  29. m_pFoundInfo = NULL;
  30. }
  31. if (m_pNextInfo != NULL)
  32. {
  33. delete m_pNextInfo;
  34. m_pNextInfo = NULL;
  35. }
  36. if (m_hContext != NULL && m_hContext != INVALID_HANDLE_VALUE)
  37. {
  38. CloseContext();
  39. m_hContext = NULL;
  40. }
  41. }
  42. void CFileFind::CloseContext()
  43. {
  44. ::FindClose(m_hContext);
  45. return;
  46. }
  47. BOOL CFileFind::FindFile(LPCTSTR pstrName /* = NULL */,
  48. DWORD dwUnused /* = 0 */)
  49. {
  50. UNUSED_ALWAYS(dwUnused);
  51. Close();
  52. m_pNextInfo = new WIN32_FIND_DATA;
  53. m_bGotLast = FALSE;
  54. if (pstrName == NULL)
  55. pstrName = _T("*.*");
  56. lstrcpy(((WIN32_FIND_DATA*) m_pNextInfo)->cFileName, pstrName);
  57. m_hContext = ::FindFirstFile(pstrName, (WIN32_FIND_DATA*) m_pNextInfo);
  58. if (m_hContext == INVALID_HANDLE_VALUE)
  59. {
  60. DWORD dwTemp = ::GetLastError();
  61. Close();
  62. ::SetLastError(dwTemp);
  63. return FALSE;
  64. }
  65. LPTSTR pstrRoot = m_strRoot.GetBufferSetLength(_MAX_PATH);
  66. LPCTSTR pstr = _tfullpath(pstrRoot, pstrName, _MAX_PATH);
  67. // passed name isn't a valid path but was found by the API
  68. ASSERT(pstr != NULL);
  69. if (pstr == NULL)
  70. {
  71. m_strRoot.ReleaseBuffer(-1);
  72. Close();
  73. ::SetLastError(ERROR_INVALID_NAME);
  74. return FALSE;
  75. }
  76. else
  77. {
  78. // find the last forward or backward whack
  79. LPTSTR pstrBack = _tcsrchr(pstrRoot, '\\');
  80. LPTSTR pstrFront = _tcsrchr(pstrRoot, '/');
  81. if (pstrFront != NULL || pstrBack != NULL)
  82. {
  83. if (pstrFront == NULL)
  84. pstrFront = pstrRoot;
  85. if (pstrBack == NULL)
  86. pstrBack = pstrRoot;
  87. // from the start to the last whack is the root
  88. if (pstrFront >= pstrBack)
  89. *pstrFront = '\0';
  90. else
  91. *pstrBack = '\0';
  92. }
  93. m_strRoot.ReleaseBuffer(-1);
  94. }
  95. return TRUE;
  96. }
  97. BOOL CFileFind::MatchesMask(DWORD dwMask) const
  98. {
  99. ASSERT(m_hContext != NULL);
  100. ASSERT_VALID(this);
  101. if (m_pFoundInfo != NULL)
  102. return (!!(((LPWIN32_FIND_DATA) m_pFoundInfo)->dwFileAttributes & dwMask));
  103. else
  104. return FALSE;
  105. }
  106. BOOL CFileFind::GetLastAccessTime(FILETIME* pTimeStamp) const
  107. {
  108. ASSERT(m_hContext != NULL);
  109. ASSERT(pTimeStamp != NULL);
  110. ASSERT_VALID(this);
  111. if (m_pFoundInfo != NULL && pTimeStamp != NULL)
  112. {
  113. *pTimeStamp = ((LPWIN32_FIND_DATA) m_pFoundInfo)->ftLastAccessTime;
  114. return TRUE;
  115. }
  116. else
  117. return FALSE;
  118. }
  119. BOOL CFileFind::GetLastWriteTime(FILETIME* pTimeStamp) const
  120. {
  121. ASSERT(m_hContext != NULL);
  122. ASSERT(pTimeStamp != NULL);
  123. ASSERT_VALID(this);
  124. if (m_pFoundInfo != NULL && pTimeStamp != NULL)
  125. {
  126. *pTimeStamp = ((LPWIN32_FIND_DATA) m_pFoundInfo)->ftLastWriteTime;
  127. return TRUE;
  128. }
  129. else
  130. return FALSE;
  131. }
  132. BOOL CFileFind::GetCreationTime(FILETIME* pTimeStamp) const
  133. {
  134. ASSERT(m_hContext != NULL);
  135. ASSERT_VALID(this);
  136. if (m_pFoundInfo != NULL && pTimeStamp != NULL)
  137. {
  138. *pTimeStamp = ((LPWIN32_FIND_DATA) m_pFoundInfo)->ftCreationTime;
  139. return TRUE;
  140. }
  141. else
  142. return FALSE;
  143. }
  144. BOOL CFileFind::GetLastAccessTime(CTime& refTime) const
  145. {
  146. ASSERT(m_hContext != NULL);
  147. ASSERT_VALID(this);
  148. if (m_pFoundInfo != NULL)
  149. {
  150. refTime = CTime(((LPWIN32_FIND_DATA) m_pFoundInfo)->ftLastAccessTime);
  151. return TRUE;
  152. }
  153. else
  154. return FALSE;
  155. }
  156. BOOL CFileFind::GetLastWriteTime(CTime& refTime) const
  157. {
  158. ASSERT(m_hContext != NULL);
  159. ASSERT_VALID(this);
  160. if (m_pFoundInfo != NULL)
  161. {
  162. refTime = CTime(((LPWIN32_FIND_DATA) m_pFoundInfo)->ftLastWriteTime);
  163. return TRUE;
  164. }
  165. else
  166. return FALSE;
  167. }
  168. BOOL CFileFind::GetCreationTime(CTime& refTime) const
  169. {
  170. ASSERT(m_hContext != NULL);
  171. ASSERT_VALID(this);
  172. if (m_pFoundInfo != NULL)
  173. {
  174. refTime = CTime(((LPWIN32_FIND_DATA) m_pFoundInfo)->ftCreationTime);
  175. return TRUE;
  176. }
  177. else
  178. return FALSE;
  179. }
  180. BOOL CFileFind::IsDots() const
  181. {
  182. ASSERT(m_hContext != NULL);
  183. ASSERT_VALID(this);
  184. // return TRUE if the file name is "." or ".." and
  185. // the file is a directory
  186. BOOL bResult = FALSE;
  187. if (m_pFoundInfo != NULL && IsDirectory())
  188. {
  189. LPWIN32_FIND_DATA pFindData = (LPWIN32_FIND_DATA) m_pFoundInfo;
  190. if (pFindData->cFileName[0] == '.')
  191. {
  192. if (pFindData->cFileName[1] == '\0' ||
  193. (pFindData->cFileName[1] == '.' &&
  194. pFindData->cFileName[2] == '\0'))
  195. {
  196. bResult = TRUE;
  197. }
  198. }
  199. }
  200. return bResult;
  201. }
  202. BOOL CFileFind::FindNextFile()
  203. {
  204. ASSERT(m_hContext != NULL);
  205. if (m_hContext == NULL)
  206. return FALSE;
  207. if (m_pFoundInfo == NULL)
  208. m_pFoundInfo = new WIN32_FIND_DATA;
  209. ASSERT_VALID(this);
  210. void* pTemp = m_pFoundInfo;
  211. m_pFoundInfo = m_pNextInfo;
  212. m_pNextInfo = pTemp;
  213. return ::FindNextFile(m_hContext, (LPWIN32_FIND_DATA) m_pNextInfo);
  214. }
  215. CString CFileFind::GetFileURL() const
  216. {
  217. ASSERT(m_hContext != NULL);
  218. ASSERT_VALID(this);
  219. CString strResult("file://");
  220. strResult += GetFilePath();
  221. return strResult;
  222. }
  223. CString CFileFind::GetRoot() const
  224. {
  225. ASSERT(m_hContext != NULL);
  226. ASSERT_VALID(this);
  227. return m_strRoot;
  228. }
  229. CString CFileFind::GetFilePath() const
  230. {
  231. ASSERT(m_hContext != NULL);
  232. ASSERT_VALID(this);
  233. CString strResult = m_strRoot;
  234. if (strResult[strResult.GetLength()-1] != '\\' &&
  235. strResult[strResult.GetLength()-1] != '/')
  236. strResult += m_chDirSeparator;
  237. strResult += GetFileName();
  238. return strResult;
  239. }
  240. CString CFileFind::GetFileTitle() const
  241. {
  242. ASSERT(m_hContext != NULL);
  243. ASSERT_VALID(this);
  244. CString strFullName = GetFileName();
  245. CString strResult;
  246. _tsplitpath(strFullName, NULL, NULL, strResult.GetBuffer(_MAX_PATH), NULL);
  247. strResult.ReleaseBuffer();
  248. return strResult;
  249. }
  250. CString CFileFind::GetFileName() const
  251. {
  252. ASSERT(m_hContext != NULL);
  253. ASSERT_VALID(this);
  254. CString ret;
  255. if (m_pFoundInfo != NULL)
  256. ret = ((LPWIN32_FIND_DATA) m_pFoundInfo)->cFileName;
  257. return ret;
  258. }
  259. DWORD CFileFind::GetLength() const
  260. {
  261. ASSERT(m_hContext != NULL);
  262. ASSERT_VALID(this);
  263. if (m_pFoundInfo != NULL)
  264. return ((LPWIN32_FIND_DATA) m_pFoundInfo)->nFileSizeLow;
  265. else
  266. return 0;
  267. }
  268. #if defined(_X86_) || defined(_ALPHA_)
  269. __int64 CFileFind::GetLength64() const
  270. {
  271. ASSERT(m_hContext != NULL);
  272. ASSERT_VALID(this);
  273. if (m_pFoundInfo != NULL)
  274. return ((LPWIN32_FIND_DATA) m_pFoundInfo)->nFileSizeLow +
  275. (((LPWIN32_FIND_DATA) m_pFoundInfo)->nFileSizeHigh << 32);
  276. else
  277. return 0;
  278. }
  279. #endif
  280. #ifdef _DEBUG
  281. void CFileFind::Dump(CDumpContext& dc) const
  282. {
  283. CObject::Dump(dc);
  284. dc << "\nm_hContext = " << (UINT) m_hContext;
  285. }
  286. void CFileFind::AssertValid() const
  287. {
  288. // if you trip the ASSERT in the else side, you've called
  289. // a Get() function without having done at least one
  290. // FindNext() call
  291. if (m_hContext == NULL)
  292. ASSERT(m_pFoundInfo == NULL && m_pNextInfo == NULL);
  293. else
  294. ASSERT(m_pFoundInfo != NULL && m_pNextInfo != NULL);
  295. }
  296. #endif
  297. #ifdef AFX_INIT_SEG
  298. #pragma code_seg(AFX_INIT_SEG)
  299. #endif
  300. IMPLEMENT_DYNAMIC(CFileFind, CObject)