typedef struct SciFileHeader
{
long lType;
char strBuffer[52];
long nDiskSize;
long nFileNum;
long nFolderNum;
long nCommentStartAddr;
long nCommentLen;
long nCommentEndAddr;
long nCoverLen;
}SciFileHeader;
typedef struct SciBlockInfo
{
long ChildItemAddr;
long NextItemAddr;
long filenamelength;
long filetype;
long info4;
long filelength;
FILETIME filetime;
}SciBlockInfo;
typedef struct TreeItem
{
TreeItem* lpChildItem;
TreeItem* lpNextItem;
long filenamelength;
long filetype;
long info4;
long filelength;
SYSTEMTIME filetime;
char strFileName[MAX_PATH];
}TreeItem;
void DeleteItem(TreeItem* lpItem);
TreeItem* NewItem();
TreeItem* ParseFile(HANDLE hFile);
void CTestMfcDlg::OnOK()
{
DWORD nRead;
HANDLE hFile = NULL;
CString m_strFileName;
HTREEITEM hRoot = NULL;
TreeItem *lpTemp = NULL;
SciFileHeader stFileHead;
m_stEditCtrl.GetWindowText(m_strFileName);
if (m_strFileName.IsEmpty())
return;
if (m_strFileName.GetLength() <= 0)
return;
if (GetFileAttributes(m_strFileName) == 0xffffffff)
return;
hFile = CreateFile(m_strFileName,GENERIC_READ,FILE_SHARE_READ,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
if (INVALID_HANDLE_VALUE == hFile)
return;
if (!ReadFile(hFile,&stFileHead,sizeof(SciFileHeader),&nRead,NULL))
goto Exit;
if (nRead != sizeof(SciFileHeader))
goto Exit;
if (stFileHead.lType != 0x46494353)
goto Exit;
//解析文件
lpTemp = ParseFile(hFile);
if (lpTemp == NULL)
goto Exit;
CloseHandle(hFile);
m_strFileName.Format("%s | %d | %d | %d",stFileHead.strBuffer,stFileHead.nDiskSize,
stFileHead.nFolderNum,stFileHead.nFileNum);
//插入树中
hRoot = m_stTreeCtrl.InsertItem(m_strFileName);
if (hRoot == NULL)
{
DeleteItem(lpTemp);
return;
}
InsertTreeItem(lpTemp,hRoot);
DeleteItem(lpTemp);
return;
Exit:
CloseHandle(hFile);
// TODO: Add extra validation here
}
TreeItem* ParseFile(HANDLE hFile)
{
DWORD nRead;
TreeItem* lpTemp;
char strBuffer[MAX_PATH];
SciBlockInfo stBlockInfo;
if (hFile == NULL)
return NULL;
lpTemp = NULL;
ZeroMemory(&stBlockInfo,sizeof(SciBlockInfo));
//读取数据
if (!ReadFile(hFile,&stBlockInfo,sizeof(SciBlockInfo),&nRead,NULL))
return NULL;
//判断读取是否正确
if (nRead != sizeof(SciBlockInfo))
return NULL;
//校验文件长度是否正确
if (stBlockInfo.filenamelength >= MAX_PATH)
return NULL;
//读取文件名
if (!ReadFile(hFile,strBuffer,stBlockInfo.filenamelength,&nRead,NULL))
return NULL;
//判断读取是否正确
if (nRead != stBlockInfo.filenamelength)
return NULL;
strBuffer[stBlockInfo.filenamelength] = ' ';
//OK可以赋值了
lpTemp = NewItem();
if (lpTemp == NULL)
return NULL;
lpTemp->filenamelength = stBlockInfo.filenamelength;
lpTemp->filetype = stBlockInfo.filetype;
lpTemp->info4 = stBlockInfo.info4;
lpTemp->filelength = stBlockInfo.filelength;
CopyMemory(lpTemp->strFileName,strBuffer,MAX_PATH);
FileTimeToSystemTime(&(stBlockInfo.filetime),&(lpTemp->filetime));
if (stBlockInfo.ChildItemAddr != -1)
lpTemp->lpChildItem = ParseFile(hFile);
if (stBlockInfo.NextItemAddr != -1)
lpTemp->lpNextItem = ParseFile(hFile);
return lpTemp;
}