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

📄 fileiodlg.cpp

📁 我要下载源代码我要源代码我要下载代码我要下载源代码
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// FileIODialog.cpp : implementation file
//

#include "stdafx.h"
#include "FileIO.h"
#include "FileIODlg.h"

#include "HeaderFileDlg.h"

#include <direct.h>

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CFileIODlg dialog


CFileIODlg::CFileIODlg(CWnd* pParent /*=NULL*/)
	: CDialog(CFileIODlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CFileIODlg)
	m_bCheckValidate = FALSE;
	m_strDescriptive = _T("");
	m_nMediaType = -1;
	m_strFileFullName = _T("");
	m_nFileCrc = 0;
	m_nFileLength = 0;
	m_nFileVersion = 0;
	m_nHeaderSize = 0;
	m_nMediaFlags = 0;
	m_nNumberHeaders = 0;
	m_strPartNumber = _T("");
	m_nPnHash = 0;
	//}}AFX_DATA_INIT
	m_strDirectory .Empty();

}


void CFileIODlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CFileIODlg)
	DDX_Control(pDX, IDC_LST_DR_FILE_DESCRIPTORS, m_lstDRFileDescriptions);
	DDX_Control(pDX, IDC_EDT_NUMBER_HEADERS, m_edtNumberHeaders);
	DDX_Control(pDX, IDC_EDT_HEADER_SIZE, m_edtHeaderSize);
	DDX_Control(pDX, IDC_EDT_FILE_LENGTH, m_edtFileLength);
	DDX_Control(pDX, IDC_BUTTON_EDIT, m_btnEdit);
	DDX_Control(pDX, IDC_BUTTON_DELETE, m_btnDelete);
	DDX_Check(pDX, IDC_CHECK_VALIDATE, m_bCheckValidate);
	DDX_Text(pDX, IDC_EDT_DESCRIPTIVE_TEXT, m_strDescriptive);
	DDV_MaxChars(pDX, m_strDescriptive, 80);
	DDX_CBIndex(pDX, IDC_CMB_MEDIA_TYPE, m_nMediaType);
	DDX_Text(pDX, IDC_EDIT_FILENAME, m_strFileFullName);
	DDX_Text(pDX, IDC_EDT_FILE_CRC, m_nFileCrc);
	DDX_Text(pDX, IDC_EDT_FILE_LENGTH, m_nFileLength);
	DDX_Text(pDX, IDC_EDT_FILE_VERSION, m_nFileVersion);
	DDX_Text(pDX, IDC_EDT_HEADER_SIZE, m_nHeaderSize);
	DDX_Text(pDX, IDC_EDT_MEDIA_FLAGS, m_nMediaFlags);
	DDX_Text(pDX, IDC_EDT_NUMBER_HEADERS, m_nNumberHeaders);
	DDX_Text(pDX, IDC_EDT_PART_NUMBER, m_strPartNumber);
	DDV_MaxChars(pDX, m_strPartNumber, 40);
	DDX_Text(pDX, IDC_EDT_PN_HASH, m_nPnHash);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CFileIODlg, CDialog)
	//{{AFX_MSG_MAP(CFileIODlg)
	ON_COMMAND(ID_FILE_EXIT, OnFileExit)
	ON_COMMAND(ID_FILE_NEW, OnFileNew)
	ON_COMMAND(ID_FILE_OPEN, OnFileOpen)
	ON_COMMAND(ID_FILE_SAVE, OnFileSave)
	ON_COMMAND(ID_FILE_SAVEAS, OnFileSaveas)
	ON_BN_CLICKED(IDC_BUTTON_ADD, OnButtonAdd)
	ON_BN_CLICKED(IDC_BUTTON_DELETE, OnButtonDelete)
	ON_BN_CLICKED(IDC_BUTTON_EDIT, OnButtonEdit)
	ON_BN_CLICKED(IDC_CHECK_VALIDATE, OnCheckValidate)
	ON_NOTIFY(NM_CLICK, IDC_LST_DR_FILE_DESCRIPTORS, OnClickLstDrFileDescriptors)
	ON_CBN_SELCHANGE(IDC_CMB_MEDIA_TYPE, OnSelchangeCmbMediaType)
	ON_EN_CHANGE(IDC_EDT_DESCRIPTIVE_TEXT, OnChangeEdtDescriptiveText)
	ON_EN_CHANGE(IDC_EDT_FILE_CRC, OnChangeEdtFileCrc)
	ON_EN_CHANGE(IDC_EDT_FILE_VERSION, OnChangeEdtFileVersion)
	ON_EN_CHANGE(IDC_EDT_MEDIA_FLAGS, OnChangeEdtMediaFlags)
	ON_EN_CHANGE(IDC_EDT_PART_NUMBER, OnChangeEdtPartNumber)
	ON_EN_CHANGE(IDC_EDT_PN_HASH, OnChangeEdtPnHash)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CFileIODlg message handlers

void CFileIODlg::OnFileExit() 
{
	// TODO: Add your command handler code here
	if(m_bNeedSave)
	{
		int answer = AfxMessageBox("File has not been saved yet, save it now?",MB_YESNOCANCEL);
		if(answer==IDYES)
			OnFileSave();
		else if(answer == IDCANCEL)
			return;
	}

	CDialog::OnCancel();
	
}

void CFileIODlg::OnFileNew() 
{
	// TODO: Add your command handler code here
	if(m_bNeedSave)
	{
		int answer = AfxMessageBox("File has not been saved yet, save it now?",MB_YESNOCANCEL);
		if(answer==IDYES)
			OnFileSave();
		else if(answer == IDCANCEL)
			return;
	}

	m_strFileName.Empty();
	m_strDirectory.Empty();
	m_strFileFullName = m_strDirectory + m_strFileName;
	m_bNeedSave = true;

	memset(&m_FileData, 0, sizeof(DRFileFormat));
	LoadData();

	UpdateData(false);
	m_btnDelete.EnableWindow(false);
	m_btnEdit.EnableWindow(false);
	
}

void CFileIODlg::OnFileOpen() 
{
	// TODO: Add your command handler code here

	if(m_bNeedSave)
	{
		int answer = AfxMessageBox("File has not been saved yet, save it now?",MB_YESNOCANCEL);
		if(answer==IDYES)
			OnFileSave();
		else if(answer == IDCANCEL)
			return;
	}

	CFileDialog dlg(true,NULL,NULL,OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT);

	dlg.m_ofn.lpstrInitialDir = m_strDirectory;
	dlg.m_ofn.lpstrTitle = "Select a DR file file";
	dlg.m_ofn.lpstrFilter = "DR Files(*.*dr*)\0*.*dr*\0\0";
	int len = m_strDirectory.GetLength() - m_strDirectory.ReverseFind('\\');
	strcpy(dlg.m_ofn.lpstrFile,  m_strDirectory.Right(len - 1));
	if(dlg.DoModal() == IDOK)
	{
		m_strDirectory = dlg.GetPathName();
		CString strFileType =  m_strDirectory.Right(2);
		strFileType.MakeUpper();
		if(strFileType == "DR")
		{
			int len1 = m_strDirectory.GetLength() - m_strDirectory.ReverseFind('\\');
			int len2 = m_strDirectory.ReverseFind('\\');

			m_strFileName = m_strDirectory.Right(len1 - 1);
			m_strDirectory = m_strDirectory.Left(len2+1);

			m_strFileFullName = m_strDirectory + m_strFileName;
			ReadFile(m_strDirectory + m_strFileName);
			LoadData();
			m_bNeedSave = false;

			UpdateData(false);
			m_btnDelete.EnableWindow(false);
			m_btnEdit.EnableWindow(false);
		}
		else
			AfxMessageBox("It is not a DR File.");

	}
	
}

void CFileIODlg::OnFileSave() 
{
	// TODO: Add your command handler code here
	UpdateData(true);

	if(m_strFileFullName.IsEmpty())
	{
		CFileDialog dlg(false,NULL,NULL,OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT);
		dlg.m_ofn.lpstrInitialDir = m_strDirectory;
		dlg.m_ofn.lpstrTitle = "Save DR file";
		dlg.m_ofn.lpstrFilter = "DR Files(*.dr)\0*.dr\0\0";
		strcpy(dlg.m_ofn.lpstrFile, m_strFileName);
		if(dlg.DoModal() == IDOK)
		{
			CString strFullName = dlg.GetPathName();
			Save(strFullName);
		}
	}
	else
	{
		SaveData();
		WriteFile(m_strDirectory + m_strFileName);
	}
	
}

void CFileIODlg::OnFileSaveas() 
{
	// TODO: Add your command handler code here
	CFileDialog dlg(false,NULL,NULL,OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT);
	dlg.m_ofn.lpstrInitialDir = m_strDirectory;
	dlg.m_ofn.lpstrTitle = "Save as DR file";
	dlg.m_ofn.lpstrFilter = "DR Files(*.dr)\0*.dr\0\0";
	strcpy(dlg.m_ofn.lpstrFile, m_strFileName);
	if(dlg.DoModal() == IDOK)
	{
		CString strFullName = dlg.GetPathName();
		Save(strFullName);
	}	
	
}

void CFileIODlg::Save(CString & strFullName)
{
	if(strFullName.Find(".",0) ==-1)
		strFullName += ".dr";
	int len1 = strFullName.ReverseFind('\\');
	int len2 = strFullName.GetLength() - len1;
	m_strDirectory = strFullName.Left(len1+1);
	m_strFileName = strFullName.Right(len2-1);
	if(!CheckFile(strFullName))
		return;
	SaveData();
	WriteFile(strFullName);
	m_strFileFullName = m_strDirectory + m_strFileName;
	UpdateData(false);
}

bool CFileIODlg::CheckFile(CString strFileName)
{
	int nDirectoryLen = strFileName.ReverseFind('\\');
	CString strDirectory = strFileName.Left(nDirectoryLen);
	CFileFind  find;
	if(!find.FindFile(strDirectory + "\\*.*"))
	{
		int i=AfxMessageBox("The destination directory does not exist, do you want to create it?",MB_YESNO|MB_ICONINFORMATION);
		if(i == IDYES)
		{
			CreateDirectory(strDirectory);
			return true;
		}
		else
			return false; 
	}
	return true;
}


void CFileIODlg::CreateDirectory(CString strDirectory)
{
	CString strParent = strDirectory.Left(strDirectory.ReverseFind('\\'));
	CFileFind find;
	if(!find.FindFile(strParent + "\\*.*"))
		CreateDirectory(strParent);
	_mkdir(strDirectory);

}


void CFileIODlg::OnButtonAdd() 
{
	// TODO: Add your control notification handler code here
	UpdateData(true);
	CHeaderFileDlg dlg;
	if(dlg.DoModal() == IDOK)
	{
		CString strText1,strText2, strText3;
		strText1.Format("%d",dlg.m_nFileSize);
		strText2.Format("%d",dlg.m_nFileUser);
		strText3.Format("%d",dlg.m_nFileIntegrity);
		m_lstDRFileDescriptions.InsertItem(m_nNumberHeaders,dlg.m_strFilePath);
		m_lstDRFileDescriptions.SetItemText(m_nNumberHeaders,1,dlg.m_strFileName);
		m_lstDRFileDescriptions.SetItemText(m_nNumberHeaders,2,strText1);
		m_lstDRFileDescriptions.SetItemText(m_nNumberHeaders,3,strText2);
		m_lstDRFileDescriptions.SetItemText(m_nNumberHeaders,4,strText3);
	    m_bNeedSave = true;
	}
	CalculateHeaderSize();
    CalculateFileLength();
	UpdateData(false);
	
}

void CFileIODlg::OnButtonDelete() 
{
	// TODO: Add your control notification handler code here
	int answer = AfxMessageBox("Are you sure?", MB_YESNO);
	if(answer == 6)
	{
		POSITION pos = m_lstDRFileDescriptions.GetFirstSelectedItemPosition( );
		int Index = m_lstDRFileDescriptions.GetNextSelectedItem( pos );
		m_lstDRFileDescriptions.DeleteItem(Index);
		m_bNeedSave = true;
		CalculateHeaderSize();
		CalculateFileLength();
		UpdateData(false);
		m_btnDelete.EnableWindow(false);
		m_btnEdit.EnableWindow(false);
	}	
	
}

void CFileIODlg::OnButtonEdit() 
{
	// TODO: Add your control notification handler code here
	CHeaderFileDlg dlg;
	POSITION pos = m_lstDRFileDescriptions.GetFirstSelectedItemPosition( );
	int Index = m_lstDRFileDescriptions.GetNextSelectedItem( pos );
	dlg.m_strFilePath = m_lstDRFileDescriptions.GetItemText(Index, 0);
	dlg.m_strFileName = m_lstDRFileDescriptions.GetItemText(Index, 1);

	CString strTemp = m_lstDRFileDescriptions.GetItemText(Index, 2 );
	dlg.m_nFileSize = StrToInt(strTemp);
	strTemp = m_lstDRFileDescriptions.GetItemText(Index, 3 );
	dlg.m_nFileUser = StrToInt(strTemp);
	strTemp = m_lstDRFileDescriptions.GetItemText(Index, 4 );
	dlg.m_nFileIntegrity = StrToInt(strTemp);

	if(dlg.DoModal() == IDOK)
	{
		m_lstDRFileDescriptions.SetItemText( Index, 0, dlg.m_strFilePath );
		m_lstDRFileDescriptions.SetItemText( Index, 1, dlg.m_strFileName );
		CString strText1,strText2, strText3;

⌨️ 快捷键说明

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