home *** CD-ROM | disk | FTP | other *** search
/ Supercompiler 1997 / SUPERCOMPILER97.iso / MS_VC.50 / VC / MFC / SRC / FILEFIND.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1996-11-01  |  7.5 KB  |  368 lines

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