CopyFilesDlg.cpp
资源名称:DXGuide.zip [点击查看]
上传用户:wymy58
上传日期:2007-01-07
资源大小:2086k
文件大小:14k
源码类别:
DirextX编程
开发平台:
Visual C++
- // CopyFilesDlg.cpp : implementation file
- //
- #include "stdafx.h"
- #include <direct.h>
- #include "PFM.h"
- #include "CopyFilesDlg.h"
- #include "MainFrm.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- /////////////////////////////////////////////////////////////////////////////
- // CCopyFilesDlg dialog
- CCopyFilesDlg::CCopyFilesDlg(CWnd* pParent /*=NULL*/)
- : CDialog(CCopyFilesDlg::IDD, pParent)
- {
- //{{AFX_DATA_INIT(CCopyFilesDlg)
- //}}AFX_DATA_INIT
- }
- void CCopyFilesDlg::DoDataExchange(CDataExchange* pDX)
- {
- CDialog::DoDataExchange(pDX);
- //{{AFX_DATA_MAP(CCopyFilesDlg)
- DDX_Control(pDX, IDC_STATIC2, m_prompt);
- DDX_Control(pDX, IDC_STATIC1, m_percent);
- DDX_Control(pDX, IDC_PROGRESS, m_prog);
- DDX_Control(pDX, IDC_ANIMATE, m_avi);
- //}}AFX_DATA_MAP
- }
- BEGIN_MESSAGE_MAP(CCopyFilesDlg, CDialog)
- //{{AFX_MSG_MAP(CCopyFilesDlg)
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
- /////////////////////////////////////////////////////////////////////////////
- // CCopyFilesDlg message handlers
- BOOL CCopyFilesDlg::OnInitDialog()
- {
- CDialog::OnInitDialog();
- // TODO: Add extra initialization here
- m_avi.ModifyStyle(WS_VISIBLE,0);
- m_prog.ModifyStyle(WS_VISIBLE,0);
- m_prompt.SetWindowText("是否要拷贝选择的文件?");
- return TRUE; // return TRUE unless you set the focus to a control
- // EXCEPTION: OCX Property Pages should return FALSE
- }
- void CCopyFilesDlg::OnOK()
- {
- // TODO: Add extra validation here
- m_avi.ModifyStyle(0, WS_VISIBLE);
- m_prog.ModifyStyle(WS_BORDER, WS_VISIBLE);
- m_prompt.SetWindowText("");
- GetDlgItem(IDOK)->EnableWindow(FALSE);
- GetDlgItem(IDCANCEL)->EnableWindow(FALSE);
- m_avi.Open(IDR_FILECOPY);
- m_avi.Play(0, -1, -1);
- if (gCurPan)
- {
- m_flSrc = gRightPan; m_strSrc = gRightPath;
- m_flDest = gLeftPan; m_strDest = gLeftPath;
- }
- else
- {
- m_flSrc = gLeftPan; m_strSrc = gLeftPath;
- m_flDest = gRightPan; m_strDest = gRightPath;
- }
- if (m_flSrc->GetFastFileManager())
- {
- if (m_flDest->GetFastFileManager())
- CopyFF();
- else
- CopyFD();
- }
- else
- {
- if (m_flDest->GetFastFileManager())
- CopyDF();
- else
- CopyDD();
- }
- CDialog::OnOK();
- }
- void CCopyFilesDlg::SearchDirectory(LPCTSTR strPath, BOOL bCreateDest)
- {
- CString curPath, strName;
- char drive[_MAX_DRIVE];
- char dir[_MAX_DIR];
- char fname[_MAX_FNAME];
- char ext[_MAX_EXT];
- if (bCreateDest)
- {
- curPath = m_strDest + strPath;
- _mkdir(curPath);
- }
- curPath = strPath;
- curPath += "\";
- BOOL bLoop;
- char sName[MAX_PATH];
- CFileFind ff;
- bLoop = ff.FindFile(curPath+"*.*");
- while (bLoop)
- {
- bLoop = ff.FindNextFile();
- strName = ff.GetFileName();
- if (ff.IsDots())
- continue;
- if (ff.IsDirectory())
- SearchDirectory(curPath+strName, bCreateDest);
- else
- {
- GetShortPathName(m_strSrc+curPath+strName, sName,MAX_PATH);
- _splitpath(sName,drive,dir,fname,ext);
- strName = fname;
- strName += ext;
- m_map.SetAt(strName, ff.GetFilePath());
- m_dwSize += ff.GetLength();
- }
- }
- }
- void CCopyFilesDlg::UpdateProg(DWORD dwSize)
- {
- DWORD dwPct = 100*dwSize/m_dwSize;
- m_prog.SetPos(dwPct);
- CString strPct;
- strPct.Format("%d%%", dwPct);
- m_percent.SetWindowText(strPct);
- }
- void CCopyFilesDlg::CopyFF()
- {
- LPWIN32_FIND_DATA pFd;
- LVITEM lvi;
- CFastFileManager *pFfmSrc = m_flSrc->GetFastFileManager();
- CFastFileManager *pFfmDest = m_flDest->GetFastFileManager();
- m_map.RemoveAll();
- m_dwSize = 0;
- // add src list items
- int nItem = m_flSrc->GetNextItem(-1, LVNI_ALL|LVNI_SELECTED);
- while (nItem != -1)
- {
- pFd = (LPWIN32_FIND_DATA)m_flSrc->GetItemData(nItem);
- if (pFd->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
- ;
- else
- {
- m_map.SetAt(pFd->cFileName, "Src");
- m_dwSize += pFd->nFileSizeLow;
- }
- lvi.mask = LVIF_STATE;
- lvi.iItem = nItem;
- lvi.iSubItem = 0;
- lvi.stateMask = 0xFFFF;
- // get all state flags
- m_flSrc->GetItem(&lvi);
- lvi.state ^= LVIS_SELECTED;
- m_flSrc->SetItem(&lvi);
- nItem = m_flSrc->GetNextItem(nItem, LVNI_ALL|LVNI_SELECTED);
- }
- // add Dest list items
- nItem = m_flDest->GetNextItem(-1, LVNI_ALL);
- while (nItem != -1)
- {
- pFd = (LPWIN32_FIND_DATA)m_flDest->GetItemData(nItem);
- if (pFd->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
- ;
- else
- {
- m_map.SetAt(pFd->cFileName, "Dest");
- m_dwSize += pFd->nFileSizeLow;
- }
- nItem = m_flDest->GetNextItem(nItem, LVNI_ALL);
- }
- CString strDest = m_strDest.Left(
- m_strDest.ReverseFind('\')+1);
- CFile fPf;
- FILE_ENTRY *pFe;
- PACK_FILE_HEAD pfh;
- if (!fPf.Open(strDest+"$$$$$$$$.$$$", CFile::modeCreate|CFile::modeWrite|CFile::typeBinary))
- {
- MessageBox("包文件创建失败!","PackFile Maker", MB_OK|MB_ICONSTOP);
- return;
- }
- pfh.dwEntryCount = m_map.GetCount();
- memcpy(&pfh.dwFlag, "DXGF", 4);
- pfh.dwHeadSize = sizeof(PACK_FILE_HEAD);
- pfh.wMajorVersion = 0x1;
- pfh.wMinorVersion = 0x0;
- strcpy(pfh.szDescription, "This file is created by DirectX Guide");
- pFe = NULL;
- pFe = new FILE_ENTRY[pfh.dwEntryCount+1];
- if (pFe == NULL)
- {
- MessageBox("内存申请失败!", "PackFile Maker", MB_OK|MB_ICONSTOP);
- return;
- }
- DWORD dwCnt = 0;
- CString key, val;
- POSITION ps;
- ps = m_map.GetStartPosition();
- while (ps)
- {
- m_map.GetNextAssoc(ps, key, val);
- strcpy(pFe[dwCnt].szFileName, key);
- dwCnt++;
- }
- qsort(pFe, pfh.dwEntryCount, sizeof(FILE_ENTRY), Compare);
- LONG lOffset = sizeof(PACK_FILE_HEAD) + (pfh.dwEntryCount+1)*sizeof(FILE_ENTRY);
- CFastFile fin;
- DWORD dwSize = 0;
- for (dwCnt=0; dwCnt<pfh.dwEntryCount; dwCnt++)
- {
- if(!m_map.Lookup(pFe[dwCnt].szFileName, val))
- {
- MessageBox("文件目录出错!", "PackFile Maker");
- delete [] pFe;
- return;
- }
- if (!fin.Open(val=="Src"?pFfmSrc:pFfmDest, pFe[dwCnt].szFileName))
- {
- MessageBox("文件打开失败!", "PackFile Maker", MB_OK|MB_ICONSTOP);
- fPf.Close();
- delete [] pFe;
- return;
- }
- pFe[dwCnt].lOffset = lOffset;
- memcpy(&(pFe[dwCnt].ftCreationTime), &(fin.m_pFE->ftCreationTime), sizeof(FILETIME));
- memcpy(&(pFe[dwCnt].ftWriteTime), &(fin.m_pFE->ftWriteTime), sizeof(FILETIME));
- // strcpy(pFe[dwCnt].szFileName, key);
- fPf.Seek(lOffset, CFile::begin);
- fPf.Write(fin.GetPtr(), fin.GetFileSize());
- lOffset += fin.GetFileSize();
- dwSize += fin.GetFileSize();
- UpdateProg(dwSize);
- //fin.Close();
- }
- strcpy(pFe[pfh.dwEntryCount].szFileName, "-[The End]-");
- pFe[pfh.dwEntryCount].lOffset = lOffset;
- pfh.dwFileSize = lOffset;
- fPf.Seek(0, CFile::begin);
- fPf.Write(&pfh, sizeof(pfh));
- fPf.Write(pFe, (pfh.dwEntryCount+1)*sizeof(FILE_ENTRY));
- fPf.Close();
- delete [] pFe;
- m_flDest->Unload();
- CFile::Remove(m_strDest);
- CFile::Rename(strDest+"$$$$$$$$.$$$", m_strDest);
- m_flSrc->LoadFastfile(m_strSrc);
- m_flDest->LoadFastfile(m_strDest);
- }
- void CCopyFilesDlg::CopyFD()
- {
- LPWIN32_FIND_DATA pFd;
- LVITEM lvi;
- m_dwSize = 0;
- m_map.RemoveAll();
- int nItem = m_flSrc->GetNextItem(-1, LVNI_ALL|LVNI_SELECTED);
- while (nItem != -1)
- {
- pFd = (LPWIN32_FIND_DATA)m_flSrc->GetItemData(nItem);
- m_dwSize += pFd->nFileSizeLow;
- m_map.SetAt(pFd->cFileName, "");
- lvi.mask = LVIF_STATE;
- lvi.iItem = nItem;
- lvi.iSubItem = 0;
- lvi.stateMask = 0xFFFF;
- // get all state flags
- m_flSrc->GetItem(&lvi);
- lvi.state ^= LVIS_SELECTED;
- m_flSrc->SetItem(&lvi);
- nItem = m_flSrc->GetNextItem(nItem, LVNI_ALL|LVNI_SELECTED);
- }
- CFastFile fin;
- CFile fout;
- DWORD dwSize = 0;
- CString key, val;
- POSITION ps;
- ps = m_map.GetStartPosition();
- while (ps)
- {
- m_map.GetNextAssoc(ps, key, val);
- if (fin.Open(m_flSrc->GetFastFileManager(), key))
- if (fout.Open(m_strDest+key, CFile::modeCreate|CFile::modeWrite|CFile::typeBinary))
- fout.Write(fin.GetPtr(), fin.GetFileSize());
- SetFileTime((HANDLE)fout.m_hFile, &(fin.m_pFE->ftCreationTime),
- &(fin.m_pFE->ftWriteTime), &(fin.m_pFE->ftWriteTime));
- fout.Close();
- dwSize += fin.GetFileSize();
- UpdateProg(dwSize);
- }
- m_flSrc->LoadFastfile(m_strSrc);
- m_flDest->LoadDirectory(m_strDest);
- }
- void CCopyFilesDlg::CopyDF()
- {
- char buf[1024];
- FILETIME ft;
- LPWIN32_FIND_DATA pFd;
- CFastFileManager *pFfmSrc = m_flSrc->GetFastFileManager();
- CFastFileManager *pFfmDest = m_flDest->GetFastFileManager();
- m_map.RemoveAll();
- m_dwSize = 0;
- // add Dest list items
- int nItem = m_flDest->GetNextItem(-1, LVNI_ALL);
- while (nItem != -1)
- {
- pFd = (LPWIN32_FIND_DATA)m_flDest->GetItemData(nItem);
- if (pFd->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
- ;
- else
- {
- m_map.SetAt(pFd->cFileName, "nDestn");
- m_dwSize += pFd->nFileSizeLow;
- }
- nItem = m_flDest->GetNextItem(nItem, LVNI_ALL);
- }
- char drive[_MAX_DRIVE];
- char dir[_MAX_DIR];
- char fname[_MAX_FNAME];
- char ext[_MAX_EXT];
- CString strName;
- LVITEM lvi;
- char sPath[14];
- nItem = m_flSrc->GetNextItem(-1, LVNI_ALL|LVNI_SELECTED);
- while (nItem != -1)
- {
- pFd = (LPWIN32_FIND_DATA)m_flSrc->GetItemData(nItem);
- _splitpath(pFd->cFileName,drive,dir,fname,ext);
- strName = fname;
- strName += ext;
- if (pFd->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
- {
- if (strName != "..")
- SearchDirectory(strName, FALSE);
- }
- else
- {
- GetShortPathName(strName, sPath,14);
- m_map.SetAt(sPath, (gCurPan?gRightPath:gLeftPath)+strName);
- }
- lvi.mask = LVIF_STATE;
- lvi.iItem = nItem;
- lvi.iSubItem = 0;
- lvi.stateMask = 0xFFFF;
- // get all state flags
- m_flSrc->GetItem(&lvi);
- lvi.state ^= LVIS_SELECTED;
- m_flSrc->SetItem(&lvi);
- //SetItemState( nItem,LVIS_SELECTED , LVIS_SELECTED);
- nItem = m_flSrc->GetNextItem(nItem, LVNI_ALL|LVNI_SELECTED);
- }
- CString strDest = m_strDest.Left(
- m_strDest.ReverseFind('\')+1);
- CFile fPf;
- FILE_ENTRY *pFe;
- PACK_FILE_HEAD pfh;
- if (!fPf.Open(strDest+"$$$$$$$$.$$$", CFile::modeCreate|CFile::modeWrite|CFile::typeBinary))
- {
- MessageBox("包文件创建失败!","PackFile Maker", MB_OK|MB_ICONSTOP);
- return;
- }
- pfh.dwEntryCount = m_map.GetCount();
- memcpy(&pfh.dwFlag, "DXGF", 4);
- pfh.dwHeadSize = sizeof(PACK_FILE_HEAD);
- pfh.wMajorVersion = 0x1;
- pfh.wMinorVersion = 0x0;
- strcpy(pfh.szDescription, "This file is created by DirectX Guide");
- pFe = NULL;
- pFe = new FILE_ENTRY[pfh.dwEntryCount+1];
- if (pFe == NULL)
- {
- MessageBox("内存申请失败!", "PackFile Maker", MB_OK|MB_ICONSTOP);
- return;
- }
- DWORD dwCnt = 0;
- CString key, val;
- POSITION ps;
- ps = m_map.GetStartPosition();
- while (ps)
- {
- m_map.GetNextAssoc(ps, key, val);
- strcpy(pFe[dwCnt].szFileName, key);
- dwCnt++;
- }
- qsort(pFe, pfh.dwEntryCount, sizeof(FILE_ENTRY), Compare);
- LONG lOffset = sizeof(PACK_FILE_HEAD) + (pfh.dwEntryCount+1)*sizeof(FILE_ENTRY);
- CFile fin;
- CFastFile ffin;
- DWORD dwSize = 0;
- for (dwCnt=0; dwCnt<pfh.dwEntryCount; dwCnt++)
- {
- if(!m_map.Lookup(pFe[dwCnt].szFileName, val))
- {
- MessageBox("文件目录出错!", "PackFile Maker");
- delete [] pFe;
- return;
- }
- if (val == "nDestn")
- {
- if (!ffin.Open(val=="Src"?pFfmSrc:pFfmDest, pFe[dwCnt].szFileName))
- {
- MessageBox("文件打开失败!", "PackFile Maker", MB_OK|MB_ICONSTOP);
- fPf.Close();
- delete [] pFe;
- return;
- }
- pFe[dwCnt].lOffset = lOffset;
- memcpy(&(pFe[dwCnt].ftCreationTime), &(ffin.m_pFE->ftCreationTime), sizeof(FILETIME));
- memcpy(&(pFe[dwCnt].ftWriteTime), &(ffin.m_pFE->ftWriteTime), sizeof(FILETIME));
- fPf.Seek(lOffset, CFile::begin);
- fPf.Write(ffin.GetPtr(), ffin.GetFileSize());
- lOffset += ffin.GetFileSize();
- dwSize += ffin.GetFileSize();
- }
- else
- {
- if (!fin.Open(val, CFile::modeRead|CFile::shareDenyNone|CFile::typeBinary))
- {
- MessageBox("文件打开失败!", "PackFile Maker", MB_OK|MB_ICONSTOP);
- fPf.Close();
- delete [] pFe;
- return;
- }
- pFe[dwCnt].lOffset = lOffset;
- GetFileTime((HANDLE)fin.m_hFile, &(pFe[dwCnt].ftCreationTime), &ft, &(pFe[dwCnt].ftWriteTime));
- fPf.Seek(lOffset, CFile::begin);
- int nRead;
- do
- {
- nRead = fin.Read(buf, 1024);
- fPf.Write(buf, nRead);
- }while (nRead == 1024);
- lOffset += fin.GetLength();
- dwSize += fin.GetLength();
- fin.Close();
- }
- UpdateProg(dwSize);
- }
- strcpy(pFe[pfh.dwEntryCount].szFileName, "-[The End]-");
- pFe[pfh.dwEntryCount].lOffset = lOffset;
- pfh.dwFileSize = lOffset;
- fPf.Seek(0, CFile::begin);
- fPf.Write(&pfh, sizeof(pfh));
- fPf.Write(pFe, (pfh.dwEntryCount+1)*sizeof(FILE_ENTRY));
- fPf.Close();
- delete [] pFe;
- m_flDest->Unload();
- CFile::Remove(m_strDest);
- CFile::Rename(strDest+"$$$$$$$$.$$$", m_strDest);
- m_flSrc->LoadDirectory(m_strSrc);
- m_flDest->LoadFastfile(m_strDest);
- }
- void CCopyFilesDlg::CopyDD()
- {
- LPWIN32_FIND_DATA pFd;
- DWORD dwSize = 0;
- LVITEM lvi;
- char drive[_MAX_DRIVE];
- char dir[_MAX_DIR];
- char fname[_MAX_FNAME];
- char ext[_MAX_EXT];
- char buf[1024];
- CString strName;
- m_map.RemoveAll();
- m_dwSize = 0;
- int nItem = m_flSrc->GetNextItem(-1, LVNI_ALL|LVNI_SELECTED);
- while (nItem!=-1)
- {
- pFd = (LPWIN32_FIND_DATA)m_flSrc->GetItemData(nItem);
- _splitpath(pFd->cFileName,drive,dir,fname,ext);
- strName = fname;
- strName += ext;
- if (pFd->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
- {
- if (strName != "..")
- SearchDirectory(strName, TRUE);
- }
- else
- {
- m_map.SetAt(strName, strName);
- m_dwSize += pFd->nFileSizeLow;
- }
- lvi.mask = LVIF_STATE;
- lvi.iItem = nItem;
- lvi.iSubItem = 0;
- lvi.stateMask = 0xFFFF;
- // get all state flags
- m_flSrc->GetItem(&lvi);
- lvi.state ^= LVIS_SELECTED;
- m_flSrc->SetItem(&lvi);
- nItem = m_flSrc->GetNextItem(nItem, LVNI_ALL|LVNI_SELECTED);
- }
- DWORD dwCnt = 0;
- CFile fin, fout;
- CString key, val;
- POSITION ps;
- ps = m_map.GetStartPosition();
- while (ps)
- {
- m_map.GetNextAssoc(ps, key, val);
- if (fin.Open(m_strSrc+val, CFile::modeRead|CFile::shareDenyNone|CFile::typeBinary))
- {
- if (fout.Open(m_strDest+val, CFile::modeCreate|CFile::modeWrite|CFile::typeBinary))
- {
- int nRead;
- do
- {
- nRead = fin.Read(buf, 1024);
- fout.Write(buf, nRead);
- dwSize += nRead;
- UpdateProg(dwSize);
- }while (nRead == 1024);
- CFileStatus status;
- fin.GetStatus(status);
- fin.Close();
- fout.Close();
- CFile::SetStatus(m_strDest+val, status);
- }
- }
- }
- }
- void CCopyFilesDlg::OnCancel()
- {
- // TODO: Add extra cleanup here
- CDialog::OnCancel();
- }