📄 md5checksumtestdlg.cpp
字号:
{
//get the full path and name of the file and checksum it
m_strSelectedFile = FileDialog.GetPathName();
ChecksumSelectedFile();
}
}
/*****************************************************************************************
FUNCTION: CMD5ChecksumTestDlg::OnButtonAbout
DETAILS: public
DESCRIPTION: Displays the Help-About dialog
RETURNS: void
ARGUMENTS: none
NOTES: none
*****************************************************************************************/
void CMD5ChecksumTestDlg::OnButtonAbout()
{
CAboutDlg AboutDlg;
AboutDlg.DoModal();
}
/*****************************************************************************************
FUNCTION: CMD5ChecksumTestDlg::EnableCtrls
DETAILS: protected
DESCRIPTION: Enable or disables user interface ccontrols
RETURNS: void
ARGUMENTS: bool bEnable - enable or disable the controls
NOTES: Controls need to be disabled when a checksum calculation is being performed
(because they will not respond while the calculation is being performed -
the program is not multi-threaded!)
*****************************************************************************************/
void CMD5ChecksumTestDlg::EnableCtrls(bool bEnable /*= true */)
{
GetDlgItem(IDC_BUTTON_SELFTEST)->EnableWindow(bEnable && m_bTestDataFolderFound);
GetDlgItem(IDC_BUTTON_SEL_FILE)->EnableWindow(bEnable);
GetDlgItem(IDC_BUTTON_ABOUT)->EnableWindow(bEnable);
GetDlgItem(IDC_EDIT_STR)->EnableWindow(bEnable);
GetDlgItem(IDC_EDIT_SEL_FILE)->EnableWindow(bEnable);
}
/*****************************************************************************************
FUNCTION: CMD5ChecksumTestDlg::OnOK
DETAILS: protected, message map function for IDOK
DESCRIPTION: Checksums the entry in the "Select a File.." edit box when "Enter" is pressed.
RETURNS: void
ARGUMENTS: none
NOTES: Called whenever the Return (or Enter) key is pressed when the
"IDC_EDIT_SEL_FILE" edit box currently has focus. This function allows the
user to enter a file name manually and then initiate the checksum on the file
by pressing 'Enter'.
*****************************************************************************************/
void CMD5ChecksumTestDlg::OnOK()
{
//if there is an entry in the "Select a file..." edit box and it is the selected control
UpdateData();
if ( m_strSelectedFile.GetLength() > 0 && GetFocus() == GetDlgItem(IDC_EDIT_SEL_FILE) )
{
//get its checksum
ChecksumSelectedFile();
}
}
/*****************************************************************************************
FUNCTION: CMD5ChecksumTestDlg::OnCancel
DETAILS: protected, virtual, overrides CDialog::OnCancel
DESCRIPTION: Exits the application after user confirmation
RETURNS: void
ARGUMENTS: none
NOTES: This function is called when the user attempts to exit the application.
Its main purpose is to prevent accidental termination should the user
mistakenly press the 'Esc' key. It also warns the user if the performance log
(if any) has not yet been saved.
*****************************************************************************************/
void CMD5ChecksumTestDlg::OnCancel()
{
bool bExit = false;
if ( m_PerformanceLogDlg.IsDirty() )
{
bExit = (AfxMessageBox("The performance log has not been saved.\nExit without saving?", MB_YESNO) == IDYES );
}
else
{
bExit = ( AfxMessageBox("Do you want to exit?", MB_YESNO ) == IDYES );
}
if ( bExit )
{
CDialog::OnCancel();
}
}
/*****************************************************************************************
FUNCTION: CMD5ChecksumTestDlg::ChecksumSelectedFile
DETAILS: protected
DESCRIPTION: Calculates and displays the checksum for the file named in 'm_strSelectedFile'.
RETURNS: void
ARGUMENTS: none
NOTES: The function does not check that m_strSelectedFile represents a valid file
name before attempting to get the checksum. If the filename is not valid,
GetMD5 throws a CFileException that this function catches.
*****************************************************************************************/
void CMD5ChecksumTestDlg::ChecksumSelectedFile()
{
//check that a file has been selected
ASSERT( m_strSelectedFile.GetLength() > 0 );
//display a wait message while the checksum is calculated
m_strFileChecksum.LoadString(IDS_PLEASE_WAIT);
EnableCtrls(false);
UpdateDataNow(FALSE);
//calculate the checksum
try
{
m_strFileChecksum = CMD5Checksum::GetMD5( m_strSelectedFile );
}
//tell the user the calculation failed if a CFileException is thrown
catch(CFileException* e)
{
m_strFileChecksum = CString("Fail");
EnableCtrls(true);
UpdateData(FALSE);
throw e;
}
//no exception was thrown - display the checksum result
EnableCtrls(true);
UpdateData(FALSE);
}
/*****************************************************************************************
FUNCTION: CMD5ChecksumTestDlg::LoadTestDataPath
DETAILS: protected
DESCRIPTION: Loads 'm_strTestDataPath' with the pathname of the test data folder.
RETURNS: bool : true if the folder is found, false otherwise
ARGUMENTS: none
NOTES: The data files used in the Self Test function are held in a folder "TestData".
In the target environment (ie, a non-development environment), this folder
should be located in the same folder as the executable. However, in a
development environment, the executable will be held in either the "debug" or
"release" folder, while the "TestData' folder will be held one level higher, ie,
with the programs source code. This function determines the path of the TestData
folder, taking account of three scenarios;
1) the release version of the program is being run standalone from the
"Release" folder
2) the debug version of the program is being run standalone from the
"Debug" folder
3) the release or debug program is being run from within the development
environment
*****************************************************************************************/
bool CMD5ChecksumTestDlg::LoadTestDataPath()
{
const CString strTestDataFolder("\\TestData");
//get the current folder
char szCurDir[512];
GetCurrentDirectory(512,szCurDir);
CString strCurDir(szCurDir);
//if the release version of the program is being run standalone from the "Release" folder
if (strCurDir.Right(7) == "Release")
{
m_strTestDataPath = strCurDir.Left( strCurDir.GetLength() - 7 ) + strTestDataFolder;
}
else
//if the debug version of the program is being run standalone from the "Debug" folder
if (strCurDir.Right(5) == "Debug")
{
m_strTestDataPath = strCurDir.Left( strCurDir.GetLength() - 5 ) + strTestDataFolder;
}
else
{
//program is being run from same folder as \\TestData
m_strTestDataPath = strCurDir + CString("\\TestData");
}
//check the folder exists (nb, no need to check it is an actual folder.)
CFileFind FileFind;
return ( FileFind.FindFile(m_strTestDataPath) ? true : false );
}
/*****************************************************************************************
FUNCTION: CMD5ChecksumTestDlg::OnButtonPerformanceTest
DETAILS: public message map function
DESCRIPTION: Times the calculation of several MD5 checksum calculations
RETURNS: none
ARGUMENTS: none
NOTES: This function times a fixed sequence of MD5 checksum calculations. Its
purpose is to facilitate fine tuning of the implementation for improved
performance. Comparison of performance figures provided by this function
will indicate whether modifications have achieved a measurable performance
improvement.
Two types of 'timer' are used. CTimerSecs returns the elapsed time in seconds.
'CTimerCycles' returns the number of elapsed processor cycles. The tests are
run twice, once for each type of timer.
Each test consists of a sequence of short tests followed by one long test.
The short tests are performed 50 times to ensure that performance improvements
that relate mostly to short calculations are reflected in the test results (other-
wise the time taken to perform them would be insignificant compared to the long
test.) The long test is performed by first loading the contents of the test
data into memory (this avoids the file read operation influencing the
performance). After the long test, the relevant memory is freed.
Test results are saved in a log and may be displayed on screen. The log
may also be saved to disc as a text file.
NB. This function makes no attempt to change its threads priority. Thus, it
could be interrupted by other processor activities. To ensure consistency and
comparison of like with like, run tests in the same 'clean' environment. This
is best achieved by rebooting windows, closing all unnecessary programs and
services, and then running the test. Consecutive timings of the same sequence
of individual tests can vary by up to 15%. It is therefore necessary to perform
the tests repeatedly and average the results. The performance dialog provides
an automatic summation and averaging facility.
*****************************************************************************************/
void CMD5ChecksumTestDlg::OnButtonPerformanceTest()
{
// show the user a wait cursor while the performance test executes
CWaitCursor WaitCursor;
// if the algorithm passes self test
if (SelfTest())
{
// read the test file into memory
const int nTestFileSize = LoadLargeTestFile();
// start the seconds timer
CTimerSecs TimerSecs;
TimerSecs.Start();
// perform a sequence of short tests
for (int i=0; i<50; i++)
{
SelfTest01();
SelfTest05();
SelfTest09();
SelfTest12();
SelfTest15();
SelfTest15a();
SelfTest18();
SelfTest18a();
SelfTest21();
SelfTest21a();
}
// perform the long test
CMD5Checksum::GetMD5( m_pTestFileBuffer, nTestFileSize );
// stop the seconds timer
m_dPerformanceSecs = TimerSecs.Stop();
// start the cycles timer
CTimerCycles TimerCycles;
TimerCycles.Start();
// perform a sequence of short tests
for (int ii=0; i<50; i++);
{
SelfTest01();
SelfTest05();
SelfTest09();
SelfTest12();
SelfTest15();
SelfTest15a();
SelfTest18();
SelfTest18a();
SelfTest21();
SelfTest21a();
}
// perform the long test
CMD5Checksum::GetMD5( m_pTestFileBuffer, nTestFileSize );
// stop the cycles timer
m_dPerformanceCycles = TimerCycles.Stop();
// add the results of both timers to the performance log
m_PerformanceLogDlg.AddLogEntry(m_dPerformanceSecs, m_dPerformanceCycles);
// free the allocated memory
UnloadLargeTestFile();
}
else
{
// if the algorithm failed its self test, report this
m_dPerformanceSecs = -1.0;
AfxMessageBox("Self Test Failed.");
}
// refresh the display of results
UpdateData(FALSE);
}
/*****************************************************************************************
FUNCTION: CMD5ChecksumTestDlg::OnButtonShowLog
DETAILS: public message map function
DESCRIPTION: Displays the performance log dialog
RETURNS: none
ARGUMENTS: none
NOTES: none
*****************************************************************************************/
void CMD5ChecksumTestDlg::OnButtonShowLog()
{
m_PerformanceLogDlg.DoModal();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -