|
上节我们已经讲解了ppf文件的结构,这节我们将继续讲解读写代码部分。使用文件结构来读写文件可以方便有效的读取和写入而又不会出错,通过结构体有效的匹配每个文件体中的各变量。
从上图可以看出借个工程文件操作函数:OnFileProjectOpen()打开工程文件、OnFileProjectSaveAs()保存工程文件、OnFileProjectAttribute()设置工程属性、OnFileProjectClose()关闭工程。打开和保存基本就是两个对立的操作过程,也是整个操作的难点。
1、打开工程
- void CUUProgDlg::OnFileProjectOpen()
- {
- // TODO: Add your command handler code here
- CFile ldfile;
- CString m_sFileExt;
- SetAppLog("加载工程开始...");
- //保存工程文件代码
- CFileDialog mDlg(TRUE,".PPF",NULL,OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,"编程器工程文件(*.ppf)|*.ppf||",this);
- if(mDlg.DoModal() == IDOK)
- {
- m_sProjectFilename = mDlg.GetPathName();
- m_sFileExt = mDlg.GetFileExt();
- m_sFileExt.MakeUpper();
- //加载文件
- if(!m_sProjectFilename.IsEmpty())
- {
- SetAppLog("加载文件:"+m_sProjectFilename);
- if(ldfile.Open(m_sProjectFilename,CFile::shareCompat|CFile::modeRead,NULL))
- {
- int nFileSize;
- nFileSize=ldfile.GetLength();
- if(m_sFileExt=="PPF")
- {
- char* pFileBuf=(char*)GlobalAlloc(GMEM_FIXED,nFileSize+1);
- memset(pFileBuf,0,nFileSize+1);
- ldfile.Read(pFileBuf,nFileSize);
- memcpy(&m_stPFileHead,pFileBuf,sizeof(ProjectFileHeadStruct));
- memcpy(&m_stPFile,&pFileBuf[sizeof(ProjectFileHeadStruct)],sizeof(ProjectFileStruct));
- CProjectPasswordDlg passworddlg;
-
- if(passworddlg.DoModal() == IDOK)
- {
- if(m_stPFile.AccessPassword != passworddlg.m_sProjectPassword)
- {
- SetAppLog("访问密码错误");
- AfxMessageBox("访问密码错误");
- GlobalFree(pFileBuf);
- ldfile.Close();
- ZeroMemory(&m_stPFile, sizeof(ProjectFileStruct));
- ZeroMemory(&m_stPFileHead, sizeof(ProjectFileHeadStruct));
- return;
- }
- }
- else
- {
- GlobalFree(pFileBuf);
- ldfile.Close();
- SetAppLog("取消加载工程");
- ZeroMemory(&m_stPFile, sizeof(ProjectFileStruct));
- ZeroMemory(&m_stPFileHead, sizeof(ProjectFileHeadStruct));
- return;
- }
- memcpy(pProgBuff,&pFileBuf[sizeof(ProjectFileHeadStruct)+sizeof(ProjectFileStruct)],nFileSize-sizeof(ProjectFileHeadStruct)-sizeof(ProjectFileStruct));
- GlobalFree(pFileBuf);
- ldfile.Close();
- SetAppLog("加载完成:"+Int2Str(nFileSize)+"字节");
- SetMenuItem(false);
- SetLoadFileName(m_sProjectFilename);
- CString temp;
- for (int i=0; i<m_arDeviceList.GetSize(); i++)
- {
- temp = m_arDeviceList.GetAt(i).DeviceName;
- if(m_stPFile.DeviceName == temp)
- {
- SetDeviceInfo(i);
- break;
-
- }
- }
- }
- else
- {
- SetAppLog("文件格式不支持");
- AfxMessageBox("文件格式不支持");
- }
- }
- else
- {
- SetAppLog("加载文件失败");
- AfxMessageBox("加载指定的文件出错");
- }
- }
- }
-
- else
- {
- SetAppLog("取消加载文件");
- }
-
- }
复制代码 2、保存工程
- void CUUProgDlg::OnFileProjectSaveAs()
- {
- // TODO: Add your command handler code here
- CString projectfilename;
- CFile savefile;
- CString m_sFileExt;
- int buffsize;
- SetAppLog("工程保存开始...");
- buffsize = GetMinLength(pProgBuff,m_dDeviceSize);
- if (buffsize ==0)
- {
- SetAppLog("缓冲区无数据,取消保存");
- AfxMessageBox("缓冲区无数据,不需保存");
- return;
- }
- CFileDialog mDlg(FALSE,".PPF",NULL,OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,"编程器工程文件(*.ppf)|*.ppf||",this);
- if(mDlg.DoModal() == IDOK)
- {
- projectfilename = mDlg.GetPathName();
- m_sFileExt = mDlg.GetFileExt();
- m_sFileExt.MakeUpper();
- SetAppLog("保存为:"+projectfilename);
- //保存文件代码
- if (m_sFileExt=="PPF")
- {
- if(savefile.Open(projectfilename,CFile::shareCompat|CFile::modeCreate|CFile::modeWrite,NULL))
- {
- m_stPFile.BuffCheckCRC = m_dBuffCheckCRC;
- m_stPFileHead.ProjectFileCRC = m_dProjectFileCRC;
- m_stPFile.BuffSize = buffsize;
- strcpy(m_stPFile.UUProgSN,m_sUUProgSN);
- strcpy(m_stPFileHead.Appname,"UUProg V"+m_sAppVer);
- strcpy(m_stPFileHead.ProjectFileVer,m_sAppVer);
- strcpy(m_stPFile.DeviceName,m_sDeviceName);
- //m_editBuf2.GetData(m_Pro.m_pReadBuf,0x10000);
- savefile.Write(&m_stPFileHead,sizeof(ProjectFileHeadStruct));
- savefile.Write(&m_stPFile,sizeof(ProjectFileStruct));
- savefile.Write(pProgBuff,buffsize);
- savefile.Close();
- SetAppLog("保存完成:"+Int2Str(sizeof(ProjectFileHeadStruct)+sizeof(ProjectFileStruct)+buffsize)+"字节");
- }
- }
- else
- {
- SetAppLog("文件格式不支持");
- AfxMessageBox("文件格式不支持");
- }
- }
- else
- {
- SetAppLog("取消保存");
- }
- }
复制代码 3、工程属性操作
- void CProgProjectAttribute::OnOk()
- {
- // TODO: Add your control notification handler code here
- UpdateData(true);
- if (parent->m_sProjectFilename.IsEmpty())
- {
- strcpy(parent->m_stPFile.CreateDate,m_sProjectCreateDate);
- }
- if(m_sAccessPassword != m_sAccessPassword1)
- {
- AfxMessageBox("两次输入的访问密码不一致");
- return;
- }
- strcpy(parent->m_stPFile.AccessPassword,m_sAccessPassword);
- if(m_sSecurityPassword != m_sSecurityPassword1)
- {
- AfxMessageBox("两次输入的安全密码不一致");
- return;
- }
- strcpy(parent->m_stPFile.SecurityPassword,m_sSecurityPassword);
- strcpy(parent->m_stPFile.EditDate,m_sProjectModifyDate);
- strcpy(parent->m_stPFile.ProjectName,m_sProjectName);
- strcpy(parent->m_stPFile.ProjectNote,m_sProjectNote);
-
- CDialog::OnOK();
- }
复制代码 4、关闭工程
- void CUUProgDlg::OnFileProjectClose()
- {
- // TODO: Add your command handler code here
- if(!m_sProjectFilename.IsEmpty())
- {
- ZeroMemory(&m_stPFile, sizeof(ProjectFileStruct));
- ZeroMemory(&m_stPFileHead, sizeof(ProjectFileHeadStruct));
- memset(pProgBuff,0xff,m_dDeviceSize);
- SetMenuItem(true);
- m_sProjectFilename = "";
- SetLoadFileName("未加载文件");
- }
- else
- {
- SetAppLog("未打开工程文件,不需关闭");
- AfxMessageBox("未打开工程文件,不需关闭");
- }
- }
复制代码 具体的请参考源代码。
|
|