📄 dirfileinfo.cpp
字号:
// DirFileInfo.cpp: implementation of the CDirFileInfo class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "resourceeditor.h"
#include "DirFileInfo.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CDirFileInfo::CDirFileInfo()
{
}
CDirFileInfo::~CDirFileInfo()
{
}
PTREENODE CDirFileInfo::MallocNode( void )
{
CDirFileNode * pNode = new CDirFileNode;
if( pNode != NULL )
ZeroNode(pNode);
return pNode;
}
void CDirFileInfo::FreeNode( PTREENODE pNode )
{
delete pNode;
}
int CDirFileInfo::GetNodeSize(void) const
{
return sizeof(CDirFileNode);
}
// 将节点信息置零。注意:由于节点有虚函数表,所以不能使用memset(...sizeof())来实现
void CDirFileInfo::ZeroNode( PTREENODE pNode )
{
CTreeInfo::ZeroNode(pNode);
CDirFileNode * pdfn = (CDirFileNode *)pNode;
pdfn->bIsDir = FALSE;
pdfn->nWidth = pdfn->nHeight = 0;
memset(pdfn->szID, 0, sizeof(pdfn->szID));
memset(pdfn->szName, 0, sizeof(pdfn->szName));
memset(pdfn->szComment, 0, sizeof(pdfn->szComment));
}
void CDirFileInfo::CalcAllLeafID( CStringArray & arrID )
{
arrID.RemoveAll();
CDirFileNode * pChild = NULL;
CDirFileNode * pRoot = (CDirFileNode *)GetRoot();
int nCount = GetChildCount(pRoot);
for( int i = 0; i < nCount; ++i )
{
pChild = (CDirFileNode *)GetChild(pRoot, i);
RecursiveCalcAllLeafID( pChild, arrID );
}
}
BOOL CDirFileInfo::RecursiveCalcAllLeafID( CDirFileNode * pNode, CStringArray & arrID )
{
if(pNode == NULL)
return TRUE;
CDirFileNode * pChild = (CDirFileNode *)GetChild(pNode);
if( pChild == NULL && !pNode->bIsDir)
arrID.Add(pNode->szID);
for( pNode = pChild; pNode != NULL; pNode = (CDirFileNode *)GetNext(pNode) )
{
if(!RecursiveCalcAllLeafID(pNode, arrID))
return FALSE;
}
return TRUE;
}
//////////////////////////////////////////////////////////////////////
// 文件操作
//////////////////////////////////////////////////////////////////////
// 将指定节点的所有子孙节点保存到文件。注意:指定的节点本身并不被保存!
BOOL CDirFileInfo::SaveToFile( CDirFileNode * pNode, FILE * pFile )
{
_ASSERTE( pNode != NULL && pFile != NULL );
int nCount = GetAllPosteritiesCount(pNode);
nCount--;
if( fwrite( &nCount, sizeof(int), 1, pFile) != 1 )
{
_ASSERTE( 0 );
return FALSE;
}
if( nCount > 0 )
{
nCount = GetChildCount(pNode);
for( int i = 0; i < nCount; ++i )
{
RecursiveSave( (CDirFileNode *)GetChild(pNode, i), pFile );
}
}
return TRUE;
}
BOOL CDirFileInfo::RecursiveSave(CDirFileNode * pNode, FILE * pFile)
{
if(pNode == NULL)
return TRUE;
CDirFileNode * pParent = (CDirFileNode *)GetParent(pNode);
if( fwrite(pParent->szID, sizeof(pParent->szID), 1, pFile) != 1 )
{
_ASSERTE( 0 );
return FALSE;
}
if( fwrite( pNode, sizeof(CDirFileNode), 1, pFile ) != 1 )
{
_ASSERTE( 0 );
return FALSE;
}
for( pNode = (CDirFileNode *)GetChild(pNode);
pNode != NULL; pNode = (CDirFileNode *)GetNext(pNode) )
{
if(!RecursiveSave(pNode, pFile))
return FALSE;
}
return TRUE;
}
// 从文件中读取所有节点信息.
// pRoot != NULL,读取的节点按链表顺序作为pRoot的子孙节点
BOOL CDirFileInfo::ReadFromFile( FILE * pFile, CDirFileNode * pRoot )
{
_ASSERTE( pFile != NULL && pRoot != NULL );
//for debug
struct DIRFILENODE_T
{
DWORD pVtal;
DWORD pParent;
DWORD pChild;
DWORD pPrev;
DWORD pNext;
char szID[ MAX_ID_LEN + 1 ];
char szName[MAX_NAME_LEN + 1];
char szComment[MAX_COMMENT_LEN + 1];
BOOL bIsDir;
int nWidth;
int nHeight;
}DirFileNode_T;
memset(&DirFileNode_T,0,sizeof(DirFileNode_T));
int nCount = 0;
fread(&nCount, sizeof(int), 1, pFile);
CDirFileNode * pParent = NULL;
CDirFileNode * pCur = NULL;
char szID[ MAX_ID_LEN + 1 ] = { 0 };
_TCHAR *pstBuf = new _TCHAR[MAX_COMMENT_LEN*4];
for( int i = 0; i < nCount; ++i )
{
pCur = (CDirFileNode *)MallocNode();
if(pCur == NULL)
return FALSE;
ZeroNode(pCur);
memset(szID, 0, sizeof(szID));
if( fread(szID, sizeof(szID), 1, pFile) != 1 )
{
_ASSERTE( 0 );
delete pCur;
delete []pstBuf;
return FALSE;
}
memset(&DirFileNode_T,0,sizeof(DirFileNode_T));
if( fread(&DirFileNode_T, sizeof(DirFileNode_T), 1, pFile) != 1 )
{
_ASSERTE( 0 );
delete pCur;
delete []pstBuf;
return FALSE;
}
//char to Unicode
LPDWORD pBuf = (LPDWORD)&m_TempNode;
memcpy(pBuf,&DirFileNode_T,sizeof(DWORD)*5);
MultiByteToWideChar(CP_ACP,0,DirFileNode_T.szID,-1,pstBuf,(MAX_ID_LEN + 1)*2);
_tcscpy(m_TempNode.szID,pstBuf);
MultiByteToWideChar(CP_ACP,0,DirFileNode_T.szName,-1,pstBuf,(MAX_NAME_LEN + 1)*2);
_tcscpy(m_TempNode.szName,pstBuf);
MultiByteToWideChar(CP_ACP,0,DirFileNode_T.szComment,-1,pstBuf,(MAX_COMMENT_LEN + 1)*2);
_tcscpy(m_TempNode.szComment,pstBuf);
m_TempNode.bIsDir = DirFileNode_T.bIsDir;
m_TempNode.nHeight = DirFileNode_T.nHeight;
m_TempNode.nWidth = DirFileNode_T.nWidth;
*pCur = m_TempNode;
//2006-11-8
//if(_tcslen(szID) != 0)
//CString strLen;
//strLen.Format(_T("%d"),strlen(szID));
//AfxMessageBox(strLen);
MultiByteToWideChar(CP_ACP,0,szID,-1,pstBuf,(MAX_ID_LEN + 1)*2);
if(strlen(szID) != 0)
{
while(pParent != NULL)
{
//if(_tcscmp(pParent->szID, szID) == 0)
if(_tcscmp(pParent->szID, pstBuf) == 0)
{
AddChild(pParent, pCur);
pParent = pCur;
break;
}
else
{
pParent = (CDirFileNode *)GetParent(pParent);
}
}
}
else
{
AddChild(pRoot, pCur);
pParent = pCur;
}
}
delete []pstBuf;
return TRUE;
}
BOOL CDirFileInfo::ReadFromFileEx( FILE * pFile, CDirFileNode * pRoot )
{
_ASSERTE( pFile != NULL && pRoot != NULL );
int nCount = 0;
fread(&nCount, sizeof(int), 1, pFile);
CDirFileNode * pParent = NULL;
CDirFileNode * pCur = NULL;
_TCHAR szID[ MAX_ID_LEN + 1 ] = { 0 };
for( int i = 0; i < nCount; ++i )
{
pCur = (CDirFileNode *)MallocNode();
if(pCur == NULL)
return FALSE;
ZeroNode(pCur);
memset(szID, 0, sizeof(szID));
if( fread(szID, sizeof(szID), 1, pFile) != 1 )
{
_ASSERTE( 0 );
delete pCur;
return FALSE;
}
if( fread(&m_TempNode, sizeof(m_TempNode), 1, pFile) != 1 )
{
_ASSERTE( 0 );
delete pCur;
return FALSE;
}
*pCur = m_TempNode;
if(_tcslen(szID) != 0)
{
while(pParent != NULL)
{
if(_tcscmp(pParent->szID, szID) == 0)
{
AddChild(pParent, pCur);
pParent = pCur;
break;
}
else
{
pParent = (CDirFileNode *)GetParent(pParent);
}
}
}
else
{
AddChild(pRoot, pCur);
pParent = pCur;
}
}
return TRUE;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -