FileQueue.cpp
资源名称:FTP总集.rar [点击查看]
上传用户:tjfeida
上传日期:2013-03-10
资源大小:1917k
文件大小:12k
源码类别:
Ftp客户端
开发平台:
Visual C++
- // FileQueue.cpp: implementation of the CFileQueue class.
- //
- //////////////////////////////////////////////////////////////////////
- /*********************************************
- **该文件是属于WolfFTP工程中的。如果有什么问题
- **请联系
- ** tablejiang@21cn.com
- **或者访问
- ** http://wolfftp.51.net
- **以得到最新的支持。
- *********************************************/
- #include "stdafx.h"
- #include "QuickFTP.h"
- #include "FileQueue.h"
- #ifdef _DEBUG
- #undef THIS_FILE
- static char THIS_FILE[]=__FILE__;
- #define new DEBUG_NEW
- #endif
- //////////////////////////////////////////////////////////////////////
- // Construction/Destruction
- //////////////////////////////////////////////////////////////////////
- CFileQueue::CFileQueue()
- {
- InitializeCriticalSection( &m_cs );
- m_pFileTransmitQueue = NULL ;
- m_bLetRun = true ;
- //m_SaveFileName[0] = 0 ;
- //strcpy( m_SaveFileName , "FileQueue.QFQ" ) ;
- m_hParentWnd = NULL ;
- }
- CFileQueue::~CFileQueue()
- {
- DeleteAllFileQueue( ) ;
- DeleteCriticalSection( &m_cs );
- }
- /****************************************************
- ** @Description
- **
- **
- ** @Parameter
- **
- **
- ** @Return:
- ** @Author: JHM
- ** e-mail: tablejiang@21cn.com
- ** @Date: 2001 3 26
- ****************************************************/
- BOOL CFileQueue::AddItem(FTPFILEINFO *pFileInfo)
- {
- EnterCriticalSection( &m_cs ) ;
- if( pFileInfo == NULL )
- return true ;
- FTPFILEINFO* pFileItem ;
- FTPFILEINFO* pCurItem ;
- FTPFILEINFO* pPreItem ;
- BOOL bFind = false ;
- pFileItem = new FTPFILEINFO ;
- memcpy( pFileItem , pFileInfo , sizeof( FTPFILEINFO ) ) ;
- if( pFileItem->state == FILE_STATE_RUNNING )
- pFileItem->state = FILE_STATE_READY ;
- pFileItem->pNext = NULL ;
- if( m_pFileTransmitQueue == NULL )
- {
- m_pFileTransmitQueue = pFileItem ;
- SaveQueue( ) ;
- LeaveCriticalSection( &m_cs ) ;
- ::PostMessage( m_hParentWnd , REFRESH_WND_MSG , 0 , 0 ) ;
- return true ;
- }
- pCurItem = m_pFileTransmitQueue ;
- pPreItem = m_pFileTransmitQueue ;
- while( pCurItem != NULL )
- {
- if( strcmp( pFileInfo->site.host , pCurItem->site.host ) == 0 )
- {
- if( strcmp( pFileInfo->remotefilename , pCurItem->remotefilename ) == 0 )
- {
- if( strcmp( pFileInfo->remotepath , pCurItem->remotepath ) == 0 )
- {
- if( pFileInfo->bfileput == pCurItem->bfileput )
- {
- if( pFileInfo->bIsDirectory == pCurItem->bIsDirectory )
- {
- if(strcmp( pFileInfo->site.user , pFileInfo->site.user ) == 0 )
- {
- bFind = true ;
- break ;
- }
- }
- }
- }
- }
- }
- pPreItem = pCurItem ;
- pCurItem = pCurItem->pNext ;
- }
- if( bFind )
- {
- delete pFileItem ;
- return true ;
- }
- else
- pPreItem->pNext = pFileItem ;
- SaveQueue( ) ;
- LeaveCriticalSection( &m_cs ) ;
- if( m_hParentWnd )
- ::PostMessage( m_hParentWnd , REFRESH_WND_MSG , 0 , 0 ) ;
- return true ;
- }
- /****************************************************
- ** @Description
- **
- **
- ** @Parameter
- **
- **
- ** @Return:
- ** @Author: JHM
- ** e-mail: tablejiang@21cn.com
- ** @Date: 2001 3 26
- ****************************************************/
- BOOL CFileQueue::DeleteItem(FTPFILEINFO *pFileInfo)
- {
- FTPFILEINFO* pFront ;
- FTPFILEINFO* pCurrent ;
- BOOL bFind = false ;
- EnterCriticalSection( &m_cs ) ;
- if( m_pFileTransmitQueue == NULL )
- {
- LeaveCriticalSection( &m_cs ) ;
- return true ;
- }
- pCurrent = m_pFileTransmitQueue ;
- pFront = m_pFileTransmitQueue ;
- while( pCurrent != NULL )
- {
- if( FtpFileCmp( pFileInfo , pCurrent ) )
- {
- bFind = true ;
- break ;
- }
- pFront = pCurrent ;
- pCurrent = pCurrent->pNext ;
- }
- if( bFind == false )
- {
- LeaveCriticalSection( &m_cs ) ;
- return false ;
- }
- if( pCurrent == m_pFileTransmitQueue )
- {
- m_pFileTransmitQueue = pCurrent->pNext ;
- }
- else
- {
- pFront->pNext = pCurrent->pNext ;
- }
- delete pCurrent ;
- SaveQueue( ) ;
- LeaveCriticalSection( &m_cs ) ;
- if( m_hParentWnd )
- ::PostMessage( m_hParentWnd , REFRESH_WND_MSG , 0 , 0 ) ;
- return true ;
- }
- /****************************************************
- ** @Description
- **
- **
- ** @Parameter
- **
- **
- ** @Return:
- ** @Author: JHM
- ** e-mail: tablejiang@21cn.com
- ** @Date: 2001 3 26
- ****************************************************/
- BOOL CFileQueue::IsInFileQueue(FTPFILEINFO *pFileInfo)
- {
- FTPFILEINFO* pFront ;
- FTPFILEINFO* pCurrent ;
- BOOL bFind = false ;
- EnterCriticalSection( &m_cs ) ;
- if( m_pFileTransmitQueue == NULL )
- {
- LeaveCriticalSection( &m_cs ) ;
- return false ;
- }
- pCurrent = m_pFileTransmitQueue ;
- pFront = m_pFileTransmitQueue ;
- while( pCurrent != NULL )
- {
- if( FtpFileCmp( pFileInfo , pCurrent ) )
- {
- bFind = true ;
- break ;
- }
- pFront = pCurrent ;
- pCurrent = pCurrent->pNext ;
- }
- LeaveCriticalSection( &m_cs ) ;
- return bFind ;
- }
- /****************************************************
- ** @Description
- **
- **
- ** @Parameter
- **
- **
- ** @Return:
- ** @Author: JHM
- ** e-mail: tablejiang@21cn.com
- ** @Date: 2001 3 26
- ****************************************************/
- BOOL CFileQueue::DeleteAllFileQueue()
- {
- FTPFILEINFO* pCurrent ;
- EnterCriticalSection( &m_cs ) ;
- if( m_pFileTransmitQueue== NULL)
- {
- LeaveCriticalSection( &m_cs ) ;
- return true ;
- }
- pCurrent = m_pFileTransmitQueue ;
- while( m_pFileTransmitQueue != NULL)
- {
- m_pFileTransmitQueue = pCurrent->pNext ;
- delete pCurrent ;
- pCurrent = m_pFileTransmitQueue ;
- }
- m_pFileTransmitQueue = NULL ;
- LeaveCriticalSection( &m_cs ) ;
- if( m_hParentWnd )
- ::PostMessage( m_hParentWnd , REFRESH_WND_MSG , 0 , 0 ) ;
- return true ;
- }
- /****************************************************
- ** @Description
- **
- **
- ** @Parameter
- **
- **
- ** @Return:
- ** @Author: JHM
- ** e-mail: tablejiang@21cn.com
- ** @Date: 2001 3 26
- ****************************************************/
- BOOL CFileQueue::IsEmpty()
- {
- EnterCriticalSection( &m_cs ) ;
- if( m_pFileTransmitQueue == NULL )
- {
- LeaveCriticalSection( &m_cs ) ;
- return true ;
- }
- else
- {
- LeaveCriticalSection( &m_cs ) ;
- return false ;
- }
- }
- /****************************************************
- ** @Description
- **
- **
- ** @Parameter
- **
- **
- ** @Return:
- ** @Author: JHM
- ** e-mail: tablejiang@21cn.com
- ** @Date: 2001 3 26
- ****************************************************/
- FTPFILEINFO* CFileQueue::GetNextItem()
- {
- if( IsEmpty( ) )
- return NULL ;
- EnterCriticalSection( &m_cs ) ;
- FTPFILEINFO* pCur ;
- pCur = m_pFileTransmitQueue ;
- while( pCur != NULL )
- {
- if( pCur->state == FILE_STATE_READY )
- break ;
- pCur = pCur->pNext ;
- }
- LeaveCriticalSection( &m_cs ) ;
- return pCur ;
- }
- /****************************************************
- ** @Description
- **
- **
- ** @Parameter
- **
- **
- ** @Return:
- ** @Author: JHM
- ** e-mail: tablejiang@21cn.com
- ** @Date: 2001 3 26
- ****************************************************/
- BOOL CFileQueue::SaveQueue()
- {
- CFile file ;
- if( !file.Open( m_SaveFileName , CFile::modeCreate|CFile::modeWrite ) )
- return false ;
- EnterCriticalSection( &m_cs ) ;
- FTPFILEINFO* pCur ;
- pCur = m_pFileTransmitQueue ;
- try
- {
- while( pCur != NULL )
- {
- file.Write( pCur , sizeof( FTPFILEINFO ) ) ;
- pCur = pCur->pNext ;
- }
- }
- catch(...)
- {
- TRACE( "file error!" ) ;
- }
- LeaveCriticalSection( &m_cs ) ;
- file.Close() ;
- return true ;
- }
- /****************************************************
- ** @Description
- **
- **
- ** @Parameter
- **
- **
- ** @Return:
- ** @Author: JHM
- ** e-mail: tablejiang@21cn.com
- ** @Date: 2001 3 26
- ****************************************************/
- BOOL CFileQueue::SetItemState(int iState , FTPFILEINFO* pFileInfo )
- {
- FTPFILEINFO* pFront ;
- FTPFILEINFO* pCurrent ;
- BOOL bFind = false ;
- EnterCriticalSection( &m_cs ) ;
- if( m_pFileTransmitQueue == NULL )
- {
- LeaveCriticalSection( &m_cs ) ;
- return false ;
- }
- pCurrent = m_pFileTransmitQueue ;
- pFront = m_pFileTransmitQueue ;
- while( pCurrent != NULL )
- {
- if( FtpFileCmp( pFileInfo , pCurrent ) )
- {
- pCurrent->state = iState ;
- bFind = true ;
- break ;
- }
- pFront = pCurrent ;
- pCurrent = pCurrent->pNext ;
- }
- if( bFind == false )
- {
- LeaveCriticalSection( &m_cs ) ;
- return false ;
- }
- SaveQueue( ) ;
- LeaveCriticalSection( &m_cs ) ;
- if( m_hParentWnd )
- ::PostMessage( m_hParentWnd , REFRESH_WND_MSG , 0 , 0 ) ;
- return true ;
- }
- /****************************************************
- ** @Description
- **
- **
- ** @Parameter
- **
- **
- ** @Return:
- ** @Author: JHM
- ** e-mail: tablejiang@21cn.com
- ** @Date: 2001 3 26
- ****************************************************/
- void CFileQueue::LockTheFileList()
- {
- EnterCriticalSection( &m_cs ) ;
- }
- /****************************************************
- ** @Description
- **
- **
- ** @Parameter
- **
- **
- ** @Return:
- ** @Author: JHM
- ** e-mail: tablejiang@21cn.com
- ** @Date: 2001 3 26
- ****************************************************/
- void CFileQueue::UnLockTheFileList()
- {
- LeaveCriticalSection( &m_cs ) ;
- }
- /****************************************************
- ** @Description
- **
- **
- ** @Parameter
- **
- **
- ** @Return:
- ** @Author: JHM
- ** e-mail: tablejiang@21cn.com
- ** @Date: 2001 3 26
- ****************************************************/
- FTPFILEINFO* CFileQueue::GetSameSiteFile(FTPFILEINFO *pFile)
- {
- EnterCriticalSection( &m_cs ) ;
- FTPFILEINFO* pCur ;
- if( m_pFileTransmitQueue == NULL )
- {
- LeaveCriticalSection( &m_cs ) ;
- return NULL ;
- }
- pCur = m_pFileTransmitQueue ;
- while( pCur != NULL )
- {
- if( pCur->state == FILE_STATE_READY )
- {
- if( strcmp( pCur->site.host , pFile->site.host ) == 0 )
- {
- if( strcmp( pCur->site.user , pFile->site.user ) == 0 )
- {
- if( strcmp( pCur->site.pass , pFile->site.pass ) == 0 )
- {
- break ;
- }
- }
- }
- }
- pCur = pCur->pNext ;
- }
- LeaveCriticalSection( &m_cs ) ;
- return pCur ;
- }
- /****************************************************
- ** @Description
- **
- **
- ** @Parameter
- **
- **
- ** @Return:
- ** @Author: JHM
- ** e-mail: tablejiang@21cn.com
- ** @Date: 2001 3 26
- ****************************************************/
- BOOL CFileQueue::LoadQueue()
- {
- CFile file ;
- if( !file.Open( m_SaveFileName , CFile::modeRead ) )
- return false ;
- EnterCriticalSection( &m_cs ) ;
- FTPFILEINFO FtpFile ;
- UINT iRead ;
- try
- {
- while( iRead )
- {
- iRead = file.Read( &FtpFile , sizeof( FTPFILEINFO ) ) ;
- if( iRead == sizeof( FTPFILEINFO ) )
- {
- AddItem( &FtpFile ) ;
- }
- }
- }
- catch(...)
- {
- TRACE( "file error!" ) ;
- }
- LeaveCriticalSection( &m_cs ) ;
- file.Close() ;
- return true ;
- }
- /****************************************************
- ** @Description
- **
- **
- ** @Parameter
- **
- **
- ** @Return:
- ** @Author: JHM
- ** e-mail: tablejiang@21cn.com
- ** @Date: 2001 3 26
- ****************************************************/
- BOOL CFileQueue::SetSaveFileName(LPCTSTR filename)
- {
- strcpy( m_SaveFileName , filename ) ;
- DeleteAllFileQueue( ) ;
- LoadQueue( ) ;
- return true ;
- }
- /****************************************************
- ** @Description
- **
- **
- ** @Parameter
- **
- **
- ** @Return:
- ** @Author: JHM
- ** e-mail: tablejiang@21cn.com
- ** @Date: 2001 3 26
- ****************************************************/
- BOOL CFileQueue::FtpFileCmp(FTPFILEINFO *pItem1, FTPFILEINFO *pItem2)
- {
- //if( pItem1->bfileput == pItem2->bfileput )
- //{
- if( pItem1->filesize == pItem2->filesize )
- {
- if( strcmp( pItem1->site.host , pItem2->site.host ) == 0 )
- {
- if( strcmp( pItem1->remotefilename , pItem2->remotefilename ) == 0 )
- {
- if( strcmp( pItem1->remotepath , pItem2->remotepath ) == 0 )
- {
- if( strcmp( pItem1->site.user , pItem2->site.user ) == 0 )
- {
- return true ;
- }
- }
- }
- }
- }
- //}
- return false ;
- }