| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363 |
- // This is a part of the Microsoft Foundation Classes C++ library.
- // Copyright (C) 1992-1998 Microsoft Corporation
- // All rights reserved.
- //
- // This source code is only intended as a supplement to the
- // Microsoft Foundation Classes Reference and related
- // electronic documentation provided with the library.
- // See these sources for detailed information regarding the
- // Microsoft Foundation Classes product.
- #include "stdafx.h"
- ////////////////////////////////////////////////////////////////////////////
- // CFileFind implementation
- CFileFind::CFileFind()
- {
- m_pFoundInfo = NULL;
- m_pNextInfo = NULL;
- m_hContext = NULL;
- m_chDirSeparator = '\\';
- }
- CFileFind::~CFileFind()
- {
- Close();
- }
- void CFileFind::Close()
- {
- if (m_pFoundInfo != NULL)
- {
- delete m_pFoundInfo;
- m_pFoundInfo = NULL;
- }
- if (m_pNextInfo != NULL)
- {
- delete m_pNextInfo;
- m_pNextInfo = NULL;
- }
- if (m_hContext != NULL && m_hContext != INVALID_HANDLE_VALUE)
- {
- CloseContext();
- m_hContext = NULL;
- }
- }
- void CFileFind::CloseContext()
- {
- ::FindClose(m_hContext);
- return;
- }
- BOOL CFileFind::FindFile(LPCTSTR pstrName /* = NULL */,
- DWORD dwUnused /* = 0 */)
- {
- UNUSED_ALWAYS(dwUnused);
- Close();
- m_pNextInfo = new WIN32_FIND_DATA;
- m_bGotLast = FALSE;
- if (pstrName == NULL)
- pstrName = _T("*.*");
- lstrcpy(((WIN32_FIND_DATA*) m_pNextInfo)->cFileName, pstrName);
- m_hContext = ::FindFirstFile(pstrName, (WIN32_FIND_DATA*) m_pNextInfo);
- if (m_hContext == INVALID_HANDLE_VALUE)
- {
- DWORD dwTemp = ::GetLastError();
- Close();
- ::SetLastError(dwTemp);
- return FALSE;
- }
- LPTSTR pstrRoot = m_strRoot.GetBufferSetLength(_MAX_PATH);
- LPCTSTR pstr = _tfullpath(pstrRoot, pstrName, _MAX_PATH);
- // passed name isn't a valid path but was found by the API
- ASSERT(pstr != NULL);
- if (pstr == NULL)
- {
- m_strRoot.ReleaseBuffer(-1);
- Close();
- ::SetLastError(ERROR_INVALID_NAME);
- return FALSE;
- }
- else
- {
- // find the last forward or backward whack
- LPTSTR pstrBack = _tcsrchr(pstrRoot, '\\');
- LPTSTR pstrFront = _tcsrchr(pstrRoot, '/');
- if (pstrFront != NULL || pstrBack != NULL)
- {
- if (pstrFront == NULL)
- pstrFront = pstrRoot;
- if (pstrBack == NULL)
- pstrBack = pstrRoot;
- // from the start to the last whack is the root
- if (pstrFront >= pstrBack)
- *pstrFront = '\0';
- else
- *pstrBack = '\0';
- }
- m_strRoot.ReleaseBuffer(-1);
- }
- return TRUE;
- }
- BOOL CFileFind::MatchesMask(DWORD dwMask) const
- {
- ASSERT(m_hContext != NULL);
- ASSERT_VALID(this);
- if (m_pFoundInfo != NULL)
- return (!!(((LPWIN32_FIND_DATA) m_pFoundInfo)->dwFileAttributes & dwMask));
- else
- return FALSE;
- }
- BOOL CFileFind::GetLastAccessTime(FILETIME* pTimeStamp) const
- {
- ASSERT(m_hContext != NULL);
- ASSERT(pTimeStamp != NULL);
- ASSERT_VALID(this);
- if (m_pFoundInfo != NULL && pTimeStamp != NULL)
- {
- *pTimeStamp = ((LPWIN32_FIND_DATA) m_pFoundInfo)->ftLastAccessTime;
- return TRUE;
- }
- else
- return FALSE;
- }
- BOOL CFileFind::GetLastWriteTime(FILETIME* pTimeStamp) const
- {
- ASSERT(m_hContext != NULL);
- ASSERT(pTimeStamp != NULL);
- ASSERT_VALID(this);
- if (m_pFoundInfo != NULL && pTimeStamp != NULL)
- {
- *pTimeStamp = ((LPWIN32_FIND_DATA) m_pFoundInfo)->ftLastWriteTime;
- return TRUE;
- }
- else
- return FALSE;
- }
- BOOL CFileFind::GetCreationTime(FILETIME* pTimeStamp) const
- {
- ASSERT(m_hContext != NULL);
- ASSERT_VALID(this);
- if (m_pFoundInfo != NULL && pTimeStamp != NULL)
- {
- *pTimeStamp = ((LPWIN32_FIND_DATA) m_pFoundInfo)->ftCreationTime;
- return TRUE;
- }
- else
- return FALSE;
- }
- BOOL CFileFind::GetLastAccessTime(CTime& refTime) const
- {
- ASSERT(m_hContext != NULL);
- ASSERT_VALID(this);
- if (m_pFoundInfo != NULL)
- {
- refTime = CTime(((LPWIN32_FIND_DATA) m_pFoundInfo)->ftLastAccessTime);
- return TRUE;
- }
- else
- return FALSE;
- }
- BOOL CFileFind::GetLastWriteTime(CTime& refTime) const
- {
- ASSERT(m_hContext != NULL);
- ASSERT_VALID(this);
- if (m_pFoundInfo != NULL)
- {
- refTime = CTime(((LPWIN32_FIND_DATA) m_pFoundInfo)->ftLastWriteTime);
- return TRUE;
- }
- else
- return FALSE;
- }
- BOOL CFileFind::GetCreationTime(CTime& refTime) const
- {
- ASSERT(m_hContext != NULL);
- ASSERT_VALID(this);
- if (m_pFoundInfo != NULL)
- {
- refTime = CTime(((LPWIN32_FIND_DATA) m_pFoundInfo)->ftCreationTime);
- return TRUE;
- }
- else
- return FALSE;
- }
- BOOL CFileFind::IsDots() const
- {
- ASSERT(m_hContext != NULL);
- ASSERT_VALID(this);
- // return TRUE if the file name is "." or ".." and
- // the file is a directory
- BOOL bResult = FALSE;
- if (m_pFoundInfo != NULL && IsDirectory())
- {
- LPWIN32_FIND_DATA pFindData = (LPWIN32_FIND_DATA) m_pFoundInfo;
- if (pFindData->cFileName[0] == '.')
- {
- if (pFindData->cFileName[1] == '\0' ||
- (pFindData->cFileName[1] == '.' &&
- pFindData->cFileName[2] == '\0'))
- {
- bResult = TRUE;
- }
- }
- }
- return bResult;
- }
- BOOL CFileFind::FindNextFile()
- {
- ASSERT(m_hContext != NULL);
- if (m_hContext == NULL)
- return FALSE;
- if (m_pFoundInfo == NULL)
- m_pFoundInfo = new WIN32_FIND_DATA;
- ASSERT_VALID(this);
- void* pTemp = m_pFoundInfo;
- m_pFoundInfo = m_pNextInfo;
- m_pNextInfo = pTemp;
- return ::FindNextFile(m_hContext, (LPWIN32_FIND_DATA) m_pNextInfo);
- }
- CString CFileFind::GetFileURL() const
- {
- ASSERT(m_hContext != NULL);
- ASSERT_VALID(this);
- CString strResult("file://");
- strResult += GetFilePath();
- return strResult;
- }
- CString CFileFind::GetRoot() const
- {
- ASSERT(m_hContext != NULL);
- ASSERT_VALID(this);
- return m_strRoot;
- }
- CString CFileFind::GetFilePath() const
- {
- ASSERT(m_hContext != NULL);
- ASSERT_VALID(this);
- CString strResult = m_strRoot;
- if (strResult[strResult.GetLength()-1] != '\\' &&
- strResult[strResult.GetLength()-1] != '/')
- strResult += m_chDirSeparator;
- strResult += GetFileName();
- return strResult;
- }
- CString CFileFind::GetFileTitle() const
- {
- ASSERT(m_hContext != NULL);
- ASSERT_VALID(this);
- CString strFullName = GetFileName();
- CString strResult;
- _tsplitpath(strFullName, NULL, NULL, strResult.GetBuffer(_MAX_PATH), NULL);
- strResult.ReleaseBuffer();
- return strResult;
- }
- CString CFileFind::GetFileName() const
- {
- ASSERT(m_hContext != NULL);
- ASSERT_VALID(this);
- CString ret;
- if (m_pFoundInfo != NULL)
- ret = ((LPWIN32_FIND_DATA) m_pFoundInfo)->cFileName;
- return ret;
- }
- DWORD CFileFind::GetLength() const
- {
- ASSERT(m_hContext != NULL);
- ASSERT_VALID(this);
- if (m_pFoundInfo != NULL)
- return ((LPWIN32_FIND_DATA) m_pFoundInfo)->nFileSizeLow;
- else
- return 0;
- }
- #if defined(_X86_) || defined(_ALPHA_)
- __int64 CFileFind::GetLength64() const
- {
- ASSERT(m_hContext != NULL);
- ASSERT_VALID(this);
- if (m_pFoundInfo != NULL)
- return ((LPWIN32_FIND_DATA) m_pFoundInfo)->nFileSizeLow +
- (((LPWIN32_FIND_DATA) m_pFoundInfo)->nFileSizeHigh << 32);
- else
- return 0;
- }
- #endif
- #ifdef _DEBUG
- void CFileFind::Dump(CDumpContext& dc) const
- {
- CObject::Dump(dc);
- dc << "\nm_hContext = " << (UINT) m_hContext;
- }
- void CFileFind::AssertValid() const
- {
- // if you trip the ASSERT in the else side, you've called
- // a Get() function without having done at least one
- // FindNext() call
- if (m_hContext == NULL)
- ASSERT(m_pFoundInfo == NULL && m_pNextInfo == NULL);
- else
- ASSERT(m_pFoundInfo != NULL && m_pNextInfo != NULL);
- }
- #endif
- #ifdef AFX_INIT_SEG
- #pragma code_seg(AFX_INIT_SEG)
- #endif
- IMPLEMENT_DYNAMIC(CFileFind, CObject)
|