www.pudn.com > CleanR.zip > CleanRDlg.cpp


/* 
Description: 
----------- 
 
Dialog box to select file, text boxes to get Search and Replace string. 
*/ 
#include "stdafx.h" 
#include "CleanR.h" 
#include "CleanR1.h" 
#include "CleanRDlg.h" 
 
///////////////////////////////////////////////////////////// 
#define OUTPUT_FILE "TestOut.txt" 
CString sFilePath,sVirusStrain,sReplacementString; 
///////////////////////////////////////////////////////////// 
 
#ifdef _DEBUG 
#define new DEBUG_NEW 
#undef THIS_FILE 
static char THIS_FILE[] = __FILE__; 
#endif 
 
 
///////////////////////////////////////////////////////////////////////////// 
// CAboutDlg dialog used for App About 
 
class CAboutDlg : public CDialog 
{ 
public: 
	CAboutDlg(); 
 
// Dialog Data 
	//{{AFX_DATA(CAboutDlg) 
	enum { IDD = IDD_ABOUTBOX }; 
	//}}AFX_DATA 
 
	// ClassWizard generated virtual function overrides 
	//{{AFX_VIRTUAL(CAboutDlg) 
	protected: 
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support 
	//}}AFX_VIRTUAL 
 
// Implementation 
protected: 
	//{{AFX_MSG(CAboutDlg) 
	//}}AFX_MSG 
	DECLARE_MESSAGE_MAP() 
}; 
 
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD) 
{ 
	//{{AFX_DATA_INIT(CAboutDlg) 
	//}}AFX_DATA_INIT 
} 
 
void CAboutDlg::DoDataExchange(CDataExchange* pDX) 
{ 
	CDialog::DoDataExchange(pDX); 
	//{{AFX_DATA_MAP(CAboutDlg) 
	//}}AFX_DATA_MAP 
} 
 
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog) 
	//{{AFX_MSG_MAP(CAboutDlg) 
		// No message handlers 
	//}}AFX_MSG_MAP 
END_MESSAGE_MAP() 
 
///////////////////////////////////////////////////////////////////////////// 
// CCleanRDlg dialog 
 
CCleanRDlg::CCleanRDlg(CWnd* pParent /*=NULL*/) 
	: CDialog(CCleanRDlg::IDD, pParent) 
{ 
	//{{AFX_DATA_INIT(CCleanRDlg) 
	m_lpszSearchText = _T(""); 
	m_lpszReplacementString = _T(""); 
	m_bStrict = FALSE; 
	m_bCheckWordsOnly = FALSE; 
	m_bIgnoreLastWordPunct = FALSE; 
	m_bCaseSensetive = TRUE; 
	m_bOverWriteFile = FALSE; 
	m_bSearchStringFromFile = FALSE; 
	m_bReplacementFromFile = FALSE; 
	m_sMatches = _T("0"); 
	m_bReplaceLFWithCRLF = FALSE; 
	m_bReplaceCRLFWithLF = FALSE; 
	//}}AFX_DATA_INIT 
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32 
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); 
} 
 
void CCleanRDlg::DoDataExchange(CDataExchange* pDX) 
{ 
	CDialog::DoDataExchange(pDX); 
	//{{AFX_DATA_MAP(CCleanRDlg) 
	DDX_Text(pDX, IDC_EDIT1, m_lpszSearchText); 
	DDX_Text(pDX, IDC_EDIT2, m_lpszReplacementString); 
	DDX_Check(pDX, IDC_CHECK1, m_bStrict); 
	DDX_Check(pDX, IDC_CHECK2, m_bCheckWordsOnly); 
	DDX_Check(pDX, IDC_CHECK3, m_bIgnoreLastWordPunct); 
	DDX_Check(pDX, IDC_CHECK4, m_bCaseSensetive); 
	DDX_Check(pDX, IDC_CHECK6, m_bOverWriteFile); 
	DDX_Check(pDX, IDC_CHECK5, m_bSearchStringFromFile); 
	DDX_Check(pDX, IDC_CHECK7, m_bReplacementFromFile); 
	DDX_Check(pDX, IDC_CHECK8, m_bReplaceLFWithCRLF); 
	DDX_Check(pDX, IDC_CHECK9, m_bReplaceCRLFWithLF); 
	//}}AFX_DATA_MAP 
} 
 
BEGIN_MESSAGE_MAP(CCleanRDlg, CDialog) 
	//{{AFX_MSG_MAP(CCleanRDlg) 
	ON_WM_SYSCOMMAND() 
	ON_WM_PAINT() 
	ON_WM_QUERYDRAGICON() 
	ON_BN_CLICKED(IDC_BUTTON2, OnBrowse) 
	ON_EN_CHANGE(IDC_EDIT1, OnChangeSearchText) 
	ON_EN_CHANGE(IDC_EDIT2, OnChangeReplacementString) 
	ON_BN_CLICKED(IDC_CHECK1, OnStrictGrammar) 
	ON_BN_CLICKED(IDC_CHECK2, OnCheckWordsOnly) 
	ON_BN_CLICKED(IDC_CHECK3, OnIgnoreLastWordPunct) 
	ON_BN_CLICKED(IDC_CHECK4, OnCaseSensetiveOption) 
	ON_BN_CLICKED(IDC_CHECK6, OnOverWriteFile) 
	ON_BN_CLICKED(IDC_CHECK5, OnGetSearchStringFromFile) 
	ON_BN_CLICKED(IDC_CHECK7, OnReplaceFromFile) 
	ON_BN_CLICKED(IDC_CHECK8, OnConvertLFtoCRLF) 
	ON_BN_CLICKED(IDC_CHECK9, OnConvertCRLFtoLF) 
	//}}AFX_MSG_MAP 
END_MESSAGE_MAP() 
 
///////////////////////////////////////////////////////////////////////////// 
// CCleanRDlg message handlers 
 
BOOL CCleanRDlg::OnInitDialog() 
{ 
	CDialog::OnInitDialog(); 
 
	// Add "About..." menu item to system menu. 
 
	// IDM_ABOUTBOX must be in the system command range. 
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX); 
	ASSERT(IDM_ABOUTBOX < 0xF000); 
 
	CMenu* pSysMenu = GetSystemMenu(FALSE); 
	if (pSysMenu != NULL) 
	{ 
		CString strAboutMenu; 
		strAboutMenu.LoadString(IDS_ABOUTBOX); 
		if (!strAboutMenu.IsEmpty()) 
		{ 
			pSysMenu->AppendMenu(MF_SEPARATOR); 
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu); 
		} 
	} 
 
	// Set the icon for this dialog.  The framework does this automatically 
	//  when the application's main window is not a dialog 
	SetIcon(m_hIcon, TRUE);			// Set big icon 
	SetIcon(m_hIcon, FALSE);		// Set small icon 
	 
	// TODO: Add extra initialization here 
	 
	return TRUE;  // return TRUE  unless you set the focus to a control 
} 
 
void CCleanRDlg::OnSysCommand(UINT nID, LPARAM lParam) 
{ 
	if ((nID & 0xFFF0) == IDM_ABOUTBOX) 
	{ 
		CAboutDlg dlgAbout; 
		dlgAbout.DoModal(); 
	} 
	else 
	{ 
		CDialog::OnSysCommand(nID, lParam); 
	} 
} 
 
// If you add a minimize button to your dialog, you will need the code below 
//  to draw the icon.  For MFC applications using the document/view model, 
//  this is automatically done for you by the framework. 
 
void CCleanRDlg::OnPaint()  
{ 
	if (IsIconic()) 
	{ 
		CPaintDC dc(this); // device context for painting 
 
		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0); 
 
		// Center icon in client rectangle 
		int cxIcon = GetSystemMetrics(SM_CXICON); 
		int cyIcon = GetSystemMetrics(SM_CYICON); 
		CRect rect; 
		GetClientRect(&rect); 
		int x = (rect.Width() - cxIcon + 1) / 2; 
		int y = (rect.Height() - cyIcon + 1) / 2; 
 
		// Draw the icon 
		dc.DrawIcon(x, y, m_hIcon); 
	} 
	else 
	{ 
		CDialog::OnPaint(); 
	} 
} 
 
// The system calls this to obtain the cursor to display while the user drags 
//  the minimized window. 
HCURSOR CCleanRDlg::OnQueryDragIcon() 
{ 
	return (HCURSOR) m_hIcon; 
} 
 
/////////////////////////////////////////////////////////////////////////////////////////////// 
void CCleanRDlg::OnOK()  
{ 
	CCleanRboolSet theSet; 
 
	if(m_bStrict) theSet.bStrict=TRUE; 
	else 
	{ 
	theSet.bLastWordMayNotHavePunct=FALSE; 
	theSet.bStrict=FALSE; 
	} 
 
	if(m_bCheckWordsOnly) theSet.bCheckOnlyWords=TRUE; 
	else theSet.bCheckOnlyWords=FALSE; 
	 
	if(m_bIgnoreLastWordPunct) theSet.bLastWordMayNotHavePunct=TRUE; 
	else theSet.bLastWordMayNotHavePunct=FALSE; 
 
	if(!((m_lpszSearchText=="")&&(sVirusStrain==""))) 
	{ 
		if(m_lpszSearchText!="") sVirusStrain=m_lpszSearchText; 
		if(m_lpszReplacementString!="") sReplacementString=m_lpszReplacementString; 
		CString sMessage="You asked to search for \""+sVirusStrain+"\""; 
		if(sReplacementString!="") sMessage+=" with replacement \""+sReplacementString+"\""; 
		else sMessage+=", and be removed after a successful search."; 
		sMessage+="\nCompleting request.."; 
		AfxMessageBox(sMessage,MB_ICONINFORMATION|MB_OK); 
	} 
	theSet.bCaseSensetive=m_bCaseSensetive; 
	theSet.bOverWriteFile=m_bOverWriteFile; 
	theSet.bReplaceLFWithCRLF=m_bReplaceLFWithCRLF; 
	theSet.bReplaceCRLFWithLF=m_bReplaceCRLFWithLF; 
	 
	CCleanR theR(theSet); //call the parametrized constructor to send the options 
	theR.SetFileName(sFilePath); 
	theR.SetReplacementString(sReplacementString); 
	theR.SetSearchString(sVirusStrain); 
	DWORD theD=theR.Process(); 
	if(theD>0) 
	{ 
		char szMsg[256]; 
		wsprintf(szMsg,"There were %d matches",theD); 
		AfxMessageBox(szMsg); 
	} 
	CDialog::OnOK(); //This default statement closes the dialog - but don't comment it out to revive the dialog ! 
} 
void CCleanRDlg::OnBrowse()  
{ 
	//This will open the 'thing' to select our file to operate on 
	CFileDialog dlg(TRUE,NULL,sFilePath,OFN_FILEMUSTEXIST,"Any text file(*.*)|*.*||",this); 
	if(dlg.DoModal()==IDOK) sFilePath=dlg.GetFileName(); 
	else return; //User cancelled 
	 
} 
 
void CCleanRDlg::OnChangeSearchText()  
{ 
	UpdateData(); //Without this, m_lpszSearchText will not be updated instantly !  
	/* 
	If Search string is NULL, there should be no replacement string. 
	*/ 
	if(m_lpszSearchText=="") 
	{ 
		GetDlgItem(IDC_CHECK5)->EnableWindow(TRUE);//Enable the check box for Search string 
		GetDlgItem(IDC_EDIT2)->EnableWindow(FALSE); 
		GetDlgItem(IDC_CHECK7)->EnableWindow(FALSE);//Disable the check box for Replacement string 
	} 
	else  
	{ 
		GetDlgItem(IDC_CHECK5)->EnableWindow(FALSE);//Disable the check box for Search string 
		GetDlgItem(IDC_EDIT2)->EnableWindow(TRUE); 
		GetDlgItem(IDC_CHECK7)->EnableWindow(TRUE);//Enable the check box for Replacement string 
	} 
} 
 
void CCleanRDlg::OnChangeReplacementString()  
{ 
	UpdateData(); 
} 
 
void CCleanRDlg::OnConvertLFtoCRLF()  
{ 
	UpdateData();	 
} 
 
void CCleanRDlg::OnStrictGrammar()  
{ 
	UpdateData();	 
	if(m_bStrict)  
	{ 
		GetDlgItem(IDC_CHECK3)->EnableWindow(TRUE); 
		GetDlgItem(IDC_CHECK2)->EnableWindow(FALSE); //Being strict with grammar automatically will qualify ONLY words and NOT sub strings. 
	} 
	else  
	{ 
		GetDlgItem(IDC_CHECK3)->EnableWindow(FALSE); 
		GetDlgItem(IDC_CHECK2)->EnableWindow(TRUE);  
	} 
} 
 
void CCleanRDlg::OnIgnoreLastWordPunct()  
{ 
	UpdateData(); 
	 
} 
 
void CCleanRDlg::OnCheckWordsOnly()  
{ 
	UpdateData();	 
} 
 
 
void CCleanRDlg::OnCaseSensetiveOption()  
{ 
	UpdateData(); 
} 
 
void CCleanRDlg::OnConvertCRLFtoLF()  
{ 
	UpdateData(); 
} 
 
void CCleanRDlg::OnOverWriteFile()  
{ 
	UpdateData(); 
	int nRet=IDNO; 
	if(m_bOverWriteFile) nRet=MessageBox("Use this option with EXTREME caution\nAre you sure !","Overwrite original file confirmation",MB_ICONQUESTION|MB_YESNO); 
	if(nRet!=IDYES) m_bOverWriteFile=FALSE; 
	//This routine overwrites the existing file - use with EXTREME caution	 
} 
 
void CCleanRDlg::OnGetSearchStringFromFile()  
{ 
	UpdateData(); 
	if(m_bSearchStringFromFile)  
	{ 
		GetDlgItem(IDC_CHECK7)->EnableWindow(TRUE);//Enable the check box for Replacement string 
		GetDlgItem(IDC_EDIT2)->EnableWindow(TRUE); 
		GetDlgItem(IDC_EDIT1)->EnableWindow(FALSE);//Disable search string box 
		/* 
		Now lets's show the file open dialog, and read in the text data 
		*/ 
		CFileDialog dlg(TRUE,NULL,sFilePath,OFN_FILEMUSTEXIST,"Any text(ASCII) file|*.*||",this); 
		if(dlg.DoModal()==IDOK) sFilePath=dlg.GetFileName(); 
		else  
		{ 
			m_bSearchStringFromFile=FALSE; 
			return; //User cancelled 
		} 
		HANDLE hFile=CreateFile((LPCTSTR)sFilePath,GENERIC_READ,FILE_SHARE_READ,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL); 
		if(hFile==INVALID_HANDLE_VALUE)  
		{ 
			AfxMessageBox(("Error opening file containing search string.Will exit.\n["+sFilePath+"]") ,MB_OK|MB_ICONINFORMATION); 
			return; 
		}	 
		DWORD dwBufferSize=GetFileSize(hFile,NULL),dwBytesRead=0; 
		 
		if(dwBufferSize==-1) return;//Could not get file size 
		BOOL bResult=ReadFile(hFile,m_lpszSearchText.GetBuffer(dwBufferSize),dwBufferSize,&dwBytesRead,NULL); //Debug Alert !! CString::GetBuffer() called, now NOTHING but ReleaseBuffer() to be called. 
		m_lpszSearchText.ReleaseBuffer(); //Done CString::ReleaseBuffer(). Object's now usable again 
		CloseHandle(hFile); 
		if((!bResult)||(dwBufferSize!=dwBytesRead)) 
		{ 
			AfxMessageBox(("File read error! Aborting..\n["+sFilePath+"]") ,MB_OK|MB_ICONINFORMATION); 
			return; 
		} 
		//m_lpszSearchText now contains the WHOLE text file containing the search string 
		sVirusStrain=m_lpszSearchText; 
	} 
	else if(m_lpszSearchText=="") 
	{ 
		GetDlgItem(IDC_CHECK7)->EnableWindow(FALSE);//Disable the check box for Replacement string 
		GetDlgItem(IDC_EDIT2)->EnableWindow(FALSE); 
		GetDlgItem(IDC_EDIT1)->EnableWindow(TRUE);//Enable search string box 
	} 
	else GetDlgItem(IDC_EDIT1)->EnableWindow(TRUE);//Enable search string box 
	sFilePath=""; 
} 
 
void CCleanRDlg::OnReplaceFromFile()  
{ 
	UpdateData();	 
	if(m_bReplacementFromFile) 
	{ 
		GetDlgItem(IDC_EDIT2)->EnableWindow(FALSE);//Disable the edit box for Replacement string 
		/* 
		Now lets's show the file open dialog, and read in the text data 
		*/ 
		CFileDialog dlg(TRUE,NULL,sFilePath,OFN_FILEMUSTEXIST,"Any text(ASCII) file|*.*||",this); 
		if(dlg.DoModal()==IDOK) sFilePath=dlg.GetFileName(); 
		else  
		{ 
			m_bReplacementFromFile=FALSE; 
			return; //User cancelled 
		} 
		HANDLE hFile=CreateFile((LPCTSTR)sFilePath,GENERIC_READ,FILE_SHARE_READ,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL); 
		if(hFile==INVALID_HANDLE_VALUE)  
		{ 
			AfxMessageBox(("Error opening file containing search string.Will exit.\n["+sFilePath+"]") ,MB_OK|MB_ICONINFORMATION); 
			return; 
		}	 
		DWORD dwBufferSize=GetFileSize(hFile,NULL),dwBytesRead=0; 
		 
		if(dwBufferSize==-1) return;//Could not get file size 
		BOOL bResult=ReadFile(hFile,m_lpszSearchText.GetBuffer(dwBufferSize),dwBufferSize,&dwBytesRead,NULL); //Debug Alert !! CString::GetBuffer() called, now NOTHING but ReleaseBuffer() to be called. 
		m_lpszSearchText.ReleaseBuffer(); //Done CString::ReleaseBuffer(). Object's now usable again 
		CloseHandle(hFile); 
		if((!bResult)||(dwBufferSize!=dwBytesRead)) 
		{ 
			AfxMessageBox(("File read error! Aborting...\n["+sFilePath+"]") ,MB_OK|MB_ICONINFORMATION); 
			return; 
		} 
		//m_lpszReplacementString now contains the WHOLE text file containing the replacement string 
		sReplacementString=m_lpszReplacementString; 
	} 
	else if((m_lpszSearchText!="")||m_bSearchStringFromFile) 
	{ 
		GetDlgItem(IDC_EDIT2)->EnableWindow(TRUE);//Enable the edit box for Replacement string 
	} 
	sFilePath=""; 
}