📄 folder.cpp
字号:
// Folder.cpp: implementation of the CFolder class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "Folder.h"
IMPLEMENT_SERIAL(CFolder, CObject, VERSIONABLE_SCHEMA | 2000)
CFolder::CFolder()
{
m_pFather = NULL;//m_pFather为空
}
CFolder::CFolder(CString strFolderName, CFolder* pFather/*=NULL*/)//赋值m_strFolderName,m_pFather
{
m_strFolderName = strFolderName;
m_pFather = pFather;
}
CFolder::~CFolder()//析构CFolder时清空m_FolderList列表 (存放文件夹地址的列表)
{
CFolder* pFolder = NULL;
while (!m_FolderList.IsEmpty())//如果m_FolderList不为空
{
pFolder = (CFolder*)m_FolderList.RemoveHead();
delete pFolder;
}
}
BOOL CFolder::AddFileName(CString strFileName)//添加文件名函数
{
if(strFileName.IsEmpty())//如果strFileName为空则返回
{ASSERT(FALSE); return FALSE;}
CString g_strStockPath;//文件库路径
int nLen = strFileName.GetLength();//获取文件名路径字符长度
CString strTemp1(strFileName);
strTemp1.MakeUpper();//转换为大写字母
CString strTemp2(g_strStockPath);
strTemp2.MakeUpper();//转换为大写字母
int nPos = strTemp1.Find(strTemp2);
if(nPos != -1)//转为相对路径
strFileName = strFileName.Right(nLen - g_strStockPath.GetLength());
int nIndex = m_FileNameArray.GetSize();//获取文件列表大小
CString strTemp;
while(nIndex--)//检查文件名是否已经存在
{
strTemp = m_FileNameArray.GetAt(nIndex);
if(strTemp.CompareNoCase(strFileName) == 0)
{
ASSERT(FALSE);
return FALSE;//文件名存在
}
}
m_FileNameArray.Add(strFileName);//文件名增加到文件列表中
return TRUE;
}
BOOL CFolder::RemoveFileName(CString strFileName, BOOL bIsPathName/*=FALSE*/)//删除文件名称
{
ASSERT(!strFileName.IsEmpty());//如果strFileName为空则返回
CString strPathName = strFileName;//当前结点名称
if(!bIsPathName)//如果不是路径名称
strPathName = FindFullFileName(strFileName);//strPathName=文件名路径长度
CString g_strStockPath;//文件库路径
int nLen = strPathName.GetLength();//获取文件名路径长度
CString strTemp1(strPathName);
strTemp1.MakeUpper();//转换为大写字母
CString strTemp2(g_strStockPath);
strTemp2.MakeUpper();//转换为大写字母
int nPos = strTemp1.Find(strTemp2);
if(nPos != -1)
//绝对路径转为相对路径
strPathName = strPathName.Right(nLen - g_strStockPath.GetLength());
int nIndex = m_FileNameArray.GetSize();//获取文件列表大小
CString strTemp;
while(nIndex--)//检查文件名是否存在
{
strTemp = m_FileNameArray.GetAt(nIndex);
if(strTemp.CompareNoCase(strPathName) == 0)//是否存在
{
m_FileNameArray.RemoveAt(nIndex);//从列表中删除文件
return TRUE;
}
}
ASSERT(FALSE);
return FALSE;//文件名存在
}
CString CFolder::FindFullFileName(CString strFileName)//获取文件完整路径
{
CString strFullFileName;
int nIndex = m_FileNameArray.GetSize();//得到文件列表大小
while(nIndex--)
{
strFullFileName = m_FileNameArray.GetAt(nIndex);//获取文件名称
if(strFullFileName.Find(strFileName) != -1)//找到相应文件
{
CString g_strStockPath;//文件库路径
if(strFullFileName[1] != _T(':'))//第一个字符不是以“:”开头
strFullFileName = g_strStockPath+strFullFileName;//相对路径转为绝对路径
return strFullFileName;//返回文件绝对路径
}
}
return CString("");
}
void CFolder::AddFolder(CFolder* pFolder)//增加文件夹函数
{
ASSERT(pFolder != NULL);//pFolder为空则返回
/**/ pFolder->m_pFather = this;
m_FolderList.AddTail(pFolder);//在m_FolderList中增加pFolder
}
void CFolder::RemoveFolder(CFolder* pFolder, BOOL bDelete/*=TRUE*/)//删除文件夹
{
ASSERT(pFolder != NULL);
POSITION pos;
pos = m_FolderList.Find(pFolder);//找到文件夹在m_FolderList中的索引
if(!pos)
return;
m_FolderList.RemoveAt(pos);//删除索引指针
if(bDelete)
delete pFolder;//释放对象
}
void CFolder::Serialize(CArchive& ar)//序列化存取文件
{
m_FileNameArray.Serialize(ar);
m_FolderList.Serialize(ar);
if (ar.IsStoring())//存储文件
{
ar << m_strFolderName;
}
else//打开文件
{
ar >> m_strFolderName;
CFolder* pFolder = NULL;
POSITION pos = m_FolderList.GetHeadPosition();//pos= m_FolderList的头结点
while (pos != NULL)//
{
pFolder = (CFolder*)m_FolderList.GetNext(pos);//
/**/ pFolder->m_pFather = this;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -