home *** CD-ROM | disk | FTP | other *** search
/ Team Palmtops 7 / Palmtops_numero07.iso / WinCE / SDKWindowsCE / HandHeldPCPro30 / sdk.exe / Jupiter SDK / data1.cab / MFC / src / filefind.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1999-02-19  |  7.8 KB  |  377 lines

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