⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 fcomparedlg.cpp

📁 二进制文件比较程序
💻 CPP
📖 第 1 页 / 共 2 页
字号:
      }
      // Restore initial target
      pfinTarget = &pcdlg->m_fiaTargetInfos[iTarget];
      
      if (pfinTarget->GetLength64() >= pfinSource->GetLength64())
         iSource++; // In case of equal sizes, iSource must be incremented first 
                    // (see inner loop for reasons)
      else
         iTarget++;

      pcdlg->m_ulProgress++;
   }

   // Set new progress values
   pcdlg->m_ulProgress = 0;
   pcdlg->m_ulProgressMax = pcdlg->m_msaMatchedInfos.GetSize();

   pcdlg->m_bCompareInProgress = FALSE;
   pcdlg->m_bFillInProgress = TRUE;
   
   // Fill listview
   pcdlg->m_lblStatus.SetWindowText(_T("Filling listview..."));
   pcdlg->m_lstMatchedFiles.LockWindowUpdate();
   // Set common LVITEM values for all elements
   lviMatched.mask = LVIF_PARAM | LVIF_TEXT;
   lviMatched.iSubItem = 0;
   lviMatched.pszText = LPSTR_TEXTCALLBACK;
   for (int i=0;(i<pcdlg->m_msaMatchedInfos.GetSize()) && (!pcdlg->m_bAbort);i++) {
      lviMatched.iItem = i;
      lviMatched.lParam = (LPARAM) &pcdlg->m_msaMatchedInfos[i];
      pcdlg->m_lstMatchedFiles.InsertItem(&lviMatched);
      pcdlg->m_ulProgress++;
   }
   pcdlg->m_lstMatchedFiles.UnlockWindowUpdate();
   pcdlg->m_bFillInProgress = FALSE;

   // Report compare thread end
   pcdlg->PostMessage(WM_COMPARE_FINISHED,dwMatchNumber, dwMatchSize);
   return 0;
}

void CFCompareDlg::SetBusyState(int m_bsState) {
   this->m_bsState = m_bsState;

   GetDlgItem(LST_SOURCEFILES)->EnableWindow(m_bsState != BS_SEARCHING);
   GetDlgItem(LST_TARGETFILES)->EnableWindow(m_bsState != BS_SEARCHING);
   GetDlgItem(CHK_RECURSEDIR)->EnableWindow(m_bsState != BS_SEARCHING);

   GetDlgItem(BTN_COMPARE)->EnableWindow(m_bsState == BS_IDLE);
   GetDlgItem(BTN_CLEARSOURCEFILES)->EnableWindow(m_bsState == BS_IDLE);
   GetDlgItem(BTN_CLEARTARGETFILES)->EnableWindow(m_bsState == BS_IDLE);
   GetDlgItem(BTN_ADDSOURCEFILES)->EnableWindow(m_bsState == BS_IDLE);
   GetDlgItem(BTN_ADDTARGETFILES)->EnableWindow(m_bsState == BS_IDLE);
   GetDlgItem(BTN_EXPORT)->EnableWindow(m_bsState == BS_IDLE);

   GetDlgItem(RAD_MATCHCRITERIA)->EnableWindow(m_bsState != BS_COMPARING);
   GetDlgItem(RAD_MATCHCHECKSUM)->EnableWindow(m_bsState != BS_COMPARING);
   GetDlgItem(RAD_MATCHCRC)->EnableWindow(m_bsState != BS_COMPARING);
   GetDlgItem(RAD_MATCHCONTENTS)->EnableWindow(m_bsState != BS_COMPARING);
   GetDlgItem(EDT_UPTO)->EnableWindow((m_bsState != BS_COMPARING) && 
      (IsDlgButtonChecked(RAD_MATCHCRITERIA) != BST_CHECKED));
   GetDlgItem(CHK_COMPAREDUPLICATES)->EnableWindow(m_bsState != BS_COMPARING);

   if (m_bsState != BS_IDLE) {
      GetDlgItem(BTN_EXIT)->SetWindowText(_T("Cancel"));
   } else {
      GetDlgItem(BTN_EXIT)->SetWindowText(_T("Exit"));
      m_lblStatus.SetWindowText(_T("Ready"));
      m_prgProgress.SetPos(0);
   }
}


void CFCompareDlg::UpdateCounters() {
   CString str;

   str.Format(_T("Matched files (%d)"), m_lstMatchedFiles.GetItemCount());
   GetDlgItem(LBL_MATCHEDFILES)->SetWindowText(str);
   str.Format(_T("Source files (%d)"), m_lstSourceFiles.GetItemCount());
   GetDlgItem(LBL_SOURCEFILES)->SetWindowText(str);
   str.Format(_T("Target files (%d)"), m_lstTargetFiles.GetItemCount());
   GetDlgItem(LBL_TARGETFILES)->SetWindowText(str);
}

void CFCompareDlg::OnCompare() {
   // Retrieve comparison settings
   UpdateData(TRUE);
   m_lblStatus.SetWindowText(_T("Comparing files..."));

   // Empty compare list
   m_lstMatchedFiles.DeleteAllItems();
   m_msaMatchedInfos.RemoveAll();

   m_bAbort = FALSE;
   SetBusyState(BS_COMPARING);

   // Launch thread
   AfxBeginThread((AFX_THREADPROC) CompareThread,(LPVOID) this);
   // Launch progress-report timer
   if (SetTimer(1,250, NULL) == 0){
      // Timer not available
      AfxMessageBox(_T("Timer is not available, real time progress will not be reported."), 
         MB_ICONINFORMATION | MB_OK);
   }
}

void CFCompareDlg::OnGetdispinfoSearchFiles(NMHDR* pNMHDR, LRESULT* pResult) {
	LV_DISPINFO* pDispInfo = (LV_DISPINFO*)pNMHDR;
   CString str;
   CFileInfo* pinf;

   pinf = (CFileInfo*) pDispInfo->item.lParam;
   switch (pDispInfo->item.iSubItem) {
      case 0:
         pDispInfo->item.mask = LVIF_TEXT;
         StrCpyN(pDispInfo->item.pszText, pinf->GetFilePath(), pDispInfo->item.cchTextMax);
      break;
      case 1:
         if (pinf->GetLength64() < 1024) 
            str.Format("%d   B", pinf->GetLength());
         else {
            // The following weird LONGLONG casts are because VC 6.0 doesn't implement int64 
            // to double conversion =8-ooo
            if (pinf->GetLength64() < (1024*1024))
               str.Format("%3.2f KB",(LONGLONG) pinf->GetLength64() / 1024.0);
            else {
               if (pinf->GetLength64() < (1024*1024*1024))
                  str.Format("%3.2f MB", (LONGLONG) pinf->GetLength64() / (1024*1024.0));
               else
                  str.Format("%1.2f GB", (LONGLONG) pinf->GetLength64() / (1024.0*1024*1024));
            }
         }     
         pDispInfo->item.mask = LVIF_TEXT;

         StrCpyN(pDispInfo->item.pszText, str, pDispInfo->item.cchTextMax);
      break;
   }
	
	*pResult = 0;
}

void CFCompareDlg::OnTimer(UINT nIDEvent) {
   CString str;

   if (m_bAddInProgress) {
      str.Format(_T("Found %d files"),m_fiaSourceInfos.GetSize() + m_fiaTargetInfos.GetSize());
      m_lblStatus.SetWindowText(str);
      str.Format(_T("Source files (%d)"), m_fiaSourceInfos.GetSize());
      m_lblSourceFiles.SetWindowText(str);
      str.Format(_T("Target files (%d)"), m_fiaTargetInfos.GetSize());
      m_lblTargetFiles.SetWindowText(str);
   }

   if (m_bFillInProgress) {
      m_prgProgress.SetRange(0, (short) m_ulProgressMax);
      m_prgProgress.SetPos(m_ulProgress);
   }

   if (m_bCompareInProgress) {
      str.Format(_T("Matched %d files"), m_msaMatchedInfos.GetSize());
      m_lblStatus.SetWindowText(str);
      str.Format(_T("Matched files (%d)"), m_msaMatchedInfos.GetSize());
      m_lblMatchedFiles.SetWindowText(str);
      m_prgProgress.SetRange(0, (short) m_ulProgressMax);
      m_prgProgress.SetPos(m_ulProgress);
   }

	CDialog::OnTimer(nIDEvent);
}

afx_msg LONG CFCompareDlg::OnSearchFinished(WPARAM /* wparam*/ , LPARAM /* lparam */) {
   if (m_bAbort) {
      // Delete items from aborted listview
      m_plstFiles->DeleteAllItems();
      // Delete items from aborted CFileInfoArray
      m_pfiaInfos->RemoveAll();
   }

   KillTimer(1);
   SetBusyState(BS_IDLE);

   m_lblStatus.SetWindowText(_T("Ready"));
   UpdateCounters();
   if (m_bExit) PostQuitMessage(0);
   return TRUE;
}

afx_msg LONG CFCompareDlg::OnCompareFinished(WPARAM wparam, LPARAM lparam) {
   CString str;

   if (m_bAbort)
      m_lstMatchedFiles.DeleteAllItems();

   KillTimer(1);
   SetBusyState(BS_IDLE);

   if (!m_bAbort) {
      str.Format(_T("Comparison results:\r\n* %d files matched\r\n* %d bytes matched"),
         wparam, lparam);

      MessageBox(str,_T("Comparison ended"),MB_OK);
   }

   m_prgProgress.SetPos(0);

   str.Format(_T("Matched files (%d)"),m_lstMatchedFiles.GetItemCount());
   m_lblMatchedFiles.SetWindowText(str);
   
   UpdateCounters();
   if (m_bExit) PostQuitMessage(0);
   return TRUE;
}

void CFCompareDlg::OnExit() {
   if (m_bsState == BS_IDLE) PostQuitMessage(0);
   m_bAbort = TRUE;
}

void CFCompareDlg::OnSelchangeAction(NMHDR* /* pNMHDR*/, LRESULT* pResult) {
   if (m_tabAction.GetCurSel() == PPG_COMPARE) {
      GetDlgItem(LBL_TARGETFILES)->ShowWindow(SW_HIDE);
      GetDlgItem(LBL_SOURCEFILES)->ShowWindow(SW_HIDE);
      GetDlgItem(LST_TARGETFILES)->ShowWindow(SW_HIDE);
      GetDlgItem(LST_SOURCEFILES)->ShowWindow(SW_HIDE);
      GetDlgItem(BTN_COMPARE)->ShowWindow(SW_HIDE);
      GetDlgItem(LBL_MATCHEDFILES)->ShowWindow(SW_SHOW);
      GetDlgItem(LST_MATCHEDFILES)->ShowWindow(SW_SHOW);
      GetDlgItem(CHK_COMPAREDUPLICATES)->ShowWindow(SW_HIDE);
   } else {
      GetDlgItem(LBL_TARGETFILES)->ShowWindow(SW_SHOW);
      GetDlgItem(LBL_SOURCEFILES)->ShowWindow(SW_SHOW);
      GetDlgItem(LST_TARGETFILES)->ShowWindow(SW_SHOW);
      GetDlgItem(LST_SOURCEFILES)->ShowWindow(SW_SHOW);
      GetDlgItem(BTN_COMPARE)->ShowWindow(SW_SHOW);
      GetDlgItem(LBL_MATCHEDFILES)->ShowWindow(SW_HIDE);
      GetDlgItem(LST_MATCHEDFILES)->ShowWindow(SW_HIDE);
      GetDlgItem(CHK_COMPAREDUPLICATES)->ShowWindow(SW_SHOW);
   }
	*pResult = 0;
}

void CFCompareDlg::OnGetdispinfoMatchedFiles(NMHDR* pNMHDR, LRESULT* pResult)  {
	LV_DISPINFO* pDispInfo = (LV_DISPINFO*)pNMHDR;
   MATCHSTRUCT* pms;
   CFileInfo* pinf;
   CString str;

	switch (pDispInfo->item.iSubItem) {
      case 0:
         pDispInfo->item.mask = LVIF_TEXT;
         pms = (MATCHSTRUCT*) pDispInfo->item.lParam;
         StrCpyN(pDispInfo->item.pszText, pms->pfinSource->GetFilePath(), 
            pDispInfo->item.cchTextMax);
      break;
      case 1:
         pDispInfo->item.mask = LVIF_TEXT;
         pms = (MATCHSTRUCT*) pDispInfo->item.lParam;
         StrCpyN(pDispInfo->item.pszText, pms->pfinTarget->GetFilePath(), 
            pDispInfo->item.cchTextMax);
      break;
      case 2:
         pms = (MATCHSTRUCT*) pDispInfo->item.lParam;
         pinf = pms->pfinSource;

         if (pinf->GetLength64() < 1024) 
            str.Format("%d   B",pinf->GetLength());
         else {
            // The following weird LONGLONG casts are because VC 6.0 doesn't implement int64 
            // to double conversion =8-ooo
            if (pinf->GetLength64() < (1024*1024))
               str.Format("%3.2f KB",(LONGLONG) pinf->GetLength64() / 1024.0);
            else {
               if (pinf->GetLength64() < (1024*1024*1024))
                  str.Format("%3.2f MB", (LONGLONG) pinf->GetLength64() / (1024*1024.0));
               else
                  str.Format("%1.2f GB", (LONGLONG) pinf->GetLength64() / (1024.0*1024*1024));
            }
         }     
         pDispInfo->item.mask = LVIF_TEXT;

         StrCpyN(pDispInfo->item.pszText, str, pDispInfo->item.cchTextMax);
      break;
   }
	
	*pResult = 0;
}

void CFCompareDlg::OnAddSourceFiles() {
   // Retrieve search settings from dialog controls
   UpdateData(TRUE);

   // Append backslash if necessary
   if ((m_strDirectory.GetLength()>0) && 
       (m_strDirectory[m_strDirectory.GetLength()-1] != TCHAR('\\'))) m_strDirectory += TCHAR('\\');
   UpdateData(FALSE);

   // Set common variables with thread
   m_plstFiles = &m_lstSourceFiles;
   m_pfiaInfos = &m_fiaSourceInfos;

   // Compare text-callback list will no longer be up to date, empty it
   m_lstMatchedFiles.DeleteAllItems();
   m_msaMatchedInfos.RemoveAll();
   m_lblStatus.SetWindowText(_T("Inserting files..."));

   m_bAbort = FALSE;
   SetBusyState(BS_SEARCHING);

   // Launch thread
   AfxBeginThread((AFX_THREADPROC) SearchThread,(LPVOID) this);
   // Launch progress-report timer
   if (SetTimer(1,250, NULL) == 0){
      // Timer not available
      AfxMessageBox(_T("Timer is not available, real time progress will not be reported."), 
         MB_ICONINFORMATION | MB_OK);
   }
}

void CFCompareDlg::OnAddTargetFiles() {
   // Retrieve search settings from dialog controls
   UpdateData(TRUE);

   // Append backslash if necessary
   if ((m_strDirectory.GetLength()>0) && 
       (m_strDirectory[m_strDirectory.GetLength()-1] != TCHAR('\\'))) m_strDirectory += TCHAR('\\');
   UpdateData(FALSE);

   // Set common variables with thread
   m_plstFiles = &m_lstTargetFiles;
   m_pfiaInfos = &m_fiaTargetInfos;

   // Compare text-callback list will no longer be up to date, empty it
   m_lstMatchedFiles.DeleteAllItems();
   m_msaMatchedInfos.RemoveAll();
   m_lblStatus.SetWindowText(_T("Inserting files..."));

   m_bAbort = FALSE;
   SetBusyState(BS_SEARCHING);

   // Launch thread
   AfxBeginThread((AFX_THREADPROC) SearchThread,(LPVOID) this);
   // Launch progress-report timer
   if (SetTimer(1,250, NULL) == 0){
      // Timer not available
      AfxMessageBox(_T("Timer is not available, real time progress will not be reported."), 
         MB_ICONINFORMATION | MB_OK);
   }
}

void CFCompareDlg::OnClearSourceFiles() {
   // Clear matched
   m_lstMatchedFiles.DeleteAllItems();
   m_msaMatchedInfos.RemoveAll();
   // Clear listview
   m_lstSourceFiles.DeleteAllItems();
   // Clear fileinfos array
   m_fiaSourceInfos.RemoveAll();
   // UpdateCounters
   UpdateCounters();
}

void CFCompareDlg::OnClearTargetFiles() {
   // Clear matched
   m_lstMatchedFiles.DeleteAllItems();
   m_msaMatchedInfos.RemoveAll();
   // Clear listview
   m_lstTargetFiles.DeleteAllItems();
   // Clear fileinfos array
   m_fiaTargetInfos.RemoveAll();
   // Update counters
   UpdateCounters();
}

void CFCompareDlg::OnClose() {
   if (m_bsState != BS_IDLE) {
      // Thread running, signal abort and exit flag
      m_bExit = TRUE;
      m_bAbort = TRUE;
   } else
	   CDialog::OnClose();
}

void CFCompareDlg::OnExport() {
   CStdioFile sf;
   CFileDialog fd(FALSE);
   int i;
   CString str;

   if (fd.DoModal() == IDOK) {
      try {
         sf.Open(fd.m_ofn.lpstrFile, CStdioFile::modeWrite | CFile::modeCreate | CStdioFile::typeText);
         sf.WriteString(_T("SOURCE FILES:\n"));
         for (i=0;i<m_fiaSourceInfos.GetSize();i++)
            sf.WriteString("\""+m_fiaSourceInfos[i].GetFilePath() + "\"\n");
         sf.WriteString(_T("TARGET FILES:\n"));
         for (i=0;i<m_fiaTargetInfos.GetSize();i++)
            sf.WriteString("\"" + m_fiaTargetInfos[i].GetFilePath() + "\"\n");
         sf.WriteString(_T("MATCHED FILES:\n"));
         for (i=0;i<m_msaMatchedInfos.GetSize();i++) 
            sf.WriteString("\"" + m_msaMatchedInfos[i].pfinSource->GetFilePath() + "\"" + 
               " \\ " + "\"" + m_msaMatchedInfos[i].pfinTarget->GetFilePath() + "\"\n");
         sf.Close();
      } catch (CException e) {
         e.Delete();
      }

   }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -